diff options
| author | Victor Stinner <victor.stinner@gmail.com> | 2017-09-19 09:36:54 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-09-19 09:36:54 -0700 |
| commit | 9abee722d448c1c00c7d4e11ce242ec7b13e5c49 (patch) | |
| tree | 52af3dbb3ff6a4a8e28be76f43ef7d4df31deec6 /Lib/test/test_selectors.py | |
| parent | a92941ff12c1d554f42c05ed24621894a758b40f (diff) | |
| download | cpython-git-9abee722d448c1c00c7d4e11ce242ec7b13e5c49.tar.gz | |
bpo-31479: Always reset the signal alarm in tests (#3588)
* bpo-31479: Always reset the signal alarm in tests
Use "try: ... finally: signal.signal(0)" pattern to make sure that
tests don't "leak" a pending fatal signal alarm.
* Move two more alarm() calls into the try block
Fix also typo: replace signal.signal(0) with signal.alarm(0)
* Move another signal.alarm() into the try block
Diffstat (limited to 'Lib/test/test_selectors.py')
| -rw-r--r-- | Lib/test/test_selectors.py | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/Lib/test/test_selectors.py b/Lib/test/test_selectors.py index f2594a6b12..79ac25c12d 100644 --- a/Lib/test/test_selectors.py +++ b/Lib/test/test_selectors.py @@ -399,17 +399,19 @@ class BaseSelectorTestCase(unittest.TestCase): orig_alrm_handler = signal.signal(signal.SIGALRM, handler) self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) - self.addCleanup(signal.alarm, 0) - signal.alarm(1) + try: + signal.alarm(1) - s.register(rd, selectors.EVENT_READ) - t = time() - # select() is interrupted by a signal which raises an exception - with self.assertRaises(InterruptSelect): - s.select(30) - # select() was interrupted before the timeout of 30 seconds - self.assertLess(time() - t, 5.0) + s.register(rd, selectors.EVENT_READ) + t = time() + # select() is interrupted by a signal which raises an exception + with self.assertRaises(InterruptSelect): + s.select(30) + # select() was interrupted before the timeout of 30 seconds + self.assertLess(time() - t, 5.0) + finally: + signal.alarm(0) @unittest.skipUnless(hasattr(signal, "alarm"), "signal.alarm() required for this test") @@ -421,17 +423,19 @@ class BaseSelectorTestCase(unittest.TestCase): orig_alrm_handler = signal.signal(signal.SIGALRM, lambda *args: None) self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) - self.addCleanup(signal.alarm, 0) - signal.alarm(1) + try: + signal.alarm(1) - s.register(rd, selectors.EVENT_READ) - t = time() - # select() is interrupted by a signal, but the signal handler doesn't - # raise an exception, so select() should by retries with a recomputed - # timeout - self.assertFalse(s.select(1.5)) - self.assertGreaterEqual(time() - t, 1.0) + s.register(rd, selectors.EVENT_READ) + t = time() + # select() is interrupted by a signal, but the signal handler doesn't + # raise an exception, so select() should by retries with a recomputed + # timeout + self.assertFalse(s.select(1.5)) + self.assertGreaterEqual(time() - t, 1.0) + finally: + signal.alarm(0) class ScalableSelectorMixIn: |
