]> granicus.if.org Git - python/commitdiff
Issue #17992: Add timeouts to asyncore and asynchat tests so that they won't accident...
authorGiampaolo Rodola' <g.rodola@gmail.com>
Thu, 16 May 2013 13:21:53 +0000 (15:21 +0200)
committerGiampaolo Rodola' <g.rodola@gmail.com>
Thu, 16 May 2013 13:21:53 +0000 (15:21 +0200)
Lib/test/test_asynchat.py
Lib/test/test_asyncore.py
Misc/NEWS

index c79fe6f6138b4fa4b71a98911ce74b2d4dea1450..b616f309de92d4057977bc60af10730c813454c7 100644 (file)
@@ -15,6 +15,7 @@ except ImportError:
 
 HOST = support.HOST
 SERVER_QUIT = b'QUIT\n'
+TIMEOUT = 3.0
 
 if threading:
     class echo_server(threading.Thread):
@@ -123,7 +124,9 @@ class TestAsynchat(unittest.TestCase):
         c.push(b"I'm not dead yet!" + term)
         c.push(SERVER_QUIT)
         asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
-        s.join()
+        s.join(timeout=TIMEOUT)
+        if s.is_alive():
+            self.fail("join() timed out")
 
         self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
 
@@ -154,7 +157,9 @@ class TestAsynchat(unittest.TestCase):
         c.push(data)
         c.push(SERVER_QUIT)
         asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
-        s.join()
+        s.join(timeout=TIMEOUT)
+        if s.is_alive():
+            self.fail("join() timed out")
 
         self.assertEqual(c.contents, [data[:termlen]])
 
@@ -174,7 +179,9 @@ class TestAsynchat(unittest.TestCase):
         c.push(data)
         c.push(SERVER_QUIT)
         asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
-        s.join()
+        s.join(timeout=TIMEOUT)
+        if s.is_alive():
+            self.fail("join() timed out")
 
         self.assertEqual(c.contents, [])
         self.assertEqual(c.buffer, data)
@@ -186,7 +193,9 @@ class TestAsynchat(unittest.TestCase):
         p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
         c.push_with_producer(p)
         asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
-        s.join()
+        s.join(timeout=TIMEOUT)
+        if s.is_alive():
+            self.fail("join() timed out")
 
         self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
 
@@ -196,7 +205,9 @@ class TestAsynchat(unittest.TestCase):
         data = b"hello world\nI'm not dead yet!\n"
         c.push_with_producer(data+SERVER_QUIT)
         asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
-        s.join()
+        s.join(timeout=TIMEOUT)
+        if s.is_alive():
+            self.fail("join() timed out")
 
         self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
 
@@ -207,7 +218,9 @@ class TestAsynchat(unittest.TestCase):
         c.push(b"hello world\n\nI'm not dead yet!\n")
         c.push(SERVER_QUIT)
         asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
-        s.join()
+        s.join(timeout=TIMEOUT)
+        if s.is_alive():
+            self.fail("join() timed out")
 
         self.assertEqual(c.contents,
                          [b"hello world", b"", b"I'm not dead yet!"])
@@ -226,7 +239,9 @@ class TestAsynchat(unittest.TestCase):
         # where the server echoes all of its data before we can check that it
         # got any down below.
         s.start_resend_event.set()
-        s.join()
+        s.join(timeout=TIMEOUT)
+        if s.is_alive():
+            self.fail("join() timed out")
 
         self.assertEqual(c.contents, [])
         # the server might have been able to send a byte or two back, but this
index 57eb4fa5e07407c4bb4b5d5ad267991680cf290b..c02a97689ef6f1df40013be9d8feda226a3f2e1e 100644 (file)
@@ -20,7 +20,7 @@ except ImportError:
     threading = None
 
 HOST = support.HOST
-
+TIMEOUT = 3
 HAS_UNIX_SOCKETS = hasattr(socket, 'AF_UNIX')
 
 class dummysocket:
@@ -397,7 +397,10 @@ class DispatcherWithSendTests(unittest.TestCase):
 
             self.assertEqual(cap.getvalue(), data*2)
         finally:
-            t.join()
+            t.join(timeout=TIMEOUT)
+            if t.is_alive():
+                self.fail("join() timed out")
+
 
 
 class DispatcherWithSendTests_UsePoll(DispatcherWithSendTests):
@@ -789,7 +792,11 @@ class BaseTestAPI:
             t = threading.Thread(target=lambda: asyncore.loop(timeout=0.1,
                                                               count=500))
             t.start()
-            self.addCleanup(t.join)
+            def cleanup():
+                t.join(timeout=TIMEOUT)
+                if t.is_alive():
+                    self.fail("join() timed out")
+            self.addCleanup(cleanup)
 
             s = socket.socket(self.family, socket.SOCK_STREAM)
             s.settimeout(.2)
index 1f7ac98c4f0d6e865c8469ff5349da7e0efdc49d..3899da51e178dcd5ea4b16f59747e8fc467ab2c6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -258,6 +258,9 @@ Library
 Tests
 -----
 
+- Issue #17992: Add timeouts to asyncore and asynchat tests so that they won't
+  accidentally hang.
+
 - Issue #17833: Fix test_gdb failures seen on machines where debug symbols
   for glibc are available (seen on PPC64 Linux).