Debian Codegraph @

test/test_wait.py

raw
1 from __future__ import annotations
2
3 import signal
4 import threading
5 import time
6 import typing
7 from socket import socket, socketpair
8 from types import FrameType
9
10 import pytest
11
12 from urllib3.util.wait import (
13 _have_working_poll,
14 poll_wait_for_socket,
15 select_wait_for_socket,
16 wait_for_read,
17 wait_for_socket,
18 wait_for_write,
19 )
20
21 TYPE_SOCKET_PAIR = tuple[socket, socket]
22 TYPE_WAIT_FOR = typing.Callable[..., bool]
23
24
25 @pytest.fixture
26 def spair() -> typing.Generator[TYPE_SOCKET_PAIR]:
27 a, b = socketpair()
28 yield a, b
29 a.close()
30 b.close()
31
32
33 variants: list[TYPE_WAIT_FOR] = [wait_for_socket, select_wait_for_socket]
34 if _have_working_poll():
35 variants.append(poll_wait_for_socket)
36
37
38 @pytest.mark.parametrize("wfs", variants)
39 def test_wait_for_socket(wfs: TYPE_WAIT_FOR, spair: TYPE_SOCKET_PAIR) -> None:
40 a, b = spair
41
42 with pytest.raises(RuntimeError):
43 wfs(a, read=False, write=False)
44
45 assert not wfs(a, read=True, timeout=0)
46 assert wfs(a, write=True, timeout=0)
47
48 b.send(b"x")
49 assert wfs(a, read=True, timeout=0)
50 assert wfs(a, read=True, timeout=10)
51 assert wfs(a, read=True, timeout=None)
52
53 # Fill up the socket with data
54 a.setblocking(False)
55 try:
56 while True:
57 a.send(b"x" * 999999)
58 except OSError:
59 pass
60
61 # Now it's not writable anymore
62 assert not wfs(a, write=True, timeout=0)
63
64 # But if we ask for read-or-write, that succeeds
65 assert wfs(a, read=True, write=True, timeout=0)
66
67 # Unless we read from it
68 assert a.recv(1) == b"x"
69 assert not wfs(a, read=True, write=True, timeout=0)
70
71 # But if the remote peer closes the socket, then it becomes readable
72 b.close()
73 assert wfs(a, read=True, timeout=0)
74
75 # Waiting for a socket that's actually been closed is just a bug, and
76 # raises some kind of helpful exception (exact details depend on the
77 # platform).
78 with pytest.raises(Exception):
79 wfs(b, read=True)
80
81
82 def test_wait_for_read_write(spair: TYPE_SOCKET_PAIR) -> None:
83 a, b = spair
84
85 assert not wait_for_read(a, 0)
86 assert wait_for_write(a, 0)
87
88 b.send(b"x")
89
90 assert wait_for_read(a, 0)
91 assert wait_for_write(a, 0)
92
93 # Fill up the socket with data
94 a.setblocking(False)
95 try:
96 while True:
97 a.send(b"x" * 999999)
98 except OSError:
99 pass
100
101 # Now it's not writable anymore
102 assert not wait_for_write(a, 0)
103
104
105 @pytest.mark.skipif(not hasattr(signal, "setitimer"), reason="need setitimer() support")
106 @pytest.mark.parametrize("wfs", variants)
107 def test_eintr(wfs: TYPE_WAIT_FOR, spair: TYPE_SOCKET_PAIR) -> None:
108 a, b = spair
109 interrupt_count = [0]
110
111 def handler(sig: int, frame: FrameType | None) -> typing.Any:
112 assert sig == signal.SIGALRM
113 interrupt_count[0] += 1
114
115 old_handler = signal.signal(signal.SIGALRM, handler)
116 try:
117 assert not wfs(a, read=True, timeout=0)
118 start = time.monotonic()
119 try:
120 # Start delivering SIGALRM 10 times per second
121 signal.setitimer(signal.ITIMER_REAL, 0.1, 0.1)
122 # Sleep for 1 second (we hope!)
123 wfs(a, read=True, timeout=1)
124 finally:
125 # Stop delivering SIGALRM
126 signal.setitimer(signal.ITIMER_REAL, 0)
127 end = time.monotonic()
128 dur = end - start
129 assert 0.9 < dur < 3
130 finally:
131 signal.signal(signal.SIGALRM, old_handler)
132
133 assert interrupt_count[0] > 0
134
135
136 @pytest.mark.skipif(not hasattr(signal, "setitimer"), reason="need setitimer() support")
137 @pytest.mark.parametrize("wfs", variants)
138 def test_eintr_zero_timeout(wfs: TYPE_WAIT_FOR, spair: TYPE_SOCKET_PAIR) -> None:
139 a, b = spair
140 interrupt_count = [0]
141
142 def handler(sig: int, frame: FrameType | None) -> typing.Any:
143 assert sig == signal.SIGALRM
144 interrupt_count[0] += 1
145
146 old_handler = signal.signal(signal.SIGALRM, handler)
147 try:
148 assert not wfs(a, read=True, timeout=0)
149 try:
150 # Start delivering SIGALRM 1000 times per second,
151 # to trigger race conditions such as
152 # https://github.com/urllib3/urllib3/issues/1396.
153 signal.setitimer(signal.ITIMER_REAL, 0.001, 0.001)
154 # Hammer the system call for a while to trigger the
155 # race.
156 end = time.monotonic() + 5
157 for i in range(100000):
158 wfs(a, read=True, timeout=0)
159 if time.monotonic() >= end:
160 break
161 finally:
162 # Stop delivering SIGALRM
163 signal.setitimer(signal.ITIMER_REAL, 0)
164 finally:
165 signal.signal(signal.SIGALRM, old_handler)
166
167 assert interrupt_count[0] > 0
168
169
170 @pytest.mark.skipif(not hasattr(signal, "setitimer"), reason="need setitimer() support")
171 @pytest.mark.parametrize("wfs", variants)
172 def test_eintr_infinite_timeout(wfs: TYPE_WAIT_FOR, spair: TYPE_SOCKET_PAIR) -> None:
173 a, b = spair
174 interrupt_count = [0]
175
176 def handler(sig: int, frame: FrameType | None) -> typing.Any:
177 assert sig == signal.SIGALRM
178 interrupt_count[0] += 1
179
180 def make_a_readable_after_one_second() -> None:
181 time.sleep(1)
182 b.send(b"x")
183
184 old_handler = signal.signal(signal.SIGALRM, handler)
185 try:
186 assert not wfs(a, read=True, timeout=0)
187 start = time.monotonic()
188 try:
189 # Start delivering SIGALRM 10 times per second
190 signal.setitimer(signal.ITIMER_REAL, 0.1, 0.1)
191 # Sleep for 1 second (we hope!)
192 thread = threading.Thread(target=make_a_readable_after_one_second)
193 thread.start()
194 wfs(a, read=True)
195 finally:
196 # Stop delivering SIGALRM
197 signal.setitimer(signal.ITIMER_REAL, 0)
198 thread.join()
199 end = time.monotonic()
200 dur = end - start
201 assert 0.9 < dur < 3
202 finally:
203 signal.signal(signal.SIGALRM, old_handler)
204
205 assert interrupt_count[0] > 0