Skip to content

Commit dee545c

Browse files
committed
increasing DeferredPool test coverage by covering all possible next_task outcomes
1 parent 19d6bf9 commit dee545c

1 file changed

Lines changed: 48 additions & 2 deletions

File tree

‎src/unittest/python/defer_tests.py‎

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import yadtshell
21
from yadtshell.defer import DeferredPool
32

43
import unittest
5-
from mock import patch, call
4+
from mock import patch, call, Mock
65

76

87
class DeferredPoolTests(unittest.TestCase):
@@ -95,3 +94,50 @@ def test_should_callback_when_queue_is_empty_and_errors_occurred_equals_errors_a
9594
pool._finish()
9695

9796
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

Comments
 (0)