What is the best method for testing a method that looks like this
class Foo(object):
is_running = False
def run(self):
self.is_running = True
while self.is_running:
do_some_work()
This is pretty standard code for a consumer, do work while the is_running flag is set.
But this is difficult to test because it's going to enter the loop and never come out unless I create a second thread to change is_running to false.
Are there any good strategies for testing this without spinning up a separate thread to run the code?
I haven't seen anything but I am thinking maybe the mock library would provide functionality that can return a [True, True, False] each time is_running is read but would that require me to change is_running from a member variable to a property or a method?
subprocess.Popen.wait()...threading.Timer- it spins up the thread for you. This object looks like its supposed to run in a separate thread anyway, so unit testing it that way seems normal to me.