Multi-threaded testing

I’ve always found testing multi-threaded code in C++ a humbling experience. It’s just so easy to make stupid mistakes and for those mistakes to lurk in code until the circumstances are just right for them to show themselves.

This weekend a unit test that has run thousands of times for many years started to fail. The reason for the failure was a fairly obvious race condition in the test code. This issue had lain dormant and only been exposed by running the tests on my new development machine. The issue itself wasn’t anything special and there’s no real reason that it shouldn’t have been triggered during previous test runs on previous machines…

I think, one of my problems here, is that I sometimes I focus more on the code that I am testing and less on the code that is doing the testing. Another problem is that testing is hard.