Skip to content

Commit

Permalink
gh-108277: Make test_os tolerate 10 ms diff for timerfd on Android em…
Browse files Browse the repository at this point in the history
…ulators (#117223)
  • Loading branch information
mhsmith authored Mar 27, 2024
1 parent 7aa89bc commit eefff68
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import locale
import os
import pickle
import platform
import select
import selectors
import shutil
Expand Down Expand Up @@ -4085,9 +4086,15 @@ def test_eventfd_select(self):
@unittest.skipUnless(hasattr(os, 'timerfd_create'), 'requires os.timerfd_create')
@support.requires_linux_version(2, 6, 30)
class TimerfdTests(unittest.TestCase):
# Tolerate a difference of 1 ms
CLOCK_RES_NS = 1_000_000
CLOCK_RES = CLOCK_RES_NS * 1e-9
# 1 ms accuracy is reliably achievable on every platform except Android
# emulators, where we allow 10 ms (gh-108277).
if sys.platform == "android" and platform.android_ver().is_emulator:
CLOCK_RES_PLACES = 2
else:
CLOCK_RES_PLACES = 3

CLOCK_RES = 10 ** -CLOCK_RES_PLACES
CLOCK_RES_NS = 10 ** (9 - CLOCK_RES_PLACES)

def timerfd_create(self, *args, **kwargs):
fd = os.timerfd_create(*args, **kwargs)
Expand All @@ -4109,18 +4116,18 @@ def test_timerfd_initval(self):

# 1st call
next_expiration, interval2 = os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
self.assertAlmostEqual(interval2, 0.0, places=3)
self.assertAlmostEqual(next_expiration, 0.0, places=3)
self.assertAlmostEqual(interval2, 0.0, places=self.CLOCK_RES_PLACES)
self.assertAlmostEqual(next_expiration, 0.0, places=self.CLOCK_RES_PLACES)

# 2nd call
next_expiration, interval2 = os.timerfd_settime(fd, initial=initial_expiration, interval=interval)
self.assertAlmostEqual(interval2, interval, places=3)
self.assertAlmostEqual(next_expiration, initial_expiration, places=3)
self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)

# timerfd_gettime
next_expiration, interval2 = os.timerfd_gettime(fd)
self.assertAlmostEqual(interval2, interval, places=3)
self.assertAlmostEqual(next_expiration, initial_expiration, places=3)
self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)

def test_timerfd_non_blocking(self):
fd = self.timerfd_create(time.CLOCK_REALTIME, flags=os.TFD_NONBLOCK)
Expand Down Expand Up @@ -4174,8 +4181,8 @@ def test_timerfd_interval(self):

# timerfd_gettime
next_expiration, interval2 = os.timerfd_gettime(fd)
self.assertAlmostEqual(interval2, interval, places=3)
self.assertAlmostEqual(next_expiration, initial_expiration, places=3)
self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
self.assertAlmostEqual(next_expiration, initial_expiration, places=self.CLOCK_RES_PLACES)

count = 3
t = time.perf_counter()
Expand Down Expand Up @@ -4206,8 +4213,8 @@ def test_timerfd_TFD_TIMER_ABSTIME(self):
# timerfd_gettime
# Note: timerfd_gettime returns relative values even if TFD_TIMER_ABSTIME is specified.
next_expiration, interval2 = os.timerfd_gettime(fd)
self.assertAlmostEqual(interval2, interval, places=3)
self.assertAlmostEqual(next_expiration, offset, places=3)
self.assertAlmostEqual(interval2, interval, places=self.CLOCK_RES_PLACES)
self.assertAlmostEqual(next_expiration, offset, places=self.CLOCK_RES_PLACES)

t = time.perf_counter()
count_signaled = self.read_count_signaled(fd)
Expand Down

0 comments on commit eefff68

Please sign in to comment.