Exemplo n.º 1
0
    def test_autoretry_backoff_jitter(self, randrange):
        task = self.autoretry_backoff_jitter_task
        task.max_retries = 3
        task.iterations = 0

        with patch.object(task, 'retry', wraps=task.retry) as fake_retry:
            task.apply(("http://httpbin.org/error", ))

        assert task.iterations == 4
        retry_call_countdowns = [
            call[1]['countdown'] for call in fake_retry.call_args_list
        ]
        assert retry_call_countdowns == [0, 1, 3, 7]
Exemplo n.º 2
0
 def test_set_timeout(self):
     # Checks that context manager sets and reverts timeout properly
     with patch.object(self.t, 'sock') as sock_mock:
         sock_mock.gettimeout.return_value = 3
         with self.t.having_timeout(5) as actual_sock:
             assert actual_sock == self.t.sock
         sock_mock.gettimeout.assert_called()
         sock_mock.settimeout.assert_has_calls(
             [
                 call(5),
                 call(3),
             ]
         )
Exemplo n.º 3
0
    def test_method_called(self):
        from kombu.transport.redis import SentinelChannel

        with patch.object(SentinelChannel, '_sentinel_managed_pool') as p:
            connection = Connection(
                'sentinel://localhost:65534/',
                transport_options={
                    'master_name': 'not_important',
                },
            )

            connection.channel()
            p.assert_called()
Exemplo n.º 4
0
    def test_channel_ignore_methods_during_close(self):
        # Test checking that py-amqp will discard any received methods
        # except Close and Close-OK after sending Channel.Close method
        # to server.
        frame_writer_cls_mock = Mock()
        conn = Connection(frame_writer=frame_writer_cls_mock)
        consumer_tag = 'amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg'
        with patch.object(conn, 'Transport') as transport_mock:
            handshake(conn, transport_mock)

            channel_id = 1
            transport_mock().read_frame.side_effect = [
                # Inject Open Handshake
                build_frame_type_1(spec.Channel.OpenOk,
                                   channel=channel_id,
                                   args=(1, False),
                                   arg_format='Lb'),
                # Inject basic-deliver response
                build_frame_type_1(
                    spec.Basic.Deliver,
                    channel=1,
                    arg_format='sLbss',
                    args=(
                        # consumer-tag, delivery-tag, redelivered,
                        consumer_tag,
                        1,
                        False,
                        # exchange-name, routing-key
                        'foo_exchange',
                        'routing-key')),
                build_frame_type_2(channel=1,
                                   body_len=12,
                                   properties=b'0\x00\x00\x00\x00\x00\x01'),
                build_frame_type_3(channel=1, body=b'Hello World!'),
                # Inject close method
                build_frame_type_1(spec.Channel.CloseOk, channel=channel_id),
            ]

            frame_writer_mock = frame_writer_cls_mock()
            frame_writer_mock.reset_mock()

            with patch('amqp.Channel._on_basic_deliver') as on_deliver_mock:
                ch = conn.channel(channel_id=channel_id)
                ch.close()
                on_deliver_mock.assert_not_called()
            frame_writer_mock.assert_has_calls([
                call(1, 1, spec.Channel.Open, dumps('s', ('', )), None),
                call(1, 1, spec.Channel.Close, dumps('BsBB', (0, '', 0, 0)),
                     None)
            ])
            assert ch.is_open is False
Exemplo n.º 5
0
    def test_unlock_with_chord_params(self):
        @self.app.task(shared=False)
        def mul(x, y):
            return x * y

        from celery import chord
        ch = chord(group(mul.s(1, 1), mul.s(2, 2)), mul.s(), interval=10)

        with patch.object(ch, 'run') as run:
            ch.apply_async()
            run.assert_called_once_with(group(mul.s(1, 1), mul.s(2, 2)),
                                        mul.s(), (),
                                        task_id=None,
                                        interval=10)
Exemplo n.º 6
0
 def test_set_timeout_exception_raised(self):
     # Checks that context manager sets and reverts timeout properly
     # when exception is raised.
     with patch.object(self.t, 'sock') as sock_mock:
         sock_mock.gettimeout.return_value = 3
         with pytest.raises(DummyException):
             with self.t.having_timeout(5) as actual_sock:
                 assert actual_sock == self.t.sock
                 raise DummyException()
         sock_mock.gettimeout.assert_called()
         sock_mock.settimeout.assert_has_calls([
             call(5),
             call(3),
         ])
Exemplo n.º 7
0
 def test_connect_multiple_addr_entries_succeed(self):
     with patch('socket.socket', return_value=MockSocket()) as sock_mock, \
         patch('socket.getaddrinfo',
               return_value=[
                   (socket.AF_INET, 1, socket.IPPROTO_TCP,
                       '', ('127.0.0.1', 5672)),
                   (socket.AF_INET, 1, socket.IPPROTO_TCP,
                       '', ('127.0.0.2', 5672))
               ]):
         self.t.sock = Mock()
         self.t.close()
         with patch.object(sock_mock.return_value, 'connect',
                           side_effect=(socket.error, None)):
             self.t.connect()
Exemplo n.º 8
0
    def test_basic_publish(self):
        # Test verifing publishing message.
        frame_writer_cls_mock = Mock()
        conn = Connection(frame_writer=frame_writer_cls_mock)
        with patch.object(conn, 'Transport') as transport_mock:
            handshake(conn, transport_mock)
            ch = create_channel(1, conn, transport_mock)

            frame_writer_mock = frame_writer_cls_mock()
            frame_writer_mock.reset_mock()
            msg = Message('test')
            ch.basic_publish(msg)
            frame_writer_mock.assert_called_once_with(
                1, 1, spec.Basic.Publish,
                dumps('Bssbb', (0, '', '', False, False)), msg)
Exemplo n.º 9
0
    def test_connect(self):
        # Test checking connection handshake
        frame_writer_cls_mock = Mock()
        on_open_mock = Mock()
        frame_writer_mock = frame_writer_cls_mock()
        conn = Connection(
            frame_writer=frame_writer_cls_mock, on_open=on_open_mock
        )

        with patch.object(conn, 'Transport') as transport_mock:
            handshake(conn, transport_mock)
            on_open_mock.assert_called_once_with(conn)
            security_mechanism = sasl.AMQPLAIN(
                'guest', 'guest'
            ).start(conn).decode('utf-8', 'surrogatepass')

            # Expected responses from client
            frame_writer_mock.assert_has_calls(
                [
                    call(
                        1, 0, spec.Connection.StartOk,
                        # Due Table type, we cannot compare bytestream directly
                        DataComparator(
                            'FsSs',
                            (
                                CLIENT_PROPERTIES, 'AMQPLAIN',
                                security_mechanism,
                                'en_US'
                            )
                        ),
                        None
                    ),
                    call(
                        1, 0, spec.Connection.TuneOk,
                        dumps(
                            'BlB',
                            (conn.channel_max, conn.frame_max, conn.heartbeat)
                        ),
                        None
                    ),
                    call(
                        1, 0, spec.Connection.Open,
                        dumps('ssb', (conn.virtual_host, '', False)),
                        None
                    )
                ]
            )
            assert conn.client_properties == CLIENT_PROPERTIES
Exemplo n.º 10
0
 def test_connect_short_curcuit_on_INET_fails(self):
     with patch('socket.socket', return_value=MockSocket()) as sock_mock, \
         patch('socket.getaddrinfo',
               side_effect=[
                   [(socket.AF_INET, 1, socket.IPPROTO_TCP,
                       '', ('127.0.0.1', 5672))],
                   [(socket.AF_INET6, 1, socket.IPPROTO_TCP,
                       '', ('::1', 5672))]
               ]) as getaddrinfo:
         self.t.sock = Mock()
         self.t.close()
         with patch.object(sock_mock.return_value, 'connect',
                           side_effect=(socket.error, None)):
             self.t.connect()
         getaddrinfo.assert_has_calls(
             [call('localhost', 5672, addr_type, ANY, ANY)
              for addr_type in (socket.AF_INET, socket.AF_INET6)])
Exemplo n.º 11
0
    def test_set_timeout_ewouldblock_exc(self):
        # We expect EWOULDBLOCK to be handled as a timeout.
        with patch.object(self.t, 'sock') as sock_mock:
            sock_mock.gettimeout.return_value = 3
            with pytest.raises(socket.timeout):
                with self.t.having_timeout(5):
                    err = socket.error()
                    err.errno = errno.EWOULDBLOCK
                    raise err

            class DummySocketError(socket.error):
                pass

            # Other socket errors shouldn't be converted.
            with pytest.raises(DummySocketError):
                with self.t.having_timeout(5):
                    raise DummySocketError()
Exemplo n.º 12
0
 def test_connection_close(self):
     # Test checking closing connection
     frame_writer_cls_mock = Mock()
     frame_writer_mock = frame_writer_cls_mock()
     conn = Connection(frame_writer=frame_writer_cls_mock)
     with patch.object(conn, 'Transport') as transport_mock:
         handshake(conn, transport_mock)
         frame_writer_mock.reset_mock()
         # Inject CloseOk response from broker
         transport_mock().read_frame.return_value = ret_factory(
             spec.Connection.CloseOk, args=(1, False), arg_format='Lb')
         t = conn.transport
         conn.close()
         frame_writer_mock.assert_called_once_with(
             1, 0, spec.Connection.Close, dumps('BsBB', (0, '', 0, 0)),
             None)
         t.close.assert_called_once_with()
Exemplo n.º 13
0
 def test_exchange_declare_error(self, reply_code, reply_text, exception):
     # Test verifying wrong declaring exchange
     frame_writer_cls_mock = Mock()
     conn = Connection(frame_writer=frame_writer_cls_mock)
     with patch.object(conn, 'Transport') as transport_mock:
         handshake(conn, transport_mock)
         ch = create_channel(1, conn, transport_mock)
         transport_mock().read_frame.return_value = build_frame_type_1(
             spec.Connection.Close,
             args=(reply_code, reply_text) + spec.Exchange.Declare,
             arg_format='BsBB')
         frame_writer_mock = frame_writer_cls_mock()
         frame_writer_mock.reset_mock()
         with pytest.raises(exception) as excinfo:
             ch.exchange_declare('exchange', 'exchange-type')
         assert excinfo.value.code == reply_code
         assert excinfo.value.message == reply_text
         assert excinfo.value.method == 'Exchange.declare'
         assert excinfo.value.method_name == 'Exchange.declare'
         assert excinfo.value.method_sig == spec.Exchange.Declare
         # Client is sending to broker:
         # 1. Exchange Declare
         # 2. Connection.CloseOk as reply to received Connecton.Close
         frame_writer_calls = [
             call(
                 1,
                 1,
                 spec.Exchange.Declare,
                 dumps(
                     'BssbbbbbF',
                     (
                         0,
                         # exchange, type, passive, durable,
                         'exchange',
                         'exchange-type',
                         False,
                         False,
                         # auto_delete, internal, nowait, arguments
                         True,
                         False,
                         False,
                         None)),
                 None),
             call(1, 0, spec.Connection.CloseOk, '', None),
         ]
         frame_writer_mock.assert_has_calls(frame_writer_calls)
Exemplo n.º 14
0
    def test_channel_open_close(self):
        # Test checking opening and closing channel
        frame_writer_cls_mock = Mock()
        conn = Connection(frame_writer=frame_writer_cls_mock)
        with patch.object(conn, 'Transport') as transport_mock:
            handshake(conn, transport_mock)

            channel_id = 1
            transport_mock().read_frame.side_effect = [
                # Inject Open Handshake
                build_frame_type_1(
                    spec.Channel.OpenOk,
                    channel=channel_id,
                    args=(1, False),
                    arg_format='Lb'
                ),
                # Inject close method
                build_frame_type_1(
                    spec.Channel.CloseOk,
                    channel=channel_id
                )
            ]

            frame_writer_mock = frame_writer_cls_mock()
            frame_writer_mock.reset_mock()

            on_open_mock = Mock()
            ch = conn.channel(channel_id=channel_id, callback=on_open_mock)
            on_open_mock.assert_called_once_with(ch)
            assert ch.is_open is True

            ch.close()
            frame_writer_mock.assert_has_calls(
                [
                    call(
                        1, 1, spec.Channel.Open, dumps('s', ('',)),
                        None
                    ),
                    call(
                        1, 1, spec.Channel.Close, dumps('BsBB', (0, '', 0, 0)),
                        None
                    )
                ]
            )
            assert ch.is_open is False
Exemplo n.º 15
0
    def test_close_database(self):
        with self.fixup_context(self.app) as (f, _, _):
            with patch.object(f, '_close_database') as _close:
                f.db_reuse_max = None
                f.close_database()
                _close.assert_called_with()
                _close.reset_mock()

                f.db_reuse_max = 10
                f._db_recycles = 3
                f.close_database()
                _close.assert_not_called()
                assert f._db_recycles == 4
                _close.reset_mock()

                f._db_recycles = 20
                f.close_database()
                _close.assert_called_with()
                assert f._db_recycles == 1
Exemplo n.º 16
0
 def test_queue_purge(self):
     # Test verifying purging queue
     frame_writer_cls_mock = Mock()
     conn = Connection(frame_writer=frame_writer_cls_mock)
     with patch.object(conn, 'Transport') as transport_mock:
         handshake(conn, transport_mock)
         ch = create_channel(1, conn, transport_mock)
         transport_mock().read_frame.return_value = build_frame_type_1(
             spec.Queue.PurgeOk, channel=1, arg_format='l', args=(4, ))
         frame_writer_mock = frame_writer_cls_mock()
         frame_writer_mock.reset_mock()
         msg_count = ch.queue_purge('foo')
         assert msg_count == 4
         frame_writer_mock.assert_called_once_with(
             1,
             1,
             spec.Queue.Purge,
             dumps(
                 'Bsb',
                 # queue, nowait
                 (0, 'foo', False)),
             None)
Exemplo n.º 17
0
 def test_queue_get_empty(self):
     # Test verifying getting message from empty queue
     frame_writer_cls_mock = Mock()
     conn = Connection(frame_writer=frame_writer_cls_mock)
     with patch.object(conn, 'Transport') as transport_mock:
         handshake(conn, transport_mock)
         ch = create_channel(1, conn, transport_mock)
         transport_mock().read_frame.return_value = build_frame_type_1(
             spec.Basic.GetEmpty, channel=1, arg_format='s', args=('s'))
         frame_writer_mock = frame_writer_cls_mock()
         frame_writer_mock.reset_mock()
         ret = ch.basic_get('foo')
         assert ret is None
         frame_writer_mock.assert_called_once_with(
             1,
             1,
             spec.Basic.Get,
             dumps(
                 'Bsb',
                 # queue, nowait
                 (0, 'foo', False)),
             None)
Exemplo n.º 18
0
 def test_connecion_ignore_methods_during_close(self, on_blocked_mock):
     # Test checking that py-amqp will discard any received methods
     # except Close and Close-OK after sending Connecion.Close method
     # to server.
     frame_writer_cls_mock = Mock()
     frame_writer_mock = frame_writer_cls_mock()
     conn = Connection(frame_writer=frame_writer_cls_mock)
     with patch.object(conn, 'Transport') as transport_mock:
         handshake(conn, transport_mock)
         frame_writer_mock.reset_mock()
         # Inject CloseOk response from broker
         transport_mock().read_frame.side_effect = [
             build_frame_type_1(spec.Connection.Blocked, channel=0),
             build_frame_type_1(spec.Connection.CloseOk)
         ]
         t = conn.transport
         conn.close()
         on_blocked_mock.assert_not_called()
         frame_writer_mock.assert_called_once_with(
             1, 0, spec.Connection.Close, dumps('BsBB', (0, '', 0, 0)),
             None)
         t.close.assert_called_once_with()
Exemplo n.º 19
0
 def test_received_channel_Close_during_connection_close(self):
     # This test verifies that library handles correctly closing channel
     # during closing of connection:
     # 1. User requests closing connection - client sends Connection.Close
     # 2. Broker requests closing Channel - client receives Channel.Close
     # 3. Broker sends Connection.CloseOk
     # see GitHub issue #218
     conn = Connection()
     with patch.object(conn, 'Transport') as transport_mock:
         handshake(conn, transport_mock)
         channel_id = 1
         create_channel(channel_id, conn, transport_mock)
         # Replies sent by broker
         transport_mock().read_frame.side_effect = [
             # Inject close methods
             build_frame_type_1(spec.Channel.Close,
                                channel=channel_id,
                                args=(1, False),
                                arg_format='Lb'),
             build_frame_type_1(spec.Connection.CloseOk)
         ]
         conn.close()
Exemplo n.º 20
0
    def test_create_broadcast_cursor(self):
        import pymongo

        with patch.object(pymongo, 'version_tuple', (2, )):
            self.channel._create_broadcast_cursor(
                'fanout_exchange',
                'foo',
                '*',
                'foobar',
            )

            self.assert_collection_accessed('messages.broadcast')
            self.assert_operation_called_with(
                'broadcast',
                'find',
                tailable=True,
                query={'queue': 'fanout_exchange'},
                sort=[('$natural', pymongo.ASCENDING)],
            )

        if pymongo.version_tuple >= (3, ):
            self.channel._create_broadcast_cursor(
                'fanout_exchange1',
                'foo',
                '*',
                'foobar',
            )

            self.assert_collection_accessed('messages.broadcast')
            self.assert_operation_called_with(
                'broadcast',
                'find',
                cursor_type=pymongo.CursorType.TAILABLE,
                filter={'queue': 'fanout_exchange1'},
                sort=[('$natural', pymongo.ASCENDING)],
            )
Exemplo n.º 21
0
 def test_queue_declare(self):
     # Test verifying declaring queue
     frame_writer_cls_mock = Mock()
     conn = Connection(frame_writer=frame_writer_cls_mock)
     with patch.object(conn, 'Transport') as transport_mock:
         handshake(conn, transport_mock)
         ch = create_channel(1, conn, transport_mock)
         transport_mock().read_frame.return_value = build_frame_type_1(
             spec.Queue.DeclareOk,
             channel=1,
             arg_format='sll',
             args=('foo', 1, 2))
         frame_writer_mock = frame_writer_cls_mock()
         frame_writer_mock.reset_mock()
         ret = ch.queue_declare('foo')
         assert ret == queue_declare_ok_t(queue='foo',
                                          message_count=1,
                                          consumer_count=2)
         frame_writer_mock.assert_called_once_with(
             1,
             1,
             spec.Queue.Declare,
             dumps(
                 'BsbbbbbF',
                 (
                     0,
                     # queue, passive, durable, exclusive,
                     'foo',
                     False,
                     False,
                     False,
                     # auto_delete, nowait, arguments
                     True,
                     False,
                     None)),
             None)
Exemplo n.º 22
0
 def test_task_with_result(self):
     with patch.object(self.app, 'send_task') as send_task:
         self.mytask.apply_async()
         expected_args, expected_kwargs = self.common_send_task_arguments()
         send_task.assert_called_once_with(*expected_args,
                                           **expected_kwargs)
