def test_send_message(mockstomp): """Test the message sending function.""" stomp = StompTransport() stomp.connect() mockconn = mockstomp.Connection.return_value stomp._send(str(mock.sentinel.channel), mock.sentinel.message) mockconn.send.assert_called_once() args, kwargs = mockconn.send.call_args assert args == ("/queue/" + str(mock.sentinel.channel), mock.sentinel.message) assert kwargs.get("headers") == {"persistent": "true"} stomp._send( str(mock.sentinel.channel), mock.sentinel.message, headers={"hdr": mock.sentinel.header}, delay=123, ) assert mockconn.send.call_count == 2 args, kwargs = mockconn.send.call_args assert args == ("/queue/" + str(mock.sentinel.channel), mock.sentinel.message) assert kwargs == { "headers": { "hdr": mock.sentinel.header, "persistent": "true", "AMQ_SCHEDULED_DELAY": 123000, } }
def test_error_handling_on_send(mockstomp): """Unrecoverable errors during sending should mark the connection as disconnected.""" stomp = StompTransport() stomp.connect() mockconn = mockstomp.Connection.return_value mockconn.send.side_effect = stomppy.exception.NotConnectedException() mockstomp.exception = stomppy.exception with pytest.raises(workflows.Disconnected): stomp._send(str(mock.sentinel.channel), mock.sentinel.message) assert not stomp.is_connected()
def test_sending_message_with_expiration(time, mockstomp): """Test sending a message that expires some time in the future.""" system_time = 1234567.1234567 message_lifetime = 120 expiration_time = int((system_time + message_lifetime) * 1000) time.time.return_value = system_time stomp = StompTransport() stomp.connect() mockconn = mockstomp.Connection.return_value stomp._send(str(mock.sentinel.channel), mock.sentinel.message, expiration=120) mockconn.send.assert_called_once() args, kwargs = mockconn.send.call_args assert args == ("/queue/" + str(mock.sentinel.channel), mock.sentinel.message) assert kwargs.get("headers") == { "persistent": "true", "expires": expiration_time }
def test_transaction_calls(mockstomp): """Test that calls to create, commit, abort transactions are passed to stomp properly.""" stomp = StompTransport() stomp.connect() mockconn = mockstomp.Connection.return_value stomp._transaction_begin(mock.sentinel.txid) mockconn.begin.assert_called_once_with(transaction=mock.sentinel.txid) stomp._send("destination", mock.sentinel.message, transaction=mock.sentinel.txid) mockconn.send.assert_called_once_with( "/queue/destination", mock.sentinel.message, headers={"persistent": "true"}, transaction=mock.sentinel.txid, ) stomp._transaction_abort(mock.sentinel.txid) mockconn.abort.assert_called_once_with(mock.sentinel.txid) stomp._transaction_commit(mock.sentinel.txid) mockconn.commit.assert_called_once_with(mock.sentinel.txid)
def test_namespace_is_used_correctly(mockstomp): """Test that a configured namespace is correctly used when subscribing and sending messages.""" mockconn = mockstomp.Connection.return_value StompTransport.defaults["--stomp-prfx"] = "" stomp = StompTransport() stomp.connect() assert stomp.get_namespace() == "" StompTransport.defaults["--stomp-prfx"] = "ns." stomp = StompTransport() stomp.connect() assert stomp.get_namespace() == "ns" stomp._send("some_queue", mock.sentinel.message1) mockconn.send.assert_called_once() assert mockconn.send.call_args[0] == ( "/queue/ns.some_queue", mock.sentinel.message1, ) stomp._send("some_queue", mock.sentinel.message2, ignore_namespace=True) assert mockconn.send.call_args[0] == ("/queue/some_queue", mock.sentinel.message2) StompTransport.defaults["--stomp-prfx"] = "ns" stomp = StompTransport() stomp.connect() assert stomp.get_namespace() == "ns" stomp._send("some_queue", mock.sentinel.message1) assert mockconn.send.call_args[0] == ( "/queue/ns.some_queue", mock.sentinel.message1, ) stomp._broadcast("some_topic", mock.sentinel.message2) assert mockconn.send.call_args[0] == ( "/topic/ns.some_topic", mock.sentinel.message2, ) stomp._broadcast("some_topic", mock.sentinel.message3, ignore_namespace=True) assert mockconn.send.call_args[0] == ("/topic/some_topic", mock.sentinel.message3) stomp._subscribe(1, "sub_queue", None) mockconn.subscribe.assert_called_once() assert mockconn.subscribe.call_args[0] == ("/queue/ns.sub_queue", 1) stomp._subscribe(2, "sub_queue", None, ignore_namespace=True) assert mockconn.subscribe.call_args[0] == ("/queue/sub_queue", 2) stomp._subscribe_broadcast(3, "sub_topic", None) assert mockconn.subscribe.call_args[0] == ("/topic/ns.sub_topic", 3) stomp._subscribe_broadcast(4, "sub_topic", None, ignore_namespace=True) assert mockconn.subscribe.call_args[0] == ("/topic/sub_topic", 4) stomp.broadcast_status("some status") assert mockconn.send.call_args[0] == ("/topic/ns.transient.status", '"some status"')