Skip to content

Commit cb4c29c

Browse files
committed
cc tests: fix flapping sleep in tests
commit_hash:b53bccd83f42ea1e82fabc33dc4d7abc2c9a374d
1 parent 33524a9 commit cb4c29c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

samples/netcat/testsuite/test_basic.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,23 @@ async def test_listen(service_binary):
4343
str(port),
4444
stdout=asyncio.subprocess.PIPE,
4545
)
46-
await asyncio.sleep(0.3) # give time to open listening socket
4746

48-
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as client:
49-
client.connect(('localhost', port))
47+
# try several times to make sure netcat has opened the tcp port
48+
for i in range(1, 100):
49+
try:
50+
client = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
51+
client.connect(('localhost', port))
52+
break
53+
except ConnectionRefusedError:
54+
if client:
55+
client.close()
56+
client = None
57+
await asyncio.sleep(0.3)
58+
continue
59+
else:
60+
assert False, 'failed to connect to netcat'
61+
62+
try:
5063
client.setblocking(False)
5164

5265
loop = asyncio.get_event_loop()
@@ -57,3 +70,5 @@ async def test_listen(service_binary):
5770

5871
subprocess.terminate()
5972
await subprocess.wait()
73+
finally:
74+
client.close()

0 commit comments

Comments
 (0)