Example #1
0
def test_nack_message(mockstomp):
    """Test that the _nack function is properly forwarded to stomp."""
    stomp = StompTransport()
    stomp.connect()
    mockconn = mockstomp.Connection.return_value

    subid = stomp._subscribe(1,
                             str(mock.sentinel.channel3),
                             None,
                             acknowledgement=True)
    stomp._nack(mock.sentinel.messageid, subid)
    mockconn.nack.assert_called_once_with(mock.sentinel.messageid, subid)

    stomp._nack(mock.sentinel.messageid, subid, transaction=mock.sentinel.txn)
    mockconn.nack.assert_called_with(mock.sentinel.messageid,
                                     subid,
                                     transaction=mock.sentinel.txn)
Example #2
0
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"')
Example #3
0
def test_subscribe_to_queue(mockstomp):
    """Test subscribing to a queue (producer-consumer), callback functions and unsubscribe."""
    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(
        1,
        str(mock.sentinel.channel1),
        mock_cb1,
    )

    mockconn.subscribe.assert_called_once()
    args, kwargs = mockconn.subscribe.call_args
    assert args == ("/queue/" + str(mock.sentinel.channel1), 1)
    assert kwargs == {
        "headers": {},
        "ack": "auto",
    }

    stomp._subscribe(
        2,
        str(mock.sentinel.channel2),
        mock_cb2,
        retroactive=True,
        selector=mock.sentinel.selector,
        exclusive=True,
        priority=42,
    )
    assert mockconn.subscribe.call_count == 2
    args, kwargs = mockconn.subscribe.call_args
    assert args == ("/queue/" + str(mock.sentinel.channel2), 2)
    assert kwargs == {
        "headers": {
            "activemq.retroactive": "true",
            "selector": mock.sentinel.selector,
            "activemq.exclusive": "true",
            "activemq.priority": 42,
        },
        "ack": "auto",
    }

    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._subscribe(3,
                     str(mock.sentinel.channel3),
                     mock_cb2,
                     acknowledgement=True)
    assert mockconn.subscribe.call_count == 3
    args, kwargs = mockconn.subscribe.call_args
    assert args == ("/queue/" + str(mock.sentinel.channel3), 3)
    assert kwargs == {"headers": {}, "ack": "client-individual"}

    stomp._unsubscribe(1)
    mockconn.unsubscribe.assert_called_once_with(id=1)
    stomp._unsubscribe(2)
    mockconn.unsubscribe.assert_called_with(id=2)