def test_instantiate_link_and_connect_to_broker(mock_pikathread): """Test the Pika connection routine.""" transport = PikaTransport() assert not transport.is_connected() transport.connect() mock_pikathread.start.assert_called_once() mock_pikathread.start.side_effect = RuntimeError assert transport.is_connected() with pytest.raises(RuntimeError): transport.connect() mock_pikathread.join.assert_not_called() assert transport.is_connected() transport.disconnect() mock_pikathread.join.assert_called_once() mock_pikathread.connection_alive = False assert not transport.is_connected() transport.disconnect() assert mock_pikathread.join.call_count == 2 assert not transport.is_connected()
def test_error_handling_when_connecting_to_broker(mockpika, mock_pikathread): """Test the Pika connection routine.""" transport = PikaTransport() mock_pikathread.start.side_effect = pika.exceptions.AMQPConnectionError mockpika.exceptions = pika.exceptions with pytest.raises(workflows.Disconnected): transport.connect() assert transport.is_connected() is mock_pikathread.connection_alive
def test_error_handling_on_broadcast(mockpika): """Unrecoverable errors during broadcasting should lead to one reconnection attempt. Further errors should raise an Exception, further send attempts to try to reconnect.""" pytest.xfail("Don't understand send lifecycle errors yet") transport = PikaTransport() transport.connect() mockconn = mockpika.BlockingConnection mockchannel = mockconn.return_value.channel.return_value mockchannel.basic_publish.side_effect = pika.exceptions.AMQPChannelError() mockpika.exceptions = pika.exceptions assert mockconn.call_count == 1 with pytest.raises(workflows.Disconnected): transport._broadcast(str(mock.sentinel.channel), mock.sentinel.message) assert not transport.is_connected() assert mockconn.call_count == 2 mockchannel.basic_publish.side_effect = None transport._broadcast(str(mock.sentinel.channel), mock.sentinel.message) assert transport.is_connected() assert mockconn.call_count == 3
def test_error_handling_on_subscribing(mockpika, mock_pikathread): """Unrecoverable errors during subscribing should mark the connection as disconnected.""" mock_cb = mock.Mock() transport = PikaTransport() transport.connect() mockpika.exceptions = pika.exceptions mock_pikathread.connection_alive = False mock_pikathread.subscribe_queue.return_value.result.side_effect = ( pika.exceptions.AMQPChannelError) with pytest.raises(workflows.Disconnected): transport._subscribe(1, str(mock.sentinel.queue1), mock_cb) assert not transport.is_connected() # Now try connectionError instead of ChannelError mock_pikathread.subscribe_queue.return_value.result.side_effect = ( pika.exceptions.AMQPConnectionError) with pytest.raises(workflows.Disconnected): transport._subscribe(1, str(mock.sentinel.queue1), mock_cb) assert not transport.is_connected()