Commit 1e48eb3b authored by Victor Stinner's avatar Victor Stinner

Issue #20910: Make tests more reliable, less dependent on time

* 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
parent f8cbf78b
...@@ -48,7 +48,12 @@ class ForkWait(unittest.TestCase): ...@@ -48,7 +48,12 @@ class ForkWait(unittest.TestCase):
for i in range(NUM_THREADS): for i in range(NUM_THREADS):
_thread.start_new(self.f, (i,)) _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()) a = sorted(self.alive.keys())
self.assertEqual(a, list(range(NUM_THREADS))) self.assertEqual(a, list(range(NUM_THREADS)))
......
...@@ -87,10 +87,11 @@ class DocXMLRPCHTTPGETServer(unittest.TestCase): ...@@ -87,10 +87,11 @@ class DocXMLRPCHTTPGETServer(unittest.TestCase):
threading.Thread(target=server, args=(self.evt, 1)).start() threading.Thread(target=server, args=(self.evt, 1)).start()
# wait for port to be assigned # wait for port to be assigned
n = 1000 deadline = time.monotonic() + 10.0
while n > 0 and PORT is None: while PORT is None:
time.sleep(0.001) time.sleep(0.010)
n -= 1 if time.monotonic() > deadline:
break
self.client = http.client.HTTPConnection("localhost:%d" % PORT) self.client = http.client.HTTPConnection("localhost:%d" % PORT)
......
...@@ -18,13 +18,14 @@ get_attribute(os, 'fork') ...@@ -18,13 +18,14 @@ get_attribute(os, 'fork')
class ForkTest(ForkWait): class ForkTest(ForkWait):
def wait_impl(self, cpid): 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 # waitpid() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem. # in the forking tests. This is an attempt to fix the problem.
spid, status = os.waitpid(cpid, os.WNOHANG) spid, status = os.waitpid(cpid, os.WNOHANG)
if spid == cpid: if spid == cpid:
break break
time.sleep(1.0) time.sleep(0.1)
self.assertEqual(spid, cpid) self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
......
...@@ -18,7 +18,8 @@ class Wait3Test(ForkWait): ...@@ -18,7 +18,8 @@ class Wait3Test(ForkWait):
# This many iterations can be required, since some previously run # This many iterations can be required, since some previously run
# tests (e.g. test_ctypes) could have spawned a lot of children # tests (e.g. test_ctypes) could have spawned a lot of children
# very quickly. # 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 # wait3() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem. # in the forking tests. This is an attempt to fix the problem.
spid, status, rusage = os.wait3(os.WNOHANG) spid, status, rusage = os.wait3(os.WNOHANG)
......
...@@ -19,13 +19,14 @@ class Wait4Test(ForkWait): ...@@ -19,13 +19,14 @@ class Wait4Test(ForkWait):
# Issue #11185: wait4 is broken on AIX and will always return 0 # Issue #11185: wait4 is broken on AIX and will always return 0
# with WNOHANG. # with WNOHANG.
option = 0 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 # wait4() shouldn't hang, but some of the buildbots seem to hang
# in the forking tests. This is an attempt to fix the problem. # in the forking tests. This is an attempt to fix the problem.
spid, status, rusage = os.wait4(cpid, option) spid, status, rusage = os.wait4(cpid, option)
if spid == cpid: if spid == cpid:
break break
time.sleep(1.0) time.sleep(0.1)
self.assertEqual(spid, cpid) self.assertEqual(spid, cpid)
self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
self.assertTrue(rusage) self.assertTrue(rusage)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment