Ejemplo n.º 1
0
 def _context(self):
     Acons = ContextMock(name='consumerA')
     Bcons = ContextMock(name='consumerB')
     c = Cons([Acons, Bcons])
     _conn = c.connection = ContextMock(name='connection')
     est = c.establish_connection = Mock(name='est_connection')
     est.return_value = _conn
     return c, Acons, Bcons
Ejemplo n.º 2
0
    def test_maybe_conn_error(self):
        conn = ContextMock(name='connection')
        conn.connection_errors = (KeyError, )
        conn.channel_errors = ()

        self.c.connection = conn

        def raises():
            raise KeyError('foo')
        self.c.maybe_conn_error(raises)
Ejemplo n.º 3
0
    def test_run(self):
        conn = ContextMock(name='connection')
        self.c.connection = conn
        conn.connection_errors = (KeyError, )
        conn.channel_errors = ()
        consume = self.c.consume = Mock(name='c.consume')

        def se(*args, **kwargs):
            self.c.should_stop = True
            return [1]
        self.c.should_stop = False
        consume.side_effect = se
        self.c.run()
Ejemplo n.º 4
0
    def test_run_raises(self):
        conn = ContextMock(name='connection')
        self.c.connection = conn
        conn.connection_errors = (KeyError, )
        conn.channel_errors = ()
        consume = self.c.consume = Mock(name='c.consume')

        with patch('kombu.mixins.warn') as warn:
            def se_raises(*args, **kwargs):
                self.c.should_stop = True
                raise KeyError('foo')
            self.c.should_stop = False
            consume.side_effect = se_raises
            self.c.run()
            self.assertTrue(warn.called)
Ejemplo n.º 5
0
    def test_restore(self):
        message = Mock(name='message')
        with patch('kombu.transport.redis.loads') as loads:
            loads.return_value = 'M', 'EX', 'RK'
            client = self.channel._create_client = Mock(name='client')
            client = client()
            client.pipeline = ContextMock()
            restore = self.channel._do_restore_message = Mock(
                name='_do_restore_message', )
            pipe = client.pipeline.return_value
            pipe_hget = Mock(name='pipe.hget')
            pipe.hget.return_value = pipe_hget
            pipe_hget_hdel = Mock(name='pipe.hget.hdel')
            pipe_hget.hdel.return_value = pipe_hget_hdel
            result = Mock(name='result')
            pipe_hget_hdel.execute.return_value = None, None

            self.channel._restore(message)
            client.pipeline.assert_called_with()
            unacked_key = self.channel.unacked_key
            loads.assert_not_called()

            tag = message.delivery_tag
            pipe.hget.assert_called_with(unacked_key, tag)
            pipe_hget.hdel.assert_called_with(unacked_key, tag)
            pipe_hget_hdel.execute.assert_called_with()

            pipe_hget_hdel.execute.return_value = result, None
            self.channel._restore(message)
            loads.assert_called_with(result)
            restore.assert_called_with('M', 'EX', 'RK', client, False)
Ejemplo n.º 6
0
 def test_basic_publish(self):
     self.c.connection.transport.having_timeout = ContextMock()
     self.c._basic_publish('msg', 'ex', 'rkey')
     self.c.send_method.assert_called_with(
         spec.Basic.Publish, 'Bssbb',
         (0, 'ex', 'rkey', False, False), 'msg',
     )
Ejemplo n.º 7
0
 def get_insured_mocks(self, insured_returns=('works', 'ignored')):
     conn = ContextMock()
     pool = MockPool(conn)
     fun = Mock()
     insured = conn.autoretry.return_value = Mock()
     insured.return_value = insured_returns
     return conn, pool, fun, insured
Ejemplo n.º 8
0
 def test_send_event(self):
     mytask = self.mytask._get_current_object()
     mytask.app.events = Mock(name='events')
     mytask.app.events.attach_mock(ContextMock(), 'default_dispatcher')
     mytask.request.id = 'fb'
     mytask.send_event('task-foo', id=3122)
     mytask.app.events.default_dispatcher().send.assert_called_with(
         'task-foo', uuid='fb', id=3122)
