Debian Codegraph @

t/unit/worker/test_bootsteps.py

raw
1 from unittest.mock import Mock, patch
2
3 import pytest
4
5 from celery import bootsteps
6
7
8 class test_StepFormatter:
9
10 def test_get_prefix(self):
11 f = bootsteps.StepFormatter()
12 s = Mock()
13 s.last = True
14 assert f._get_prefix(s) == f.blueprint_prefix
15
16 s2 = Mock()
17 s2.last = False
18 s2.conditional = True
19 assert f._get_prefix(s2) == f.conditional_prefix
20
21 s3 = Mock()
22 s3.last = s3.conditional = False
23 assert f._get_prefix(s3) == ''
24
25 def test_node(self):
26 f = bootsteps.StepFormatter()
27 f.draw_node = Mock()
28 step = Mock()
29 step.last = False
30 f.node(step, x=3)
31 f.draw_node.assert_called_with(step, f.node_scheme, {'x': 3})
32
33 step.last = True
34 f.node(step, x=3)
35 f.draw_node.assert_called_with(step, f.blueprint_scheme, {'x': 3})
36
37 def test_edge(self):
38 f = bootsteps.StepFormatter()
39 f.draw_edge = Mock()
40 a, b = Mock(), Mock()
41 a.last = True
42 f.edge(a, b, x=6)
43 f.draw_edge.assert_called_with(a, b, f.edge_scheme, {
44 'x': 6, 'arrowhead': 'none', 'color': 'darkseagreen3',
45 })
46
47 a.last = False
48 f.edge(a, b, x=6)
49 f.draw_edge.assert_called_with(a, b, f.edge_scheme, {
50 'x': 6,
51 })
52
53
54 class test_Step:
55
56 class Def(bootsteps.StartStopStep):
57 name = 'test_Step.Def'
58
59 def setup_method(self):
60 self.steps = []
61
62 def test_blueprint_name(self, bp='test_blueprint_name'):
63
64 class X(bootsteps.Step):
65 blueprint = bp
66 name = 'X'
67 assert X.name == 'X'
68
69 class Y(bootsteps.Step):
70 name = '%s.Y' % bp
71 assert Y.name == f'{bp}.Y'
72
73 def test_init(self):
74 assert self.Def(self)
75
76 def test_create(self):
77 self.Def(self).create(self)
78
79 def test_include_if(self):
80 x = self.Def(self)
81 x.enabled = True
82 assert x.include_if(self)
83
84 x.enabled = False
85 assert not x.include_if(self)
86
87 def test_instantiate(self):
88 assert isinstance(
89 self.Def(self).instantiate(self.Def, self),
90 self.Def,
91 )
92
93 def test_include_when_enabled(self):
94 x = self.Def(self)
95 x.create = Mock()
96 x.create.return_value = 'George'
97 assert x.include(self)
98
99 assert x.obj == 'George'
100 x.create.assert_called_with(self)
101
102 def test_include_when_disabled(self):
103 x = self.Def(self)
104 x.enabled = False
105 x.create = Mock()
106
107 assert not x.include(self)
108 x.create.assert_not_called()
109
110 def test_repr(self):
111 x = self.Def(self)
112 assert repr(x)
113
114
115 class test_ConsumerStep:
116
117 def test_interface(self):
118 step = bootsteps.ConsumerStep(self)
119 with pytest.raises(NotImplementedError):
120 step.get_consumers(self)
121
122 def test_start_stop_shutdown(self):
123 consumer = Mock()
124 self.connection = Mock()
125 self.connection.connection_errors = ()
126 self.connection.channel_errors = ()
127
128 class Step(bootsteps.ConsumerStep):
129
130 def get_consumers(self, c):
131 return [consumer]
132
133 step = Step(self)
134 assert step.get_consumers(self) == [consumer]
135
136 step.start(self)
137 consumer.consume.assert_called_with()
138 step.stop(self)
139 consumer.cancel.assert_called_with()
140
141 step.shutdown(self)
142 consumer.channel.close.assert_called_with()
143
144 def test_start_no_consumers(self):
145 self.connection = Mock()
146 self.connection.connection_errors = ()
147 self.connection.channel_errors = ()
148
149 class Step(bootsteps.ConsumerStep):
150
151 def get_consumers(self, c):
152 return ()
153
154 step = Step(self)
155 step.start(self)
156
157 def test_close_no_consumer_channel(self):
158 step = bootsteps.ConsumerStep(Mock())
159 step.consumers = [Mock()]
160 step.consumers[0].channel = None
161 step._close(Mock())
162
163
164 class test_StartStopStep:
165
166 class Def(bootsteps.StartStopStep):
167 name = 'test_StartStopStep.Def'
168
169 def setup_method(self):
170 self.steps = []
171
172 def test_start__stop(self):
173 x = self.Def(self)
174 x.create = Mock()
175
176 # include creates the underlying object and sets
177 # its x.obj attribute to it, as well as appending
178 # it to the parent.steps list.
179 x.include(self)
180 assert self.steps
181 assert self.steps[0] is x
182
183 x.start(self)
184 x.obj.start.assert_called_with()
185
186 x.stop(self)
187 x.obj.stop.assert_called_with()
188
189 x.obj = None
190 assert x.start(self) is None
191
192 def test_terminate__no_obj(self):
193 x = self.Def(self)
194 x.obj = None
195 x.terminate(Mock())
196
197 def test_include_when_disabled(self):
198 x = self.Def(self)
199 x.enabled = False
200 x.include(self)
201 assert not self.steps
202
203 def test_terminate(self):
204 x = self.Def(self)
205 x.create = Mock()
206
207 x.include(self)
208 delattr(x.obj, 'terminate')
209 x.terminate(self)
210 x.obj.stop.assert_called_with()
211
212
213 class test_Blueprint:
214
215 class Blueprint(bootsteps.Blueprint):
216 name = 'test_Blueprint'
217
218 def test_steps_added_to_unclaimed(self):
219
220 class tnA(bootsteps.Step):
221 name = 'test_Blueprint.A'
222
223 class tnB(bootsteps.Step):
224 name = 'test_Blueprint.B'
225
226 class xxA(bootsteps.Step):
227 name = 'xx.A'
228
229 class Blueprint(self.Blueprint):
230 default_steps = [tnA, tnB]
231 blueprint = Blueprint()
232
233 assert tnA in blueprint.types
234 assert tnB in blueprint.types
235 assert xxA not in blueprint.types
236
237 def test_init(self):
238 blueprint = self.Blueprint()
239 assert blueprint.name == 'test_Blueprint'
240
241 def test_close__on_close_is_None(self):
242 blueprint = self.Blueprint()
243 blueprint.on_close = None
244 blueprint.send_all = Mock()
245 blueprint.close(1)
246 blueprint.send_all.assert_called_with(
247 1, 'close', 'closing', reverse=False,
248 )
249
250 def test_send_all_with_None_steps(self):
251 parent = Mock()
252 blueprint = self.Blueprint()
253 parent.steps = [None, None, None]
254 blueprint.send_all(parent, 'close', 'Closing', reverse=False)
255
256 def test_send_all_raises(self):
257 parent = Mock()
258 blueprint = self.Blueprint()
259 parent.steps = [Mock()]
260 parent.steps[0].foo.side_effect = KeyError()
261 blueprint.send_all(parent, 'foo', propagate=False)
262 with pytest.raises(KeyError):
263 blueprint.send_all(parent, 'foo', propagate=True)
264
265 def test_stop_state_in_TERMINATE(self):
266 blueprint = self.Blueprint()
267 blueprint.state = bootsteps.TERMINATE
268 blueprint.stop(Mock())
269
270 def test_join_raises_IGNORE_ERRORS(self):
271 prev, bootsteps.IGNORE_ERRORS = bootsteps.IGNORE_ERRORS, (KeyError,)
272 try:
273 blueprint = self.Blueprint()
274 blueprint.shutdown_complete = Mock()
275 blueprint.shutdown_complete.wait.side_effect = KeyError('luke')
276 blueprint.join(timeout=10)
277 blueprint.shutdown_complete.wait.assert_called_with(timeout=10)
278 finally:
279 bootsteps.IGNORE_ERRORS = prev
280
281 def test_connect_with(self):
282
283 class b1s1(bootsteps.Step):
284 pass
285
286 class b1s2(bootsteps.Step):
287 last = True
288
289 class b2s1(bootsteps.Step):
290 pass
291
292 class b2s2(bootsteps.Step):
293 last = True
294
295 b1 = self.Blueprint([b1s1, b1s2])
296 b2 = self.Blueprint([b2s1, b2s2])
297 b1.apply(Mock())
298 b2.apply(Mock())
299 b1.connect_with(b2)
300
301 assert b1s1 in b1.graph
302 assert b2s1 in b1.graph
303 assert b2s2 in b1.graph
304
305 assert repr(b1s1)
306 assert str(b1s1)
307
308 def test_topsort_raises_KeyError(self):
309
310 class Step(bootsteps.Step):
311 requires = ('xyxxx.fsdasewe.Unknown',)
312
313 b = self.Blueprint([Step])
314 b.steps = b.claim_steps()
315 with pytest.raises(ImportError):
316 b._finalize_steps(b.steps)
317 Step.requires = ()
318
319 b.steps = b.claim_steps()
320 b._finalize_steps(b.steps)
321
322 with patch('celery.bootsteps.DependencyGraph') as Dep:
323 g = Dep.return_value = Mock()
324 g.topsort.side_effect = KeyError('foo')
325 with pytest.raises(KeyError):
326 b._finalize_steps(b.steps)
327
328 def test_apply(self):
329
330 class MyBlueprint(bootsteps.Blueprint):
331 name = 'test_apply'
332
333 def modules(self):
334 return ['A', 'B']
335
336 class B(bootsteps.Step):
337 name = 'test_apply.B'
338
339 class C(bootsteps.Step):
340 name = 'test_apply.C'
341 requires = [B]
342
343 class A(bootsteps.Step):
344 name = 'test_apply.A'
345 requires = [C]
346
347 class D(bootsteps.Step):
348 name = 'test_apply.D'
349 last = True
350
351 x = MyBlueprint([A, D])
352 x.apply(self)
353
354 assert isinstance(x.order[0], B)
355 assert isinstance(x.order[1], C)
356 assert isinstance(x.order[2], A)
357 assert isinstance(x.order[3], D)
358 assert A in x.types
359 assert x[A.name] is x.order[2]
360
361 def test_find_last_but_no_steps(self):
362
363 class MyBlueprint(bootsteps.Blueprint):
364 name = 'qwejwioqjewoqiej'
365
366 x = MyBlueprint()
367 x.apply(self)
368 assert x._find_last() is None