1"""This test checks for correct wait4() behavior.
2"""
3
4import os
5import time
6import sys
7import unittest
8from test.fork_wait import ForkWait
9from test.support import reap_children, get_attribute
10
11# If either of these do not exist, skip this test.
12get_attribute(os, 'fork')
13get_attribute(os, 'wait4')
14
15
16class Wait4Test(ForkWait):
17    def wait_impl(self, cpid):
18        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
23        deadline = time.monotonic() + 10.0
24        while time.monotonic() <= deadline:
25            # 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.
27            spid, status, rusage = os.wait4(cpid, option)
28            if spid == cpid:
29                break
30            time.sleep(0.1)
31        self.assertEqual(spid, cpid)
32        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
33        self.assertTrue(rusage)
34
35def tearDownModule():
36    reap_children()
37
38if __name__ == "__main__":
39    unittest.main()
40