bpo-32662: Try making test_asyncio.test_server more reliable (#5338) · python/cpython@4112c5b (original) (raw)

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
1 1 import asyncio
2 2 import socket
3 +import time
3 4 import threading
4 5 import unittest
5 6
7 +from test import support
6 8 from test.test_asyncio import utils as test_utils
7 9 from test.test_asyncio import functional as func_tests
8 10
@@ -16,6 +18,14 @@ def test_start_server_1(self):
16 18 HELLO_MSG = b'1' * 1024 * 5 + b'\n'
17 19
18 20 def client(sock, addr):
21 +for i in range(10):
22 +time.sleep(0.2)
23 +if srv.is_serving():
24 +break
25 +else:
26 +raise RuntimeError
27 +
28 +sock.settimeout(2)
19 29 sock.connect(addr)
20 30 sock.send(HELLO_MSG)
21 31 sock.recv_all(1)
@@ -33,7 +43,7 @@ async def main(srv):
33 43 await srv.serve_forever()
34 44
35 45 srv = self.loop.run_until_complete(asyncio.start_server(
36 -serve, '127.0.0.1', 0, loop=self.loop, start_serving=False))
46 +serve, support.HOSTv4, 0, loop=self.loop, start_serving=False))
37 47
38 48 self.assertFalse(srv.is_serving())
39 49
@@ -65,6 +75,7 @@ def test_start_unix_server_1(self):
65 75 started = threading.Event()
66 76
67 77 def client(sock, addr):
78 +sock.settimeout(2)
68 79 started.wait(5)
69 80 sock.connect(addr)
70 81 sock.send(HELLO_MSG)