Skip to content

Commit 3cb0906

Browse files
committed
Issue #17992: Add timeouts to asyncore and asynchat tests so that they won't accidentally hang.
1 parent 0d4f08c commit 3cb0906

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

Lib/test/test_asynchat.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
HOST = support.HOST
1717
SERVER_QUIT = b'QUIT\n'
18+
TIMEOUT = 3.0
1819

1920
if threading:
2021
class echo_server(threading.Thread):
@@ -123,7 +124,9 @@ def line_terminator_check(self, term, server_chunk):
123124
c.push(b"I'm not dead yet!" + term)
124125
c.push(SERVER_QUIT)
125126
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
126-
s.join()
127+
s.join(timeout=TIMEOUT)
128+
if s.is_alive():
129+
self.fail("join() timed out")
127130

128131
self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
129132

@@ -154,7 +157,9 @@ def numeric_terminator_check(self, termlen):
154157
c.push(data)
155158
c.push(SERVER_QUIT)
156159
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
157-
s.join()
160+
s.join(timeout=TIMEOUT)
161+
if s.is_alive():
162+
self.fail("join() timed out")
158163

159164
self.assertEqual(c.contents, [data[:termlen]])
160165

@@ -174,7 +179,9 @@ def test_none_terminator(self):
174179
c.push(data)
175180
c.push(SERVER_QUIT)
176181
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
177-
s.join()
182+
s.join(timeout=TIMEOUT)
183+
if s.is_alive():
184+
self.fail("join() timed out")
178185

179186
self.assertEqual(c.contents, [])
180187
self.assertEqual(c.buffer, data)
@@ -186,7 +193,9 @@ def test_simple_producer(self):
186193
p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
187194
c.push_with_producer(p)
188195
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
189-
s.join()
196+
s.join(timeout=TIMEOUT)
197+
if s.is_alive():
198+
self.fail("join() timed out")
190199

191200
self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
192201

@@ -196,7 +205,9 @@ def test_string_producer(self):
196205
data = b"hello world\nI'm not dead yet!\n"
197206
c.push_with_producer(data+SERVER_QUIT)
198207
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
199-
s.join()
208+
s.join(timeout=TIMEOUT)
209+
if s.is_alive():
210+
self.fail("join() timed out")
200211

201212
self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
202213

@@ -207,7 +218,9 @@ def test_empty_line(self):
207218
c.push(b"hello world\n\nI'm not dead yet!\n")
208219
c.push(SERVER_QUIT)
209220
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
210-
s.join()
221+
s.join(timeout=TIMEOUT)
222+
if s.is_alive():
223+
self.fail("join() timed out")
211224

212225
self.assertEqual(c.contents,
213226
[b"hello world", b"", b"I'm not dead yet!"])
@@ -226,7 +239,9 @@ def test_close_when_done(self):
226239
# where the server echoes all of its data before we can check that it
227240
# got any down below.
228241
s.start_resend_event.set()
229-
s.join()
242+
s.join(timeout=TIMEOUT)
243+
if s.is_alive():
244+
self.fail("join() timed out")
230245

231246
self.assertEqual(c.contents, [])
232247
# the server might have been able to send a byte or two back, but this

Lib/test/test_asyncore.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
threading = None
2121

2222
HOST = support.HOST
23-
23+
TIMEOUT = 3
2424
HAS_UNIX_SOCKETS = hasattr(socket, 'AF_UNIX')
2525

2626
class dummysocket:
@@ -397,7 +397,10 @@ def test_send(self):
397397

398398
self.assertEqual(cap.getvalue(), data*2)
399399
finally:
400-
t.join()
400+
t.join(timeout=TIMEOUT)
401+
if t.is_alive():
402+
self.fail("join() timed out")
403+
401404

402405

403406
class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests):
@@ -789,7 +792,11 @@ def test_quick_connect(self):
789792
t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1,
790793
count=500))
791794
t.start()
792-
self.addCleanup(t.join)
795+
def cleanup():
796+
t.join(timeout=TIMEOUT)
797+
if t.is_alive():
798+
self.fail("join() timed out")
799+
self.addCleanup(cleanup)
793800

794801
s = socket.socket(self.family, socket.SOCK_STREAM)
795802
s.settimeout(.2)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ Library
258258
Tests
259259
-----
260260

261+
- Issue #17992: Add timeouts to asyncore and asynchat tests so that they won't
262+
accidentally hang.
263+
261264
- Issue #17833: Fix test_gdb failures seen on machines where debug symbols
262265
for glibc are available (seen on PPC64 Linux).
263266

0 commit comments

Comments
 (0)