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 import support
10
11# If either of these do not exist, skip this test.
12if not support.has_fork_support:
13    raise unittest.SkipTest("requires working os.fork()")
14
15support.get_attribute(os, 'wait4')
16
17
18class Wait4Test(ForkWait):
19    def wait_impl(self, cpid, *, exitcode):
20        option = os.WNOHANG
21        if sys.platform.startswith('aix'):
22            # Issue #11185: wait4 is broken on AIX and will always return 0
23            # with WNOHANG.
24            option = 0
25        deadline = time.monotonic() + support.SHORT_TIMEOUT
26        while time.monotonic() <= deadline:
27            # wait4() shouldn't hang, but some of the buildbots seem to hang
28            # in the forking tests.  This is an attempt to fix the problem.
29            spid, status, rusage = os.wait4(cpid, option)
30            if spid == cpid:
31                break
32            time.sleep(0.1)
33        self.assertEqual(spid, cpid)
34        self.assertEqual(os.waitstatus_to_exitcode(status), exitcode)
35        self.assertTrue(rusage)
36
37def tearDownModule():
38    support.reap_children()
39
40if __name__ == "__main__":
41    unittest.main()
42