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
def build(self): response = proto.ReadAllEventsCompleted() response.result = self.result response.commit_position = self.position.commit response.prepare_position = self.position.prepare response.next_commit_position = self.next_position.commit response.next_prepare_position = self.next_position.prepare response.events.extend(self.events) return ( msg.TcpCommand.ReadAllEventsForwardCompleted, response.SerializeToString(), )
async def reply(self, message: InboundMessage, output: Queue): result = proto.ReadAllEventsCompleted() result.ParseFromString(message.payload) if result.result == ReadAllResult.Success: await self.success(result, output) elif result.result == ReadAllResult.Error: await self.error( exceptions.ReadError(self.conversation_id, "$all", result.error)) elif result.result == ReadAllResult.AccessDenied: await self.error( exceptions.AccessDenied(self.conversation_id, type(self).__name__, result.error))
async def test_all_events_access_denied(): convo = ReadAllEvents() response = proto.ReadAllEventsCompleted() response.result = msg.ReadAllResult.AccessDenied response.next_commit_position = 10 response.next_prepare_position = 10 response.commit_position = 9 response.prepare_position = 9 await convo.respond_to( msg.InboundMessage(uuid4(), msg.TcpCommand.ReadAllEventsForward, response.SerializeToString()), None, ) with pytest.raises(exceptions.AccessDenied) as exn: await convo.result assert exn.conversation_id == convo.conversation_id assert exn.conversation_type == "ReadAllEvents"
async def test_error_mid_stream(): output = TeeQueue() convo = IterAllEvents("my-stream") response = proto.ReadAllEventsCompleted() response.result = msg.ReadStreamResult.Success response.commit_position = 0 response.prepare_position = 0 response.next_commit_position = 10 response.next_prepare_position = 10 await convo.respond_to( msg.InboundMessage( uuid4(), msg.TcpCommand.ReadAllEventsForwardCompleted, response.SerializeToString(), ), output, ) response.result = msg.ReadAllResult.AccessDenied await convo.respond_to( msg.InboundMessage( uuid4(), msg.TcpCommand.ReadAllEventsForwardCompleted, response.SerializeToString(), ), output, ) iterator = await convo.result with pytest.raises(exceptions.AccessDenied) as exn: await iterator.anext() assert exn.conversation_id == convo.conversation_id assert exn.conversation_type == "IterStreamEvents" assert len(output.items) == 1
async def test_all_events_error(): convo = ReadAllEvents() response = proto.ReadAllEventsCompleted() response.result = msg.ReadAllResult.Error response.next_commit_position = 10 response.next_prepare_position = 10 response.commit_position = 9 response.prepare_position = 9 response.error = "Something really weird just happened" await convo.respond_to( msg.InboundMessage( uuid4(), msg.TcpCommand.ReadAllEventsForwardCompleted, response.SerializeToString(), ), None, ) with pytest.raises(exceptions.ReadError) as exn: await convo.result assert exn.stream == "$all" assert exn.conversation_id == convo.conversation_id
async def test_read_all_success(): event_1_id = uuid4() event_2_id = uuid4() convo = ReadAllEvents() response = proto.ReadAllEventsCompleted() response.result = msg.ReadEventResult.Success response.next_commit_position = 10 response.next_prepare_position = 10 response.commit_position = 9 response.prepare_position = 9 event_1 = proto.ResolvedEvent() event_1.commit_position = 8 event_1.prepare_position = 8 event_1.event.event_stream_id = "stream-123" event_1.event.event_number = 32 event_1.event.event_id = event_1_id.bytes_le event_1.event.event_type = "event-type" event_1.event.data_content_type = msg.ContentType.Json event_1.event.metadata_content_type = msg.ContentType.Binary event_1.event.data = """ { 'color': 'red', 'winner': true } """.encode("UTF-8") event_2 = proto.ResolvedEvent() event_2.CopyFrom(event_1) event_2.event.event_stream_id = "stream-456" event_2.event.event_type = "event-2-type" event_2.event.event_id = event_2_id.bytes_le event_2.event.event_number = 32 response.events.extend([event_1, event_2]) await convo.respond_to( msg.InboundMessage( uuid4(), msg.TcpCommand.ReadAllEventsForwardCompleted, response.SerializeToString(), ), None, ) result = await convo.result assert isinstance(result, msg.AllStreamSlice) [event_1, event_2] = result.events assert event_1.stream == "stream-123" assert event_1.id == event_1_id assert event_1.type == "event-type" assert event_1.event_number == 32 assert event_2.stream == "stream-456" assert event_2.id == event_2_id assert event_2.type == "event-2-type" assert event_2.event_number == 32
async def test_end_of_stream(): output = TeeQueue() event_1_id = uuid4() event_2_id = uuid4() convo = IterAllEvents() response = proto.ReadAllEventsCompleted() response.result = msg.ReadAllResult.Success response.commit_position = 10 response.prepare_position = 10 response.next_commit_position = 10 response.next_prepare_position = 10 event_1 = proto.ResolvedEvent() event_1.commit_position = 9 event_1.prepare_position = 9 event_1.event.event_stream_id = "stream-123" event_1.event.event_number = 32 event_1.event.event_id = event_1_id.bytes_le event_1.event.event_type = "event-type" event_1.event.data_content_type = msg.ContentType.Json event_1.event.metadata_content_type = msg.ContentType.Binary event_1.event.data = """ { 'color': 'red', 'winner': true } """.encode("UTF-8") event_2 = proto.ResolvedEvent() event_2.CopyFrom(event_1) event_2.commit_position = 10 event_2.prepare_position = 10 event_2.event.event_type = "event-2-type" event_2.event.event_id = event_2_id.bytes_le event_2.event.event_number = 33 response.events.extend([event_1, event_2]) await convo.respond_to( msg.InboundMessage( uuid4(), msg.TcpCommand.ReadAllEventsForwardCompleted, response.SerializeToString(), ), output, ) # Todo: Use a slice here so that we can give information # to the iterator about its position in the stream? # assert isinstance(reply.result, msg.StreamSlice) result = await convo.result [event_1, event_2] = [e async for e in result] assert event_1.stream == "stream-123" assert event_1.id == event_1_id assert event_1.type == "event-type" assert event_1.event_number == 32 assert event_2.stream == "stream-123" assert event_2.id == event_2_id assert event_2.type == "event-2-type" assert event_2.event_number == 33