Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | """This test case provides support for checking forking and wait behavior. |
| 2 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 3 | To test different wait behavior, override the wait_impl method. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4 | |
| 5 | We want fork1() semantics -- only the forking thread survives in the |
| 6 | child after a fork(). |
| 7 | |
| 8 | On some systems (e.g. Solaris without posix threads) we find that all |
| 9 | active threads survive in the child after a fork(); this is an error. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 10 | """ |
| 11 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 12 | import os, sys, time, unittest |
| 13 | import test.support as support |
| 14 | _thread = support.import_module('_thread') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15 | |
| 16 | LONGSLEEP = 2 |
| 17 | SHORTSLEEP = 0.5 |
| 18 | NUM_THREADS = 4 |
| 19 | |
| 20 | class ForkWait(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.alive = {} |
| 24 | self.stop = 0 |
| 25 | |
| 26 | def f(self, id): |
| 27 | while not self.stop: |
| 28 | self.alive[id] = os.getpid() |
| 29 | try: |
| 30 | time.sleep(SHORTSLEEP) |
| 31 | except IOError: |
| 32 | pass |
| 33 | |
| 34 | def wait_impl(self, cpid): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 35 | for i in range(10): |
| 36 | # waitpid() shouldn't hang, but some of the buildbots seem to hang |
| 37 | # in the forking tests. This is an attempt to fix the problem. |
| 38 | spid, status = os.waitpid(cpid, os.WNOHANG) |
| 39 | if spid == cpid: |
| 40 | break |
| 41 | time.sleep(2 * SHORTSLEEP) |
| 42 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | self.assertEquals(spid, cpid) |
| 44 | self.assertEquals(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) |
| 45 | |
| 46 | def test_wait(self): |
| 47 | for i in range(NUM_THREADS): |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 48 | _thread.start_new(self.f, (i,)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 49 | |
| 50 | time.sleep(LONGSLEEP) |
| 51 | |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 52 | a = sorted(self.alive.keys()) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 53 | self.assertEquals(a, list(range(NUM_THREADS))) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 54 | |
| 55 | prefork_lives = self.alive.copy() |
| 56 | |
| 57 | if sys.platform in ['unixware7']: |
| 58 | cpid = os.fork1() |
| 59 | else: |
| 60 | cpid = os.fork() |
| 61 | |
| 62 | if cpid == 0: |
| 63 | # Child |
| 64 | time.sleep(LONGSLEEP) |
| 65 | n = 0 |
| 66 | for key in self.alive: |
| 67 | if self.alive[key] != prefork_lives[key]: |
| 68 | n += 1 |
| 69 | os._exit(n) |
| 70 | else: |
| 71 | # Parent |
| 72 | self.wait_impl(cpid) |
| 73 | # Tell threads to die |
| 74 | self.stop = 1 |
| 75 | time.sleep(2*SHORTSLEEP) # Wait for threads to die |