annotate Lib/test/fork_wait.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 1d84bf5f9478
children 3a0484200034
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37099
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
1 """This test case provides support for checking forking and wait behavior.
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
2
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
3 To test different wait behavior, overrise the wait_impl method.
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
4
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
5 We want fork1() semantics -- only the forking thread survives in the
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
6 child after a fork().
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
7
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
8 On some systems (e.g. Solaris without posix threads) we find that all
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
9 active threads survive in the child after a fork(); this is an error.
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
10
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
11 While BeOS doesn't officially support fork and native threading in
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
12 the same application, the present example should work just fine. DC
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
13 """
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
14
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
15 import os, sys, time, thread, unittest
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
16
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
17 LONGSLEEP = 2
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
18 SHORTSLEEP = 0.5
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
19 NUM_THREADS = 4
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
20
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
21 class ForkWait(unittest.TestCase):
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
22
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
23 def setUp(self):
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
24 self.alive = {}
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
25 self.stop = 0
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
26
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
27 def f(self, id):
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
28 while not self.stop:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
29 self.alive[id] = os.getpid()
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
30 try:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
31 time.sleep(SHORTSLEEP)
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
32 except IOError:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
33 pass
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
34
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
35 def wait_impl(self, cpid):
39032
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
36 for i in range(10):
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
37 # waitpid() shouldn't hang, but some of the buildbots seem to hang
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
38 # in the forking tests. This is an attempt to fix the problem.
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
39 spid, status = os.waitpid(cpid, os.WNOHANG)
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
40 if spid == cpid:
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
41 break
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
42 time.sleep(2 * SHORTSLEEP)
b3c0e0759ac0 Restore rev 47014:
Neal Norwitz <nnorwitz@gmail.com>
parents: 38869
diff changeset
43
37099
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
44 self.assertEquals(spid, cpid)
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
45 self.assertEquals(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
46
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
47 def test_wait(self):
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
48 for i in range(NUM_THREADS):
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
49 thread.start_new(self.f, (i,))
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
50
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
51 time.sleep(LONGSLEEP)
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
52
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
53 a = self.alive.keys()
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
54 a.sort()
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
55 self.assertEquals(a, range(NUM_THREADS))
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
56
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
57 prefork_lives = self.alive.copy()
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
58
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
59 if sys.platform in ['unixware7']:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
60 cpid = os.fork1()
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
61 else:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
62 cpid = os.fork()
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
63
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
64 if cpid == 0:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
65 # Child
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
66 time.sleep(LONGSLEEP)
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
67 n = 0
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
68 for key in self.alive:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
69 if self.alive[key] != prefork_lives[key]:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
70 n += 1
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
71 os._exit(n)
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
72 else:
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
73 # Parent
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
74 self.wait_impl(cpid)
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
75 # Tell threads to die
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
76 self.stop = 1
d62b13488943 Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff changeset
77 time.sleep(2*SHORTSLEEP) # Wait for threads to die