Exemplo n.º 1
0
def test_error_handling_on_subscribing(mockpika, mock_pikathread):
    """Unrecoverable errors during subscribing should mark the connection as disconnected."""
    mock_cb = mock.Mock()

    transport = PikaTransport()
    transport.connect()
    mockpika.exceptions = pika.exceptions
    mock_pikathread.connection_alive = False
    mock_pikathread.subscribe_queue.return_value.result.side_effect = (
        pika.exceptions.AMQPChannelError)

    with pytest.raises(workflows.Disconnected):
        transport._subscribe(1, str(mock.sentinel.queue1), mock_cb)
    assert not transport.is_connected()

    # Now try connectionError instead of ChannelError
    mock_pikathread.subscribe_queue.return_value.result.side_effect = (
        pika.exceptions.AMQPConnectionError)

    with pytest.raises(workflows.Disconnected):
        transport._subscribe(1, str(mock.sentinel.queue1), mock_cb)
    assert not transport.is_connected()
Exemplo n.º 2
0
def test_subscribe_to_queue(mock_pikathread):
    """Test subscribing to a queue (producer-consumer), callback functions and unsubscribe"""
    transport = PikaTransport()
    transport.connect()

    mock_cb = mock.Mock()
    transport._subscribe(1, str(mock.sentinel.queue1), mock_cb)

    mock_pikathread.subscribe_queue.assert_called_once()

    args, kwargs = mock_pikathread.subscribe_queue.call_args
    assert not args
    assert kwargs == {
        "auto_ack": True,
        "callback": mock.ANY,
        "subscription_id": 1,
        "prefetch_count": 1,
        "queue": str(mock.sentinel.queue1),
        "reconnectable": False,
    }

    transport._subscribe(2, str(mock.sentinel.queue2), mock_cb)

    assert mock_pikathread.subscribe_queue.call_count == 2
    args, kwargs = mock_pikathread.subscribe_queue.call_args
    assert not args
    assert kwargs == {
        "auto_ack": True,
        "callback": mock.ANY,
        "subscription_id": 2,
        "prefetch_count": 1,
        "queue": str(mock.sentinel.queue2),
        "reconnectable": False,
    }

    transport._subscribe(3,
                         str(mock.sentinel.queue3),
                         mock_cb,
                         acknowledgement=True)
    assert mock_pikathread.subscribe_queue.call_count == 3
    args, kwargs = mock_pikathread.subscribe_queue.call_args
    assert not args
    assert kwargs == {
        "auto_ack": False,
        "callback": mock.ANY,
        "subscription_id": 3,
        "prefetch_count": 1,
        "queue": str(mock.sentinel.queue3),
        "reconnectable": False,
    }

    transport._unsubscribe(1)
    mock_pikathread.unsubscribe.assert_called_once_with(1)
    transport._unsubscribe(2)
    mock_pikathread.unsubscribe.assert_called_with(2)