Mercurial > cpython-lru_cache-weakref
annotate Lib/test/fork_wait.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | 614c84670c63 |
| children |
| 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 |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
45468
diff
changeset
|
3 To test different wait behavior, override the wait_impl method. |
|
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
|
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 |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
45468
diff
changeset
|
15 import os, sys, time, unittest |
|
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
45468
diff
changeset
|
16 import test.test_support as test_support |
|
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
45468
diff
changeset
|
17 thread = test_support.import_module('thread') |
|
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
|
18 |
|
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 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
|
20 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
|
21 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
|
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 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
|
24 |
|
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 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
|
26 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
|
27 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
|
28 |
|
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 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 |
|
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
diff
changeset
|
37 def wait_impl(self, cpid): |
| 39032 | 38 for i in range(10): |
| 39 # waitpid() shouldn't hang, but some of the buildbots seem to hang | |
| 40 # in the forking tests. This is an attempt to fix the problem. | |
| 41 spid, status = os.waitpid(cpid, os.WNOHANG) | |
| 42 if spid == cpid: | |
| 43 break | |
| 44 time.sleep(2 * SHORTSLEEP) | |
| 45 | |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61046
diff
changeset
|
46 self.assertEqual(spid, cpid) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61046
diff
changeset
|
47 self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) |
|
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
|
48 |
|
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 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
|
50 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
|
51 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
|
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 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
|
54 |
|
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 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
|
56 a.sort() |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
61046
diff
changeset
|
57 self.assertEqual(a, range(NUM_THREADS)) |
|
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
|
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 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
|
60 |
|
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 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
|
62 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
|
63 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
|
64 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
|
65 |
|
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 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
|
67 # 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
|
68 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
|
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 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
|
75 # 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
|
76 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
|
77 # 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
|
78 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
|
79 time.sleep(2*SHORTSLEEP) # Wait for threads to die |
