blob: 3e6a79df463cb47669fcc492eef8c9e49a0dca5d [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001"""This test checks for correct wait4() behavior.
2"""
3
4import os
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005import time
Antoine Pitroube9c8412013-07-04 21:03:10 +02006import sys
Zachary Wareac28b792015-12-04 23:32:23 -06007import unittest
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008from test.fork_wait import ForkWait
Zachary Ware38c707e2015-04-13 15:00:43 -05009from test.support import reap_children, get_attribute
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010
R. David Murray6af6d262009-03-31 23:50:31 +000011# If either of these do not exist, skip this test.
12get_attribute(os, 'fork')
13get_attribute(os, 'wait4')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015
16class Wait4Test(ForkWait):
17 def wait_impl(self, cpid):
Antoine Pitroube9c8412013-07-04 21:03:10 +020018 option = os.WNOHANG
19 if sys.platform.startswith('aix'):
20 # Issue #11185: wait4 is broken on AIX and will always return 0
21 # with WNOHANG.
22 option = 0
Victor Stinner1e48eb32014-03-18 00:39:04 +010023 deadline = time.monotonic() + 10.0
24 while time.monotonic() <= deadline:
Thomas Wouters0e3f5912006-08-11 14:57:12 +000025 # wait4() shouldn't hang, but some of the buildbots seem to hang
26 # in the forking tests. This is an attempt to fix the problem.
Antoine Pitroube9c8412013-07-04 21:03:10 +020027 spid, status, rusage = os.wait4(cpid, option)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000028 if spid == cpid:
29 break
Victor Stinner1e48eb32014-03-18 00:39:04 +010030 time.sleep(0.1)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031 self.assertEqual(spid, cpid)
32 self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
33 self.assertTrue(rusage)
34
Zachary Ware38c707e2015-04-13 15:00:43 -050035def tearDownModule():
Thomas Wouters0e3f5912006-08-11 14:57:12 +000036 reap_children()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037
38if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -050039 unittest.main()