test_routes.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import pytest
  2. from case import ANY, Mock
  3. from kombu import Exchange, Queue
  4. from kombu.utils.functional import maybe_evaluate
  5. from celery.app import routes
  6. from celery.exceptions import QueueNotFound
  7. from celery.utils.imports import qualname
  8. def Router(app, *args, **kwargs):
  9. return routes.Router(*args, app=app, **kwargs)
  10. def E(app, queues):
  11. def expand(answer):
  12. return Router(app, [], queues).expand_destination(answer)
  13. return expand
  14. def set_queues(app, **queues):
  15. app.conf.task_queues = queues
  16. app.amqp.queues = app.amqp.Queues(queues)
  17. class RouteCase:
  18. def setup(self):
  19. self.a_queue = {
  20. 'exchange': 'fooexchange',
  21. 'exchange_type': 'fanout',
  22. 'routing_key': 'xuzzy',
  23. }
  24. self.b_queue = {
  25. 'exchange': 'barexchange',
  26. 'exchange_type': 'topic',
  27. 'routing_key': 'b.b.#',
  28. }
  29. self.d_queue = {
  30. 'exchange': self.app.conf.task_default_exchange,
  31. 'exchange_type': self.app.conf.task_default_exchange_type,
  32. 'routing_key': self.app.conf.task_default_routing_key,
  33. }
  34. @self.app.task(shared=False)
  35. def mytask(*args, **kwargs):
  36. pass
  37. self.mytask = mytask
  38. def assert_routes_to_queue(self, queue, router, name,
  39. args=[], kwargs={}, options={}):
  40. assert router.route(options, name, args, kwargs)['queue'].name == queue
  41. def assert_routes_to_default_queue(self, router, name, *args, **kwargs):
  42. self.assert_routes_to_queue(
  43. self.app.conf.task_default_queue, router, name, *args, **kwargs)
  44. class test_MapRoute(RouteCase):
  45. def test_route_for_task_expanded_route(self):
  46. set_queues(self.app, foo=self.a_queue, bar=self.b_queue)
  47. expand = E(self.app, self.app.amqp.queues)
  48. route = routes.MapRoute({self.mytask.name: {'queue': 'foo'}})
  49. assert expand(route(self.mytask.name))['queue'].name == 'foo'
  50. assert route('celery.awesome') is None
  51. def test_route_for_task(self):
  52. set_queues(self.app, foo=self.a_queue, bar=self.b_queue)
  53. expand = E(self.app, self.app.amqp.queues)
  54. route = routes.MapRoute({self.mytask.name: self.b_queue})
  55. eroute = expand(route(self.mytask.name))
  56. for key, value in self.b_queue.items():
  57. assert eroute[key] == value
  58. assert route('celery.awesome') is None
  59. def test_route_for_task__glob(self):
  60. route = routes.MapRoute([
  61. ('proj.tasks.*', 'routeA'),
  62. ('demoapp.tasks.bar.*', {'exchange': 'routeB'}),
  63. ])
  64. assert route('proj.tasks.foo') == {'queue': 'routeA'}
  65. assert route('demoapp.tasks.bar.moo') == {'exchange': 'routeB'}
  66. assert route('demoapp.foo.bar.moo') is None
  67. def test_expand_route_not_found(self):
  68. expand = E(self.app, self.app.amqp.Queues(
  69. self.app.conf.task_queues, False))
  70. route = routes.MapRoute({'a': {'queue': 'x'}})
  71. with pytest.raises(QueueNotFound):
  72. expand(route('a'))
  73. class test_lookup_route(RouteCase):
  74. def test_init_queues(self):
  75. router = Router(self.app, queues=None)
  76. assert router.queues == {}
  77. def test_lookup_takes_first(self):
  78. set_queues(self.app, foo=self.a_queue, bar=self.b_queue)
  79. R = routes.prepare(({self.mytask.name: {'queue': 'bar'}},
  80. {self.mytask.name: {'queue': 'foo'}}))
  81. router = Router(self.app, R, self.app.amqp.queues)
  82. self.assert_routes_to_queue('bar', router, self.mytask.name)
  83. def test_expands_queue_in_options(self):
  84. set_queues(self.app)
  85. R = routes.prepare(())
  86. router = Router(
  87. self.app, R, self.app.amqp.queues, create_missing=True,
  88. )
  89. # apply_async forwards all arguments, even exchange=None etc,
  90. # so need to make sure it's merged correctly.
  91. route = router.route(
  92. {'queue': 'testq',
  93. 'exchange': None,
  94. 'routing_key': None,
  95. 'immediate': False},
  96. self.mytask.name,
  97. args=[1, 2], kwargs={},
  98. )
  99. assert route['queue'].name == 'testq'
  100. assert route['queue'].exchange == Exchange('testq')
  101. assert route['queue'].routing_key == 'testq'
  102. assert route['immediate'] is False
  103. def test_expand_destination_string(self):
  104. set_queues(self.app, foo=self.a_queue, bar=self.b_queue)
  105. x = Router(self.app, {}, self.app.amqp.queues)
  106. dest = x.expand_destination('foo')
  107. assert dest['queue'].name == 'foo'
  108. def test_expand_destination__Queue(self):
  109. queue = Queue('foo')
  110. x = Router(self.app, {}, self.app.amqp.queues)
  111. dest = x.expand_destination({'queue': queue})
  112. assert dest['queue'] is queue
  113. def test_lookup_paths_traversed(self):
  114. self.simple_queue_setup()
  115. R = routes.prepare((
  116. {'celery.xaza': {'queue': 'bar'}},
  117. {self.mytask.name: {'queue': 'foo'}}
  118. ))
  119. router = Router(self.app, R, self.app.amqp.queues)
  120. self.assert_routes_to_queue('foo', router, self.mytask.name)
  121. self.assert_routes_to_default_queue(router, 'celery.poza')
  122. def test_compat_router_class(self):
  123. self.simple_queue_setup()
  124. R = routes.prepare((
  125. TestRouter(),
  126. ))
  127. router = Router(self.app, R, self.app.amqp.queues)
  128. self.assert_routes_to_queue('bar', router, 'celery.xaza')
  129. self.assert_routes_to_default_queue(router, 'celery.poza')
  130. def test_router_fun__called_with(self):
  131. self.simple_queue_setup()
  132. step = Mock(spec=['__call__'])
  133. step.return_value = None
  134. R = routes.prepare([step])
  135. router = Router(self.app, R, self.app.amqp.queues)
  136. self.mytask.apply_async((2, 2), {'kw': 3}, router=router, priority=3)
  137. step.assert_called_with(
  138. self.mytask.name, (2, 2), {'kw': 3}, ANY,
  139. task=self.mytask,
  140. )
  141. options = step.call_args[0][3]
  142. assert options['priority'] == 3
  143. def test_compat_router_classes__called_with(self):
  144. self.simple_queue_setup()
  145. step = Mock(spec=['route_for_task'])
  146. step.route_for_task.return_value = None
  147. R = routes.prepare([step])
  148. router = Router(self.app, R, self.app.amqp.queues)
  149. self.mytask.apply_async((2, 2), {'kw': 3}, router=router, priority=3)
  150. step.route_for_task.assert_called_with(
  151. self.mytask.name, (2, 2), {'kw': 3},
  152. )
  153. def simple_queue_setup(self):
  154. set_queues(
  155. self.app, foo=self.a_queue, bar=self.b_queue,
  156. **{self.app.conf.task_default_queue: self.d_queue})
  157. class TestRouter:
  158. def route_for_task(self, task, args, kwargs):
  159. if task == 'celery.xaza':
  160. return 'bar'
  161. class test_prepare:
  162. def test_prepare(self):
  163. o = object()
  164. R = [
  165. {'foo': 'bar'},
  166. qualname(TestRouter),
  167. o,
  168. ]
  169. p = routes.prepare(R)
  170. assert isinstance(p[0], routes.MapRoute)
  171. assert isinstance(maybe_evaluate(p[1]), TestRouter)
  172. assert p[2] is o
  173. assert routes.prepare(o) == [o]
  174. def test_prepare_item_is_dict(self):
  175. R = {'foo': 'bar'}
  176. p = routes.prepare(R)
  177. assert isinstance(p[0], routes.MapRoute)