def test_subscribe_to_broadcast(mockstomp): """Test subscribing to a topic (publish-subscribe) and callback functions.""" mock_cb1 = mock.Mock() mock_cb2 = mock.Mock() stomp = StompTransport() stomp.connect() mockconn = mockstomp.Connection.return_value def callback_resolver(cbid): if cbid == 1: return mock_cb1 if cbid == 2: return mock_cb2 raise ValueError("Unknown subscription ID %r" % cbid) stomp.subscription_callback = callback_resolver mockconn.set_listener.assert_called_once() listener = mockconn.set_listener.call_args[0][1] assert listener is not None stomp._subscribe_broadcast( 1, str(mock.sentinel.channel1), mock_cb1, ) mockconn.subscribe.assert_called_once() args, kwargs = mockconn.subscribe.call_args assert args == ("/topic/" + str(mock.sentinel.channel1), 1) assert kwargs == {"headers": {}} stomp._subscribe_broadcast(2, str(mock.sentinel.channel2), mock_cb2, retroactive=True) assert mockconn.subscribe.call_count == 2 args, kwargs = mockconn.subscribe.call_args assert args == ("/topic/" + str(mock.sentinel.channel2), 2) assert kwargs == {"headers": {"activemq.retroactive": "true"}} assert mock_cb1.call_count == 0 listener.on_message(_frame({"subscription": 1}, mock.sentinel.message1)) mock_cb1.assert_called_once_with({"subscription": 1}, mock.sentinel.message1) assert mock_cb2.call_count == 0 listener.on_message(_frame({"subscription": 2}, mock.sentinel.message2)) mock_cb2.assert_called_once_with({"subscription": 2}, mock.sentinel.message2) stomp._unsubscribe(1) mockconn.unsubscribe.assert_called_once_with(id=1) stomp._unsubscribe(2) mockconn.unsubscribe.assert_called_with(id=2)
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"')