Skip to content

[3.12] gh-109592: test_eintr tolerates 20 ms when comparing timings (GH-110102) #110106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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