Skip to content

Commit 3f79009

Browse files
add test for tee buffer sharing
1 parent f44c81f commit 3f79009

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

unittests/test_itertools.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,29 @@ async def test_peer(peer_tee):
361361
assert results == items
362362

363363

364+
@sync
365+
async def test_tee_share() -> None:
366+
"""Test that related tees share their buffer and see all items"""
367+
items = [1, 2, 3, -5, 12, 78, -1, 111]
368+
369+
async def tee_spawn_walker(
370+
tee_state: AsyncIterator[int], start_idx: int = 0
371+
) -> None:
372+
"""Recursively check `tee_state` elements and spawn new walkers on every step"""
373+
for idx in range(start_idx, len(items)):
374+
await Switch(0, 3)
375+
assert await a.anext(tee_state) == items[idx]
376+
tee_state, child_state = a.tee(tee_state)
377+
await Schedule(tee_spawn_walker(child_state, idx + 1))
378+
await Switch()
379+
380+
head_peer, *child_peers = a.tee(items, n=2)
381+
await Schedule(*(tee_spawn_walker(child, 0) for child in child_peers))
382+
await Switch(len(items) // 2)
383+
results = [item async for item in head_peer]
384+
assert results == items
385+
386+
364387
# see https://github.com/python/cpython/issues/74956
365388
@pytest.mark.skipif(
366389
sys.version_info < (3, 8),

0 commit comments

Comments
 (0)