1+ import asyncio
2+
13import pytest
24
35import python_utils
@@ -16,12 +18,42 @@ async def test_abatcher():
1618async def test_abatcher_timed ():
1719 batches = []
1820 async for batch in python_utils .abatcher (
19- python_utils .acount (stop = 10 , delay = 0.08 ), interval = 0.2
21+ python_utils .acount (stop = 10 , delay = 0.08 ), interval = 0.1
2022 ):
2123 batches .append (batch )
2224
23- assert len (batches ) == 3
24- assert sum (len (batch ) for batch in batches ) == 10
25+ assert batches == [[0 , 1 , 2 ], [3 , 4 ], [5 , 6 ], [7 , 8 ], [9 ]]
26+ assert len (batches ) == 5
27+
28+
29+ @pytest .mark .asyncio
30+ async def test_abatcher_timed_with_timeout ():
31+ async def generator ():
32+ # Test if the timeout is respected
33+ yield 0
34+ yield 1
35+ await asyncio .sleep (0.11 )
36+
37+ # Test if the timeout is respected
38+ yield 2
39+ yield 3
40+ await asyncio .sleep (0.11 )
41+
42+ # Test if exceptions are handled correctly
43+ await asyncio .wait_for (asyncio .sleep (1 ), timeout = 0.05 )
44+
45+ # Test if StopAsyncIteration is handled correctly
46+ yield 4
47+
48+ batcher = python_utils .abatcher (generator (), interval = 0.1 )
49+ assert await batcher .__anext__ () == [0 , 1 ]
50+ assert await batcher .__anext__ () == [2 , 3 ]
51+
52+ with pytest .raises (asyncio .TimeoutError ):
53+ await batcher .__anext__ ()
54+
55+ with pytest .raises (StopAsyncIteration ):
56+ await batcher .__anext__ ()
2557
2658
2759def test_batcher ():
0 commit comments