test_async_generator.py 658 B

12345678910111213141516171819202122232425262728
  1. from __future__ import annotations
  2. from asyncio import run
  3. from prompt_toolkit.eventloop import generator_to_async_generator
  4. def _sync_generator():
  5. yield 1
  6. yield 10
  7. def test_generator_to_async_generator():
  8. """
  9. Test conversion of sync to async generator.
  10. This should run the synchronous parts in a background thread.
  11. """
  12. async_gen = generator_to_async_generator(_sync_generator)
  13. items = []
  14. async def consume_async_generator():
  15. async for item in async_gen:
  16. items.append(item)
  17. # Run the event loop until all items are collected.
  18. run(consume_async_generator())
  19. assert items == [1, 10]