Testing threaded applications
Key 4: Make threaded application tests like nonthreaded ones.
My experience with testing on threaded application is to perform the following actions:
Try to make the threaded application as nonthreaded as possible for tests. By this, I mean that group logic that is nonthreaded in one code segment. Do not try to test business logic with thread logic. Try to keep them separate.
Work with as little global state as possible. Functions should pass around objects that are needed to work.
Try to make queues of tasks to synchronize them. Instead of creating producer consumer chains yourself, first try to use queues.
Also note that sleep statements make test cases run slower. If you add up sleeps in the code for more than 20 places, the whole test suite starts to become slow. Threaded code should pass on information with events and notifications rather than a while loop checking some condition.
The _thread
module in Python 2 and the _thread
module in Python 3 are a big help...