|
| 1 | +import asyncio |
| 2 | +import sys |
| 3 | +from typing import (TYPE_CHECKING, Any, AsyncIterator, Awaitable, Protocol, |
| 4 | + Tuple, TypeVar) |
| 5 | + |
1 | 6 | import pytest
|
2 | 7 |
|
3 |
| -from vllm.utils import deprecate_kwargs |
| 8 | +from vllm.utils import deprecate_kwargs, merge_async_iterators |
4 | 9 |
|
5 | 10 | from .utils import error_on_warning
|
6 | 11 |
|
| 12 | +if sys.version_info < (3, 10): |
| 13 | + if TYPE_CHECKING: |
| 14 | + _AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any]) |
| 15 | + _AwaitableT_co = TypeVar("_AwaitableT_co", |
| 16 | + bound=Awaitable[Any], |
| 17 | + covariant=True) |
| 18 | + |
| 19 | + class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]): |
| 20 | + |
| 21 | + def __anext__(self) -> _AwaitableT_co: |
| 22 | + ... |
| 23 | + |
| 24 | + def anext(i: "_SupportsSynchronousAnext[_AwaitableT]", /) -> "_AwaitableT": |
| 25 | + return i.__anext__() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.asyncio |
| 29 | +async def test_merge_async_iterators(): |
| 30 | + |
| 31 | + async def mock_async_iterator(idx: int) -> AsyncIterator[str]: |
| 32 | + try: |
| 33 | + while True: |
| 34 | + yield f"item from iterator {idx}" |
| 35 | + await asyncio.sleep(0.1) |
| 36 | + except asyncio.CancelledError: |
| 37 | + pass |
| 38 | + |
| 39 | + iterators = [mock_async_iterator(i) for i in range(3)] |
| 40 | + merged_iterator: AsyncIterator[Tuple[int, str]] = merge_async_iterators( |
| 41 | + *iterators) |
| 42 | + |
| 43 | + async def stream_output(generator: AsyncIterator[Tuple[int, str]]): |
| 44 | + async for idx, output in generator: |
| 45 | + print(f"idx: {idx}, output: {output}") |
| 46 | + |
| 47 | + task = asyncio.create_task(stream_output(merged_iterator)) |
| 48 | + await asyncio.sleep(0.5) |
| 49 | + task.cancel() |
| 50 | + with pytest.raises(asyncio.CancelledError): |
| 51 | + await task |
| 52 | + |
| 53 | + for iterator in iterators: |
| 54 | + try: |
| 55 | + await asyncio.wait_for(anext(iterator), 1) |
| 56 | + except StopAsyncIteration: |
| 57 | + # All iterators should be cancelled and print this message. |
| 58 | + print("Iterator was cancelled normally") |
| 59 | + except (Exception, asyncio.CancelledError) as e: |
| 60 | + raise AssertionError() from e |
| 61 | + |
7 | 62 |
|
8 | 63 | def test_deprecate_kwargs_always():
|
9 | 64 |
|
|
0 commit comments