- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathtest_server.py
352 lines (271 loc) · 11.2 KB
/
test_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
importasyncio
importos
importsocket
importtime
importthreading
importunittest
fromtest.supportimportsocket_helper
fromtest.test_asyncioimportutilsastest_utils
fromtest.test_asyncioimportfunctionalasfunc_tests
deftearDownModule():
asyncio.set_event_loop_policy(None)
classBaseStartServer(func_tests.FunctionalTestCaseMixin):
defnew_loop(self):
raiseNotImplementedError
deftest_start_server_1(self):
HELLO_MSG=b'1'*1024*5+b'\n'
defclient(sock, addr):
foriinrange(10):
time.sleep(0.2)
ifsrv.is_serving():
break
else:
raiseRuntimeError
sock.settimeout(2)
sock.connect(addr)
sock.send(HELLO_MSG)
sock.recv_all(1)
sock.close()
asyncdefserve(reader, writer):
awaitreader.readline()
main_task.cancel()
writer.write(b'1')
writer.close()
awaitwriter.wait_closed()
asyncdefmain(srv):
asyncwithsrv:
awaitsrv.serve_forever()
srv=self.loop.run_until_complete(asyncio.start_server(
serve, socket_helper.HOSTv4, 0, start_serving=False))
self.assertFalse(srv.is_serving())
main_task=self.loop.create_task(main(srv))
addr=srv.sockets[0].getsockname()
withself.assertRaises(asyncio.CancelledError):
withself.tcp_client(lambdasock: client(sock, addr)):
self.loop.run_until_complete(main_task)
self.assertEqual(srv.sockets, ())
self.assertIsNone(srv._sockets)
self.assertIsNone(srv._waiters)
self.assertFalse(srv.is_serving())
withself.assertRaisesRegex(RuntimeError, r'is closed'):
self.loop.run_until_complete(srv.serve_forever())
classSelectorStartServerTests(BaseStartServer, unittest.TestCase):
defnew_loop(self):
returnasyncio.SelectorEventLoop()
@socket_helper.skip_unless_bind_unix_socket
deftest_start_unix_server_1(self):
HELLO_MSG=b'1'*1024*5+b'\n'
started=threading.Event()
defclient(sock, addr):
sock.settimeout(2)
started.wait(5)
sock.connect(addr)
sock.send(HELLO_MSG)
sock.recv_all(1)
sock.close()
asyncdefserve(reader, writer):
awaitreader.readline()
main_task.cancel()
writer.write(b'1')
writer.close()
awaitwriter.wait_closed()
asyncdefmain(srv):
asyncwithsrv:
self.assertFalse(srv.is_serving())
awaitsrv.start_serving()
self.assertTrue(srv.is_serving())
started.set()
awaitsrv.serve_forever()
withtest_utils.unix_socket_path() asaddr:
srv=self.loop.run_until_complete(asyncio.start_unix_server(
serve, addr, start_serving=False))
main_task=self.loop.create_task(main(srv))
withself.assertRaises(asyncio.CancelledError):
withself.unix_client(lambdasock: client(sock, addr)):
self.loop.run_until_complete(main_task)
self.assertEqual(srv.sockets, ())
self.assertIsNone(srv._sockets)
self.assertIsNone(srv._waiters)
self.assertFalse(srv.is_serving())
withself.assertRaisesRegex(RuntimeError, r'is closed'):
self.loop.run_until_complete(srv.serve_forever())
classTestServer2(unittest.IsolatedAsyncioTestCase):
asyncdeftest_wait_closed_basic(self):
asyncdefserve(rd, wr):
try:
awaitrd.read()
finally:
wr.close()
awaitwr.wait_closed()
srv=awaitasyncio.start_server(serve, socket_helper.HOSTv4, 0)
self.addCleanup(srv.close)
# active count = 0, not closed: should block
task1=asyncio.create_task(srv.wait_closed())
awaitasyncio.sleep(0)
self.assertFalse(task1.done())
# active count != 0, not closed: should block
addr=srv.sockets[0].getsockname()
(rd, wr) =awaitasyncio.open_connection(addr[0], addr[1])
task2=asyncio.create_task(srv.wait_closed())
awaitasyncio.sleep(0)
self.assertFalse(task1.done())
self.assertFalse(task2.done())
srv.close()
awaitasyncio.sleep(0)
# active count != 0, closed: should block
task3=asyncio.create_task(srv.wait_closed())
awaitasyncio.sleep(0)
self.assertFalse(task1.done())
self.assertFalse(task2.done())
self.assertFalse(task3.done())
wr.close()
awaitwr.wait_closed()
# active count == 0, closed: should unblock
awaittask1
awaittask2
awaittask3
awaitsrv.wait_closed() # Return immediately
asyncdeftest_wait_closed_race(self):
# Test a regression in 3.12.0, should be fixed in 3.12.1
asyncdefserve(rd, wr):
try:
awaitrd.read()
finally:
wr.close()
awaitwr.wait_closed()
srv=awaitasyncio.start_server(serve, socket_helper.HOSTv4, 0)
self.addCleanup(srv.close)
task=asyncio.create_task(srv.wait_closed())
awaitasyncio.sleep(0)
self.assertFalse(task.done())
addr=srv.sockets[0].getsockname()
(rd, wr) =awaitasyncio.open_connection(addr[0], addr[1])
loop=asyncio.get_running_loop()
loop.call_soon(srv.close)
loop.call_soon(wr.close)
awaitsrv.wait_closed()
asyncdeftest_close_clients(self):
asyncdefserve(rd, wr):
try:
awaitrd.read()
finally:
wr.close()
awaitwr.wait_closed()
srv=awaitasyncio.start_server(serve, socket_helper.HOSTv4, 0)
self.addCleanup(srv.close)
addr=srv.sockets[0].getsockname()
(rd, wr) =awaitasyncio.open_connection(addr[0], addr[1])
self.addCleanup(wr.close)
task=asyncio.create_task(srv.wait_closed())
awaitasyncio.sleep(0)
self.assertFalse(task.done())
srv.close()
srv.close_clients()
awaitasyncio.sleep(0)
awaitasyncio.sleep(0)
self.assertTrue(task.done())
asyncdeftest_abort_clients(self):
asyncdefserve(rd, wr):
fut.set_result((rd, wr))
awaitwr.wait_closed()
fut=asyncio.Future()
srv=awaitasyncio.start_server(serve, socket_helper.HOSTv4, 0)
self.addCleanup(srv.close)
addr=srv.sockets[0].getsockname()
(c_rd, c_wr) =awaitasyncio.open_connection(addr[0], addr[1], limit=4096)
self.addCleanup(c_wr.close)
(s_rd, s_wr) =awaitfut
# Limit the socket buffers so we can more reliably overfill them
s_sock=s_wr.get_extra_info('socket')
s_sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 65536)
c_sock=c_wr.get_extra_info('socket')
c_sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536)
# Get the reader in to a paused state by sending more than twice
# the configured limit
s_wr.write(b'a'*4096)
s_wr.write(b'a'*4096)
s_wr.write(b'a'*4096)
whilec_wr.transport.is_reading():
awaitasyncio.sleep(0)
# Get the writer in a waiting state by sending data until the
# kernel stops accepting more data in the send buffer.
# gh-122136: getsockopt() does not reliably report the buffer size
# available for message content.
# We loop until we start filling up the asyncio buffer.
# To avoid an infinite loop we cap at 10 times the expected value
c_bufsize=c_sock.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
s_bufsize=s_sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
foriinrange(10):
s_wr.write(b'a'*c_bufsize)
s_wr.write(b'a'*s_bufsize)
ifs_wr.transport.get_write_buffer_size() >0:
break
self.assertNotEqual(s_wr.transport.get_write_buffer_size(), 0)
task=asyncio.create_task(srv.wait_closed())
awaitasyncio.sleep(0)
self.assertFalse(task.done())
srv.close()
srv.abort_clients()
awaitasyncio.sleep(0)
awaitasyncio.sleep(0)
self.assertTrue(task.done())
# Test the various corner cases of Unix server socket removal
classUnixServerCleanupTests(unittest.IsolatedAsyncioTestCase):
@socket_helper.skip_unless_bind_unix_socket
asyncdeftest_unix_server_addr_cleanup(self):
# Default scenario
withtest_utils.unix_socket_path() asaddr:
asyncdefserve(*args):
pass
srv=awaitasyncio.start_unix_server(serve, addr)
srv.close()
self.assertFalse(os.path.exists(addr))
@socket_helper.skip_unless_bind_unix_socket
asyncdeftest_unix_server_sock_cleanup(self):
# Using already bound socket
withtest_utils.unix_socket_path() asaddr:
asyncdefserve(*args):
pass
withsocket.socket(socket.AF_UNIX, socket.SOCK_STREAM) assock:
sock.bind(addr)
srv=awaitasyncio.start_unix_server(serve, sock=sock)
srv.close()
self.assertFalse(os.path.exists(addr))
@socket_helper.skip_unless_bind_unix_socket
asyncdeftest_unix_server_cleanup_gone(self):
# Someone else has already cleaned up the socket
withtest_utils.unix_socket_path() asaddr:
asyncdefserve(*args):
pass
withsocket.socket(socket.AF_UNIX, socket.SOCK_STREAM) assock:
sock.bind(addr)
srv=awaitasyncio.start_unix_server(serve, sock=sock)
os.unlink(addr)
srv.close()
@socket_helper.skip_unless_bind_unix_socket
asyncdeftest_unix_server_cleanup_replaced(self):
# Someone else has replaced the socket with their own
withtest_utils.unix_socket_path() asaddr:
asyncdefserve(*args):
pass
srv=awaitasyncio.start_unix_server(serve, addr)
os.unlink(addr)
withsocket.socket(socket.AF_UNIX, socket.SOCK_STREAM) assock:
sock.bind(addr)
srv.close()
self.assertTrue(os.path.exists(addr))
@socket_helper.skip_unless_bind_unix_socket
asyncdeftest_unix_server_cleanup_prevented(self):
# Automatic cleanup explicitly disabled
withtest_utils.unix_socket_path() asaddr:
asyncdefserve(*args):
pass
srv=awaitasyncio.start_unix_server(serve, addr, cleanup_socket=False)
srv.close()
self.assertTrue(os.path.exists(addr))
@unittest.skipUnless(hasattr(asyncio, 'ProactorEventLoop'), 'Windows only')
classProactorStartServerTests(BaseStartServer, unittest.TestCase):
defnew_loop(self):
returnasyncio.ProactorEventLoop()
if__name__=='__main__':
unittest.main()