Beispiel #1
0
def test_error_handling_on_broadcast(mockstomp):
    """Unrecoverable errors during broadcasting 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._broadcast(str(mock.sentinel.channel), mock.sentinel.message)
    assert not stomp.is_connected()
Beispiel #2
0
def test_broadcasting_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._broadcast(str(mock.sentinel.channel),
                     mock.sentinel.message,
                     expiration=120)

    mockconn.send.assert_called_once()
    args, kwargs = mockconn.send.call_args
    assert args == ("/topic/" + str(mock.sentinel.channel),
                    mock.sentinel.message)
    assert kwargs.get("headers") == {"expires": expiration_time}
Beispiel #3
0
def test_send_broadcast(mockstomp):
    """Test the broadcast sending function."""
    stomp = StompTransport()
    stomp.connect()
    mockconn = mockstomp.Connection.return_value

    stomp._broadcast(str(mock.sentinel.channel), mock.sentinel.message)

    mockconn.send.assert_called_once()
    args, kwargs = mockconn.send.call_args
    assert args == ("/topic/" + str(mock.sentinel.channel),
                    mock.sentinel.message)
    assert kwargs.get("headers") in (None, {})

    stomp._broadcast(str(mock.sentinel.channel),
                     mock.sentinel.message,
                     headers=mock.sentinel.headers)
    assert mockconn.send.call_count == 2
    args, kwargs = mockconn.send.call_args
    assert args == ("/topic/" + str(mock.sentinel.channel),
                    mock.sentinel.message)
    assert kwargs == {"headers": mock.sentinel.headers}

    stomp._broadcast(str(mock.sentinel.channel),
                     mock.sentinel.message,
                     delay=123)
    assert mockconn.send.call_count == 3
    args, kwargs = mockconn.send.call_args
    assert args == ("/topic/" + str(mock.sentinel.channel),
                    mock.sentinel.message)
    assert kwargs["headers"].get("AMQ_SCHEDULED_DELAY") == 123000
Beispiel #4
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"')