blob: e6d9eed237099167201f08580261efd538ee69cd [file] [log] [blame]
Guido van Rossumf7221c32000-02-25 19:25:05 +00001"""This test checks for correct fork() behavior.
Guido van Rossumf7221c32000-02-25 19:25:05 +00002"""
3
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004import os
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005import time
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006from test.fork_wait import ForkWait
Benjamin Petersonee8712c2008-05-20 21:35:26 +00007from test.support import TestSkipped, run_unittest, reap_children
Guido van Rossumf7221c32000-02-25 19:25:05 +00008
Guido van Rossum49517822000-05-04 00:36:42 +00009try:
10 os.fork
11except AttributeError:
Collin Winter3add4d72007-08-29 23:37:32 +000012 raise TestSkipped("os.fork not defined -- skipping test_fork1")
Guido van Rossum49517822000-05-04 00:36:42 +000013
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014class ForkTest(ForkWait):
15 def wait_impl(self, cpid):
Thomas Wouters0e3f5912006-08-11 14:57:12 +000016 for i in range(10):
17 # waitpid() shouldn't hang, but some of the buildbots seem to hang
18 # in the forking tests. This is an attempt to fix the problem.
19 spid, status = os.waitpid(cpid, os.WNOHANG)
20 if spid == cpid:
21 break
22 time.sleep(1.0)
23
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000024 self.assertEqual(spid, cpid)
25 self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
Guido van Rossumf7221c32000-02-25 19:25:05 +000026
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027def test_main():
28 run_unittest(ForkTest)
Thomas Wouters0e3f5912006-08-11 14:57:12 +000029 reap_children()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000030
31if __name__ == "__main__":
32 test_main()