diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-03-19 20:54:09 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-03-19 20:54:09 (GMT) |
commit | 79d68f929d8def878766965f513b628023f809b5 (patch) | |
tree | 943c927ae839eb2b115f88d997cd29094ddd23ab /Lib | |
parent | 0ed56a0b421e02da3ed3626a5b9e0ad076f68ce0 (diff) | |
download | cpython-79d68f929d8def878766965f513b628023f809b5.zip cpython-79d68f929d8def878766965f513b628023f809b5.tar.gz cpython-79d68f929d8def878766965f513b628023f809b5.tar.bz2 |
Issue #23646: If time.sleep() is interrupted by a signal, the sleep is now
retried with the recomputed delay, except if the signal handler raises an
exception (PEP 475).
Modify also test_signal to use a monotonic clock instead of the system clock.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/eintrdata/eintr_tester.py | 20 | ||||
-rw-r--r-- | Lib/test/test_signal.py | 31 |
2 files changed, 36 insertions, 15 deletions
diff --git a/Lib/test/eintrdata/eintr_tester.py b/Lib/test/eintrdata/eintr_tester.py index 40dca84..400dd21 100644 --- a/Lib/test/eintrdata/eintr_tester.py +++ b/Lib/test/eintrdata/eintr_tester.py @@ -252,8 +252,26 @@ class SocketEINTRTest(EINTRBaseTest): lambda path: os.close(os.open(path, os.O_WRONLY))) +@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()") +class TimeEINTRTest(EINTRBaseTest): + """ EINTR tests for the time module. """ + + def test_sleep(self): + t0 = time.monotonic() + # time.sleep() may retry when interrupted by a signal + time.sleep(2) + signal.alarm(0) + dt = time.monotonic() - t0 + # Tolerate a difference 100 ms: on Windows, time.monotonic() has + # a resolution of 15.6 ms or greater + self.assertGreaterEqual(dt, 1.9) + + def test_main(): - support.run_unittest(OSEINTRTest, SocketEINTRTest) + support.run_unittest( + OSEINTRTest, + SocketEINTRTest, + TimeEINTRTest) if __name__ == "__main__": diff --git a/Lib/test/test_signal.py b/Lib/test/test_signal.py index 774fc80..4e7cbe2 100644 --- a/Lib/test/test_signal.py +++ b/Lib/test/test_signal.py @@ -419,17 +419,20 @@ class WakeupSignalTests(unittest.TestCase): TIMEOUT_HALF = 5 signal.alarm(1) - before_time = time.time() + # We attempt to get a signal during the sleep, # before select is called - time.sleep(TIMEOUT_FULL) - mid_time = time.time() - dt = mid_time - before_time - if dt >= TIMEOUT_HALF: - raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) + try: + select.select([], [], [], TIMEOUT_FULL) + except InterruptedError: + pass + else: + raise Exception("select() was not interrupted") + + before_time = time.monotonic() select.select([read], [], [], TIMEOUT_FULL) - after_time = time.time() - dt = after_time - mid_time + after_time = time.monotonic() + dt = after_time - before_time if dt >= TIMEOUT_HALF: raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) """, signal.SIGALRM) @@ -443,7 +446,7 @@ class WakeupSignalTests(unittest.TestCase): TIMEOUT_HALF = 5 signal.alarm(1) - before_time = time.time() + before_time = time.monotonic() # We attempt to get a signal during the select call try: select.select([read], [], [], TIMEOUT_FULL) @@ -451,7 +454,7 @@ class WakeupSignalTests(unittest.TestCase): pass else: raise Exception("OSError not raised") - after_time = time.time() + after_time = time.monotonic() dt = after_time - before_time if dt >= TIMEOUT_HALF: raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) @@ -709,8 +712,8 @@ class ItimerTest(unittest.TestCase): signal.signal(signal.SIGVTALRM, self.sig_vtalrm) signal.setitimer(self.itimer, 0.3, 0.2) - start_time = time.time() - while time.time() - start_time < 60.0: + start_time = time.monotonic() + while time.monotonic() - start_time < 60.0: # use up some virtual time by doing real work _ = pow(12345, 67890, 10000019) if signal.getitimer(self.itimer) == (0.0, 0.0): @@ -732,8 +735,8 @@ class ItimerTest(unittest.TestCase): signal.signal(signal.SIGPROF, self.sig_prof) signal.setitimer(self.itimer, 0.2, 0.2) - start_time = time.time() - while time.time() - start_time < 60.0: + start_time = time.monotonic() + while time.monotonic() - start_time < 60.0: # do some work _ = pow(12345, 67890, 10000019) if signal.getitimer(self.itimer) == (0.0, 0.0): |