Ejemplo n.º 9
0
 def setup(self):
     self.maybe_signature = self.patching('celery.canvas.maybe_signature')
     self.maybe_signature.side_effect = pass1
     self.app.producer_or_acquire = Mock()
     self.app.producer_or_acquire.attach_mock(ContextMock(), 'return_value')
     self.app.conf.task_always_eager = True
     self.task = builtins.add_group_task(self.app)
     BuiltinsCase.setup(self)
Ejemplo n.º 10
0
 def test_blocking_read__no_timeout(self):
     self.conn.on_inbound_frame = Mock(name='on_inbound_frame')
     self.conn.transport.having_timeout = ContextMock()
     ret = self.conn.blocking_read(None)
     self.conn.transport.read_frame.assert_called_with()
     self.conn.on_inbound_frame.assert_called_with(
         self.conn.transport.read_frame(), )
     assert ret is self.conn.on_inbound_frame()
Ejemplo n.º 11
0
    def get_ipublish_args(self, ensure_returns=None):
        producer = ContextMock()
        pool = MockPool(producer)
        fun = Mock()
        ensure_returns = ensure_returns or Mock()

        producer.connection.ensure.return_value = ensure_returns

        return producer, pool, fun, ensure_returns
Ejemplo n.º 12
0
    def test_run_restart_rate_limited(self):
        conn = ContextMock(name='connection')
        self.c.connection = conn
        conn.connection_errors = (KeyError, )
        conn.channel_errors = ()
        consume = self.c.consume = Mock(name='c.consume')
        with patch('kombu.mixins.sleep') as sleep:
            counter = [0]

            def se(*args, **kwargs):
                if counter[0] >= 1:
                    self.c.should_stop = True
                counter[0] += 1
                return counter
            self.c.should_stop = False
            consume.side_effect = se
            self.c.run()
            self.assertTrue(sleep.called)
Ejemplo n.º 13
0
    def assert_sent_with_ids(self, task, rid, pid, **options):
        self.app.amqp.send_task_message = Mock(name='send_task_message')
        self.app.backend = Mock()
        self.app.producer_or_acquire = ContextMock()

        task.apply_async(**options)
        self.app.amqp.send_task_message.assert_called()
        message = self.app.amqp.send_task_message.call_args[0][2]
        assert message.headers['parent_id'] == pid
        assert message.headers['root_id'] == rid
Ejemplo n.º 14
0
    def test_establish_connection(self):
        conn = ContextMock(name='connection')
        conn.clone.return_value = conn
        self.c.connection = conn
        self.c.connect_max_retries = 3

        with self.c.establish_connection() as conn:
            self.assertTrue(conn)
        conn.ensure_connection.assert_called_with(
            self.c.on_connection_error, 3,
        )
Ejemplo n.º 15
0
 def test_send_task__connection_provided(self):
     connection = Mock(name='connection')
     router = Mock(name='router')
     router.route.return_value = {}
     self.app.amqp = Mock(name='amqp')
     self.app.amqp.Producer.attach_mock(ContextMock(), 'return_value')
     self.app.send_task('foo', (1, 2), connection=connection, router=router)
     self.app.amqp.Producer.assert_called_with(connection)
     self.app.amqp.send_task_message.assert_called_with(
         self.app.amqp.Producer(), 'foo',
         self.app.amqp.create_task_message())
Ejemplo n.º 16
0
 def test_on_chord_part_return__other_error(self):
     with self.chord_context(1) as (_, request, callback):
         self.b.client.pipeline = ContextMock()
         raise_on_second_call(self.b.client.pipeline, RuntimeError())
         self.b.client.pipeline.return_value.zadd().zcount().get().expire(
         ).expire().execute.return_value = (1, 1, 0, 4, 5)
         task = self.app._tasks['add'] = Mock(name='add_task')
         self.b.on_chord_part_return(request, states.SUCCESS, 10)
         task.backend.fail_from_current_stack.assert_called_with(
             callback.id, exc=ANY,
         )
