New submission from Atul Varma:
This patch refactors test_signal.py to use unittest. When this patch is
applied, Lib/test/output/test_signal can be removed.
I tried to refactor out individual tests from the original script and
place them in separate test cases.
--
components: Tests
files: refactored_test_signal.py.patch
messages: 55235
nosy: varmaa
severity: normal
status: open
title: Refactor test_signal.py to use unittest.
versions: Python 2.6
__
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1008>
__--- test_signal.py 2007-07-12 18:54:12.0 -0500
+++ test_signal_new.py 2007-07-13 03:13:00.0 -0500
@@ -1,167 +1,179 @@
-# Test the signal module
-from test.test_support import verbose, TestSkipped, TestFailed, vereq
+import unittest
+from test import test_support
import signal
import os, sys, time
-if sys.platform[:3] in ('win', 'os2') or sys.platform=='riscos':
-raise TestSkipped, "Can't test signal on %s" % sys.platform
+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)
-if verbose:
-x = '-x'
-else:
-x = '+x'
-
-pid = os.getpid()
-if verbose:
-print "test runner's pid is", pid
-
-# Shell script that will send us asynchronous signals
-script = """
- (
-set %(x)s
-sleep 2
-kill -HUP %(pid)d
-sleep 2
-kill -USR1 %(pid)d
-sleep 2
-kill -USR2 %(pid)d
- ) &
-""" % vars()
-
-a_called = b_called = False
-
-def handlerA(*args):
-global a_called
-a_called = True
-if verbose:
-print "handlerA invoked", args
-
-class HandlerBCalled(Exception):
-pass
-
-def handlerB(*args):
-global b_called
-b_called = True
-if verbose:
-print "handlerB invoked", args
-raise HandlerBCalled, args
-
-# 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 force_test_exit():
-# 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(MAX_DURATION + 5)
-print >> sys.__stdout__, ' child should not have to kill parent'
-for signame in "SIGHUP", "SIGUSR1", "SIGUSR2", "SIGALRM":
-os.kill(pid, getattr(signal, signame))
-print >> sys.__stdout__, "child sent", signame, "to", pid
-time.sleep(1)
-finally:
-os._exit(0)
-
-# Install handlers.
-hup = signal.signal(signal.SIGHUP, handlerA)
-usr1 = signal.signal(signal.SIGUSR1, handlerB)
-usr2 = si