|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from acp.task import MessageSender |
| 8 | + |
| 9 | + |
| 10 | +class _FakeWriter: |
| 11 | + def __init__(self, *, fail_with: Exception | None = None) -> None: |
| 12 | + self.fail_with = fail_with |
| 13 | + self.written: list[bytes] = [] |
| 14 | + self.release = asyncio.Event() |
| 15 | + self.block = False |
| 16 | + |
| 17 | + def write(self, data: bytes) -> None: |
| 18 | + self.written.append(data) |
| 19 | + |
| 20 | + async def drain(self) -> None: |
| 21 | + if self.block: |
| 22 | + await self.release.wait() |
| 23 | + if self.fail_with is not None: |
| 24 | + raise self.fail_with |
| 25 | + |
| 26 | + |
| 27 | +def _make_sender(writer: _FakeWriter) -> MessageSender: |
| 28 | + return MessageSender(writer) # type: ignore[arg-type] |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.asyncio |
| 32 | +async def test_sends_are_written_in_call_order() -> None: |
| 33 | + writer = _FakeWriter() |
| 34 | + writer.block = True |
| 35 | + sender = _make_sender(writer) |
| 36 | + |
| 37 | + tasks = [asyncio.create_task(sender.send({"n": i})) for i in range(3)] |
| 38 | + await asyncio.sleep(0) |
| 39 | + writer.release.set() |
| 40 | + await asyncio.wait_for(asyncio.gather(*tasks), 1) |
| 41 | + assert writer.written == [b'{"n":0}\n', b'{"n":1}\n', b'{"n":2}\n'] |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_send_after_close_raises() -> None: |
| 46 | + writer = _FakeWriter() |
| 47 | + sender = _make_sender(writer) |
| 48 | + await sender.send({"a": 1}) |
| 49 | + await sender.close() |
| 50 | + with pytest.raises(ConnectionError): |
| 51 | + await asyncio.wait_for(sender.send({"a": 2}), 1) |
| 52 | + assert writer.written == [b'{"a":1}\n'] |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_write_failure_fails_waiting_and_later_sends() -> None: |
| 57 | + writer = _FakeWriter(fail_with=BrokenPipeError("boom")) |
| 58 | + writer.block = True |
| 59 | + sender = _make_sender(writer) |
| 60 | + |
| 61 | + first = asyncio.create_task(sender.send({"n": 1})) |
| 62 | + waiting = asyncio.create_task(sender.send({"n": 2})) |
| 63 | + await asyncio.sleep(0) |
| 64 | + writer.release.set() |
| 65 | + |
| 66 | + with pytest.raises(BrokenPipeError): |
| 67 | + await asyncio.wait_for(first, 1) |
| 68 | + with pytest.raises(ConnectionError) as excinfo: |
| 69 | + await asyncio.wait_for(waiting, 1) |
| 70 | + assert isinstance(excinfo.value.__cause__, BrokenPipeError) |
| 71 | + with pytest.raises(ConnectionError): |
| 72 | + await asyncio.wait_for(sender.send({"n": 3}), 1) |
| 73 | + assert len(writer.written) == 1 |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_cancelled_send_releases_lock() -> None: |
| 78 | + writer = _FakeWriter() |
| 79 | + writer.block = True |
| 80 | + sender = _make_sender(writer) |
| 81 | + |
| 82 | + stuck = asyncio.create_task(sender.send({"n": 1})) |
| 83 | + waiting = asyncio.create_task(sender.send({"n": 2})) |
| 84 | + await asyncio.sleep(0) |
| 85 | + stuck.cancel() |
| 86 | + with pytest.raises(asyncio.CancelledError): |
| 87 | + await stuck |
| 88 | + |
| 89 | + writer.release.set() |
| 90 | + await asyncio.wait_for(waiting, 1) |
| 91 | + assert writer.written == [b'{"n":1}\n', b'{"n":2}\n'] |
0 commit comments