Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_fork1.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b36cb4602e21 |
| children |
| rev | line source |
|---|---|
|
11855
79ea19085fb6
Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """This test checks for correct fork() behavior. |
|
79ea19085fb6
Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 """ |
|
79ea19085fb6
Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 |
|
56008
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
4 import imp |
|
37099
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
5 import os |
|
56008
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
6 import signal |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
7 import sys |
| 39032 | 8 import time |
|
56008
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
9 |
|
37099
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
10 from test.fork_wait import ForkWait |
|
61046
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
11 from test.test_support import run_unittest, reap_children, get_attribute, import_module |
|
3a0484200034
Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents:
58817
diff
changeset
|
12 threading = import_module('threading') |
|
11855
79ea19085fb6
Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
52456
f2c90fd05d66
Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents:
52440
diff
changeset
|
14 #Skip test if fork does not exist. |
|
52491
f4c5e3a64be2
Change more tests to use import_module for the modules that
R. David Murray <rdmurray@bitdance.com>
parents:
52456
diff
changeset
|
15 get_attribute(os, 'fork') |
|
52456
f2c90fd05d66
Add import_function method to test.test_support, and modify a number of
R. David Murray <rdmurray@bitdance.com>
parents:
52440
diff
changeset
|
16 |
|
12578
077456c324bc
Raise ImportError when os.fork does not exist.
Guido van Rossum <guido@python.org>
parents:
12470
diff
changeset
|
17 |
|
37099
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
18 class ForkTest(ForkWait): |
|
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
19 def wait_impl(self, cpid): |
| 39032 | 20 for i in range(10): |
| 21 # waitpid() shouldn't hang, but some of the buildbots seem to hang | |
| 22 # in the forking tests. This is an attempt to fix the problem. | |
| 23 spid, status = os.waitpid(cpid, os.WNOHANG) | |
| 24 if spid == cpid: | |
| 25 break | |
| 26 time.sleep(1.0) | |
| 27 | |
|
37099
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
28 self.assertEqual(spid, cpid) |
|
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
29 self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8)) |
|
12332
6b4667fa4938
Use a constant to specify the number of child threads to create.
Fred Drake <fdrake@acm.org>
parents:
11855
diff
changeset
|
30 |
|
56008
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
31 def test_import_lock_fork(self): |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
32 import_started = threading.Event() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
33 fake_module_name = "fake test module" |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
34 partial_module = "partial" |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
35 complete_module = "complete" |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
36 def importer(): |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
37 imp.acquire_lock() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
38 sys.modules[fake_module_name] = partial_module |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
39 import_started.set() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
40 time.sleep(0.01) # Give the other thread time to try and acquire. |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
41 sys.modules[fake_module_name] = complete_module |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
42 imp.release_lock() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
43 t = threading.Thread(target=importer) |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
44 t.start() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
45 import_started.wait() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
46 pid = os.fork() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
47 try: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
48 if not pid: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
49 m = __import__(fake_module_name) |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
50 if m == complete_module: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
51 os._exit(0) |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
52 else: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
53 os._exit(1) |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
54 else: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
55 t.join() |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
56 # Exitcode 1 means the child got a partial module (bad.) No |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
57 # exitcode (but a hang, which manifests as 'got pid 0') |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
58 # means the child deadlocked (also bad.) |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
59 self.wait_impl(pid) |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
60 finally: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
61 try: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
62 os.kill(pid, signal.SIGKILL) |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
63 except OSError: |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
64 pass |
|
1ad9f2bda20a
Merged revisions 74841 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39032
diff
changeset
|
65 |
|
37099
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
66 def test_main(): |
|
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
67 run_unittest(ForkTest) |
|
38961
0a83dc3529d1
Add new utility function, reap_children(), to test_support. This should
Neal Norwitz <nnorwitz@gmail.com>
parents:
38869
diff
changeset
|
68 reap_children() |
|
11855
79ea19085fb6
Test case for fork1() behavior.
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 |
|
37099
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
70 if __name__ == "__main__": |
|
d62b13488943
Patch #1309579: wait3 and wait4 were added to the posix module by Chad J. Schroeder.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24366
diff
changeset
|
71 test_main() |
