test_control.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. import sys
  4. import socket
  5. from collections import defaultdict
  6. from datetime import datetime, timedelta
  7. from case import Mock, call, patch
  8. from kombu import pidbox
  9. from kombu.utils.uuid import uuid
  10. from celery.five import Queue as FastQueue
  11. from celery.utils.timer2 import Timer
  12. from celery.worker import WorkController as _WC # noqa
  13. from celery.worker import consumer
  14. from celery.worker import control
  15. from celery.worker import state as worker_state
  16. from celery.worker.request import Request
  17. from celery.worker.state import revoked
  18. from celery.worker.pidbox import Pidbox, gPidbox
  19. from celery.utils.collections import AttributeDict
  20. hostname = socket.gethostname()
  21. class WorkController(object):
  22. autoscaler = None
  23. def stats(self):
  24. return {'total': worker_state.total_count}
  25. class Consumer(consumer.Consumer):
  26. def __init__(self, app):
  27. self.app = app
  28. self.buffer = FastQueue()
  29. self.timer = Timer()
  30. self.event_dispatcher = Mock()
  31. self.controller = WorkController()
  32. self.task_consumer = Mock()
  33. self.prefetch_multiplier = 1
  34. self.initial_prefetch_count = 1
  35. from celery.concurrency.base import BasePool
  36. self.pool = BasePool(10)
  37. self.task_buckets = defaultdict(lambda: None)
  38. self.hub = None
  39. def call_soon(self, p, *args, **kwargs):
  40. return p(*args, **kwargs)
  41. class test_Pidbox:
  42. def test_shutdown(self):
  43. with patch('celery.worker.pidbox.ignore_errors') as eig:
  44. parent = Mock()
  45. pbox = Pidbox(parent)
  46. pbox._close_channel = Mock()
  47. assert pbox.c is parent
  48. pconsumer = pbox.consumer = Mock()
  49. cancel = pconsumer.cancel
  50. pbox.shutdown(parent)
  51. eig.assert_called_with(parent, cancel)
  52. pbox._close_channel.assert_called_with(parent)
  53. class test_Pidbox_green:
  54. def test_stop(self):
  55. parent = Mock()
  56. g = gPidbox(parent)
  57. stopped = g._node_stopped = Mock()
  58. shutdown = g._node_shutdown = Mock()
  59. close_chan = g._close_channel = Mock()
  60. g.stop(parent)
  61. shutdown.set.assert_called_with()
  62. stopped.wait.assert_called_with()
  63. close_chan.assert_called_with(parent)
  64. assert g._node_stopped is None
  65. assert g._node_shutdown is None
  66. close_chan.reset()
  67. g.stop(parent)
  68. close_chan.assert_called_with(parent)
  69. def test_resets(self):
  70. parent = Mock()
  71. g = gPidbox(parent)
  72. g._resets = 100
  73. g.reset()
  74. assert g._resets == 101
  75. def test_loop(self):
  76. parent = Mock()
  77. conn = self.app.connection_for_read()
  78. parent.connection_for_read.return_value = conn
  79. drain = conn.drain_events = Mock()
  80. g = gPidbox(parent)
  81. parent.connection = Mock()
  82. do_reset = g._do_reset = Mock()
  83. call_count = [0]
  84. def se(*args, **kwargs):
  85. if call_count[0] > 2:
  86. g._node_shutdown.set()
  87. g.reset()
  88. call_count[0] += 1
  89. drain.side_effect = se
  90. g.loop(parent)
  91. assert do_reset.call_count == 4
  92. class test_ControlPanel:
  93. def setup(self):
  94. self.panel = self.create_panel(consumer=Consumer(self.app))
  95. @self.app.task(name='c.unittest.mytask', rate_limit=200, shared=False)
  96. def mytask():
  97. pass
  98. self.mytask = mytask
  99. def create_state(self, **kwargs):
  100. kwargs.setdefault('app', self.app)
  101. kwargs.setdefault('hostname', hostname)
  102. kwargs.setdefault('tset', set)
  103. return AttributeDict(kwargs)
  104. def create_panel(self, **kwargs):
  105. return self.app.control.mailbox.Node(
  106. hostname=hostname,
  107. state=self.create_state(**kwargs),
  108. handlers=control.Panel.data,
  109. )
  110. def test_enable_events(self):
  111. consumer = Consumer(self.app)
  112. panel = self.create_panel(consumer=consumer)
  113. evd = consumer.event_dispatcher
  114. evd.groups = set()
  115. panel.handle('enable_events')
  116. assert not evd.groups
  117. evd.groups = {'worker'}
  118. panel.handle('enable_events')
  119. assert 'task' in evd.groups
  120. evd.groups = {'task'}
  121. assert 'already enabled' in panel.handle('enable_events')['ok']
  122. def test_disable_events(self):
  123. consumer = Consumer(self.app)
  124. panel = self.create_panel(consumer=consumer)
  125. evd = consumer.event_dispatcher
  126. evd.enabled = True
  127. evd.groups = {'task'}
  128. panel.handle('disable_events')
  129. assert 'task' not in evd.groups
  130. assert 'already disabled' in panel.handle('disable_events')['ok']
  131. def test_clock(self):
  132. consumer = Consumer(self.app)
  133. panel = self.create_panel(consumer=consumer)
  134. panel.state.app.clock.value = 313
  135. x = panel.handle('clock')
  136. assert x['clock'] == 313
  137. def test_hello(self):
  138. consumer = Consumer(self.app)
  139. panel = self.create_panel(consumer=consumer)
  140. panel.state.app.clock.value = 313
  141. panel.state.hostname = 'elaine@vandelay.com'
  142. worker_state.revoked.add('revoked1')
  143. try:
  144. assert panel.handle('hello', {
  145. 'from_node': 'elaine@vandelay.com',
  146. }) is None
  147. x = panel.handle('hello', {
  148. 'from_node': 'george@vandelay.com',
  149. })
  150. assert x['clock'] == 314 # incremented
  151. x = panel.handle('hello', {
  152. 'from_node': 'george@vandelay.com',
  153. 'revoked': {'1234', '4567', '891'}
  154. })
  155. assert 'revoked1' in x['revoked']
  156. assert '1234' in x['revoked']
  157. assert '4567' in x['revoked']
  158. assert '891' in x['revoked']
  159. assert x['clock'] == 315 # incremented
  160. finally:
  161. worker_state.revoked.discard('revoked1')
  162. def test_conf(self):
  163. consumer = Consumer(self.app)
  164. panel = self.create_panel(consumer=consumer)
  165. panel.app = self.app
  166. panel.app.finalize()
  167. self.app.conf.some_key6 = 'hello world'
  168. x = panel.handle('dump_conf')
  169. assert 'some_key6' in x
  170. def test_election(self):
  171. consumer = Consumer(self.app)
  172. panel = self.create_panel(consumer=consumer)
  173. consumer.gossip = Mock()
  174. panel.handle(
  175. 'election', {'id': 'id', 'topic': 'topic', 'action': 'action'},
  176. )
  177. consumer.gossip.election.assert_called_with('id', 'topic', 'action')
  178. def test_election__no_gossip(self):
  179. consumer = Mock(name='consumer')
  180. consumer.gossip = None
  181. panel = self.create_panel(consumer=consumer)
  182. panel.handle(
  183. 'election', {'id': 'id', 'topic': 'topic', 'action': 'action'},
  184. )
  185. def test_heartbeat(self):
  186. consumer = Consumer(self.app)
  187. panel = self.create_panel(consumer=consumer)
  188. event_dispatcher = consumer.event_dispatcher
  189. event_dispatcher.enabled = True
  190. panel.handle('heartbeat')
  191. assert ('worker-heartbeat',) in event_dispatcher.send.call_args
  192. def test_time_limit(self):
  193. panel = self.create_panel(consumer=Mock())
  194. r = panel.handle('time_limit', arguments={
  195. 'task_name': self.mytask.name, 'hard': 30, 'soft': 10})
  196. assert self.mytask.time_limit == 30
  197. assert self.mytask.soft_time_limit == 10
  198. assert 'ok' in r
  199. r = panel.handle('time_limit', arguments={
  200. 'task_name': self.mytask.name, 'hard': None, 'soft': None})
  201. assert self.mytask.time_limit is None
  202. assert self.mytask.soft_time_limit is None
  203. assert 'ok' in r
  204. r = panel.handle('time_limit', arguments={
  205. 'task_name': '248e8afya9s8dh921eh928', 'hard': 30})
  206. assert 'error' in r
  207. def test_active_queues(self):
  208. import kombu
  209. x = kombu.Consumer(self.app.connection_for_read(),
  210. [kombu.Queue('foo', kombu.Exchange('foo'), 'foo'),
  211. kombu.Queue('bar', kombu.Exchange('bar'), 'bar')],
  212. auto_declare=False)
  213. consumer = Mock()
  214. consumer.task_consumer = x
  215. panel = self.create_panel(consumer=consumer)
  216. r = panel.handle('active_queues')
  217. assert list(sorted(q['name'] for q in r)) == ['bar', 'foo']
  218. def test_active_queues__empty(self):
  219. consumer = Mock(name='consumer')
  220. panel = self.create_panel(consumer=consumer)
  221. consumer.task_consumer = None
  222. assert not panel.handle('active_queues')
  223. def test_dump_tasks(self):
  224. info = '\n'.join(self.panel.handle('dump_tasks'))
  225. assert 'mytask' in info
  226. assert 'rate_limit=200' in info
  227. def test_dump_tasks2(self):
  228. prev, control.DEFAULT_TASK_INFO_ITEMS = (
  229. control.DEFAULT_TASK_INFO_ITEMS, [])
  230. try:
  231. info = '\n'.join(self.panel.handle('dump_tasks'))
  232. assert 'mytask' in info
  233. assert 'rate_limit=200' not in info
  234. finally:
  235. control.DEFAULT_TASK_INFO_ITEMS = prev
  236. def test_stats(self):
  237. prev_count, worker_state.total_count = worker_state.total_count, 100
  238. try:
  239. assert self.panel.handle('stats')['total'] == 100
  240. finally:
  241. worker_state.total_count = prev_count
  242. def test_report(self):
  243. self.panel.handle('report')
  244. def test_active(self):
  245. r = Request(
  246. self.TaskMessage(self.mytask.name, 'do re mi'),
  247. app=self.app,
  248. )
  249. worker_state.active_requests.add(r)
  250. try:
  251. assert self.panel.handle('dump_active')
  252. finally:
  253. worker_state.active_requests.discard(r)
  254. def test_pool_grow(self):
  255. class MockPool(object):
  256. def __init__(self, size=1):
  257. self.size = size
  258. def grow(self, n=1):
  259. self.size += n
  260. def shrink(self, n=1):
  261. self.size -= n
  262. @property
  263. def num_processes(self):
  264. return self.size
  265. consumer = Consumer(self.app)
  266. consumer.prefetch_multiplier = 8
  267. consumer.qos = Mock(name='qos')
  268. consumer.pool = MockPool(1)
  269. panel = self.create_panel(consumer=consumer)
  270. panel.handle('pool_grow')
  271. assert consumer.pool.size == 2
  272. consumer.qos.increment_eventually.assert_called_with(8)
  273. assert consumer.initial_prefetch_count == 16
  274. panel.handle('pool_shrink')
  275. assert consumer.pool.size == 1
  276. consumer.qos.decrement_eventually.assert_called_with(8)
  277. assert consumer.initial_prefetch_count == 8
  278. panel.state.consumer = Mock()
  279. panel.state.consumer.controller = Mock()
  280. sc = panel.state.consumer.controller.autoscaler = Mock()
  281. panel.handle('pool_grow')
  282. sc.force_scale_up.assert_called()
  283. panel.handle('pool_shrink')
  284. sc.force_scale_down.assert_called()
  285. def test_add__cancel_consumer(self):
  286. class MockConsumer(object):
  287. queues = []
  288. canceled = []
  289. consuming = False
  290. hub = Mock(name='hub')
  291. def add_queue(self, queue):
  292. self.queues.append(queue.name)
  293. def consume(self):
  294. self.consuming = True
  295. def cancel_by_queue(self, queue):
  296. self.canceled.append(queue)
  297. def consuming_from(self, queue):
  298. return queue in self.queues
  299. consumer = Consumer(self.app)
  300. consumer.task_consumer = MockConsumer()
  301. panel = self.create_panel(consumer=consumer)
  302. panel.handle('add_consumer', {'queue': 'MyQueue'})
  303. assert 'MyQueue' in consumer.task_consumer.queues
  304. assert consumer.task_consumer.consuming
  305. panel.handle('add_consumer', {'queue': 'MyQueue'})
  306. panel.handle('cancel_consumer', {'queue': 'MyQueue'})
  307. assert 'MyQueue' in consumer.task_consumer.canceled
  308. def test_revoked(self):
  309. worker_state.revoked.clear()
  310. worker_state.revoked.add('a1')
  311. worker_state.revoked.add('a2')
  312. try:
  313. assert sorted(self.panel.handle('dump_revoked')) == ['a1', 'a2']
  314. finally:
  315. worker_state.revoked.clear()
  316. def test_dump_schedule(self):
  317. consumer = Consumer(self.app)
  318. panel = self.create_panel(consumer=consumer)
  319. assert not panel.handle('dump_schedule')
  320. r = Request(
  321. self.TaskMessage(self.mytask.name, 'CAFEBABE'),
  322. app=self.app,
  323. )
  324. consumer.timer.schedule.enter_at(
  325. consumer.timer.Entry(lambda x: x, (r,)),
  326. datetime.now() + timedelta(seconds=10))
  327. consumer.timer.schedule.enter_at(
  328. consumer.timer.Entry(lambda x: x, (object(),)),
  329. datetime.now() + timedelta(seconds=10))
  330. assert panel.handle('dump_schedule')
  331. def test_dump_reserved(self):
  332. consumer = Consumer(self.app)
  333. req = Request(
  334. self.TaskMessage(self.mytask.name, args=(2, 2)), app=self.app,
  335. ) # ^ need to keep reference for reserved_tasks WeakSet.
  336. worker_state.task_reserved(req)
  337. try:
  338. panel = self.create_panel(consumer=consumer)
  339. response = panel.handle('dump_reserved', {'safe': True})
  340. assert response[0]['name'] == self.mytask.name
  341. assert response[0]['hostname'] == socket.gethostname()
  342. worker_state.reserved_requests.clear()
  343. assert not panel.handle('dump_reserved')
  344. finally:
  345. worker_state.reserved_requests.clear()
  346. def test_rate_limit_invalid_rate_limit_string(self):
  347. e = self.panel.handle('rate_limit', arguments={
  348. 'task_name': 'tasks.add', 'rate_limit': 'x1240301#%!'})
  349. assert 'Invalid rate limit string' in e.get('error')
  350. def test_rate_limit(self):
  351. class xConsumer(object):
  352. reset = False
  353. def reset_rate_limits(self):
  354. self.reset = True
  355. consumer = xConsumer()
  356. panel = self.create_panel(app=self.app, consumer=consumer)
  357. task = self.app.tasks[self.mytask.name]
  358. panel.handle('rate_limit', arguments={'task_name': task.name,
  359. 'rate_limit': '100/m'})
  360. assert task.rate_limit == '100/m'
  361. assert consumer.reset
  362. consumer.reset = False
  363. panel.handle('rate_limit', arguments={
  364. 'task_name': task.name,
  365. 'rate_limit': 0,
  366. })
  367. assert task.rate_limit == 0
  368. assert consumer.reset
  369. def test_rate_limit_nonexistant_task(self):
  370. self.panel.handle('rate_limit', arguments={
  371. 'task_name': 'xxxx.does.not.exist',
  372. 'rate_limit': '1000/s'})
  373. def test_unexposed_command(self):
  374. with pytest.raises(KeyError):
  375. self.panel.handle('foo', arguments={})
  376. def test_revoke_with_name(self):
  377. tid = uuid()
  378. m = {
  379. 'method': 'revoke',
  380. 'destination': hostname,
  381. 'arguments': {
  382. 'task_id': tid,
  383. 'task_name': self.mytask.name,
  384. },
  385. }
  386. self.panel.handle_message(m, None)
  387. assert tid in revoked
  388. def test_revoke_with_name_not_in_registry(self):
  389. tid = uuid()
  390. m = {
  391. 'method': 'revoke',
  392. 'destination': hostname,
  393. 'arguments': {
  394. 'task_id': tid,
  395. 'task_name': 'xxxxxxxxx33333333388888',
  396. },
  397. }
  398. self.panel.handle_message(m, None)
  399. assert tid in revoked
  400. def test_revoke(self):
  401. tid = uuid()
  402. m = {
  403. 'method': 'revoke',
  404. 'destination': hostname,
  405. 'arguments': {
  406. 'task_id': tid,
  407. },
  408. }
  409. self.panel.handle_message(m, None)
  410. assert tid in revoked
  411. m = {
  412. 'method': 'revoke',
  413. 'destination': 'does.not.exist',
  414. 'arguments': {
  415. 'task_id': tid + 'xxx',
  416. },
  417. }
  418. self.panel.handle_message(m, None)
  419. assert tid + 'xxx' not in revoked
  420. def test_revoke_terminate(self):
  421. request = Mock()
  422. request.id = tid = uuid()
  423. state = self.create_state()
  424. state.consumer = Mock()
  425. worker_state.task_reserved(request)
  426. try:
  427. r = control.revoke(state, tid, terminate=True)
  428. assert tid in revoked
  429. assert request.terminate.call_count
  430. assert 'terminate:' in r['ok']
  431. # unknown task id only revokes
  432. r = control.revoke(state, uuid(), terminate=True)
  433. assert 'tasks unknown' in r['ok']
  434. finally:
  435. worker_state.task_ready(request)
  436. def test_autoscale(self):
  437. self.panel.state.consumer = Mock()
  438. self.panel.state.consumer.controller = Mock()
  439. sc = self.panel.state.consumer.controller.autoscaler = Mock()
  440. sc.update.return_value = 10, 2
  441. m = {'method': 'autoscale',
  442. 'destination': hostname,
  443. 'arguments': {'max': '10', 'min': '2'}}
  444. r = self.panel.handle_message(m, None)
  445. assert 'ok' in r
  446. self.panel.state.consumer.controller.autoscaler = None
  447. r = self.panel.handle_message(m, None)
  448. assert 'error' in r
  449. def test_ping(self):
  450. m = {'method': 'ping',
  451. 'destination': hostname}
  452. r = self.panel.handle_message(m, None)
  453. assert r == {'ok': 'pong'}
  454. def test_shutdown(self):
  455. m = {'method': 'shutdown',
  456. 'destination': hostname}
  457. with pytest.raises(SystemExit):
  458. self.panel.handle_message(m, None)
  459. def test_panel_reply(self):
  460. replies = []
  461. class _Node(pidbox.Node):
  462. def reply(self, data, exchange, routing_key, **kwargs):
  463. replies.append(data)
  464. panel = _Node(
  465. hostname=hostname,
  466. state=self.create_state(consumer=Consumer(self.app)),
  467. handlers=control.Panel.data,
  468. mailbox=self.app.control.mailbox,
  469. )
  470. r = panel.dispatch('ping', reply_to={
  471. 'exchange': 'x',
  472. 'routing_key': 'x',
  473. })
  474. assert r == {'ok': 'pong'}
  475. assert replies[0] == {panel.hostname: {'ok': 'pong'}}
  476. def test_pool_restart(self):
  477. consumer = Consumer(self.app)
  478. consumer.controller = _WC(app=self.app)
  479. consumer.controller.consumer = consumer
  480. consumer.controller.pool.restart = Mock()
  481. consumer.reset_rate_limits = Mock(name='reset_rate_limits()')
  482. consumer.update_strategies = Mock(name='update_strategies()')
  483. consumer.event_dispatcher = Mock(name='evd')
  484. panel = self.create_panel(consumer=consumer)
  485. assert panel.state.consumer.controller.consumer is consumer
  486. panel.app = self.app
  487. _import = panel.app.loader.import_from_cwd = Mock()
  488. _reload = Mock()
  489. with pytest.raises(ValueError):
  490. panel.handle('pool_restart', {'reloader': _reload})
  491. self.app.conf.worker_pool_restarts = True
  492. panel.handle('pool_restart', {'reloader': _reload})
  493. consumer.controller.pool.restart.assert_called()
  494. consumer.reset_rate_limits.assert_called_with()
  495. consumer.update_strategies.assert_called_with()
  496. _reload.assert_not_called()
  497. _import.assert_not_called()
  498. consumer.controller.pool.restart.side_effect = NotImplementedError()
  499. panel.handle('pool_restart', {'reloader': _reload})
  500. consumer.controller.consumer = None
  501. panel.handle('pool_restart', {'reloader': _reload})
  502. @patch('celery.worker.worker.logger.debug')
  503. def test_pool_restart_import_modules(self, _debug):
  504. consumer = Consumer(self.app)
  505. consumer.controller = _WC(app=self.app)
  506. consumer.controller.consumer = consumer
  507. consumer.controller.pool.restart = Mock()
  508. consumer.reset_rate_limits = Mock(name='reset_rate_limits()')
  509. consumer.update_strategies = Mock(name='update_strategies()')
  510. panel = self.create_panel(consumer=consumer)
  511. panel.app = self.app
  512. assert panel.state.consumer.controller.consumer is consumer
  513. _import = consumer.controller.app.loader.import_from_cwd = Mock()
  514. _reload = Mock()
  515. self.app.conf.worker_pool_restarts = True
  516. with patch('sys.modules'):
  517. panel.handle('pool_restart', {
  518. 'modules': ['foo', 'bar'],
  519. 'reloader': _reload,
  520. })
  521. consumer.controller.pool.restart.assert_called()
  522. consumer.reset_rate_limits.assert_called_with()
  523. consumer.update_strategies.assert_called_with()
  524. _reload.assert_not_called()
  525. _import.assert_has_calls([call('bar'), call('foo')], any_order=True)
  526. assert _import.call_count == 2
  527. def test_pool_restart_reload_modules(self):
  528. consumer = Consumer(self.app)
  529. consumer.controller = _WC(app=self.app)
  530. consumer.controller.consumer = consumer
  531. consumer.controller.pool.restart = Mock()
  532. consumer.reset_rate_limits = Mock(name='reset_rate_limits()')
  533. consumer.update_strategies = Mock(name='update_strategies()')
  534. panel = self.create_panel(consumer=consumer)
  535. panel.app = self.app
  536. _import = panel.app.loader.import_from_cwd = Mock()
  537. _reload = Mock()
  538. self.app.conf.worker_pool_restarts = True
  539. with patch.dict(sys.modules, {'foo': None}):
  540. panel.handle('pool_restart', {
  541. 'modules': ['foo'],
  542. 'reload': False,
  543. 'reloader': _reload,
  544. })
  545. consumer.controller.pool.restart.assert_called()
  546. _reload.assert_not_called()
  547. _import.assert_not_called()
  548. _import.reset_mock()
  549. _reload.reset_mock()
  550. consumer.controller.pool.restart.reset_mock()
  551. panel.handle('pool_restart', {
  552. 'modules': ['foo'],
  553. 'reload': True,
  554. 'reloader': _reload,
  555. })
  556. consumer.controller.pool.restart.assert_called()
  557. _reload.assert_called()
  558. _import.assert_not_called()
  559. def test_query_task(self):
  560. consumer = Consumer(self.app)
  561. consumer.controller = _WC(app=self.app)
  562. consumer.controller.consumer = consumer
  563. panel = self.create_panel(consumer=consumer)
  564. panel.app = self.app
  565. req1 = Request(
  566. self.TaskMessage(self.mytask.name, args=(2, 2)),
  567. app=self.app,
  568. )
  569. worker_state.task_reserved(req1)
  570. try:
  571. assert not panel.handle('query_task', {'ids': {'1daa'}})
  572. ret = panel.handle('query_task', {'ids': {req1.id}})
  573. assert req1.id in ret
  574. assert ret[req1.id][0] == 'reserved'
  575. worker_state.active_requests.add(req1)
  576. try:
  577. ret = panel.handle('query_task', {'ids': {req1.id}})
  578. assert ret[req1.id][0] == 'active'
  579. finally:
  580. worker_state.active_requests.clear()
  581. ret = panel.handle('query_task', {'ids': {req1.id}})
  582. assert ret[req1.id][0] == 'reserved'
  583. finally:
  584. worker_state.reserved_requests.clear()