blob: eb51b2c03b232d32037302bc7ba47829b30115ed [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001"""This test checks for correct wait3() behavior.
2"""
3
4import os
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005import time
Benjamin Petersone549ead2009-03-28 21:42:05 +00006import unittest
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007from test.fork_wait import ForkWait
Zachary Ware38c707e2015-04-13 15:00:43 -05008from test.support import reap_children
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009
Brett Cannoncc134312012-11-14 15:49:55 -050010if not hasattr(os, 'fork'):
Brett Cannoncd8efa32012-11-14 15:16:53 -050011 raise unittest.SkipTest("os.fork not defined")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000012
Brett Cannoncd8efa32012-11-14 15:16:53 -050013if not hasattr(os, 'wait3'):
14 raise unittest.SkipTest("os.wait3 not defined")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015
16class Wait3Test(ForkWait):
17 def wait_impl(self, cpid):
Antoine Pitrou606c3f52011-05-09 21:17:02 +020018 # This many iterations can be required, since some previously run
19 # tests (e.g. test_ctypes) could have spawned a lot of children
20 # very quickly.
Victor Stinner1e48eb32014-03-18 00:39:04 +010021 deadline = time.monotonic() + 10.0
22 while time.monotonic() <= deadline:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000023 # wait3() shouldn't hang, but some of the buildbots seem to hang
24 # in the forking tests. This is an attempt to fix the problem.
25 spid, status, rusage = os.wait3(os.WNOHANG)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026 if spid == cpid:
27 break
Antoine Pitrou606c3f52011-05-09 21:17:02 +020028 time.sleep(0.1)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000029
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030 self.assertEqual(spid, cpid)
31 self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
32 self.assertTrue(rusage)
33
Zachary Ware38c707e2015-04-13 15:00:43 -050034def tearDownModule():
Thomas Wouters0e3f5912006-08-11 14:57:12 +000035 reap_children()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036
37if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050038 unittest.main()