Пример #1
0
def test_publisher_failure():
    messages = []
    publisher = Publisher(messages.extend)

    with pytest.raises(NotImplementedError):
        with publisher.batch(batch_identifier, begin):
            raise NotImplementedError

    published_messages = map(reserialize, messages)

    assert get_operation(get_operation(published_messages[0])) == begin
    assert get_operation(get_operation(published_messages[1])) == rollback

    # Ensure it actually generates valid data.
    assert list(states.validate(published_messages))
    assert list(sequences.validate(published_messages))

    for i, message in enumerate(published_messages):
        assert message.header.publisher == publisher.id
        assert message.header.sequence == i

    # Write another message to ensure that the publisher can continue to be used.
    assert len(messages) == 2
    publisher.publish()
    assert len(messages) == 3
    assert messages[2].header.sequence == 2
Пример #2
0
def test_incorrect_sequence_start():
    messages = [
        Message(header=build_header(1)),
    ]

    stream = validate(messages)
    with pytest.raises(InvalidSequenceStartError):
        next(stream)
Пример #3
0
def test_missing_message():
    messages = [Message(header=build_header(0)), Message(header=build_header(2))]

    stream = validate(messages)

    assert next(stream) is messages[0]
    with pytest.raises(SequencingError):
        next(stream)
Пример #4
0
def test_simple_sequence():
    messages = [
        Message(header=build_header(0)),
        Message(header=build_header(1)),
        Message(header=build_header(2)),
    ]

    stream = validate(messages)
    assert list(stream) == messages
Пример #5
0
def test_duplicate_message():
    messages = [
        Message(header=build_header(0)),
        Message(header=build_header(1)),
        Message(header=build_header(1)),
        Message(header=build_header(2)),
    ]

    stream = validate(messages)
    assert list(stream) == [messages[0], messages[1], messages[3]]
Пример #6
0
def test_duplicate_message():
    messages = [
        Message(header=build_header(0)),
        Message(header=build_header(1)),
        Message(header=build_header(1)),
        Message(header=build_header(2)),
    ]

    stream = validate(messages)
    assert list(stream) == [messages[0], messages[1], messages[3]]
Пример #7
0
def test_repeated_sequence():
    messages = [
        Message(header=build_header(0, timestamp=Timestamp(seconds=0, nanos=0))),
        Message(header=build_header(0, timestamp=Timestamp(seconds=1, nanos=0))),
    ]

    stream = validate(messages)
    assert next(stream) is messages[0]

    with pytest.raises(RepeatedSequenceError):
        next(stream)
Пример #8
0
def test_missing_message():
    messages = [
        Message(header=build_header(0)),
        Message(header=build_header(2)),
    ]

    stream = validate(messages)

    assert next(stream) is messages[0]
    with pytest.raises(SequencingError):
        next(stream)
Пример #9
0
def test_repeated_sequence():
    messages = [
        Message(header=build_header(0, timestamp=Timestamp(seconds=0, nanos=0))),
        Message(header=build_header(0, timestamp=Timestamp(seconds=1, nanos=0))),
    ]

    stream = validate(messages)
    assert next(stream) is messages[0]

    with pytest.raises(RepeatedSequenceError):
        next(stream)
Пример #10
0
def test_invalid_multiplexed_sequence():
    messages = [
        Message(header=build_header(0, publisher="a")),
        Message(header=build_header(1, publisher="a")),
        Message(header=build_header(0, publisher="b")),
        Message(header=build_header(2, publisher="a")),
    ]

    stream = validate(messages)
    assert next(stream) is messages[0]
    assert next(stream) is messages[1]
    assert next(stream) is messages[2]
    with pytest.raises(InvalidPublisher):
        next(stream)
Пример #11
0
def test_invalid_multiplexed_sequence():
    messages = [
        Message(header=build_header(0, publisher='a')),
        Message(header=build_header(1, publisher='a')),
        Message(header=build_header(0, publisher='b')),
        Message(header=build_header(2, publisher='a')),
    ]

    stream = validate(messages)
    assert next(stream) is messages[0]
    assert next(stream) is messages[1]
    assert next(stream) is messages[2]
    with pytest.raises(InvalidPublisher):
        next(stream)
Пример #12
0
def test_out_of_order_message():
    messages = [
        Message(header=build_header(0)),
        Message(header=build_header(1)),
        Message(header=build_header(2)),
        Message(header=build_header(1)),
    ]

    stream = validate(messages)

    assert next(stream) is messages[0]
    assert next(stream) is messages[1]
    assert next(stream) is messages[2]
    with pytest.raises(SequencingError):
        next(stream)
Пример #13
0
def test_out_of_order_message():
    messages = [
        Message(header=build_header(0)),
        Message(header=build_header(1)),
        Message(header=build_header(2)),
        Message(header=build_header(1)),
    ]

    stream = validate(messages)

    assert next(stream) is messages[0]
    assert next(stream) is messages[1]
    assert next(stream) is messages[2]
    with pytest.raises(SequencingError):
        next(stream)
Пример #14
0
def test_publisher():
    messages = []
    publisher = Publisher(messages.extend)

    with publisher.batch(batch_identifier, begin) as publish:
        publish(mutation)

    published_messages = map(reserialize, messages)

    assert get_operation(get_operation(published_messages[0])) == begin
    assert get_operation(get_operation(published_messages[1])) == mutation
    assert get_operation(get_operation(published_messages[2])) == commit

    for i, message in enumerate(published_messages):
        assert message.header.publisher == publisher.id
        assert message.header.sequence == i

    # Ensure it actually generates valid data.
    assert list(states.validate(published_messages))
    assert list(sequences.validate(published_messages))
Пример #15
0
def test_incorrect_sequence_start():
    messages = [Message(header=build_header(1))]

    stream = validate(messages)
    with pytest.raises(InvalidSequenceStartError):
        next(stream)
Пример #16
0
def test_simple_sequence():
    messages = [Message(header=build_header(0)), Message(header=build_header(1)), Message(header=build_header(2))]

    stream = validate(messages)
    assert list(stream) == messages