Testing Concurrent Code

Wed Apr 17, 2019
~200 Words
Tags: programming

With concurrent code, non-determinism is often present. I knew this, but obviously did not internalize it. Tests of an optimization of sync.Once passed, but the code was incorrect. I got good feedback on the mailing list about what invariant I’d missed, but, additionally, I got feedback that the error could have caught by testing if I had run the test repeatedly.

go test -count 10000

Adding the command line option -count 10000 made a difference, and the tests failed. The above is obviously specific to Go, but the lesson is not. When testing concurrent code, beware non-determinism, and use repeated tests.