]> granicus.if.org Git - python/commitdiff
Issue #20910: Make tests more reliable, less dependent on time
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 17 Mar 2014 23:39:04 +0000 (00:39 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 17 Mar 2014 23:39:04 +0000 (00:39 +0100)
* Tolerate 10 seconds instead of 3 seconds for slow test
* Faster test, use sleep of 100 ms instead of 1 sec
* Replace a number of iterations with an explicit deadline for the timeout

Lib/test/fork_wait.py
Lib/test/test_docxmlrpc.py
Lib/test/test_fork1.py
Lib/test/test_wait3.py
Lib/test/test_wait4.py

index 19b54ec736b77ee627ff11c4715b4d2b06e46aeb..8c7c3aac992a503fc6ad6cb1fb052830a7e99b80 100644 (file)
@@ -48,7 +48,12 @@ class ForkWait(unittest.TestCase):
         for i in range(NUM_THREADS):
             _thread.start_new(self.f, (i,))
 
-        time.sleep(LONGSLEEP)
+        # busy-loop to wait for threads
+        deadline = time.monotonic() + 10.0
+        while len(self.alive) < NUM_THREADS:
+            time.sleep(0.1)
+            if time.monotonic() <= deadline:
+                break
 
         a = sorted(self.alive.keys())
         self.assertEqual(a, list(range(NUM_THREADS)))
index cb6366c7d5c2a0e519fa05fe5dd6e59a54c6432f..eb975161af017ac3b1dd0801c7d2d4c7dd9f68d8 100644 (file)
@@ -87,10 +87,11 @@ class DocXMLRPCHTTPGETServer(unittest.TestCase):
         threading.Thread(target=server, args=(self.evt, 1)).start()
 
         # wait for port to be assigned
-        n = 1000
-        while n > 0 and PORT is None:
-            time.sleep(0.001)
-            n -= 1
+        deadline = time.monotonic() + 10.0
+        while PORT is None:
+            time.sleep(0.010)
+            if time.monotonic() > deadline:
+                break
 
         self.client = http.client.HTTPConnection("localhost:%d" % PORT)
 
index e0626dffdcb9eb03cd78f3f362f1cd0e4c2e1dc4..8bcbd465cc32138e95259a90cd893eed5b3f870f 100644 (file)
@@ -18,13 +18,14 @@ get_attribute(os, 'fork')
 
 class ForkTest(ForkWait):
     def wait_impl(self, cpid):
-        for i in range(10):
+        deadline = time.monotonic() + 10.0
+        while time.monotonic() <= deadline:
             # waitpid() shouldn't hang, but some of the buildbots seem to hang
             # in the forking tests.  This is an attempt to fix the problem.
             spid, status = os.waitpid(cpid, os.WNOHANG)
             if spid == cpid:
                 break
-            time.sleep(1.0)
+            time.sleep(0.1)
 
         self.assertEqual(spid, cpid)
         self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
index f6a065d850d9ce8f993e6c8bd34fe4f3ea08cbc7..bb71481adc3f365f221aa307c0e71b52f715ae23 100644 (file)
@@ -18,7 +18,8 @@ class Wait3Test(ForkWait):
         # This many iterations can be required, since some previously run
         # tests (e.g. test_ctypes) could have spawned a lot of children
         # very quickly.
-        for i in range(30):
+        deadline = time.monotonic() + 10.0
+        while time.monotonic() <= deadline:
             # wait3() shouldn't hang, but some of the buildbots seem to hang
             # in the forking tests.  This is an attempt to fix the problem.
             spid, status, rusage = os.wait3(os.WNOHANG)
index 352c11aadec36d042a64e68eae14ff7b342bf847..b427a9b1a4cb2cb0becefa7cdb3aee70bffd5b2a 100644 (file)
@@ -19,13 +19,14 @@ class Wait4Test(ForkWait):
             # Issue #11185: wait4 is broken on AIX and will always return 0
             # with WNOHANG.
             option = 0
-        for i in range(10):
+        deadline = time.monotonic() + 10.0
+        while time.monotonic() <= deadline:
             # wait4() shouldn't hang, but some of the buildbots seem to hang
             # in the forking tests.  This is an attempt to fix the problem.
             spid, status, rusage = os.wait4(cpid, option)
             if spid == cpid:
                 break
-            time.sleep(1.0)
+            time.sleep(0.1)
         self.assertEqual(spid, cpid)
         self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
         self.assertTrue(rusage)