Commit c4d7864e authored by Antoine Pitrou's avatar Antoine Pitrou

Use shared testing facilities in test_threading

parent 66a18fd8
# Very rudimentary test of threading module # Very rudimentary test of threading module
import test.support import test.support
from test.support import verbose, strip_python_stderr from test.support import verbose, strip_python_stderr, import_module
import random import random
import re import re
import sys import sys
_thread = test.support.import_module('_thread') _thread = import_module('_thread')
threading = test.support.import_module('threading') threading = import_module('threading')
import time import time
import unittest import unittest
import weakref import weakref
import os import os
import subprocess from test.script_helper import assert_python_ok, assert_python_failure
from test.script_helper import assert_python_ok
from test import lock_tests from test import lock_tests
...@@ -163,10 +162,7 @@ class ThreadTests(BaseTestCase): ...@@ -163,10 +162,7 @@ class ThreadTests(BaseTestCase):
# PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently) # PyThreadState_SetAsyncExc() is a CPython-only gimmick, not (currently)
# exposed at the Python level. This test relies on ctypes to get at it. # exposed at the Python level. This test relies on ctypes to get at it.
def test_PyThreadState_SetAsyncExc(self): def test_PyThreadState_SetAsyncExc(self):
try: ctypes = import_module("ctypes")
import ctypes
except ImportError:
raise unittest.SkipTest("cannot import ctypes")
set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc set_async_exc = ctypes.pythonapi.PyThreadState_SetAsyncExc
...@@ -269,12 +265,9 @@ class ThreadTests(BaseTestCase): ...@@ -269,12 +265,9 @@ class ThreadTests(BaseTestCase):
# Issue 1402: the PyGILState_Ensure / _Release functions may be called # Issue 1402: the PyGILState_Ensure / _Release functions may be called
# very late on python exit: on deallocation of a running thread for # very late on python exit: on deallocation of a running thread for
# example. # example.
try: import_module("ctypes")
import ctypes
except ImportError:
raise unittest.SkipTest("cannot import ctypes")
rc = subprocess.call([sys.executable, "-c", """if 1: rc, out, err = assert_python_failure("-c", """if 1:
import ctypes, sys, time, _thread import ctypes, sys, time, _thread
# This lock is used as a simple event variable. # This lock is used as a simple event variable.
...@@ -298,13 +291,13 @@ class ThreadTests(BaseTestCase): ...@@ -298,13 +291,13 @@ class ThreadTests(BaseTestCase):
_thread.start_new_thread(waitingThread, ()) _thread.start_new_thread(waitingThread, ())
ready.acquire() # Be sure the other thread is waiting. ready.acquire() # Be sure the other thread is waiting.
sys.exit(42) sys.exit(42)
"""]) """)
self.assertEqual(rc, 42) self.assertEqual(rc, 42)
def test_finalize_with_trace(self): def test_finalize_with_trace(self):
# Issue1733757 # Issue1733757
# Avoid a deadlock when sys.settrace steps into threading._shutdown # Avoid a deadlock when sys.settrace steps into threading._shutdown
p = subprocess.Popen([sys.executable, "-c", """if 1: assert_python_ok("-c", """if 1:
import sys, threading import sys, threading
# A deadlock-killer, to prevent the # A deadlock-killer, to prevent the
...@@ -324,21 +317,12 @@ class ThreadTests(BaseTestCase): ...@@ -324,21 +317,12 @@ class ThreadTests(BaseTestCase):
return func return func
sys.settrace(func) sys.settrace(func)
"""], """)
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
self.addCleanup(p.stdout.close)
self.addCleanup(p.stderr.close)
stdout, stderr = p.communicate()
rc = p.returncode
self.assertFalse(rc == 2, "interpreted was blocked")
self.assertTrue(rc == 0,
"Unexpected error: " + ascii(stderr))
def test_join_nondaemon_on_shutdown(self): def test_join_nondaemon_on_shutdown(self):
# Issue 1722344 # Issue 1722344
# Raising SystemExit skipped threading._shutdown # Raising SystemExit skipped threading._shutdown
p = subprocess.Popen([sys.executable, "-c", """if 1: rc, out, err = assert_python_ok("-c", """if 1:
import threading import threading
from time import sleep from time import sleep
...@@ -350,16 +334,10 @@ class ThreadTests(BaseTestCase): ...@@ -350,16 +334,10 @@ class ThreadTests(BaseTestCase):
threading.Thread(target=child).start() threading.Thread(target=child).start()
raise SystemExit raise SystemExit
"""], """)
stdout=subprocess.PIPE, self.assertEqual(out.strip(),
stderr=subprocess.PIPE)
self.addCleanup(p.stdout.close)
self.addCleanup(p.stderr.close)
stdout, stderr = p.communicate()
self.assertEqual(stdout.strip(),
b"Woke up, sleep function is: <built-in function sleep>") b"Woke up, sleep function is: <built-in function sleep>")
stderr = strip_python_stderr(stderr) self.assertEqual(err, b"")
self.assertEqual(stderr, b"")
def test_enumerate_after_join(self): def test_enumerate_after_join(self):
# Try hard to trigger #1703448: a thread is still returned in # Try hard to trigger #1703448: a thread is still returned in
...@@ -444,13 +422,9 @@ class ThreadJoinOnShutdown(BaseTestCase): ...@@ -444,13 +422,9 @@ class ThreadJoinOnShutdown(BaseTestCase):
sys.stdout.flush() sys.stdout.flush()
\n""" + script \n""" + script
p = subprocess.Popen([sys.executable, "-c", script], stdout=subprocess.PIPE) rc, out, err = assert_python_ok("-c", script)
rc = p.wait() data = out.decode().replace('\r', '')
data = p.stdout.read().decode().replace('\r', '')
p.stdout.close()
self.assertEqual(data, "end of main\nend of thread\n") self.assertEqual(data, "end of main\nend of thread\n")
self.assertFalse(rc == 2, "interpreter was blocked")
self.assertTrue(rc == 0, "Unexpected error")
def test_1_join_on_shutdown(self): def test_1_join_on_shutdown(self):
# The usual case: on exit, wait for a non-daemon thread # The usual case: on exit, wait for a non-daemon thread
...@@ -510,11 +484,8 @@ class ThreadJoinOnShutdown(BaseTestCase): ...@@ -510,11 +484,8 @@ class ThreadJoinOnShutdown(BaseTestCase):
self._run_and_join(script) self._run_and_join(script)
def assertScriptHasOutput(self, script, expected_output): def assertScriptHasOutput(self, script, expected_output):
p = subprocess.Popen([sys.executable, "-c", script], rc, out, err = assert_python_ok("-c", script)
stdout=subprocess.PIPE) data = out.decode().replace('\r', '')
stdout, stderr = p.communicate()
data = stdout.decode().replace('\r', '')
self.assertEqual(p.returncode, 0, "Unexpected error")
self.assertEqual(data, expected_output) self.assertEqual(data, expected_output)
@unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()") @unittest.skipUnless(hasattr(os, 'fork'), "needs os.fork()")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment