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)
def test_successful_transaction(): messages = list(make_batch_messages(batch_identifier, [ {'begin_operation': begin}, {'mutation_operation': mutation}, {'commit_operation': commit}, ])) state = None state = reserialize(validate_transaction_state(state, 0, messages[0])) assert get_oneof_value(state, 'state') == ( InTransaction( publisher=messages[0].header.publisher, batch_identifier=batch_identifier ) ) state = reserialize(validate_transaction_state(state, 1, messages[1])) assert get_oneof_value(state, 'state') == ( InTransaction( publisher=messages[1].header.publisher, batch_identifier=batch_identifier ) ) state = reserialize(validate_transaction_state(state, 2, messages[2])) assert get_oneof_value(state, 'state') == ( Committed( publisher=messages[2].header.publisher, batch_identifier=batch_identifier ) )
def invisible_transaction(source_transaction_snapshot): # TODO: Figure out if we need to set the min, max here. begin = BeginOperation( start=Tick( id=1, snapshot=Snapshot(min=100, max=200), timestamp=Timestamp(seconds=0, nanos=0), ), end=Tick( id=2, snapshot=Snapshot(min=150, max=250), timestamp=Timestamp(seconds=10, nanos=0), ), ) mutation = get_mutation(source_transaction_snapshot.max + 1000) commit = CommitOperation() generator = make_batch_messages( batch_identifier, ( {'begin_operation': begin}, {'mutation_operation': mutation}, {'commit_operation': commit}, ) ) yield list(generator)
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)
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])
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)
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)
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)
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
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)
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)
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)
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])
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