Beispiel #1
0
def test_transaction_calls(mockpika):
    """Test that calls to create, commit, abort transactions are passed to Pika properly."""
    pytest.xfail("Transactions not implemented in pika transport yet")
    transport = PikaTransport()
    transport.connect()
    mockconn = mockpika.BlockingConnection
    mockchannel = mockconn.return_value.channel.return_value
    mockproperties = mockpika.BasicProperties

    transport._transaction_begin()
    mockchannel.tx_select.assert_called_once()

    transport._send("destination",
                    mock.sentinel.message,
                    transaction=mock.sentinel.txid)
    args, kwargs = mockchannel.basic_publish.call_args
    assert not args
    assert kwargs == {
        "exchange": "",
        "routing_key": "destination",
        "body": mock.sentinel.message,
        "mandatory": True,
        "properties": mock.ANY,
    }
    assert mockproperties.call_args[1] == {"headers": {}, "delivery_mode": 2}

    transport._transaction_abort()
    mockchannel.tx_rollback.assert_called_once()

    transport._transaction_commit()
    mockchannel.tx_commit.assert_called_once()
Beispiel #2
0
def test_send_message(mockpika, mock_pikathread):
    """Test the message sending function"""
    transport = PikaTransport()
    transport.connect()

    mockproperties = mockpika.BasicProperties

    transport._send(mock.sentinel.queue, mock.sentinel.message)
    mock_pikathread.send.assert_called_once()
    args, kwargs = mock_pikathread.send.call_args

    assert not args
    assert kwargs == {
        "exchange": "",
        "routing_key": str(mock.sentinel.queue),
        "body": mock.sentinel.message,
        "mandatory": True,
        "properties": mock.ANY,
        "transaction_id": None,
    }
    assert mockproperties.call_args[1].get("headers") == {}
    assert int(mockproperties.call_args[1].get("delivery_mode")) == 2

    # Was a test for delayed sending, this is advanced in rabbitMQ and
    # to be implemented later
    with pytest.raises(AssertionError):
        transport._send(
            str(mock.sentinel.queue),
            mock.sentinel.message,
            headers={"hdr": mock.sentinel.header},
            delay=123,
        )
Beispiel #3
0
def test_sending_message_with_expiration(mockpika, mock_pikathread):
    """Test sending a message that expires some time in the future."""
    transport = PikaTransport()
    transport.connect()
    mockproperties = mockpika.BasicProperties

    transport._send(str(mock.sentinel.queue),
                    mock.sentinel.message,
                    expiration=120)

    mock_pikathread.send.assert_called_once()

    args, kwargs = mock_pikathread.send.call_args
    assert not args
    assert kwargs == {
        "exchange": "",
        "routing_key": str(mock.sentinel.queue),
        "body": mock.sentinel.message,
        "mandatory": True,
        "properties": mock.ANY,
        "transaction_id": None,
    }
    assert int(mockproperties.return_value.expiration) == 120 * 1000