Debian Codegraph @

src/trio/_tests/test_highlevel_generic.py

raw
1 from __future__ import annotations
2
3 from typing import NoReturn
4
5 import attrs
6 import pytest
7
8 from .._highlevel_generic import StapledStream
9 from ..abc import ReceiveStream, SendStream
10
11
12 @attrs.define(slots=False)
13 class RecordSendStream(SendStream):
14 record: list[str | tuple[str, object]] = attrs.Factory(list)
15
16 async def send_all(self, data: object) -> None:
17 self.record.append(("send_all", data))
18
19 async def wait_send_all_might_not_block(self) -> None:
20 self.record.append("wait_send_all_might_not_block")
21
22 async def aclose(self) -> None:
23 self.record.append("aclose")
24
25
26 @attrs.define(slots=False)
27 class RecordReceiveStream(ReceiveStream):
28 record: list[str | tuple[str, int | None]] = attrs.Factory(list)
29
30 async def receive_some(self, max_bytes: int | None = None) -> bytes:
31 self.record.append(("receive_some", max_bytes))
32 return b""
33
34 async def aclose(self) -> None:
35 self.record.append("aclose")
36
37
38 async def test_StapledStream() -> None:
39 send_stream = RecordSendStream()
40 receive_stream = RecordReceiveStream()
41 stapled = StapledStream(send_stream, receive_stream)
42
43 assert stapled.send_stream is send_stream
44 assert stapled.receive_stream is receive_stream
45
46 await stapled.send_all(b"foo")
47 await stapled.wait_send_all_might_not_block()
48 assert send_stream.record == [
49 ("send_all", b"foo"),
50 "wait_send_all_might_not_block",
51 ]
52 send_stream.record.clear()
53
54 await stapled.send_eof()
55 assert send_stream.record == ["aclose"]
56 send_stream.record.clear()
57
58 async def fake_send_eof() -> None:
59 send_stream.record.append("send_eof")
60
61 send_stream.send_eof = fake_send_eof # type: ignore[attr-defined]
62 await stapled.send_eof()
63 assert send_stream.record == ["send_eof"]
64
65 send_stream.record.clear()
66 assert receive_stream.record == []
67
68 await stapled.receive_some(1234)
69 assert receive_stream.record == [("receive_some", 1234)]
70 assert send_stream.record == []
71 receive_stream.record.clear()
72
73 await stapled.aclose()
74 assert receive_stream.record == ["aclose"]
75 assert send_stream.record == ["aclose"]
76
77
78 async def test_StapledStream_with_erroring_close() -> None:
79 # Make sure that if one of the aclose methods errors out, then the other
80 # one still gets called.
81 class BrokenSendStream(RecordSendStream):
82 async def aclose(self) -> NoReturn:
83 await super().aclose()
84 raise ValueError("send error")
85
86 class BrokenReceiveStream(RecordReceiveStream):
87 async def aclose(self) -> NoReturn:
88 await super().aclose()
89 raise ValueError("recv error")
90
91 stapled = StapledStream(BrokenSendStream(), BrokenReceiveStream())
92
93 with pytest.raises(ValueError, match=r"^(send|recv) error$") as excinfo:
94 await stapled.aclose()
95 assert isinstance(excinfo.value.__context__, ValueError)
96
97 assert stapled.send_stream.record == ["aclose"]
98 assert stapled.receive_stream.record == ["aclose"]