Say you have a function f that is or contains a critical section. How would you unit test that only one thread can run it at once, that it doesn't have race conditions, and that it doesn't cause a deadlock, etc...?
|
|
|||||||||||||
|
|
You can't. Testing can only prove that you didn't find any bugs, not that none exist, and in the presence of multi-threaded code, I would say that it's almost pointless. You cannot test for how your function behaves when the OS changes it out or pauses execution or any of a thousand external events which can affect when the thread is scheduled and it's behaviour. You need to, at least informally, prove the function correct. |
|||
|
|
|
You cannot do a "strong" test, but you can mitigate some risk by doing a "weak" test. Parameterize |
||||
|
|
|
Add sleeps around operations within |
|||
|
|
|
TDD is useless in multi-threaded scenarios. You don't test thread safety, you prove it, or it counter-proves you, painfully... |
|||
|
|