|
4 | 4 | import datetime |
5 | 5 | import gzip |
6 | 6 | import typing |
| 7 | +from collections import deque |
7 | 8 | from dataclasses import dataclass |
8 | 9 | from unittest import mock |
9 | 10 |
|
@@ -53,6 +54,34 @@ def default_executor(): |
53 | 54 | executor.shutdown() |
54 | 55 |
|
55 | 56 |
|
| 57 | +def stub_partition_session(): |
| 58 | + return datatypes.PartitionSession( |
| 59 | + id=0, |
| 60 | + state=datatypes.PartitionSession.State.Active, |
| 61 | + topic_path="asd", |
| 62 | + partition_id=1, |
| 63 | + committed_offset=0, |
| 64 | + reader_reconnector_id=415, |
| 65 | + reader_stream_id=513, |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def stub_message(id: int): |
| 70 | + return PublicMessage( |
| 71 | + seqno=id, |
| 72 | + created_at=datetime.datetime(2023, 3, 18, 14, 15), |
| 73 | + message_group_id="", |
| 74 | + session_metadata={}, |
| 75 | + offset=0, |
| 76 | + written_at=datetime.datetime(2023, 3, 18, 14, 15), |
| 77 | + producer_id="", |
| 78 | + data=bytes(), |
| 79 | + _partition_session=stub_partition_session(), |
| 80 | + _commit_start_offset=0, |
| 81 | + _commit_end_offset=1, |
| 82 | + ) |
| 83 | + |
| 84 | + |
56 | 85 | @pytest.fixture() |
57 | 86 | def default_reader_settings(default_executor): |
58 | 87 | return PublicReaderSettings( |
@@ -179,7 +208,9 @@ async def stream_reader_finish_with_error( |
179 | 208 |
|
180 | 209 | @staticmethod |
181 | 210 | def create_message( |
182 | | - partition_session: datatypes.PartitionSession, seqno: int, offset_delta: int |
| 211 | + partition_session: typing.Optional[datatypes.PartitionSession], |
| 212 | + seqno: int, |
| 213 | + offset_delta: int, |
183 | 214 | ): |
184 | 215 | return PublicMessage( |
185 | 216 | seqno=seqno, |
@@ -963,6 +994,101 @@ async def test_read_batches( |
963 | 994 | _codec=Codec.CODEC_RAW, |
964 | 995 | ) |
965 | 996 |
|
| 997 | + @pytest.mark.parametrize( |
| 998 | + "batches_before,expected_message,batches_after", |
| 999 | + [ |
| 1000 | + ([], None, []), |
| 1001 | + ( |
| 1002 | + [ |
| 1003 | + PublicBatch( |
| 1004 | + session_metadata={}, |
| 1005 | + messages=[stub_message(1)], |
| 1006 | + _partition_session=stub_partition_session(), |
| 1007 | + _bytes_size=0, |
| 1008 | + _codec=Codec.CODEC_RAW, |
| 1009 | + ) |
| 1010 | + ], |
| 1011 | + stub_message(1), |
| 1012 | + [], |
| 1013 | + ), |
| 1014 | + ( |
| 1015 | + [ |
| 1016 | + PublicBatch( |
| 1017 | + session_metadata={}, |
| 1018 | + messages=[stub_message(1), stub_message(2)], |
| 1019 | + _partition_session=stub_partition_session(), |
| 1020 | + _bytes_size=0, |
| 1021 | + _codec=Codec.CODEC_RAW, |
| 1022 | + ), |
| 1023 | + PublicBatch( |
| 1024 | + session_metadata={}, |
| 1025 | + messages=[stub_message(3), stub_message(4)], |
| 1026 | + _partition_session=stub_partition_session(), |
| 1027 | + _bytes_size=0, |
| 1028 | + _codec=Codec.CODEC_RAW, |
| 1029 | + ), |
| 1030 | + ], |
| 1031 | + stub_message(1), |
| 1032 | + [ |
| 1033 | + PublicBatch( |
| 1034 | + session_metadata={}, |
| 1035 | + messages=[stub_message(2)], |
| 1036 | + _partition_session=stub_partition_session(), |
| 1037 | + _bytes_size=0, |
| 1038 | + _codec=Codec.CODEC_RAW, |
| 1039 | + ), |
| 1040 | + PublicBatch( |
| 1041 | + session_metadata={}, |
| 1042 | + messages=[stub_message(3), stub_message(4)], |
| 1043 | + _partition_session=stub_partition_session(), |
| 1044 | + _bytes_size=0, |
| 1045 | + _codec=Codec.CODEC_RAW, |
| 1046 | + ), |
| 1047 | + ], |
| 1048 | + ), |
| 1049 | + ( |
| 1050 | + [ |
| 1051 | + PublicBatch( |
| 1052 | + session_metadata={}, |
| 1053 | + messages=[stub_message(1)], |
| 1054 | + _partition_session=stub_partition_session(), |
| 1055 | + _bytes_size=0, |
| 1056 | + _codec=Codec.CODEC_RAW, |
| 1057 | + ), |
| 1058 | + PublicBatch( |
| 1059 | + session_metadata={}, |
| 1060 | + messages=[stub_message(2), stub_message(3)], |
| 1061 | + _partition_session=stub_partition_session(), |
| 1062 | + _bytes_size=0, |
| 1063 | + _codec=Codec.CODEC_RAW, |
| 1064 | + ), |
| 1065 | + ], |
| 1066 | + stub_message(1), |
| 1067 | + [ |
| 1068 | + PublicBatch( |
| 1069 | + session_metadata={}, |
| 1070 | + messages=[stub_message(2), stub_message(3)], |
| 1071 | + _partition_session=stub_partition_session(), |
| 1072 | + _bytes_size=0, |
| 1073 | + _codec=Codec.CODEC_RAW, |
| 1074 | + ) |
| 1075 | + ], |
| 1076 | + ), |
| 1077 | + ], |
| 1078 | + ) |
| 1079 | + async def test_read_message( |
| 1080 | + self, |
| 1081 | + stream_reader, |
| 1082 | + batches_before: typing.List[datatypes.PublicBatch], |
| 1083 | + expected_message: PublicMessage, |
| 1084 | + batches_after: typing.List[datatypes.PublicBatch], |
| 1085 | + ): |
| 1086 | + stream_reader._message_batches = deque(batches_before) |
| 1087 | + mess = stream_reader.receive_message_nowait() |
| 1088 | + |
| 1089 | + assert mess == expected_message |
| 1090 | + assert list(stream_reader._message_batches) == batches_after |
| 1091 | + |
966 | 1092 | async def test_receive_batch_nowait(self, stream, stream_reader, partition_session): |
967 | 1093 | assert stream_reader.receive_batch_nowait() is None |
968 | 1094 |
|
|
0 commit comments