4
4
import datetime
5
5
import gzip
6
6
import typing
7
- from collections import deque
7
+ from collections import OrderedDict
8
8
from dataclasses import dataclass
9
9
from unittest import mock
10
10
@@ -52,9 +52,9 @@ def default_executor():
52
52
executor .shutdown ()
53
53
54
54
55
- def stub_partition_session ():
55
+ def stub_partition_session (id : int = 0 ):
56
56
return datatypes .PartitionSession (
57
- id = 0 ,
57
+ id = id ,
58
58
state = datatypes .PartitionSession .State .Active ,
59
59
topic_path = "asd" ,
60
60
partition_id = 1 ,
@@ -212,21 +212,27 @@ def create_message(
212
212
_commit_end_offset = partition_session ._next_message_start_commit_offset + offset_delta ,
213
213
)
214
214
215
- async def send_message (self , stream_reader , message : PublicMessage ):
216
- await self .send_batch (stream_reader , [message ])
215
+ async def send_message (self , stream_reader , message : PublicMessage , new_batch = True ):
216
+ await self .send_batch (stream_reader , [message ], new_batch = new_batch )
217
217
218
- async def send_batch (self , stream_reader , batch : typing .List [PublicMessage ]):
218
+ async def send_batch (self , stream_reader , batch : typing .List [PublicMessage ], new_batch = True ):
219
219
if len (batch ) == 0 :
220
220
return
221
221
222
222
first_message = batch [0 ]
223
223
for message in batch :
224
224
assert message ._partition_session is first_message ._partition_session
225
225
226
+ partition_session_id = first_message ._partition_session .id
227
+
226
228
def batch_count ():
227
229
return len (stream_reader ._message_batches )
228
230
231
+ def batch_size ():
232
+ return len (stream_reader ._message_batches [partition_session_id ].messages )
233
+
229
234
initial_batches = batch_count ()
235
+ initial_batch_size = batch_size () if not new_batch else 0
230
236
231
237
stream = stream_reader ._stream # type: StreamMock
232
238
stream .from_server .put_nowait (
@@ -261,7 +267,10 @@ def batch_count():
261
267
),
262
268
)
263
269
)
264
- await wait_condition (lambda : batch_count () > initial_batches )
270
+ if new_batch :
271
+ await wait_condition (lambda : batch_count () > initial_batches )
272
+ else :
273
+ await wait_condition (lambda : batch_size () > initial_batch_size )
265
274
266
275
async def test_unknown_error (self , stream , stream_reader_finish_with_error ):
267
276
class TestError (Exception ):
@@ -412,15 +421,11 @@ async def test_commit_ranges_for_received_messages(
412
421
m2 ._commit_start_offset = m1 .offset + 1
413
422
414
423
await self .send_message (stream_reader_started , m1 )
415
- await self .send_message (stream_reader_started , m2 )
416
-
417
- await stream_reader_started .wait_messages ()
418
- received = stream_reader_started .receive_batch_nowait ().messages
419
- assert received == [m1 ]
424
+ await self .send_message (stream_reader_started , m2 , new_batch = False )
420
425
421
426
await stream_reader_started .wait_messages ()
422
427
received = stream_reader_started .receive_batch_nowait ().messages
423
- assert received == [m2 ]
428
+ assert received == [m1 , m2 ]
424
429
425
430
await stream_reader_started .close (False )
426
431
@@ -860,7 +865,7 @@ def reader_batch_count():
860
865
861
866
assert stream_reader ._buffer_size_bytes == initial_buffer_size - bytes_size
862
867
863
- last_batch = stream_reader ._message_batches [ - 1 ]
868
+ _ , last_batch = stream_reader ._message_batches . popitem ()
864
869
assert last_batch == PublicBatch (
865
870
messages = [
866
871
PublicMessage (
@@ -1059,74 +1064,74 @@ async def test_read_batches(self, stream_reader, partition_session, second_parti
1059
1064
@pytest .mark .parametrize (
1060
1065
"batches_before,expected_message,batches_after" ,
1061
1066
[
1062
- ([] , None , [] ),
1067
+ ({} , None , {} ),
1063
1068
(
1064
- [
1065
- PublicBatch (
1069
+ {
1070
+ 0 : PublicBatch (
1066
1071
messages = [stub_message (1 )],
1067
1072
_partition_session = stub_partition_session (),
1068
1073
_bytes_size = 0 ,
1069
1074
_codec = Codec .CODEC_RAW ,
1070
1075
)
1071
- ] ,
1076
+ } ,
1072
1077
stub_message (1 ),
1073
- [] ,
1078
+ {} ,
1074
1079
),
1075
1080
(
1076
- [
1077
- PublicBatch (
1081
+ {
1082
+ 0 : PublicBatch (
1078
1083
messages = [stub_message (1 ), stub_message (2 )],
1079
1084
_partition_session = stub_partition_session (),
1080
1085
_bytes_size = 0 ,
1081
1086
_codec = Codec .CODEC_RAW ,
1082
1087
),
1083
- PublicBatch (
1088
+ 1 : PublicBatch (
1084
1089
messages = [stub_message (3 ), stub_message (4 )],
1085
- _partition_session = stub_partition_session (),
1090
+ _partition_session = stub_partition_session (1 ),
1086
1091
_bytes_size = 0 ,
1087
1092
_codec = Codec .CODEC_RAW ,
1088
1093
),
1089
- ] ,
1094
+ } ,
1090
1095
stub_message (1 ),
1091
- [
1092
- PublicBatch (
1096
+ {
1097
+ 0 : PublicBatch (
1093
1098
messages = [stub_message (2 )],
1094
1099
_partition_session = stub_partition_session (),
1095
1100
_bytes_size = 0 ,
1096
1101
_codec = Codec .CODEC_RAW ,
1097
1102
),
1098
- PublicBatch (
1103
+ 1 : PublicBatch (
1099
1104
messages = [stub_message (3 ), stub_message (4 )],
1100
- _partition_session = stub_partition_session (),
1105
+ _partition_session = stub_partition_session (1 ),
1101
1106
_bytes_size = 0 ,
1102
1107
_codec = Codec .CODEC_RAW ,
1103
1108
),
1104
- ] ,
1109
+ } ,
1105
1110
),
1106
1111
(
1107
- [
1108
- PublicBatch (
1112
+ {
1113
+ 0 : PublicBatch (
1109
1114
messages = [stub_message (1 )],
1110
1115
_partition_session = stub_partition_session (),
1111
1116
_bytes_size = 0 ,
1112
1117
_codec = Codec .CODEC_RAW ,
1113
1118
),
1114
- PublicBatch (
1119
+ 1 : PublicBatch (
1115
1120
messages = [stub_message (2 ), stub_message (3 )],
1116
- _partition_session = stub_partition_session (),
1121
+ _partition_session = stub_partition_session (1 ),
1117
1122
_bytes_size = 0 ,
1118
1123
_codec = Codec .CODEC_RAW ,
1119
1124
),
1120
- ] ,
1125
+ } ,
1121
1126
stub_message (1 ),
1122
- [
1123
- PublicBatch (
1127
+ {
1128
+ 1 : PublicBatch (
1124
1129
messages = [stub_message (2 ), stub_message (3 )],
1125
- _partition_session = stub_partition_session (),
1130
+ _partition_session = stub_partition_session (1 ),
1126
1131
_bytes_size = 0 ,
1127
1132
_codec = Codec .CODEC_RAW ,
1128
1133
)
1129
- ] ,
1134
+ } ,
1130
1135
),
1131
1136
],
1132
1137
)
@@ -1137,11 +1142,11 @@ async def test_read_message(
1137
1142
expected_message : PublicMessage ,
1138
1143
batches_after : typing .List [datatypes .PublicBatch ],
1139
1144
):
1140
- stream_reader ._message_batches = deque (batches_before )
1145
+ stream_reader ._message_batches = OrderedDict (batches_before )
1141
1146
mess = stream_reader .receive_message_nowait ()
1142
1147
1143
1148
assert mess == expected_message
1144
- assert list (stream_reader ._message_batches ) == batches_after
1149
+ assert dict (stream_reader ._message_batches ) == batches_after
1145
1150
1146
1151
async def test_receive_batch_nowait (self , stream , stream_reader , partition_session ):
1147
1152
assert stream_reader .receive_batch_nowait () is None
@@ -1152,30 +1157,21 @@ async def test_receive_batch_nowait(self, stream, stream_reader, partition_sessi
1152
1157
await self .send_message (stream_reader , mess1 )
1153
1158
1154
1159
mess2 = self .create_message (partition_session , 2 , 1 )
1155
- await self .send_message (stream_reader , mess2 )
1160
+ await self .send_message (stream_reader , mess2 , new_batch = False )
1156
1161
1157
1162
assert stream_reader ._buffer_size_bytes == initial_buffer_size - 2 * self .default_batch_size
1158
1163
1159
1164
received = stream_reader .receive_batch_nowait ()
1160
1165
assert received == PublicBatch (
1161
- messages = [mess1 ],
1166
+ messages = [mess1 , mess2 ],
1162
1167
_partition_session = mess1 ._partition_session ,
1163
- _bytes_size = self .default_batch_size ,
1164
- _codec = Codec .CODEC_RAW ,
1165
- )
1166
-
1167
- received = stream_reader .receive_batch_nowait ()
1168
- assert received == PublicBatch (
1169
- messages = [mess2 ],
1170
- _partition_session = mess2 ._partition_session ,
1171
- _bytes_size = self .default_batch_size ,
1168
+ _bytes_size = self .default_batch_size * 2 ,
1172
1169
_codec = Codec .CODEC_RAW ,
1173
1170
)
1174
1171
1175
1172
assert stream_reader ._buffer_size_bytes == initial_buffer_size
1176
1173
1177
- assert StreamReadMessage .ReadRequest (self .default_batch_size ) == stream .from_client .get_nowait ().client_message
1178
- assert StreamReadMessage .ReadRequest (self .default_batch_size ) == stream .from_client .get_nowait ().client_message
1174
+ assert StreamReadMessage .ReadRequest (self .default_batch_size * 2 ) == stream .from_client .get_nowait ().client_message
1179
1175
1180
1176
with pytest .raises (asyncio .QueueEmpty ):
1181
1177
stream .from_client .get_nowait ()
@@ -1186,13 +1182,18 @@ async def test_receive_message_nowait(self, stream, stream_reader, partition_ses
1186
1182
initial_buffer_size = stream_reader ._buffer_size_bytes
1187
1183
1188
1184
await self .send_batch (
1189
- stream_reader , [self .create_message (partition_session , 1 , 1 ), self .create_message (partition_session , 2 , 1 )]
1185
+ stream_reader ,
1186
+ [
1187
+ self .create_message (partition_session , 1 , 1 ),
1188
+ self .create_message (partition_session , 2 , 1 ),
1189
+ ],
1190
1190
)
1191
1191
await self .send_batch (
1192
1192
stream_reader ,
1193
1193
[
1194
1194
self .create_message (partition_session , 10 , 1 ),
1195
1195
],
1196
+ new_batch = False ,
1196
1197
)
1197
1198
1198
1199
assert stream_reader ._buffer_size_bytes == initial_buffer_size - 2 * self .default_batch_size
0 commit comments