Пример #1
0
def test_batch_restarted():
    incomplete = make_batch_messages(batch_identifier, [
        {'begin_operation': begin},
        {'mutation_operation': mutation},
    ], publisher=uuid.uuid1().bytes)
    complete = make_batch_messages(batch_identifier, [
        {'begin_operation': begin},
        {'mutation_operation': mutation},
        {'commit_operation': commit},
    ], publisher=uuid.uuid1().bytes)
    messages = itertools.chain(incomplete, complete)

    batches = batched(states.validate(messages))

    # The first batch should be aborted, since it didn't end with a
    # commit/rollback before switching publishers.
    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(TransactionAborted):
        next(mutations)

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(StopIteration):
        next(mutations)
Пример #2
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
Пример #3
0
def test_batch_no_mutations():
    messages = make_batch_messages(batch_identifier, [
        {'begin_operation': begin},
        {'commit_operation': commit},
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    with pytest.raises(StopIteration):
        next(mutations)
Пример #4
0
def test_successful_transaction():
    messages = list(make_batch_messages(batch_identifier, [
        {'begin_operation': begin},
        {'mutation_operation': mutation},
        {'commit_operation': commit},
    ]))

    validated = validate(messages)

    assert next(validated) == (InTransaction(messages[0].header.publisher, batch_identifier), messages[0])
    assert next(validated) == (InTransaction(messages[1].header.publisher, batch_identifier), messages[1])
    assert next(validated) == (Committed(messages[2].header.publisher, batch_identifier), messages[2])
Пример #5
0
def test_batch_iterator_early_exit():
    messages = make_batch_messages(batch_identifier, [
        {'begin_operation': begin},
        {'mutation_operation': mutation},
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(TransactionAborted):
        next(mutations)
Пример #6
0
def test_batch_iterator_rolled_back():
    messages = make_batch_messages(batch_identifier, [
        {'begin_operation': begin},
        {'mutation_operation': mutation},
        {'rollback_operation': rollback},
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(TransactionCancelled):
        next(mutations)
Пример #7
0
def test_batch_iterator():
    messages = make_batch_messages(batch_identifier, [
        {'begin_operation': begin},
        {'mutation_operation': mutation},
        {'mutation_operation': mutation},
        {'mutation_operation': mutation},
        {'commit_operation': commit},
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert list(mutations) == [mutation] * 3
Пример #8
0
def test_batch_no_mutations():
    messages = make_batch_messages(batch_identifier, [
        {
            'begin_operation': begin
        },
        {
            'commit_operation': commit
        },
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    with pytest.raises(StopIteration):
        next(mutations)
Пример #9
0
def test_batch_iterator_early_exit():
    messages = make_batch_messages(batch_identifier, [
        {
            'begin_operation': begin
        },
        {
            'mutation_operation': mutation
        },
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(TransactionAborted):
        next(mutations)
Пример #10
0
def test_batch_iterator_rolled_back():
    messages = make_batch_messages(batch_identifier, [
        {
            'begin_operation': begin
        },
        {
            'mutation_operation': mutation
        },
        {
            'rollback_operation': rollback
        },
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(TransactionCancelled):
        next(mutations)
Пример #11
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))
Пример #12
0
def test_successful_transaction():
    messages = list(
        make_batch_messages(batch_identifier, [
            {
                'begin_operation': begin
            },
            {
                'mutation_operation': mutation
            },
            {
                'commit_operation': commit
            },
        ]))

    validated = validate(messages)

    assert next(validated) == (InTransaction(messages[0].header.publisher,
                                             batch_identifier), messages[0])
    assert next(validated) == (InTransaction(messages[1].header.publisher,
                                             batch_identifier), messages[1])
    assert next(validated) == (Committed(messages[2].header.publisher,
                                         batch_identifier), messages[2])
Пример #13
0
def test_batch_restarted():
    incomplete = make_batch_messages(batch_identifier, [
        {
            'begin_operation': begin
        },
        {
            'mutation_operation': mutation
        },
    ],
                                     publisher=uuid.uuid1().bytes)
    complete = make_batch_messages(batch_identifier, [
        {
            'begin_operation': begin
        },
        {
            'mutation_operation': mutation
        },
        {
            'commit_operation': commit
        },
    ],
                                   publisher=uuid.uuid1().bytes)
    messages = itertools.chain(incomplete, complete)

    batches = batched(states.validate(messages))

    # The first batch should be aborted, since it didn't end with a
    # commit/rollback before switching publishers.
    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(TransactionAborted):
        next(mutations)

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert next(mutations) == mutation
    with pytest.raises(StopIteration):
        next(mutations)
Пример #14
0
def test_batch_iterator():
    messages = make_batch_messages(batch_identifier, [
        {
            'begin_operation': begin
        },
        {
            'mutation_operation': mutation
        },
        {
            'mutation_operation': mutation
        },
        {
            'mutation_operation': mutation
        },
        {
            'commit_operation': commit
        },
    ])
    batches = batched(states.validate(messages))

    received_batch_identifier, mutations = next(batches)
    assert received_batch_identifier == batch_identifier
    assert list(mutations) == [mutation] * 3