Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 1 | """This test checks for correct fork() behavior. |
Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 2 | """ |
| 3 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4 | import os |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5 | import time |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | from test.fork_wait import ForkWait |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test.support import TestSkipped, run_unittest, reap_children |
Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 4951782 | 2000-05-04 00:36:42 +0000 | [diff] [blame] | 9 | try: |
| 10 | os.fork |
| 11 | except AttributeError: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 12 | raise TestSkipped("os.fork not defined -- skipping test_fork1") |
Guido van Rossum | 4951782 | 2000-05-04 00:36:42 +0000 | [diff] [blame] | 13 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14 | class ForkTest(ForkWait): |
| 15 | def wait_impl(self, cpid): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 16 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 24 | self.assertEqual(spid, cpid) |
| 25 | self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) |
Guido van Rossum | f7221c3 | 2000-02-25 19:25:05 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 27 | def test_main(): |
| 28 | run_unittest(ForkTest) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 29 | reap_children() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 30 | |
| 31 | if __name__ == "__main__": |
| 32 | test_main() |