Last active
August 29, 2015 13:56
-
-
Save andersonvom/9283467 to your computer and use it in GitHub Desktop.
Race Condition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Runner(threading.Thread): | |
def __init__(self, in_queue, out_queue): | |
super(Runner, self).__init__() | |
self.in_queue = in_queue | |
self.out_queue = out_queue | |
self._keep_running = True | |
def run(self): | |
while self._keep_running or not self.in_queue.empty(): | |
task, args, kwargs = self.in_queue.get() | |
if task: | |
results = task(*args, **kwargs) | |
self.out_queue.put(results) | |
self.in_queue.task_done() | |
def stop(self): | |
self._keep_running = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_run_polls_in_queue_for_tasks(self): | |
def fake_empty(): | |
return self.in_queue.get.called | |
mock_callable = mock.Mock(return_value='fake result') | |
task = (mock_callable, ('a1', 'a2'), {'k1': 'k1', 'k2': 'k2'}) | |
self.in_queue.get.return_value = task | |
self.in_queue.empty = fake_empty | |
self.runner.start() | |
self.runner.stop() | |
# the next line sometimes fails | |
self.assertTrue(self.in_queue.get.called) | |
self.assertTrue(mock_callable.called) | |
mock_callable.assert_called_with('a1', 'a2', k1='k1', k2='k2') | |
self.out_queue.put.assert_called_with('fake result') | |
self.assertTrue(self.in_queue.task_done.called) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment