コード例 #1
0
 async def test_ok(self, event_loop, exchange):
     connection = cabbage.AmqpConnection(hosts=[(HOST, 5672)],
                                         loop=event_loop)
     rpc = cabbage.AsyncAmqpRpc(connection=connection)
     rpc.callback_exchange = exchange
     with patch('cabbage.amqp.aioamqp_connect') as mock_connect:
         mock_connect.return_value = (MockTransport(), MockProtocol())
         await rpc.connect()
     mock_connect.assert_called_once()
     # check that client is set up correctly:
     assert isinstance(rpc.channel, aioamqp.channel.Channel
                       )  # actually it's a mock pretending to be a Channel
     rpc.channel.queue_declare.assert_called_with(exclusive=True)
     rpc.channel.basic_consume.assert_called_once_with(
         callback=rpc._on_response, queue_name=rpc.callback_queue)
     if rpc.callback_exchange != '':
         rpc.channel.exchange_declare.assert_called_once_with(
             exchange_name=rpc.callback_exchange,
             type_name='topic',
             durable=True)
         rpc.channel.queue_bind.assert_called_once_with(
             queue_name=rpc.callback_queue,
             exchange_name=rpc.callback_exchange,
             routing_key=rpc.callback_queue)
     else:
         rpc.channel.exchange_declare.assert_not_called()
         rpc.channel.queue_bind.assert_not_called()
コード例 #2
0
    async def test_shuffle(self, shuffle):
        """
        Test for cabbage.AmqpConnection.cycle_hosts
        It checks correct work of parameter 'shuffle' and also changing the state of
        internal variable self.hosts during shuffling
        """

        # Generating random hosts
        hosts_length = 100
        hosts = [random.random() for _ in range(hosts_length)]

        # Creating a copy, and use its in constructor of AmqpConnection
        hosts_copy = hosts.copy()
        connection = cabbage.AmqpConnection(hosts_copy)

        # Retrieving one period of hosts after cycle hosts
        retrieved_hosts_two_periods = list(
            islice(connection.cycle_hosts(shuffle), hosts_length * 2))
        retrieved_hosts = retrieved_hosts_two_periods[0:hosts_length]

        # Check for correct work of cycle
        assert retrieved_hosts + retrieved_hosts == retrieved_hosts_two_periods

        # cycle_hosts() always includes self.hosts
        assert retrieved_hosts == hosts_copy

        # Check for shuffling
        assert (hosts == retrieved_hosts) != shuffle
コード例 #3
0
    async def test_retry(self, event_loop):
        """Keep reconnecting if an error occurs."""
        attempts = 10

        async def faulty_connect(_attempts=[attempts], **kwargs):
            assert kwargs == dict(host=HOST,
                                  port=PORT,
                                  login=USERNAME,
                                  password=PASSWORD,
                                  virtualhost=VIRTUALHOST,
                                  loop=event_loop,
                                  ssl=False)
            _attempts[0] -= 1
            if _attempts[0]:
                raise OSError('[Errno 113] Connect call failed')
            return mock_transport, mock_protocol

        connection = cabbage.AmqpConnection(hosts=[(HOST, PORT)],
                                            username=USERNAME,
                                            password=PASSWORD,
                                            virtualhost=VIRTUALHOST,
                                            loop=event_loop)
        mock_transport, mock_protocol = MockTransport(), MockProtocol()

        with patch('cabbage.amqp.aioamqp_connect',
                   new=faulty_connect), patch('asyncio.sleep') as mock_sleep:
            await connection.connect()

        assert mock_sleep.call_count == attempts - 1
        assert connection.transport is mock_transport
        assert connection.protocol is mock_protocol
コード例 #4
0
 def __init__(self, loop=None):
     self.loop = loop or asyncio.get_event_loop()
     self.connection = cabbage.AmqpConnection(hosts=[(TEST_RABBITMQ_HOST, 5672)], loop=self.loop)
     self.rpc = cabbage.AsyncAmqpRpc(
         connection=self.connection,
         subscriptions=[(self.handle, 'fake', '', 'fake')]
     )
     self.responses = {}
コード例 #5
0
    async def test_called_twice(self, event_loop):
        """Check that calling the method twice has no effect."""
        connection = cabbage.AmqpConnection(loop=event_loop)
        mock_transport, mock_protocol = MockTransport(), MockProtocol()
        connection.transport = mock_transport
        connection.protocol = mock_protocol
        with patch('cabbage.amqp.aioamqp_connect') as mock_connect:
            await connection.connect()

        mock_connect.assert_not_called()
        assert connection.transport is mock_transport
        assert connection.protocol is mock_protocol
コード例 #6
0
async def test_ok():
    fake_rpc = FakeRpcServer()
    fake_rpc.responses['abc'] = '123'
    await fake_rpc.run()

    conn = cabbage.AmqpConnection(hosts=[(TEST_RABBITMQ_HOST, 5672)])
    rpc = cabbage.AsyncAmqpRpc(connection=conn)
    await rpc.run()
    result = await rpc.send_rpc('fake', data='abc', await_response=True)
    assert result == '123'

    await rpc.stop()
    await fake_rpc.stop()
コード例 #7
0
    async def test_fatal_error(self, event_loop):
        """Some connection errors are not worth trying to recover from."""
        connection = cabbage.AmqpConnection(hosts=[('angrydev.ru', 80)],
                                            loop=event_loop)

        with pytest.raises(asyncio.streams.IncompleteReadError):
            with patch('cabbage.amqp.aioamqp_connect',
                       side_effect=asyncio.streams.IncompleteReadError(
                           [], 160)) as mock_connect:
                await connection.connect()

        mock_connect.assert_called_once()
        assert connection.transport is None
        assert connection.protocol is None
コード例 #8
0
    async def test_default_params(self, event_loop):
        """Check that AmqpConnection supplies reasonable defaults."""
        connection = cabbage.AmqpConnection(loop=event_loop)
        mock_transport, mock_protocol = MockTransport(), MockProtocol()
        with patch('cabbage.amqp.aioamqp_connect') as mock_connect:
            mock_connect.return_value = (mock_transport, mock_protocol)
            await connection.connect()

        mock_connect.assert_called_once_with(host='localhost',
                                             port=5672,
                                             login='******',
                                             password='******',
                                             virtualhost='/',
                                             loop=event_loop,
                                             ssl=False)
        assert connection.transport is mock_transport
        assert connection.protocol is mock_protocol
コード例 #9
0
    async def test_connect(self, event_loop):
        """Check typical connection call."""
        mock_transport, mock_protocol = MockTransport(), MockProtocol()
        connection = cabbage.AmqpConnection(hosts=[(HOST, PORT)],
                                            username=USERNAME,
                                            password=PASSWORD,
                                            virtualhost=VIRTUALHOST,
                                            loop=event_loop)
        with patch('cabbage.amqp.aioamqp_connect') as mock_connect:
            mock_connect.return_value = (mock_transport, mock_protocol)
            await connection.connect()

        mock_connect.assert_called_once_with(host=HOST,
                                             port=PORT,
                                             login=USERNAME,
                                             password=PASSWORD,
                                             virtualhost=VIRTUALHOST,
                                             loop=event_loop,
                                             ssl=False)
        assert connection.transport is mock_transport
        assert connection.protocol is mock_protocol
コード例 #10
0
def connection(event_loop):
    conn = cabbage.AmqpConnection(hosts=[(HOST, 5672)], loop=event_loop)
    conn.transport = MockTransport()
    conn.protocol = MockProtocol()
    return conn
コード例 #11
0
 async def test_no_protocol(self, event_loop):
     connection = cabbage.AmqpConnection(loop=event_loop)
     connection.protocol = MagicMock()
     await connection.disconnect()
     connection.protocol.close.assert_not_called()