1 """This test checks for correct fork() behavior.
10 from test
.fork_wait
import ForkWait
11 from test
.test_support
import run_unittest
, reap_children
, get_attribute
, import_module
12 threading
= import_module('threading')
14 #Skip test if fork does not exist.
15 get_attribute(os
, 'fork')
18 class ForkTest(ForkWait
):
19 def wait_impl(self
, cpid
):
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
)
28 self
.assertEqual(spid
, cpid
)
29 self
.assertEqual(status
, 0, "cause = %d, exit = %d" % (status
&0xff, status
>>8))
31 def test_import_lock_fork(self
):
32 import_started
= threading
.Event()
33 fake_module_name
= "fake test module"
34 partial_module
= "partial"
35 complete_module
= "complete"
38 sys
.modules
[fake_module_name
] = partial_module
40 time
.sleep(0.01) # Give the other thread time to try and acquire.
41 sys
.modules
[fake_module_name
] = complete_module
43 t
= threading
.Thread(target
=importer
)
49 m
= __import__(fake_module_name
)
50 if m
== complete_module
:
56 # Exitcode 1 means the child got a partial module (bad.) No
57 # exitcode (but a hang, which manifests as 'got pid 0')
58 # means the child deadlocked (also bad.)
62 os
.kill(pid
, signal
.SIGKILL
)
67 run_unittest(ForkTest
)
70 if __name__
== "__main__":