summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_signal.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2008-03-24 13:39:54 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2008-03-24 13:39:54 (GMT)
commit823725e93cdd85371eae53352dd83fc634e95694 (patch)
tree9c9a313f8f0859aaf6790aa4b4900adb6d1b3e60 /Lib/test/test_signal.py
parent6cf49cf10689bf2ff23fff05928daa23ecdf6fc2 (diff)
downloadcpython-823725e93cdd85371eae53352dd83fc634e95694.zip
cpython-823725e93cdd85371eae53352dd83fc634e95694.tar.gz
cpython-823725e93cdd85371eae53352dd83fc634e95694.tar.bz2
Merged revisions 61846-61847 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r61846 | martin.v.loewis | 2008-03-24 13:57:53 +0100 (Mo, 24 Mär 2008) | 2 lines Install 2to3 script. ........ r61847 | martin.v.loewis | 2008-03-24 14:31:16 +0100 (Mo, 24 Mär 2008) | 2 lines Patch #2240: Implement signal.setitimer and signal.getitimer. ........
Diffstat (limited to 'Lib/test/test_signal.py')
-rw-r--r--Lib/test/test_signal.py86
1 files changed, 85 insertions, 1 deletions
diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py
index 2834076..2cee626 100644
--- a/Lib/test/test_signal.py
+++ b/Lib/test/test_signal.py
@@ -258,9 +258,93 @@ class SiginterruptTest(unittest.TestCase):
i=self.readpipe_interrupted(lambda: signal.siginterrupt(self.signum, 0))
self.assertEquals(i, False)
+class ItimerTest(unittest.TestCase):
+ def setUp(self):
+ self.hndl_called = False
+ self.hndl_count = 0
+ self.itimer = None
+
+ def tearDown(self):
+ if self.itimer is not None: # test_itimer_exc doesn't change this attr
+ # just ensure that itimer is stopped
+ signal.setitimer(self.itimer, 0)
+
+ def sig_alrm(self, *args):
+ self.hndl_called = True
+ if test_support.verbose:
+ print("SIGALRM handler invoked", args)
+
+ def sig_vtalrm(self, *args):
+ self.hndl_called = True
+
+ if self.hndl_count > 3:
+ # it shouldn't be here, because it should have been disabled.
+ raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL "
+ "timer.")
+ elif self.hndl_count == 3:
+ # disable ITIMER_VIRTUAL, this function shouldn't be called anymore
+ signal.setitimer(signal.ITIMER_VIRTUAL, 0)
+ if test_support.verbose:
+ print("last SIGVTALRM handler call")
+
+ self.hndl_count += 1
+
+ if test_support.verbose:
+ print("SIGVTALRM handler invoked", args)
+
+ def sig_prof(self, *args):
+ self.hndl_called = True
+ signal.setitimer(signal.ITIMER_PROF, 0)
+
+ if test_support.verbose:
+ print("SIGPROF handler invoked", args)
+
+ def test_itimer_exc(self):
+ # XXX I'm assuming -1 is an invalid itimer, but maybe some platform
+ # defines it ?
+ self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0)
+ # negative time
+ self.assertRaises(signal.ItimerError, signal.setitimer,
+ signal.ITIMER_REAL, -1)
+
+ def test_itimer_real(self):
+ self.itimer = signal.ITIMER_REAL
+ signal.signal(signal.SIGALRM, self.sig_alrm)
+ signal.setitimer(self.itimer, 1.0)
+ if test_support.verbose:
+ print("\ncall pause()...")
+ signal.pause()
+
+ self.assertEqual(self.hndl_called, True)
+
+ def test_itimer_virtual(self):
+ self.itimer = signal.ITIMER_VIRTUAL
+ signal.signal(signal.SIGVTALRM, self.sig_vtalrm)
+ signal.setitimer(self.itimer, 0.3, 0.2)
+
+ for i in range(100000000):
+ if signal.getitimer(self.itimer) == (0.0, 0.0):
+ break # sig_vtalrm handler stopped this itimer
+
+ # virtual itimer should be (0.0, 0.0) now
+ self.assertEquals(signal.getitimer(self.itimer), (0.0, 0.0))
+ # and the handler should have been called
+ self.assertEquals(self.hndl_called, True)
+
+ def test_itimer_prof(self):
+ self.itimer = signal.ITIMER_PROF
+ signal.signal(signal.SIGPROF, self.sig_prof)
+ signal.setitimer(self.itimer, 0.2)
+
+ for i in range(100000000):
+ if signal.getitimer(self.itimer) == (0.0, 0.0):
+ break # sig_prof handler stopped this itimer
+
+ self.assertEqual(self.hndl_called, True)
+
def test_main():
test_support.run_unittest(BasicSignalTests, InterProcessSignalTests,
- WakeupSignalTests, SiginterruptTest)
+ WakeupSignalTests, SiginterruptTest, ItimerTest)
if __name__ == "__main__":