Ejemplo n.º 17
0
 def __init__(self, consumers):
     self.calls = Mock(name='ConsumerMixin')
     self.calls.get_consumers.return_value = consumers
     self.get_consumers = self.calls.get_consumers
     self.on_connection_revived = self.calls.on_connection_revived
     self.on_consume_ready = self.calls.on_consume_ready
     self.on_consume_end = self.calls.on_consume_end
     self.on_iteration = self.calls.on_iteration
     self.on_decode_error = self.calls.on_decode_error
     self.on_connection_error = self.calls.on_connection_error
     self.extra_context = ContextMock(name='extra_context')
     self.extra_context.return_value = self.extra_context
Ejemplo n.º 18
0
    def test_inherit_parent_priority_child_task(self):
        self.app.conf.task_inherit_parent_priority = True

        self.app.producer_or_acquire = Mock()
        self.app.producer_or_acquire.attach_mock(
            ContextMock(serializer='json'), 'return_value')
        self.app.amqp.send_task_message = Mock(name="send_task_message")

        self.task_which_calls_other_task.apply(args=[])

        self.app.amqp.send_task_message.assert_called_with(
            ANY, 't.unit.tasks.test_tasks.task_called_by_other_task',
            ANY, priority=5, queue=ANY, serializer=ANY)
Ejemplo n.º 19
0
 def test__consume_from(self):
     a = ContextMock(name='A')
     b = ContextMock(name='B')
     a.__enter__ = Mock(name='A.__enter__')
     b.__enter__ = Mock(name='B.__enter__')
     with self.c._consume_from(a, b):
         pass
     a.__enter__.assert_called_with()
     b.__enter__.assert_called_with()
Ejemplo n.º 20
0
    def test_on_chord_part_return__other_error__unordered(self):
        self.app.conf.result_backend_transport_options = dict(
            result_chord_ordered=False, )

        with self.chord_context(1) as (_, request, callback):
            self.b.client.pipeline = ContextMock()
            raise_on_second_call(self.b.client.pipeline, RuntimeError())
            self.b.client.pipeline.return_value.rpush().llen().get().expire(
            ).expire().execute.return_value = (1, 1, 0, 4, 5)
            task = self.app._tasks['add'] = Mock(name='add_task')
            self.b.on_chord_part_return(request, states.SUCCESS, 10)
            task.backend.fail_from_current_stack.assert_called_with(
                callback.id,
                exc=ANY,
            )
Ejemplo n.º 21
0
    def test_mutex(self, lock_id='xxx'):
        client = Mock(name='client')
        with patch('kombu.transport.redis.uuid') as uuid:
            # Won
            uuid.return_value = lock_id
            client.setnx.return_value = True
            client.pipeline = ContextMock()
            pipe = client.pipeline.return_value
            pipe.get.return_value = lock_id
            held = False
            with redis.Mutex(client, 'foo1', 100):
                held = True
            assert held
            client.setnx.assert_called_with('foo1', lock_id)
            pipe.get.return_value = 'yyy'
            held = False
            with redis.Mutex(client, 'foo1', 100):
                held = True
            assert held

            # Did not win
            client.expire.reset_mock()
            pipe.get.return_value = lock_id
            client.setnx.return_value = False
            with pytest.raises(redis.MutexHeld):
                held = False
                with redis.Mutex(client, 'foo1', '100'):
                    held = True
                assert not held
            client.ttl.return_value = 0
            with pytest.raises(redis.MutexHeld):
                held = False
                with redis.Mutex(client, 'foo1', '100'):
                    held = True
                assert not held
            client.expire.assert_called()

            # Wins but raises WatchError (and that is ignored)
            client.setnx.return_value = True
            pipe.watch.side_effect = redis.redis.WatchError()
            held = False
            with redis.Mutex(client, 'foo1', 100):
                held = True
            assert held
Ejemplo n.º 22
0
 def __init__(self, value=None):
     self.value = value or ContextMock()
Ejemplo n.º 23
0
def _amqp_connection():
    connection = ContextMock(name='Connection')
    connection.return_value = ContextMock(name='connection')
    connection.return_value.transport.driver_type = 'amqp'
    return connection