Exemplo n.º 23
0
    def test_connect_missing_capabilities(self):
        # Test checking connection handshake with broker
        # supporting subset of capabilities
        frame_writer_cls_mock = Mock()
        on_open_mock = Mock()
        frame_writer_mock = frame_writer_cls_mock()
        conn = Connection(
            frame_writer=frame_writer_cls_mock, on_open=on_open_mock
        )

        with patch.object(conn, 'Transport') as transport_mock:
            server_properties = dict(SERVER_PROPERTIES)
            server_properties['capabilities'] = {
                # This capability is not supported by client
                'basic.nack': True,
                'consumer_cancel_notify': True,
                'connection.blocked': False,
                # server does not support 'authentication_failure_close'
                # which is supported by client
            }

            client_properties = dict(CLIENT_PROPERTIES)
            client_properties['capabilities'] = {
                'consumer_cancel_notify': True,
            }

            handshake(
                conn, transport_mock, server_properties=server_properties
            )
            on_open_mock.assert_called_once_with(conn)
            security_mechanism = sasl.AMQPLAIN(
                'guest', 'guest'
            ).start(conn).decode('utf-8', 'surrogatepass')

            # Expected responses from client
            frame_writer_mock.assert_has_calls(
                [
                    call(
                        1, 0, spec.Connection.StartOk,
                        # Due Table type, we cannot compare bytestream directly
                        DataComparator(
                            'FsSs',
                            (
                                client_properties, 'AMQPLAIN',
                                security_mechanism,
                                'en_US'
                            )
                        ),
                        None
                    ),
                    call(
                        1, 0, spec.Connection.TuneOk,
                        dumps(
                            'BlB',
                            (conn.channel_max, conn.frame_max, conn.heartbeat)
                        ),
                        None
                    ),
                    call(
                        1, 0, spec.Connection.Open,
                        dumps('ssb', (conn.virtual_host, '', False)),
                        None
                    )
                ]
            )
            assert conn.client_properties == client_properties
Exemplo n.º 24
0
    def test_basic_deliver(self):
        # Test checking delivering single message
        callback_mock = Mock()
        frame_writer_cls_mock = Mock()
        conn = Connection(frame_writer=frame_writer_cls_mock)
        consumer_tag = 'amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg'
        with patch.object(conn, 'Transport') as transport_mock:
            handshake(conn, transport_mock)
            ch = create_channel(1, conn, transport_mock)

            # Inject ConsumeOk response from Broker
            transport_mock().read_frame.side_effect = [
                # Inject Consume-ok response
                build_frame_type_1(
                    spec.Basic.ConsumeOk,
                    channel=1,
                    args=(consumer_tag,),
                    arg_format='s'
                ),
                # Inject basic-deliver response
                build_frame_type_1(
                    spec.Basic.Deliver,
                    channel=1,
                    arg_format='sLbss',
                    args=(
                        # consumer-tag, delivery-tag, redelivered,
                        consumer_tag, 1, False,
                        # exchange-name, routing-key
                        'foo_exchange', 'routing-key'
                    )
                ),
                build_frame_type_2(
                    channel=1,
                    body_len=12,
                    properties=b'0\x00\x00\x00\x00\x00\x01'
                ),
                build_frame_type_3(
                    channel=1,
                    body=b'Hello World!'
                ),
            ]
            frame_writer_mock = frame_writer_cls_mock()
            frame_writer_mock.reset_mock()
            ch.basic_consume('my_queue', callback=callback_mock)
            conn.drain_events()
            callback_mock.assert_called_once_with(ANY)
            msg = callback_mock.call_args[0][0]
            assert isinstance(msg, Message)
            assert msg.body_size == 12
            assert msg.body == b'Hello World!'
            assert msg.frame_method == spec.Basic.Deliver
            assert msg.delivery_tag == 1
            assert msg.ready is True
            assert msg.delivery_info == {
                'consumer_tag': 'amq.ctag-PCmzXGkhCw_v0Zq7jXyvkg',
                'delivery_tag': 1,
                'redelivered': False,
                'exchange': 'foo_exchange',
                'routing_key': 'routing-key'
            }
            assert msg.properties == {
                'application_headers': {}, 'delivery_mode': 1
            }