Skip to content

Commit

Permalink
pythongh-109592: test_eintr tolerates 20 ms when comparing timings (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner authored Sep 29, 2023
1 parent 86e76ab commit 9c73a9a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Lib/test/_test_eintr.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
from test.support import os_helper
from test.support import socket_helper


# gh-109592: Tolerate a difference of 20 ms when comparing timings
# (clock resolution)
CLOCK_RES = 0.020


@contextlib.contextmanager
def kill_on_error(proc):
"""Context manager killing the subprocess if a Python exception is raised."""
Expand Down Expand Up @@ -75,6 +81,9 @@ def subprocess(self, *args, **kw):
cmd_args = (sys.executable, '-c') + args
return subprocess.Popen(cmd_args, **kw)

def check_elapsed_time(self, elapsed):
self.assertGreaterEqual(elapsed, self.sleep_time - CLOCK_RES)


@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
class OSEINTRTest(EINTRBaseTest):
Expand Down Expand Up @@ -373,7 +382,7 @@ def test_sleep(self):
time.sleep(self.sleep_time)
self.stop_alarm()
dt = time.monotonic() - t0
self.assertGreaterEqual(dt, self.sleep_time)
self.check_elapsed_time(dt)


@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
Expand Down Expand Up @@ -435,7 +444,7 @@ def test_select(self):
select.select([], [], [], self.sleep_time)
dt = time.monotonic() - t0
self.stop_alarm()
self.assertGreaterEqual(dt, self.sleep_time)
self.check_elapsed_time(dt)

@unittest.skipIf(sys.platform == "darwin",
"poll may fail on macOS; see issue #28087")
Expand All @@ -447,7 +456,7 @@ def test_poll(self):
poller.poll(self.sleep_time * 1e3)
dt = time.monotonic() - t0
self.stop_alarm()
self.assertGreaterEqual(dt, self.sleep_time)
self.check_elapsed_time(dt)

@unittest.skipUnless(hasattr(select, 'epoll'), 'need select.epoll')
def test_epoll(self):
Expand All @@ -458,7 +467,7 @@ def test_epoll(self):
poller.poll(self.sleep_time)
dt = time.monotonic() - t0
self.stop_alarm()
self.assertGreaterEqual(dt, self.sleep_time)
self.check_elapsed_time(dt)

@unittest.skipUnless(hasattr(select, 'kqueue'), 'need select.kqueue')
def test_kqueue(self):
Expand All @@ -469,7 +478,7 @@ def test_kqueue(self):
kqueue.control(None, 1, self.sleep_time)
dt = time.monotonic() - t0
self.stop_alarm()
self.assertGreaterEqual(dt, self.sleep_time)
self.check_elapsed_time(dt)

@unittest.skipUnless(hasattr(select, 'devpoll'), 'need select.devpoll')
def test_devpoll(self):
Expand All @@ -480,7 +489,7 @@ def test_devpoll(self):
poller.poll(self.sleep_time * 1e3)
dt = time.monotonic() - t0
self.stop_alarm()
self.assertGreaterEqual(dt, self.sleep_time)
self.check_elapsed_time(dt)


class FNTLEINTRTest(EINTRBaseTest):
Expand Down Expand Up @@ -512,8 +521,8 @@ def _lock(self, lock_func, lock_name):
# potential context switch delay
lock_func(f, fcntl.LOCK_EX)
dt = time.monotonic() - start_time
self.assertGreaterEqual(dt, self.sleep_time)
self.stop_alarm()
self.check_elapsed_time(dt)
proc.wait()

# Issue 35633: See https://bugs.python.org/issue35633#msg333662
Expand Down

0 comments on commit 9c73a9a

Please sign in to comment.