Debian Codegraph @

t/unit/app/test_control.py

raw
1 from unittest.mock import Mock
2
3 import pytest
4
5 from celery import uuid
6 from celery.app import control
7 from celery.exceptions import DuplicateNodenameWarning, ImproperlyConfigured
8 from celery.utils.collections import LimitedSet
9
10
11 def _info_for_commandclass(type_):
12 from celery.worker.control import Panel
13 return [
14 (name, info)
15 for name, info in Panel.meta.items()
16 if info.type == type_
17 ]
18
19
20 def test_client_implements_all_commands(app):
21 commands = _info_for_commandclass('control')
22 assert commands
23 for name, info in commands:
24 assert getattr(app.control, name)
25
26
27 def test_inspect_implements_all_commands(app):
28 inspect = app.control.inspect()
29 commands = _info_for_commandclass('inspect')
30 assert commands
31 for name, info in commands:
32 if info.type == 'inspect':
33 assert getattr(inspect, name)
34
35
36 class test_flatten_reply:
37
38 def test_flatten_reply(self):
39 reply = [
40 {'[email protected]': {'hello': 10}},
41 {'[email protected]': {'hello': 20}},
42 {'[email protected]': {'hello': 30}}
43 ]
44 with pytest.warns(DuplicateNodenameWarning) as w:
45 nodes = control.flatten_reply(reply)
46
47 assert 'Received multiple replies from node name: {}.'.format(
48 next(iter(reply[0]))) in str(w[0].message.args[0])
49 assert '[email protected]' in nodes
50 assert '[email protected]' in nodes
51
52
53 class test_inspect:
54
55 def setup_method(self):
56 self.app.control.broadcast = Mock(name='broadcast')
57 self.app.control.broadcast.return_value = {}
58 self.inspect = self.app.control.inspect()
59
60 def test_prepare_reply(self):
61 reply = self.inspect._prepare([
62 {'w1': {'ok': 1}},
63 {'w2': {'ok': 1}},
64 ])
65 assert reply == {
66 'w1': {'ok': 1},
67 'w2': {'ok': 1},
68 }
69
70 i = self.app.control.inspect(destination='w1')
71 assert i._prepare([{'w1': {'ok': 1}}]) == {'ok': 1}
72
73 def assert_broadcast_called(self, command,
74 destination=None,
75 callback=None,
76 connection=None,
77 limit=None,
78 timeout=None,
79 reply=True,
80 pattern=None,
81 matcher=None,
82 **arguments):
83 self.app.control.broadcast.assert_called_with(
84 command,
85 arguments=arguments,
86 destination=destination or self.inspect.destination,
87 pattern=pattern or self.inspect.pattern,
88 matcher=matcher or self.inspect.destination,
89 callback=callback or self.inspect.callback,
90 connection=connection or self.inspect.connection,
91 limit=limit if limit is not None else self.inspect.limit,
92 timeout=timeout if timeout is not None else self.inspect.timeout,
93 reply=reply,
94 )
95
96 def test_active(self):
97 self.inspect.active()
98 self.assert_broadcast_called('active', safe=None)
99
100 def test_active_safe(self):
101 self.inspect.active(safe=True)
102 self.assert_broadcast_called('active', safe=True)
103
104 def test_clock(self):
105 self.inspect.clock()
106 self.assert_broadcast_called('clock')
107
108 def test_conf(self):
109 self.inspect.conf()
110 self.assert_broadcast_called('conf', with_defaults=False)
111
112 def test_conf__with_defaults(self):
113 self.inspect.conf(with_defaults=True)
114 self.assert_broadcast_called('conf', with_defaults=True)
115
116 def test_hello(self):
117 self.inspect.hello('[email protected]')
118 self.assert_broadcast_called(
119 'hello', from_node='[email protected]', revoked=None)
120
121 def test_hello__with_revoked(self):
122 revoked = LimitedSet(100)
123 for i in range(100):
124 revoked.add(f'id{i}')
125 self.inspect.hello('[email protected]', revoked=revoked._data)
126 self.assert_broadcast_called(
127 'hello', from_node='[email protected]', revoked=revoked._data)
128
129 def test_memsample(self):
130 self.inspect.memsample()
131 self.assert_broadcast_called('memsample')
132
133 def test_memdump(self):
134 self.inspect.memdump()
135 self.assert_broadcast_called('memdump', samples=10)
136
137 def test_memdump__samples_specified(self):
138 self.inspect.memdump(samples=303)
139 self.assert_broadcast_called('memdump', samples=303)
140
141 def test_objgraph(self):
142 self.inspect.objgraph()
143 self.assert_broadcast_called(
144 'objgraph', num=200, type='Request', max_depth=10)
145
146 def test_scheduled(self):
147 self.inspect.scheduled()
148 self.assert_broadcast_called('scheduled')
149
150 def test_reserved(self):
151 self.inspect.reserved()
152 self.assert_broadcast_called('reserved')
153
154 def test_stats(self):
155 self.inspect.stats()
156 self.assert_broadcast_called('stats')
157
158 def test_revoked(self):
159 self.inspect.revoked()
160 self.assert_broadcast_called('revoked')
161
162 def test_registered(self):
163 self.inspect.registered()
164 self.assert_broadcast_called('registered', taskinfoitems=())
165
166 def test_registered__taskinfoitems(self):
167 self.inspect.registered('rate_limit', 'time_limit')
168 self.assert_broadcast_called(
169 'registered',
170 taskinfoitems=('rate_limit', 'time_limit'),
171 )
172
173 def test_ping(self):
174 self.inspect.ping()
175 self.assert_broadcast_called('ping')
176
177 def test_ping_matcher_pattern(self):
178 orig_inspect = self.inspect
179 self.inspect = self.app.control.inspect(pattern=".*", matcher="pcre")
180 self.inspect.ping()
181 try:
182 self.assert_broadcast_called('ping', pattern=".*", matcher="pcre")
183 except AssertionError as e:
184 self.inspect = orig_inspect
185 raise e
186
187 def test_active_queues(self):
188 self.inspect.active_queues()
189 self.assert_broadcast_called('active_queues')
190
191 def test_query_task(self):
192 self.inspect.query_task('foo', 'bar')
193 self.assert_broadcast_called('query_task', ids=('foo', 'bar'))
194
195 def test_query_task__compat_single_list_argument(self):
196 self.inspect.query_task(['foo', 'bar'])
197 self.assert_broadcast_called('query_task', ids=['foo', 'bar'])
198
199 def test_query_task__scalar(self):
200 self.inspect.query_task('foo')
201 self.assert_broadcast_called('query_task', ids=('foo',))
202
203 def test_report(self):
204 self.inspect.report()
205 self.assert_broadcast_called('report')
206
207
208 class test_Control_broadcast:
209
210 def setup_method(self):
211 self.app.control.mailbox = Mock(name='mailbox')
212
213 def test_broadcast(self):
214 self.app.control.broadcast('foobarbaz', arguments={'foo': 2})
215 self.app.control.mailbox.assert_called()
216 self.app.control.mailbox()._broadcast.assert_called_with(
217 'foobarbaz', {'foo': 2}, None, False, 1.0, None, None,
218 channel=None,
219 )
220
221 def test_broadcast_limit(self):
222 self.app.control.broadcast(
223 'foobarbaz1', arguments=None, limit=None, destination=[1, 2, 3],
224 )
225 self.app.control.mailbox.assert_called()
226 self.app.control.mailbox()._broadcast.assert_called_with(
227 'foobarbaz1', {}, [1, 2, 3], False, 1.0, None, None,
228 channel=None,
229 )
230
231
232 class test_Control:
233
234 def setup_method(self):
235 self.app.control.broadcast = Mock(name='broadcast')
236 self.app.control.broadcast.return_value = {}
237
238 @self.app.task(shared=False)
239 def mytask():
240 pass
241 self.mytask = mytask
242
243 def assert_control_called_with_args(self, name, destination=None,
244 _options=None, **args):
245 self.app.control.broadcast.assert_called_with(
246 name, destination=destination, arguments=args, **_options or {})
247
248 def test_serializer(self):
249 self.app.conf['task_serializer'] = 'test'
250 self.app.conf['accept_content'] = ['test']
251 assert control.Control(self.app).mailbox.serializer == 'test'
252 assert control.Control(self.app).mailbox.accept == ['test']
253
254 def test_purge(self):
255 self.app.amqp.TaskConsumer = Mock(name='TaskConsumer')
256 self.app.control.purge()
257 self.app.amqp.TaskConsumer().purge.assert_called_with()
258
259 def test_rate_limit(self):
260 self.app.control.rate_limit(self.mytask.name, '100/m')
261 self.assert_control_called_with_args(
262 'rate_limit',
263 destination=None,
264 task_name=self.mytask.name,
265 rate_limit='100/m',
266 )
267
268 def test_rate_limit__with_destination(self):
269 self.app.control.rate_limit(
270 self.mytask.name, '100/m', '[email protected]', limit=100)
271 self.assert_control_called_with_args(
272 'rate_limit',
273 destination='[email protected]',
274 task_name=self.mytask.name,
275 rate_limit='100/m',
276 _options={'limit': 100},
277 )
278
279 def test_time_limit(self):
280 self.app.control.time_limit(self.mytask.name, soft=10, hard=20)
281 self.assert_control_called_with_args(
282 'time_limit',
283 destination=None,
284 task_name=self.mytask.name,
285 soft=10,
286 hard=20,
287 )
288
289 def test_time_limit__with_destination(self):
290 self.app.control.time_limit(
291 self.mytask.name, soft=10, hard=20,
292 destination='[email protected]', limit=99,
293 )
294
295 self.assert_control_called_with_args(
296 'time_limit',
297 destination='[email protected]',
298 task_name=self.mytask.name,
299 soft=10,
300 hard=20,
301 _options={'limit': 99},
302 )
303
304 def test_add_consumer(self):
305 self.app.control.add_consumer('foo')
306 self.assert_control_called_with_args(
307 'add_consumer',
308 destination=None,
309 queue='foo',
310 exchange=None,
311 exchange_type='direct',
312 routing_key=None,
313 )
314
315 def test_add_consumer__with_options_and_dest(self):
316 self.app.control.add_consumer(
317 'foo', 'ex', 'topic', 'rkey', destination='[email protected]', limit=78)
318 self.assert_control_called_with_args(
319 'add_consumer',
320 destination='[email protected]',
321 queue='foo',
322 exchange='ex',
323 exchange_type='topic',
324 routing_key='rkey',
325 _options={'limit': 78},
326 )
327
328 def test_cancel_consumer(self):
329 self.app.control.cancel_consumer('foo')
330 self.assert_control_called_with_args(
331 'cancel_consumer',
332 destination=None,
333 queue='foo',
334 )
335
336 def test_cancel_consumer__with_destination(self):
337 self.app.control.cancel_consumer(
338 'foo', destination='[email protected]', limit=3)
339 self.assert_control_called_with_args(
340 'cancel_consumer',
341 destination='[email protected]',
342 queue='foo',
343 _options={'limit': 3},
344 )
345
346 def test_shutdown(self):
347 self.app.control.shutdown()
348 self.assert_control_called_with_args('shutdown', destination=None)
349
350 def test_shutdown__with_destination(self):
351 self.app.control.shutdown(destination='[email protected]', limit=3)
352 self.assert_control_called_with_args(
353 'shutdown', destination='[email protected]', _options={'limit': 3})
354
355 def test_heartbeat(self):
356 self.app.control.heartbeat()
357 self.assert_control_called_with_args('heartbeat', destination=None)
358
359 def test_heartbeat__with_destination(self):
360 self.app.control.heartbeat(destination='[email protected]', limit=3)
361 self.assert_control_called_with_args(
362 'heartbeat', destination='[email protected]', _options={'limit': 3})
363
364 def test_pool_restart(self):
365 self.app.control.pool_restart()
366 self.assert_control_called_with_args(
367 'pool_restart',
368 destination=None,
369 modules=None,
370 reload=False,
371 reloader=None)
372
373 def test_terminate(self):
374 self.app.control.revoke = Mock(name='revoke')
375 self.app.control.terminate('124')
376 self.app.control.revoke.assert_called_with(
377 '124', destination=None,
378 terminate=True,
379 signal=control.TERM_SIGNAME,
380 )
381
382 def test_enable_events(self):
383 self.app.control.enable_events()
384 self.assert_control_called_with_args('enable_events', destination=None)
385
386 def test_enable_events_with_destination(self):
387 self.app.control.enable_events(destination='[email protected]', limit=3)
388 self.assert_control_called_with_args(
389 'enable_events', destination='[email protected]', _options={'limit': 3})
390
391 def test_disable_events(self):
392 self.app.control.disable_events()
393 self.assert_control_called_with_args(
394 'disable_events', destination=None)
395
396 def test_disable_events_with_destination(self):
397 self.app.control.disable_events(destination='[email protected]', limit=3)
398 self.assert_control_called_with_args(
399 'disable_events', destination='[email protected]', _options={'limit': 3})
400
401 def test_ping(self):
402 self.app.control.ping()
403 self.assert_control_called_with_args(
404 'ping', destination=None,
405 _options={'timeout': 1.0, 'reply': True})
406
407 def test_ping_with_destination(self):
408 self.app.control.ping(destination='[email protected]', limit=3)
409 self.assert_control_called_with_args(
410 'ping',
411 destination='[email protected]',
412 _options={
413 'limit': 3,
414 'timeout': 1.0,
415 'reply': True,
416 })
417
418 def test_revoke(self):
419 self.app.control.revoke('foozbaaz')
420 self.assert_control_called_with_args(
421 'revoke',
422 destination=None,
423 task_id='foozbaaz',
424 signal=control.TERM_SIGNAME,
425 terminate=False,
426 )
427
428 def test_revoke_by_stamped_headers(self):
429 self.app.control.revoke_by_stamped_headers({'foo': 'bar'})
430 self.assert_control_called_with_args(
431 'revoke_by_stamped_headers',
432 destination=None,
433 headers={'foo': 'bar'},
434 signal=control.TERM_SIGNAME,
435 terminate=False,
436 )
437
438 def test_revoke__with_options(self):
439 self.app.control.revoke(
440 'foozbaaz',
441 destination='[email protected]',
442 terminate=True,
443 signal='KILL',
444 limit=404,
445 )
446 self.assert_control_called_with_args(
447 'revoke',
448 destination='[email protected]',
449 task_id='foozbaaz',
450 signal='KILL',
451 terminate=True,
452 _options={'limit': 404},
453 )
454
455 def test_revoke_by_stamped_headers__with_options(self):
456 self.app.control.revoke_by_stamped_headers(
457 {'foo': 'bar'},
458 destination='[email protected]',
459 terminate=True,
460 signal='KILL',
461 limit=404,
462 )
463 self.assert_control_called_with_args(
464 'revoke_by_stamped_headers',
465 destination='[email protected]',
466 headers={'foo': 'bar'},
467 signal='KILL',
468 terminate=True,
469 _options={'limit': 404},
470 )
471
472 def test_election(self):
473 self.app.control.election('some_id', 'topic', 'action')
474 self.assert_control_called_with_args(
475 'election',
476 destination=None,
477 topic='topic',
478 action='action',
479 id='some_id',
480 _options={'connection': None},
481 )
482
483 def test_autoscale(self):
484 self.app.control.autoscale(300, 10)
485 self.assert_control_called_with_args(
486 'autoscale', max=300, min=10, destination=None)
487
488 def test_autoscale__with_options(self):
489 self.app.control.autoscale(300, 10, destination='[email protected]', limit=39)
490 self.assert_control_called_with_args(
491 'autoscale', max=300, min=10,
492 destination='[email protected]',
493 _options={'limit': 39}
494 )
495
496 def test_pool_grow(self):
497 self.app.control.pool_grow(2)
498 self.assert_control_called_with_args(
499 'pool_grow', n=2, destination=None)
500
501 def test_pool_grow__with_options(self):
502 self.app.control.pool_grow(2, destination='[email protected]', limit=39)
503 self.assert_control_called_with_args(
504 'pool_grow', n=2,
505 destination='[email protected]',
506 _options={'limit': 39}
507 )
508
509 def test_pool_shrink(self):
510 self.app.control.pool_shrink(2)
511 self.assert_control_called_with_args(
512 'pool_shrink', n=2, destination=None)
513
514 def test_pool_shrink__with_options(self):
515 self.app.control.pool_shrink(2, destination='[email protected]', limit=39)
516 self.assert_control_called_with_args(
517 'pool_shrink', n=2,
518 destination='[email protected]',
519 _options={'limit': 39}
520 )
521
522 def test_revoke_from_result(self):
523 self.app.control.revoke = Mock(name='revoke')
524 self.app.AsyncResult('foozbazzbar').revoke()
525 self.app.control.revoke.assert_called_with(
526 'foozbazzbar',
527 connection=None, reply=False, signal=None,
528 terminate=False, timeout=None)
529
530 def test_revoke_by_stamped_headers_from_result(self):
531 self.app.control.revoke_by_stamped_headers = Mock(name='revoke_by_stamped_headers')
532 self.app.AsyncResult('foozbazzbar').revoke_by_stamped_headers({'foo': 'bar'})
533 self.app.control.revoke_by_stamped_headers.assert_called_with(
534 {'foo': 'bar'},
535 connection=None, reply=False, signal=None,
536 terminate=False, timeout=None)
537
538 def test_revoke_from_resultset(self):
539 self.app.control.revoke = Mock(name='revoke')
540 uuids = [uuid() for _ in range(10)]
541 r = self.app.GroupResult(
542 uuid(), [self.app.AsyncResult(x) for x in uuids])
543 r.revoke()
544 self.app.control.revoke.assert_called_with(
545 uuids,
546 connection=None, reply=False, signal=None,
547 terminate=False, timeout=None)
548
549 def test_after_fork_clears_mailbox_pool(self):
550 amqp = Mock(name='amqp')
551 self.app.amqp = amqp
552 closed_pool = Mock(name='closed pool')
553 amqp.producer_pool = closed_pool
554 assert closed_pool is self.app.control.mailbox.producer_pool
555 self.app.control._after_fork()
556 new_pool = Mock(name='new pool')
557 amqp.producer_pool = new_pool
558 assert new_pool is self.app.control.mailbox.producer_pool
559
560 def test_control_exchange__default(self):
561 c = control.Control(self.app)
562 assert c.mailbox.namespace == 'celery'
563
564 def test_control_exchange__setting(self):
565 self.app.conf.control_exchange = 'test_exchange'
566 c = control.Control(self.app)
567 assert c.mailbox.namespace == 'test_exchange'
568
569 def test_control_mailbox_queue_options(self):
570 self.app.conf.control_queue_durable = True
571 self.app.conf.control_queue_exclusive = False
572
573 c = control.Control(self.app)
574 assert c.mailbox.queue_durable is True
575 assert c.mailbox.queue_exclusive is False
576
577 def test_control_mailbox_invalid_combination(self):
578 self.app.conf.control_queue_durable = True
579 self.app.conf.control_queue_exclusive = True
580
581 with pytest.raises(ImproperlyConfigured):
582 control.Control(self.app)