I’m curious if there is a way to test functions/methods that contain a go routine. I specifically want to be able to test that the function calls the go routine. Of course, I want to test other elements of the function, so if there are any codebases that can serve as an example for testing multi-threaded functions, that would be appreciated!
I’m not very experienced with writing concurrent code, so not sure if this is possible in the first place or if unit testing is abandoned in favour of integration testing.
What is it about the goroutines that you’re testing? My tests usually set up some state, call a function and then check that the result matches what’s expected. If the function uses goroutines or not doesn’t affect my tests.
Hey Sean, thanks for getting back to me. The question was a hypothetical one, so apologies in advance for not showing any code. In the hypothetical scenario, I’m not testing the go routine itself, but a function that creates a go routine.
Let’s say in this case the function doesn’t care about the results of the goroutine (i.e. it’s not using any channels to get the results from the goroutine). Would the best approach here be to “stub” the goroutine to get it to do nothing, then unit test the function as normal?
What is the test testing if not the results of the function? I think tests are supposed to feed inputs into functions as if they were black boxes and then inspect the results to see if they match. You could rewrite the implementation of the function under test and as long as all the tests pass, the rewrite is valid.
If your test doesn’t check the results, then how do you know if it succeeds or not?