I'm writing unit tests for a Python multithreaded program. Unit testing multithreaded programs is always difficult, but I have it working pretty well. One issue I have is that since the Python threads take turns, instead of running in parellel or time sliced, I often find myself writing tests like:
# Act
object_being_tested.do_function_that_triggers_other_thread_to_run()
time.sleep(0.1) # Let the worker thread run
# Verify
self.assertEqual(value_that_worker_thread_should_have_set, object_being_tested.get_value())
# Cleanup
object_being_tested.worker_thread.join()
This works pretty well but the 'time.sleep(0.1)' seems awkward to me. Is there a clean way in python to indicate that I don't want to sleep, I just want to switch away from the current thread and let the other thread run? Or is a short sleep the best way?