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