diff options
author | Victor Stinner <vstinner@python.org> | 2020-03-31 19:49:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-31 19:49:44 (GMT) |
commit | a9f9687a7ce25e7c0c89f88f52db323104668ae0 (patch) | |
tree | 0049f147ffb3311373bb5a5501457d73796e7474 /Lib/test/test_thread.py | |
parent | 27c6231f5827fe17c6cb6f097391931f30b511ec (diff) | |
download | cpython-a9f9687a7ce25e7c0c89f88f52db323104668ae0.zip cpython-a9f9687a7ce25e7c0c89f88f52db323104668ae0.tar.gz cpython-a9f9687a7ce25e7c0c89f88f52db323104668ae0.tar.bz2 |
bpo-40094: Enhance threading tests (GH-19260)
* Rewrite test_thread.test_forkinthread() to use
support.wait_process() and wait for the child process in the main
thread, not in the spawned thread.
* test_threading now uses support.wait_process() and checks the child
process exit code to detect crashes.
Diffstat (limited to 'Lib/test/test_thread.py')
-rw-r--r-- | Lib/test/test_thread.py | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/Lib/test/test_thread.py b/Lib/test/test_thread.py index 9f4801f..77e46f2 100644 --- a/Lib/test/test_thread.py +++ b/Lib/test/test_thread.py @@ -225,30 +225,31 @@ class TestForkInThread(unittest.TestCase): @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: |