|
1 | | -import yadtshell |
2 | 1 | from yadtshell.defer import DeferredPool |
3 | 2 |
|
4 | 3 | import unittest |
5 | | -from mock import patch, call |
| 4 | +from mock import patch, call, Mock |
6 | 5 |
|
7 | 6 |
|
8 | 7 | class DeferredPoolTests(unittest.TestCase): |
@@ -95,3 +94,50 @@ def test_should_callback_when_queue_is_empty_and_errors_occurred_equals_errors_a |
95 | 94 | pool._finish() |
96 | 95 |
|
97 | 96 | fake_reactor.callLater.assert_called_with(0, pool.callback) |
| 97 | + |
| 98 | + def test_next_task_should_return_nothing_when_pool_has_already_callbacked(self): |
| 99 | + pool = DeferredPool('pool-name', queue=[]) |
| 100 | + pool.callback(Mock()) |
| 101 | + |
| 102 | + next_task = pool._next_task() |
| 103 | + |
| 104 | + self.assertEqual(next_task, None) |
| 105 | + |
| 106 | + def test_next_task_should_return_nothing_when_queue_is_empty_but_workers_are_still_running(self): |
| 107 | + pool = DeferredPool('pool-name', queue=[]) |
| 108 | + pool.all_workers_idle = lambda: False |
| 109 | + |
| 110 | + next_task = pool._next_task() |
| 111 | + |
| 112 | + self.assertEqual(next_task, None) |
| 113 | + |
| 114 | + @patch('yadtshell.defer.DeferredPool._stop_workers') |
| 115 | + def test_next_task_should_return_nothing_and_stop_workers_when_queue_is_empty_and_workers_are_idle(self, stop_workers): |
| 116 | + pool = DeferredPool('pool-name', queue=[]) |
| 117 | + pool.all_workers_idle = lambda: True |
| 118 | + |
| 119 | + next_task = pool._next_task() |
| 120 | + |
| 121 | + self.assertEqual(next_task, None) |
| 122 | + stop_workers.assert_called_with() |
| 123 | + |
| 124 | + @patch('yadtshell.defer.DeferredPool.Worker.run') |
| 125 | + @patch('yadtshell.defer.DeferredPool._stop_workers') |
| 126 | + def test_next_task_should_stop_workers_when_queue_is_not_empty_but_no_tasks_are_available_and_workers_are_idle(self, stop_workers, _): |
| 127 | + pool = DeferredPool('pool-name', queue=['some-stuff']) |
| 128 | + pool.all_workers_idle = lambda: True |
| 129 | + pool.next_task_fun = lambda _: None |
| 130 | + |
| 131 | + next_task = pool._next_task() |
| 132 | + |
| 133 | + self.assertEqual(next_task, None) |
| 134 | + stop_workers.assert_called_with() |
| 135 | + |
| 136 | + @patch('yadtshell.defer.DeferredPool.Worker.run') |
| 137 | + def test_next_task_should_return_task_when_tasks_are_available(self, _): |
| 138 | + pool = DeferredPool('pool-name', queue=['some-stuff']) |
| 139 | + pool.all_workers_idle = lambda: False |
| 140 | + |
| 141 | + next_task = pool._next_task() |
| 142 | + |
| 143 | + self.assertEqual(next_task, 'some-stuff') |
0 commit comments