def test_broadcasting_message_with_expiration(mockpika, mock_pikathread): """Test sending a message that expires some time in the future""" message_lifetime = 120 transport = PikaTransport() transport.connect() mockproperties = mockpika.BasicProperties transport._broadcast(str(mock.sentinel.exchange), mock.sentinel.message, expiration=message_lifetime) mock_pikathread.send.assert_called_once() args, kwargs = mock_pikathread.send.call_args assert not args assert mockproperties.return_value.expiration == str(message_lifetime * 1000) assert kwargs == { "exchange": str(mock.sentinel.exchange), "routing_key": "", "body": mock.sentinel.message, "properties": mock.ANY, "mandatory": False, "transaction_id": None, }
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_send_broadcast(mockpika, mock_pikathread): """Test the broadcast sending function""" transport = PikaTransport() transport.connect() mockproperties = mockpika.BasicProperties transport._broadcast(str(mock.sentinel.exchange), mock.sentinel.message) mock_pikathread.send.assert_called_once() args, kwargs = mock_pikathread.send.call_args assert not args properties = mockproperties.call_args[1] assert properties.get("headers") in (None, {}) assert kwargs == { "exchange": str(mock.sentinel.exchange), "routing_key": "", "body": mock.sentinel.message, "properties": mock.ANY, "mandatory": False, "transaction_id": None, } transport._broadcast( str(mock.sentinel.exchange), mock.sentinel.message, headers=mock.sentinel.headers, ) mock_pikathread.send.call_count == 2 args, kwargs = mock_pikathread.send.call_args assert not args properties = mockproperties.call_args[1] assert properties == {"headers": mock.sentinel.headers, "delivery_mode": 2} assert kwargs == { "exchange": str(mock.sentinel.exchange), "routing_key": "", "body": mock.sentinel.message, "properties": mock.ANY, "mandatory": False, "transaction_id": None, } # Delay not implemented yet with pytest.raises(AssertionError): transport._broadcast(str(mock.sentinel.exchange), mock.sentinel.message, delay=123)