1 """This test checks for correct fork() behavior.
12 from test
.fork_wait
import ForkWait
13 from test
.test_support
import run_unittest
, reap_children
, get_attribute
15 #Skip test if fork does not exist.
16 get_attribute(os
, 'fork')
19 class ForkTest(ForkWait
):
20 def wait_impl(self
, cpid
):
22 # waitpid() shouldn't hang, but some of the buildbots seem to hang
23 # in the forking tests. This is an attempt to fix the problem.
24 spid
, status
= os
.waitpid(cpid
, os
.WNOHANG
)
29 self
.assertEqual(spid
, cpid
)
30 self
.assertEqual(status
, 0, "cause = %d, exit = %d" % (status
&0xff, status
>>8))
32 def test_import_lock_fork(self
):
33 import_started
= threading
.Event()
34 fake_module_name
= "fake test module"
35 partial_module
= "partial"
36 complete_module
= "complete"
39 sys
.modules
[fake_module_name
] = partial_module
41 time
.sleep(0.01) # Give the other thread time to try and acquire.
42 sys
.modules
[fake_module_name
] = complete_module
44 t
= threading
.Thread(target
=importer
)
50 m
= __import__(fake_module_name
)
51 if m
== complete_module
:
57 # Exitcode 1 means the child got a partial module (bad.) No
58 # exitcode (but a hang, which manifests as 'got pid 0')
59 # means the child deadlocked (also bad.)
63 os
.kill(pid
, signal
.SIGKILL
)
68 run_unittest(ForkTest
)
71 if __name__
== "__main__":