Speed up test_signal from ~24s to 4s by avoiding nearly all of the sleep calls.

This commit is contained in:
Jeffrey Yasskin 2008-03-21 05:02:44 +00:00
parent 816a168053
commit cf26f5419e
1 changed files with 127 additions and 119 deletions

View File

@ -1,6 +1,10 @@
import unittest import unittest
from test import test_support from test import test_support
from contextlib import closing, nested
import pickle
import select
import signal import signal
import traceback
import sys, os, time, errno import sys, os, time, errno
if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos': if sys.platform[:3] in ('win', 'os2') or sys.platform == 'riscos':
@ -11,39 +15,20 @@
class HandlerBCalled(Exception): class HandlerBCalled(Exception):
pass pass
def exit_subprocess():
"""Use os._exit(0) to exit the current subprocess.
Otherwise, the test catches the SystemExit and continues executing
in parallel with the original test, so you wind up with an
exponential number of tests running concurrently.
"""
os._exit(0)
class InterProcessSignalTests(unittest.TestCase): class InterProcessSignalTests(unittest.TestCase):
MAX_DURATION = 20 # Entire test should last at most 20 sec. MAX_DURATION = 20 # Entire test should last at most 20 sec.
# Set up a child to send signals to us (the parent) after waiting
# long enough to receive the alarm. It seems we miss the alarm
# for some reason. This will hopefully stop the hangs on
# Tru64/Alpha. Alas, it doesn't. Tru64 appears to miss all the
# signals at times, or seemingly random subsets of them, and
# nothing done in force_test_exit so far has actually helped.
def spawn_force_test_exit_process(self, parent_pid):
# Sigh, both imports seem necessary to avoid errors.
import os
fork_pid = os.fork()
if fork_pid:
# In parent.
return fork_pid
# In child.
import os, time
try:
# Wait 5 seconds longer than the expected alarm to give enough
# time for the normal sequence of events to occur. This is
# just a stop-gap to try to prevent the test from hanging.
time.sleep(self.MAX_DURATION + 5)
print >> sys.__stdout__, " child should not have to kill parent"
for signame in "SIGHUP", "SIGUSR1", "SIGUSR2", "SIGALRM":
os.kill(parent_pid, getattr(signal, signame))
print >> sys.__stdout__, " child sent", signame, "to", \
parent_pid
time.sleep(1)
finally:
os._exit(0)
def handlerA(self, *args): def handlerA(self, *args):
self.a_called = True self.a_called = True
if test_support.verbose: if test_support.verbose:
@ -55,121 +40,144 @@ def handlerB(self, *args):
print "handlerB invoked", args print "handlerB invoked", args
raise HandlerBCalled(*args) raise HandlerBCalled(*args)
def test_main(self): def wait(self, child_pid):
self.assertEquals(signal.getsignal(signal.SIGHUP), self.handlerA) """Wait for child_pid to finish, ignoring EINTR."""
self.assertEquals(signal.getsignal(signal.SIGUSR1), self.handlerB) while True:
self.assertEquals(signal.getsignal(signal.SIGUSR2), signal.SIG_IGN) try:
self.assertEquals(signal.getsignal(signal.SIGALRM), pid, status = os.waitpid(child_pid, 0)
signal.default_int_handler) return status
except OSError as e:
if e.errno != errno.EINTR:
raise
# Launch an external script to send us signals. def run_test(self):
# We expect the external script to: # Install handlers. This function runs in a sub-process, so we
# send HUP, which invokes handlerA to set a_called # don't worry about re-setting the default handlers.
# send USR1, which invokes handlerB to set b_called and raise signal.signal(signal.SIGHUP, self.handlerA)
# HandlerBCalled signal.signal(signal.SIGUSR1, self.handlerB)
# send USR2, which is ignored signal.signal(signal.SIGUSR2, signal.SIG_IGN)
# signal.signal(signal.SIGALRM, signal.default_int_handler)
# Then we expect the alarm to go off, and its handler raises
# KeyboardInterrupt, finally getting us out of the loop.
if test_support.verbose: # Variables the signals will modify:
verboseflag = '-x' self.a_called = False
else: self.b_called = False
verboseflag = '+x'
pid = self.pid # Let the sub-processes know who to send signals to.
pid = os.getpid()
if test_support.verbose: if test_support.verbose:
print "test runner's pid is", pid print "test runner's pid is", pid
# Shell script that will send us asynchronous signals child = os.fork()
script = """ if child == 0:
( os.kill(pid, signal.SIGHUP)
set %(verboseflag)s exit_subprocess()
sleep 2 self.wait(child)
kill -HUP %(pid)d self.assertTrue(self.a_called)
sleep 2 self.assertFalse(self.b_called)
kill -USR1 %(pid)d self.a_called = False
sleep 2
kill -USR2 %(pid)d
) &
""" % vars()
signal.alarm(self.MAX_DURATION)
handler_b_exception_raised = False
os.system(script)
try: try:
child = os.fork()
if child == 0:
os.kill(pid, signal.SIGUSR1)
exit_subprocess()
# This wait should be interrupted by the signal's exception.
self.wait(child)
self.fail('HandlerBCalled exception not thrown')
except HandlerBCalled:
# So we call it again to reap the child's zombie.
self.wait(child)
self.assertTrue(self.b_called)
self.assertFalse(self.a_called)
if test_support.verbose: if test_support.verbose:
print "starting pause() loop..." print "HandlerBCalled exception caught"
while 1:
try:
if test_support.verbose:
print "call pause()..."
signal.pause()
if test_support.verbose:
print "pause() returned"
except HandlerBCalled:
handler_b_exception_raised = True
if test_support.verbose:
print "HandlerBCalled exception caught"
child = os.fork()
if child == 0:
os.kill(pid, signal.SIGUSR2)
exit_subprocess()
self.wait(child) # Nothing should happen.
try:
signal.alarm(1)
# The race condition in pause doesn't matter in this case,
# since alarm is going to raise a KeyboardException, which
# will skip the call.
signal.pause()
except KeyboardInterrupt: except KeyboardInterrupt:
if test_support.verbose: if test_support.verbose:
print "KeyboardInterrupt (the alarm() went off)" print "KeyboardInterrupt (the alarm() went off)"
self.assert_(self.a_called)
self.assert_(self.b_called)
self.assert_(handler_b_exception_raised)
def setUp(self):
# Install handlers.
self.hup = signal.signal(signal.SIGHUP, self.handlerA)
self.usr1 = signal.signal(signal.SIGUSR1, self.handlerB)
self.usr2 = signal.signal(signal.SIGUSR2, signal.SIG_IGN)
self.alrm = signal.signal(signal.SIGALRM,
signal.default_int_handler)
self.a_called = False
self.b_called = False
self.pid = os.getpid()
self.fork_pid = self.spawn_force_test_exit_process(self.pid)
def tearDown(self):
# Forcibly kill the child we created to ping us if there was a
# test error.
try:
# Make sure we don't kill ourself if there was a fork
# error.
if self.fork_pid > 0:
os.kill(self.fork_pid, signal.SIGKILL)
except: except:
# If the child killed us, it has probably exited. Killing self.fail('Some other exception woke us from pause: %s' %
# a non-existent process will raise an error which we traceback.format_exc())
# don't care about. else:
pass self.fail('pause returned of its own accord')
# Restore handlers. def test_main(self):
signal.alarm(0) # cancel alarm in case we died early # This function spawns a child process to insulate the main
signal.signal(signal.SIGHUP, self.hup) # test-running process from all the signals. It then
signal.signal(signal.SIGUSR1, self.usr1) # communicates with that child process over a pipe and
signal.signal(signal.SIGUSR2, self.usr2) # re-raises information about any exceptions the child
signal.signal(signal.SIGALRM, self.alrm) # throws. The real work happens in self.run_test().
os_done_r, os_done_w = os.pipe()
with nested(closing(os.fdopen(os_done_r)),
closing(os.fdopen(os_done_w, 'w'))) as (done_r, done_w):
child = os.fork()
if child == 0:
# In the child process; run the test and report results
# through the pipe.
try:
done_r.close()
# Have to close done_w again here because
# exit_subprocess() will skip the enclosing with block.
with closing(done_w):
try:
self.run_test()
except:
pickle.dump(traceback.format_exc(), done_w)
else:
pickle.dump(None, done_w)
except:
print 'Uh oh, raised from pickle.'
traceback.print_exc()
finally:
exit_subprocess()
done_w.close()
# Block for up to MAX_DURATION seconds for the test to finish.
r, w, x = select.select([done_r], [], [], self.MAX_DURATION)
if done_r in r:
tb = pickle.load(done_r)
if tb:
self.fail(tb)
else:
os.kill(child, signal.SIGKILL)
self.fail('Test deadlocked after %d seconds.' %
self.MAX_DURATION)
class BasicSignalTests(unittest.TestCase): class BasicSignalTests(unittest.TestCase):
def trivial_signal_handler(self, *args):
pass
def test_out_of_range_signal_number_raises_error(self): def test_out_of_range_signal_number_raises_error(self):
self.assertRaises(ValueError, signal.getsignal, 4242) self.assertRaises(ValueError, signal.getsignal, 4242)
def trivial_signal_handler(*args):
pass
self.assertRaises(ValueError, signal.signal, 4242, self.assertRaises(ValueError, signal.signal, 4242,
trivial_signal_handler) self.trivial_signal_handler)
def test_setting_signal_handler_to_none_raises_error(self): def test_setting_signal_handler_to_none_raises_error(self):
self.assertRaises(TypeError, signal.signal, self.assertRaises(TypeError, signal.signal,
signal.SIGUSR1, None) signal.SIGUSR1, None)
def test_getsignal(self):
hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler)
self.assertEquals(signal.getsignal(signal.SIGHUP),
self.trivial_signal_handler)
signal.signal(signal.SIGHUP, hup)
self.assertEquals(signal.getsignal(signal.SIGHUP), hup)
class WakeupSignalTests(unittest.TestCase): class WakeupSignalTests(unittest.TestCase):
TIMEOUT_FULL = 10 TIMEOUT_FULL = 10
TIMEOUT_HALF = 5 TIMEOUT_HALF = 5
@ -232,7 +240,7 @@ def readpipe_interrupted(self, cb):
os.kill(ppid, self.signum) os.kill(ppid, self.signum)
time.sleep(0.2) time.sleep(0.2)
finally: finally:
os._exit(0) exit_subprocess()
try: try:
os.close(w) os.close(w)