Debian Codegraph @

t/unit/concurrency/test_concurrency.py

raw
1 import importlib
2 import os
3 import sys
4 from itertools import count
5 from unittest.mock import Mock, patch
6
7 import pytest
8
9 from celery import concurrency
10 from celery.concurrency.base import BasePool, apply_target
11 from celery.exceptions import WorkerShutdown, WorkerTerminate
12
13
14 class test_BasePool:
15
16 def test_apply_target(self):
17
18 scratch = {}
19 counter = count(0)
20
21 def gen_callback(name, retval=None):
22
23 def callback(*args):
24 scratch[name] = (next(counter), args)
25 return retval
26
27 return callback
28
29 apply_target(gen_callback('target', 42),
30 args=(8, 16),
31 callback=gen_callback('callback'),
32 accept_callback=gen_callback('accept_callback'))
33
34 assert scratch['target'] == (1, (8, 16))
35 assert scratch['callback'] == (2, (42,))
36 pa1 = scratch['accept_callback']
37 assert pa1[0] == 0
38 assert pa1[1][0] == os.getpid()
39 assert pa1[1][1]
40
41 # No accept callback
42 scratch.clear()
43 apply_target(gen_callback('target', 42),
44 args=(8, 16),
45 callback=gen_callback('callback'),
46 accept_callback=None)
47 assert scratch == {
48 'target': (3, (8, 16)),
49 'callback': (4, (42,)),
50 }
51
52 def test_apply_target__propagate(self):
53 target = Mock(name='target')
54 target.side_effect = KeyError()
55 with pytest.raises(KeyError):
56 apply_target(target, propagate=(KeyError,))
57
58 def test_apply_target__raises(self):
59 target = Mock(name='target')
60 target.side_effect = KeyError()
61 with pytest.raises(KeyError):
62 apply_target(target)
63
64 def test_apply_target__raises_WorkerShutdown(self):
65 target = Mock(name='target')
66 target.side_effect = WorkerShutdown()
67 with pytest.raises(WorkerShutdown):
68 apply_target(target)
69
70 def test_apply_target__raises_WorkerTerminate(self):
71 target = Mock(name='target')
72 target.side_effect = WorkerTerminate()
73 with pytest.raises(WorkerTerminate):
74 apply_target(target)
75
76 def test_apply_target__raises_BaseException(self):
77 target = Mock(name='target')
78 callback = Mock(name='callback')
79 target.side_effect = BaseException()
80 apply_target(target, callback=callback)
81 callback.assert_called()
82
83 @patch('celery.concurrency.base.reraise')
84 def test_apply_target__raises_BaseException_raises_else(self, reraise):
85 target = Mock(name='target')
86 callback = Mock(name='callback')
87 reraise.side_effect = KeyError()
88 target.side_effect = BaseException()
89 with pytest.raises(KeyError):
90 apply_target(target, callback=callback)
91 callback.assert_not_called()
92
93 def test_does_not_debug(self):
94 x = BasePool(10)
95 x._does_debug = False
96 x.apply_async(object)
97
98 def test_num_processes(self):
99 assert BasePool(7).num_processes == 7
100
101 def test_interface_on_start(self):
102 BasePool(10).on_start()
103
104 def test_interface_on_stop(self):
105 BasePool(10).on_stop()
106
107 def test_interface_on_apply(self):
108 BasePool(10).on_apply()
109
110 def test_interface_info(self):
111 assert BasePool(10).info == {
112 'implementation': 'celery.concurrency.base:BasePool',
113 'max-concurrency': 10,
114 }
115
116 def test_interface_flush(self):
117 assert BasePool(10).flush() is None
118
119 def test_active(self):
120 p = BasePool(10)
121 assert not p.active
122 p._state = p.RUN
123 assert p.active
124
125 def test_restart(self):
126 p = BasePool(10)
127 with pytest.raises(NotImplementedError):
128 p.restart()
129
130 def test_interface_on_terminate(self):
131 p = BasePool(10)
132 p.on_terminate()
133
134 def test_interface_terminate_job(self):
135 with pytest.raises(NotImplementedError):
136 BasePool(10).terminate_job(101)
137
138 def test_interface_did_start_ok(self):
139 assert BasePool(10).did_start_ok()
140
141 def test_interface_register_with_event_loop(self):
142 assert BasePool(10).register_with_event_loop(Mock()) is None
143
144 def test_interface_on_soft_timeout(self):
145 assert BasePool(10).on_soft_timeout(Mock()) is None
146
147 def test_interface_on_hard_timeout(self):
148 assert BasePool(10).on_hard_timeout(Mock()) is None
149
150 def test_interface_close(self):
151 p = BasePool(10)
152 p.on_close = Mock()
153 p.close()
154 assert p._state == p.CLOSE
155 p.on_close.assert_called_with()
156
157 def test_interface_no_close(self):
158 assert BasePool(10).on_close() is None
159
160
161 class test_get_available_pool_names:
162
163 def test_no_concurrent_futures__returns_no_threads_pool_name(self):
164 expected_pool_names = (
165 'prefork',
166 'eventlet',
167 'gevent',
168 'solo',
169 'processes',
170 'custom',
171 )
172 with patch.dict(sys.modules, {'concurrent.futures': None}):
173 importlib.reload(concurrency)
174 assert concurrency.get_available_pool_names() == expected_pool_names
175
176 def test_concurrent_futures__returns_threads_pool_name(self):
177 expected_pool_names = (
178 'prefork',
179 'eventlet',
180 'gevent',
181 'solo',
182 'processes',
183 'threads',
184 'custom',
185 )
186 with patch.dict(sys.modules, {'concurrent.futures': Mock()}):
187 importlib.reload(concurrency)
188 assert concurrency.get_available_pool_names() == expected_pool_names