Debian Codegraph @

src/trio/_tests/test_windows_pipes.py

raw
1 from __future__ import annotations
2
3 import sys
4 from typing import TYPE_CHECKING
5
6 import pytest
7
8 from .. import _core
9 from ..testing import check_one_way_stream, wait_all_tasks_blocked
10
11 # Mark all the tests in this file as being windows-only
12 pytestmark = pytest.mark.skipif(sys.platform != "win32", reason="windows only")
13
14 assert ( # Skip type checking when not on Windows
15 sys.platform == "win32" or not TYPE_CHECKING
16 )
17
18 if sys.platform == "win32":
19 from asyncio.windows_utils import pipe
20
21 from .._core._windows_cffi import _handle, kernel32
22 from .._windows_pipes import PipeReceiveStream, PipeSendStream
23
24
25 async def make_pipe() -> tuple[PipeSendStream, PipeReceiveStream]:
26 """Makes a new pair of pipes."""
27 r, w = pipe()
28 return PipeSendStream(w), PipeReceiveStream(r)
29
30
31 def test_pipe_typecheck() -> None:
32 with pytest.raises(TypeError):
33 PipeSendStream(1.0) # type: ignore[arg-type]
34 with pytest.raises(TypeError):
35 PipeReceiveStream(None) # type: ignore[arg-type]
36
37
38 async def test_pipe_error_on_close() -> None:
39 # Make sure we correctly handle a failure from kernel32.CloseHandle
40 r, w = pipe()
41
42 send_stream = PipeSendStream(w)
43 receive_stream = PipeReceiveStream(r)
44
45 assert kernel32.CloseHandle(_handle(r))
46 assert kernel32.CloseHandle(_handle(w))
47
48 with pytest.raises(OSError, match=r"^\[WinError 6\] The handle is invalid$"):
49 await send_stream.aclose()
50 with pytest.raises(OSError, match=r"^\[WinError 6\] The handle is invalid$"):
51 await receive_stream.aclose()
52
53
54 async def test_pipes_combined() -> None:
55 write, read = await make_pipe()
56 count = 2**20
57 replicas = 3
58
59 async def sender() -> None:
60 async with write:
61 big = bytearray(count)
62 for _ in range(replicas):
63 await write.send_all(big)
64
65 async def reader() -> None:
66 async with read:
67 await wait_all_tasks_blocked()
68 total_received = 0
69 while True:
70 # 5000 is chosen because it doesn't evenly divide 2**20
71 received = len(await read.receive_some(5000))
72 if not received:
73 break
74 total_received += received
75
76 assert total_received == count * replicas
77
78 async with _core.open_nursery() as n:
79 n.start_soon(sender)
80 n.start_soon(reader)
81
82
83 async def test_async_with() -> None:
84 w, r = await make_pipe()
85 async with w, r:
86 pass
87
88 with pytest.raises(_core.ClosedResourceError):
89 await w.send_all(b"")
90 with pytest.raises(_core.ClosedResourceError):
91 await r.receive_some(10)
92
93
94 async def test_close_during_write() -> None:
95 w, _r = await make_pipe()
96 async with _core.open_nursery() as nursery:
97
98 async def write_forever() -> None:
99 with pytest.raises(_core.ClosedResourceError) as excinfo:
100 while True:
101 await w.send_all(b"x" * 4096)
102 assert "another task" in str(excinfo.value)
103
104 nursery.start_soon(write_forever)
105 await wait_all_tasks_blocked(0.1)
106 await w.aclose()
107
108
109 async def test_pipe_fully() -> None:
110 # passing make_clogged_pipe tests wait_send_all_might_not_block, and we
111 # can't implement that on Windows
112 await check_one_way_stream(make_pipe, None)