def test_configured_subscribers__callback(self): callback1 = Mock(name='callback1') callback1.return_value = ['http://a.com/1', 'http://a.com/2'] callback2 = Mock(name='callback2') callback2.return_value = iter(['http://b.com/1', 'http://b.com/2']) self._app.settings.THORN_SUBSCRIBERS = { 'foo.bar': [callback1, callback2], } self._app.Subscriber.from_dict = subscriber_from_dict assert list(self.dispatcher._configured_subscribers('foo.bar')) == [ dict(event='foo.bar', url='http://a.com/1'), dict(event='foo.bar', url='http://a.com/2'), dict(event='foo.bar', url='http://b.com/1'), dict(event='foo.bar', url='http://b.com/2'), ]
def test_find_module(): assert find_module('celery') imp = Mock() imp.return_value = None with pytest.raises(NotAPackage): find_module('foo.bar.baz', imp=imp) assert find_module('celery.worker.request')
def test_add_queue_when_not_auto_declare(self): consumer = self.connection.Consumer(auto_declare=False) q = Mock() q.return_value = q consumer.add_queue(q) assert q in consumer.queues assert not q.declare.call_count
def test_add_queue_when_auto_declare(self): consumer = self.connection.Consumer(auto_declare=True) q = Mock() q.return_value = q consumer.add_queue(q) assert q in consumer.queues q.declare.assert_called_with()
def test_subscribers_for_event(self): source1, source2 = Mock(name='source1'), Mock(name='source2') source1.return_value = [1, 2, 3] source2.return_value = [4, 5, 6] self.dispatcher.subscriber_sources = [source1, source2] assert list(self.dispatcher.subscribers_for_event('foo.bar')) == [ 1, 2, 3, 4, 5, 6 ]
def test_enter_after(self): t = Timer() t._enter = Mock() fun = Mock(name='fun') time = Mock(name='time') time.return_value = 10 t.enter_after(10, fun, time=time) time.assert_called_with() t._enter.assert_called_with(20, 0, fun)
def patch_unlock_retry(app): unlock = app.tasks['celery.chord_unlock'] retry = Mock() retry.return_value = Retry() prev, unlock.retry = unlock.retry, retry try: yield unlock, retry finally: unlock.retry = prev
def test_move_transform(self): trans = Mock(name="transform") trans.return_value = Queue("bar") with self.move_context(transform=trans) as (callback, pred, republish): pred.return_value = "foo" body, message = self.msgpair() with patch("celery.contrib.migrate.maybe_declare") as maybed: callback(body, message) trans.assert_called_with("foo") maybed.assert_called() republish.assert_called()
def test_on_message(self): c = self.Consumer() c.app.connection_for_read = _amqp_connection() g = Gossip(c) assert g.enabled prepare = Mock() prepare.return_value = 'worker-online', {} c.app.events.State.assert_called_with( on_node_join=g.on_node_join, on_node_leave=g.on_node_leave, max_tasks_in_memory=1, ) g.update_state = Mock() worker = Mock() g.on_node_join = Mock() g.on_node_leave = Mock() g.update_state.return_value = worker, 1 message = Mock() message.delivery_info = {'routing_key': 'worker-online'} message.headers = {'hostname': 'other'} handler = g.event_handlers['worker-online'] = Mock() g.on_message(prepare, message) handler.assert_called_with(message.payload) g.event_handlers = {} g.on_message(prepare, message) message.delivery_info = {'routing_key': 'worker-offline'} prepare.return_value = 'worker-offline', {} g.on_message(prepare, message) message.delivery_info = {'routing_key': 'worker-baz'} prepare.return_value = 'worker-baz', {} g.update_state.return_value = worker, 0 g.on_message(prepare, message) message.headers = {'hostname': g.hostname} g.on_message(prepare, message) g.clock.forward.assert_called_with()
def test_router_fun__called_with(self): self.simple_queue_setup() step = Mock(spec=['__call__']) step.return_value = None R = routes.prepare([step]) router = Router(self.app, R, self.app.amqp.queues) self.mytask.apply_async((2, 2), {'kw': 3}, router=router, priority=3) step.assert_called_with( self.mytask.name, (2, 2), {'kw': 3}, ANY, task=self.mytask, ) options = step.call_args[0][3] assert options['priority'] == 3