1"""This test checks for correct wait3() behavior.
2"""
3
4import os
5import time
6import unittest
7from test.fork_wait import ForkWait
8from test.support import reap_children
9
10if not hasattr(os, 'fork'):
11    raise unittest.SkipTest("os.fork not defined")
12
13if not hasattr(os, 'wait3'):
14    raise unittest.SkipTest("os.wait3 not defined")
15
16class Wait3Test(ForkWait):
17    def wait_impl(self, cpid):
18        # 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.
21        deadline = time.monotonic() + 10.0
22        while time.monotonic() <= deadline:
23            # 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)
26            if spid == cpid:
27                break
28            time.sleep(0.1)
29
30        self.assertEqual(spid, cpid)
31        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
32        self.assertTrue(rusage)
33
34def tearDownModule():
35    reap_children()
36
37if __name__ == "__main__":
38    unittest.main()
39