async def test_stream_paging(): output = TeeQueue() convo = IterAllEvents() response = proto.ReadAllEventsCompleted() response.result = msg.ReadEventResult.Success response.commit_position = 10 response.prepare_position = 10 response.next_commit_position = 11 response.next_prepare_position = 12 await convo.respond_to( msg.InboundMessage( uuid4(), msg.TcpCommand.ReadStreamEventsForwardCompleted, response.SerializeToString(), ), output, ) reply = await output.get() body = proto.ReadAllEvents() body.ParseFromString(reply.payload) assert body.commit_position == 11 assert body.prepare_position == 12
async def test_should_perform_a_catchup_when_subscription_is_confirmed(): """ When we have read all the events in the stream, we should send a request to subscribe for new events. We should start reading catchup events from the `next_commit_position` returned by the historical event read. """ convo = CatchupAllSubscription() output = TeeQueue() await convo.start(output) await reply_to( convo, ReadAllEventsResponseBuilder() .with_position(18, 19) .with_next_position(18, 19) .build(), output, ) await confirm_subscription(convo, output, event_number=42, commit_pos=40) [read_historial, subscribe, catch_up] = await output.next_event(3) assert read_historial.command == msg.TcpCommand.ReadAllEventsForward assert subscribe.command == msg.TcpCommand.SubscribeToStream assert catch_up.command == msg.TcpCommand.ReadAllEventsForward payload = proto.ReadAllEvents() payload.ParseFromString(catch_up.payload) assert payload.commit_position == 18 assert payload.prepare_position == 19
async def test_start_read_phase(): """ A "catchup" subscription starts by iterating the events in the stream until it reaches the most recent event. This is the "Read" phase. """ output = TeeQueue() conversation_id = uuid.uuid4() convo = CatchupAllSubscription( start_from=msg.Position(0, 0), conversation_id=conversation_id ) await convo.start(output) [request] = output.items body = proto.ReadAllEvents() body.ParseFromString(request.payload) assert request.command is msg.TcpCommand.ReadAllEventsForward assert body.commit_position == 0 assert body.prepare_position == 0 assert body.resolve_link_tos is True assert body.require_master is False assert body.max_count == 100
async def test_paging(): """ During the read phase, we expect to page through multiple batches of events. In this scenario we have two batches, each of two events. """ convo = CatchupAllSubscription() output = TeeQueue() await convo.start(output) await output.get() event_1_id = uuid.uuid4() event_2_id = uuid.uuid4() event_3_id = uuid.uuid4() event_4_id = uuid.uuid4() first_response = ( ReadAllEventsResponseBuilder() .with_event(event_id=event_1_id, event_number=32) .with_event(event_id=event_2_id, event_number=33) .with_position(1, 1) .with_next_position(2, 2) ).build() second_response = ( ReadAllEventsResponseBuilder() .with_event(event_id=event_3_id, event_number=34) .with_event(event_id=event_4_id, event_number=35) ).build() await reply_to(convo, first_response, output) subscription = await convo.result event_1 = await anext(subscription.events) event_2 = await anext(subscription.events) assert event_1.id == event_1_id assert event_2.id == event_2_id reply = await output.get() body = proto.ReadAllEvents() body.ParseFromString(reply.payload) assert body.commit_position == 2 assert body.prepare_position == 2 await reply_to(convo, second_response, output) event_3 = await anext(subscription.events) event_4 = await anext(subscription.events) assert event_3.id == event_3_id assert event_4.id == event_4_id
async def test_read_backward(): output = TeeQueue() convo = IterAllEvents(direction=msg.StreamDirection.Backward, batch_size=10) await convo.start(output) request = await output.get() body = proto.ReadAllEvents() body.ParseFromString(request.payload) assert request.command is msg.TcpCommand.ReadAllEventsBackward assert body.commit_position == 0 assert body.resolve_link_tos is True assert body.require_master is False assert body.max_count == 10
async def test_read_request(): output = TeeQueue() convo = IterAllEvents(msg.Position(0, 0)) await convo.start(output) request = await output.get() body = proto.ReadAllEvents() body.ParseFromString(request.payload) assert request.command is msg.TcpCommand.ReadAllEventsForward assert body.commit_position == 0 assert body.prepare_position == 0 assert body.resolve_link_tos is True assert body.require_master is False assert body.max_count == 100
def _fetch_page_message(self, from_position): if self.direction == StreamDirection.Forward: command = TcpCommand.ReadAllEventsForward else: command = TcpCommand.ReadAllEventsBackward msg = proto.ReadAllEvents() msg.commit_position = from_position.commit msg.prepare_position = from_position.prepare msg.max_count = self.batch_size msg.resolve_link_tos = self.resolve_link_tos msg.require_master = self.require_master data = msg.SerializeToString() return OutboundMessage(self.conversation_id, command, data, self.credential)
def _fetch_page_message(self, from_position: Position): self._logger.debug("Requesting page of %d events from %s", self.max_count, from_position) if self.direction == StreamDirection.Forward: command = TcpCommand.ReadAllEventsForward else: command = TcpCommand.ReadAllEventsBackward msg = proto.ReadAllEvents() msg.commit_position = from_position.commit msg.prepare_position = from_position.prepare msg.max_count = self.max_count msg.require_master = self.require_master msg.resolve_link_tos = self.resolve_link_tos data = msg.SerializeToString() return OutboundMessage(self.conversation_id, command, data, self.credential)
async def test_read_all_backward(): output = Queue() convo = ReadAllEvents( from_position=msg.Position(10, 11), direction=msg.StreamDirection.Backward, max_count=20, ) await convo.start(output) request = await output.get() body = proto.ReadAllEvents() body.ParseFromString(request.payload) assert request.command is msg.TcpCommand.ReadAllEventsBackward assert body.commit_position == 10 assert body.prepare_position == 11 assert body.resolve_link_tos is True assert body.require_master is False assert body.max_count == 20