Beispiel #1
0
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)
Beispiel #2
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)