8000 bpo-40094: Enhance threading tests by vstinner · Pull Request #19260 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-40094: Enhance threading tests #19260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions Lib/test/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,31 @@ def setUp(self):
@unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork')
@support.reap_threads
def test_forkinthread(self):
status = "not set"
pid = None

def thread1():
nonlocal status
def fork_thread(read_fd, write_fd):
nonlocal pid

# fork in a thread
pid = os.fork()
if pid == 0:
# child
try:
os.close(self.read_fd)
os.write(self.write_fd, b"OK")
finally:
os._exit(0)
else:
# parent
os.close(self.write_fd)
pid, status = os.waitpid(pid, 0)
if pid:
# parent process
return

# child process
try:
os.close(read_fd)
os.write(write_fd, b"OK")
finally:
os._exit(0)

with support.wait_threads_exit():
thread.start_new_thread(thread1, ())
self.assertEqual(os.read(self.read_fd, 2), b"OK",
"Unable to fork() in thread")
self.assertEqual(status, 0)
thread.start_new_thread(fork_thread, (self.read_fd, self.write_fd))
self.assertEqual(os.read(self.read_fd, 2), b"OK")
os.close(self.write_fd)

self.assertIsNotNone(pid)
support.wait_process(pid, exitcode=0)

def tearDown(self):
try:
Expand Down
33 changes: 20 additions & 13 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,7 @@ def test_is_alive_after_fork(self):
else:
t.join()

pid, status = os.waitpid(pid, 0)
self.assertTrue(os.WIFEXITED(status))
self.assertEqual(10, os.WEXITSTATUS(status))
support.wait_process(pid, exitcode=10)

def test_main_thread(self):
main = threading.main_thread()
Expand All @@ -507,6 +505,7 @@ def f():
def test_main_thread_after_fork(self):
code = """if 1:
import os, threading
from test import support

pid = os.fork()
if pid == 0:
Expand All @@ -515,7 +514,7 @@ def test_main_thread_after_fork(self):
print(main.ident == threading.current_thread().ident)
print(main.ident == threading.get_ident())
else:
os.waitpid(pid, 0)
support.wait_process(pid, exitcode=0)
"""
_, out, err = assert_python_ok("-c", code)
data = out.decode().replace('\r', '')
Expand All @@ -528,6 +527,7 @@ def test_main_thread_after_fork(self):
def test_main_thread_after_fork_from_nonmain_thread(self):
code = """if 1:
import os, threading, sys
from test import support

def f():
pid = os.fork()
Expand All @@ -540,7 +540,7 @@ def f():
# we have to flush before exit.
sys.stdout.flush()
else:
os.waitpid(pid, 0)
support.wait_process(pid, exitcode=0)

th = threading.Thread(target=f)
th.start()
Expand Down Expand Up @@ -813,11 +813,15 @@ def test_1_join_on_shutdown(self):
def test_2_join_in_forked_process(self):
# Like the test above, but from a forked interpreter
script = """if 1:
from test import support

childpid = os.fork()
if childpid != 0:
os.waitpid(childpid, 0)
# parent process
support.wait_process(childpid, exitcode=0)
sys.exit(0)

# child process
t = threading.Thread(target=joiningfunc,
args=(threading.current_thread(),))
t.start()
Expand All @@ -832,13 +836,17 @@ def test_3_join_in_forked_from_thread(self):
# In the forked process, the main Thread object must be marked as stopped.

script = """if 1:
from test import support

main_thread = threading.current_thread()
def worker():
childpid = os.fork()
if childpid != 0:
os.waitpid(childpid, 0)
# parent process
support.wait_process(childpid, exitcode=0)
sys.exit(0)

# child process
t = threading.Thread(target=joiningfunc,
args=(main_thread,))
print('end of main')
Expand Down Expand Up @@ -901,9 +909,9 @@ def do_fork_and_wait():
# just fork a child process and wait it
pid = os.fork()
if pid > 0:
os.waitpid(pid, 0)
support.wait_process(pid, exitcode=50)
else:
os._exit(0)
os._exit(50)

# start a bunch of threads that will fork() child processes
threads = []
Expand All @@ -930,12 +938,11 @@ def test_clear_threads_states_after_fork(self):
if pid == 0:
# check that threads states have been cleared
if len(sys._current_frames()) == 1:
os._exit(0)
os._exit(51)
else:
os._exit(1)
os._exit(52)
else:
_, status = os.waitpid(pid, 0)
self.assertEqual(0, status)
support.wait_process(pid, exitcode=51)

for t in threads:
t.join()
Expand Down
0