@@ -253,6 +253,11 @@ async def wait(fut):
253
253
await reader1 .close ()
254
254
255
255
256
+ @pytest .fixture ()
257
+ def topic_selector (topic_with_messages ):
258
+ return ydb .TopicReaderSelector (path = topic_with_messages , partitions = [0 ])
259
+
260
+
256
261
@pytest .mark .asyncio
257
262
class TestTopicNoConsumerReaderAsyncIO :
258
263
async def test_reader_with_no_partition_ids_raises (self , driver , topic_with_messages ):
@@ -262,57 +267,65 @@ async def test_reader_with_no_partition_ids_raises(self, driver, topic_with_mess
262
267
consumer = None ,
263
268
)
264
269
265
- async def test_reader_with_default_lambda (self , driver , topic_with_messages ):
266
- reader = driver .topic_client .reader (
267
- topic_with_messages ,
268
- consumer = None ,
269
- partition_ids = [0 ],
270
- )
270
+ async def test_reader_with_no_partition_ids_selector_raises (self , driver , topic_selector ):
271
+ topic_selector .partitions = None
272
+
273
+ with pytest .raises (ydb .Error ):
274
+ driver .topic_client .reader (
275
+ topic_selector ,
276
+ consumer = None ,
277
+ )
278
+
279
+ async def test_reader_with_default_lambda (self , driver , topic_selector ):
280
+ reader = driver .topic_client .reader (topic_selector , consumer = None )
271
281
msg = await reader .receive_message ()
272
282
273
283
assert msg .seqno == 1
274
284
275
285
await reader .close ()
276
286
277
- async def test_reader_with_sync_lambda (self , driver , topic_with_messages ):
278
- def sync_lambda (partition_id : int ):
279
- assert partition_id == 0
280
- return 1
287
+ async def test_reader_with_sync_lambda (self , driver , topic_selector ):
288
+ class CustomEventHandler (ydb .TopicReaderEvents .EventHandler ):
289
+ def on_partition_get_start_offset (self , event ):
290
+ assert topic_selector .path .endswith (event .topic )
291
+ assert event .partition_id == 0
292
+ return ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse (1 )
281
293
282
294
reader = driver .topic_client .reader (
283
- topic_with_messages ,
295
+ topic_selector ,
284
296
consumer = None ,
285
- partition_ids = [0 ],
286
- get_start_offset_lambda = sync_lambda ,
297
+ event_handler = CustomEventHandler (),
287
298
)
299
+
288
300
msg = await reader .receive_message ()
289
301
290
302
assert msg .seqno == 2
291
303
292
304
await reader .close ()
293
305
294
- async def test_reader_with_async_lambda (self , driver , topic_with_messages ):
295
- async def async_lambda (partition_id : int ) -> int :
296
- assert partition_id == 0
297
- return 1
306
+ async def test_reader_with_async_lambda (self , driver , topic_selector ):
307
+ class CustomEventHandler (ydb .TopicReaderEvents .EventHandler ):
308
+ async def on_partition_get_start_offset (self , event ):
309
+ assert topic_selector .path .endswith (event .topic )
310
+ assert event .partition_id == 0
311
+ return ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse (1 )
298
312
299
313
reader = driver .topic_client .reader (
300
- topic_with_messages ,
314
+ topic_selector ,
301
315
consumer = None ,
302
- partition_ids = [0 ],
303
- get_start_offset_lambda = async_lambda ,
316
+ event_handler = CustomEventHandler (),
304
317
)
318
+
305
319
msg = await reader .receive_message ()
306
320
307
321
assert msg .seqno == 2
308
322
309
323
await reader .close ()
310
324
311
- async def test_commit_not_allowed (self , driver , topic_with_messages ):
325
+ async def test_commit_not_allowed (self , driver , topic_selector ):
312
326
reader = driver .topic_client .reader (
313
- topic_with_messages ,
327
+ topic_selector ,
314
328
consumer = None ,
315
- partition_ids = [0 ],
316
329
)
317
330
batch = await reader .receive_batch ()
318
331
@@ -324,18 +337,18 @@ async def test_commit_not_allowed(self, driver, topic_with_messages):
324
337
325
338
await reader .close ()
326
339
327
- async def test_offsets_updated_after_reconnect (self , driver , topic_with_messages ):
340
+ async def test_offsets_updated_after_reconnect (self , driver , topic_selector ):
328
341
current_offset = 0
329
342
330
- def get_start_offset_lambda (partition_id : int ) -> int :
331
- nonlocal current_offset
332
- return current_offset
343
+ class CustomEventHandler (ydb .TopicReaderEvents .EventHandler ):
344
+ def on_partition_get_start_offset (self , event ):
345
+ nonlocal current_offset
346
+ return ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse (current_offset )
333
347
334
348
reader = driver .topic_client .reader (
335
- topic_with_messages ,
349
+ topic_selector ,
336
350
consumer = None ,
337
- partition_ids = [0 ],
338
- get_start_offset_lambda = get_start_offset_lambda ,
351
+ event_handler = CustomEventHandler (),
339
352
)
340
353
msg = await reader .receive_message ()
341
354
@@ -361,57 +374,65 @@ def test_reader_with_no_partition_ids_raises(self, driver_sync, topic_with_messa
361
374
consumer = None ,
362
375
)
363
376
364
- def test_reader_with_default_lambda (self , driver_sync , topic_with_messages ):
365
- reader = driver_sync .topic_client .reader (
366
- topic_with_messages ,
367
- consumer = None ,
368
- partition_ids = [0 ],
369
- )
377
+ def test_reader_with_no_partition_ids_selector_raises (self , driver_sync , topic_selector ):
378
+ topic_selector .partitions = None
379
+
380
+ with pytest .raises (ydb .Error ):
381
+ driver_sync .topic_client .reader (
382
+ topic_selector ,
383
+ consumer = None ,
384
+ )
385
+
386
+ def test_reader_with_default_lambda (self , driver_sync , topic_selector ):
387
+ reader = driver_sync .topic_client .reader (topic_selector , consumer = None )
370
388
msg = reader .receive_message ()
371
389
372
390
assert msg .seqno == 1
373
391
374
392
reader .close ()
375
393
376
- def test_reader_with_sync_lambda (self , driver_sync , topic_with_messages ):
377
- def sync_lambda (partition_id : int ):
378
- assert partition_id == 0
379
- return 1
394
+ def test_reader_with_sync_lambda (self , driver_sync , topic_selector ):
395
+ class CustomEventHandler (ydb .TopicReaderEvents .EventHandler ):
396
+ def on_partition_get_start_offset (self , event ):
397
+ assert topic_selector .path .endswith (event .topic )
398
+ assert event .partition_id == 0
399
+ return ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse (1 )
380
400
381
401
reader = driver_sync .topic_client .reader (
382
- topic_with_messages ,
402
+ topic_selector ,
383
403
consumer = None ,
384
- partition_ids = [0 ],
385
- get_start_offset_lambda = sync_lambda ,
404
+ event_handler = CustomEventHandler (),
386
405
)
406
+
387
407
msg = reader .receive_message ()
388
408
389
409
assert msg .seqno == 2
390
410
391
411
reader .close ()
392
412
393
- def test_reader_with_async_lambda (self , driver_sync , topic_with_messages ):
394
- async def async_lambda (partition_id : int ) -> int :
395
- assert partition_id == 0
396
- return 1
413
+ def test_reader_with_async_lambda (self , driver_sync , topic_selector ):
414
+ class CustomEventHandler (ydb .TopicReaderEvents .EventHandler ):
415
+ async def on_partition_get_start_offset (self , event ):
416
+ assert topic_selector .path .endswith (event .topic )
417
+ assert event .partition_id == 0
418
+ return ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse (1 )
397
419
398
420
reader = driver_sync .topic_client .reader (
399
- topic_with_messages ,
421
+ topic_selector ,
400
422
consumer = None ,
401
- partition_ids = [0 ],
402
- get_start_offset_lambda = async_lambda ,
423
+ event_handler = CustomEventHandler (),
403
424
)
425
+
404
426
msg = reader .receive_message ()
405
427
406
428
assert msg .seqno == 2
407
429
408
430
reader .close ()
409
431
410
- def test_commit_not_allowed (self , driver_sync , topic_with_messages ):
432
+ def test_commit_not_allowed (self , driver_sync , topic_selector ):
411
433
reader = driver_sync .topic_client .reader (
412
- topic_with_messages ,
434
+ topic_selector ,
413
435
consumer = None ,
414
- partition_ids = [0 ],
415
436
)
416
437
batch = reader .receive_batch ()
417
438
@@ -421,23 +442,20 @@ def test_commit_not_allowed(self, driver_sync, topic_with_messages):
421
442
with pytest .raises (ydb .Error ):
422
443
reader .commit_with_ack (batch )
423
444
424
- with pytest .raises (ydb .Error ):
425
- reader .async_commit_with_ack (batch )
426
-
427
445
reader .close ()
428
446
429
- def test_offsets_updated_after_reconnect (self , driver_sync , topic_with_messages ):
447
+ def test_offsets_updated_after_reconnect (self , driver_sync , topic_selector ):
430
448
current_offset = 0
431
449
432
- def get_start_offset_lambda (partition_id : int ) -> int :
433
- nonlocal current_offset
434
- return current_offset
450
+ class CustomEventHandler (ydb .TopicReaderEvents .EventHandler ):
451
+ def on_partition_get_start_offset (self , event ):
452
+ nonlocal current_offset
453
+ return ydb .TopicReaderEvents .OnPartitionGetStartOffsetResponse (current_offset )
435
454
436
455
reader = driver_sync .topic_client .reader (
437
- topic_with_messages ,
456
+ topic_selector ,
438
457
consumer = None ,
439
- partition_ids = [0 ],
440
- get_start_offset_lambda = get_start_offset_lambda ,
458
+ event_handler = CustomEventHandler (),
441
459
)
442
460
msg = reader .receive_message ()
443
461
0 commit comments