コード例 #1
0
async def test_end_of_stream():

    output = TeeQueue()
    event_1_id = uuid4()
    event_2_id = uuid4()

    convo = IterStreamEvents("my-stream")
    response = proto.ReadStreamEventsCompleted()
    response.result = msg.ReadEventResult.Success
    response.next_event_number = 10
    response.last_event_number = 9
    response.is_end_of_stream = True
    response.last_commit_position = 8

    event_1 = proto.ResolvedIndexedEvent()
    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.ResolvedIndexedEvent()
    event_2.CopyFrom(event_1)
    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.ReadEventCompleted, 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.event.stream == "stream-123"
    assert event_1.event.id == event_1_id
    assert event_1.event.type == "event-type"
    assert event_1.event.event_number == 32

    assert event_2.event.stream == "stream-123"
    assert event_2.event.id == event_2_id
    assert event_2.event.type == "event-2-type"
    assert event_2.event.event_number == 33
コード例 #2
0
async def test_reconnect_at_last_event_number():

    output = TeeQueue()
    event_1_id = uuid4()
    event_2_id = uuid4()
    convo = IterStreamEvents("my-stream", from_event=32)
    await convo.start(output)

    response = proto.ReadStreamEventsCompleted()
    response.result = msg.ReadEventResult.Success
    response.next_event_number = 32
    response.last_event_number = 31
    response.is_end_of_stream = False
    response.last_commit_position = 31

    event_1 = proto.ResolvedIndexedEvent()
    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.ResolvedIndexedEvent()
    event_2.CopyFrom(event_1)
    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.ReadStreamEventsForwardCompleted,
            response.SerializeToString(),
        ),
        output,
    )

    await convo.start(output)
    request = output.items[-1]

    body = proto.ReadStreamEvents()
    body.ParseFromString(request.payload)

    assert request.command is msg.TcpCommand.ReadStreamEventsForward
    assert body.event_stream_id == "my-stream"
    assert body.from_event_number == 33
    assert body.resolve_link_tos is True
    assert body.require_master is False
    assert body.max_count == 100
コード例 #3
0
async def test_read_stream_success():

    event_1_id = uuid4()
    event_2_id = uuid4()

    convo = ReadStreamEvents("my-stream", 0)
    response = proto.ReadStreamEventsCompleted()
    response.result = msg.ReadEventResult.Success
    response.next_event_number = 10
    response.last_event_number = 9
    response.is_end_of_stream = True
    response.last_commit_position = 8

    event_1 = proto.ResolvedIndexedEvent()
    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.ResolvedIndexedEvent()
    event_2.CopyFrom(event_1)
    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.ReadEventCompleted,
                           response.SerializeToString()),
        None,
    )

    result = await convo.result

    assert isinstance(result, msg.StreamSlice)

    [event_1, event_2] = result.events
    assert event_1.event.stream == "stream-123"
    assert event_1.event.id == event_1_id
    assert event_1.event.type == "event-type"
    assert event_1.event.event_number == 32

    assert event_2.event.stream == "stream-123"
    assert event_2.event.id == event_2_id
    assert event_2.event.type == "event-2-type"
    assert event_2.event.event_number == 33
コード例 #4
0
    def with_event(
        self,
        event_number=10,
        event_id=None,
        type="some-event",
        data=None,
        link_event_number=None,
    ):
        event = proto.ResolvedIndexedEvent()
        event.event.event_stream_id = self.stream
        event.event.event_number = event_number
        event.event.event_id = (event_id or uuid.uuid4()).bytes_le
        event.event.event_type = type
        event.event.data_content_type = msg.ContentType.Json
        event.event.metadata_content_type = msg.ContentType.Binary
        event.event.data = json.dumps(data).encode(
            "UTF-8") if data else bytes()
        if link_event_number is not None:
            event.link.event_number = link_event_number
            event.link.event_stream_id = "some-stream-name"
            event.link.event_id = uuid.uuid4().bytes_le
            event.link.event_type = "$>"
            event.link.data_content_type = msg.ContentType.Json
            event.link.metadata_content_type = msg.ContentType.Binary
            event.link.data = f"{event_number}@{self.stream}".encode("UTF-8")

        self.events.append(event)

        return self
コード例 #5
0
    def with_event(self,
                   event_number=10,
                   event_id=None,
                   type="some-event",
                   data=None):
        event = proto.ResolvedIndexedEvent()
        event.event.event_stream_id = self.stream
        event.event.event_number = event_number
        event.event.event_id = (event_id or uuid.uuid4()).bytes_le
        event.event.event_type = type
        event.event.data_content_type = msg.ContentType.Json
        event.event.metadata_content_type = msg.ContentType.Binary
        event.event.data = json.dumps(data).encode(
            "UTF-8") if data else bytes()

        self.events.append(event)

        return self