8000 gh-125997: ensure that `time.sleep(0)` is not delayed on non-Windows platforms by picnixz · Pull Request #128274 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
10000

gh-125997: ensure that time.sleep(0) is not delayed on non-Windows platforms #128274

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

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
improve time.sleep() tests
  • Loading branch information
picnixz committed Dec 30, 2024
commit f6a817fe31aea1822091afe2c45e15a27fcb602b
40 changes: 38 additions & 2 deletions Lib/test/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,46 @@ def test_conversions(self):
self.assertEqual(int(time.mktime(time.localtime(self.t))),
int(self.t))

def test_sleep(self):
def test_sleep_exceptions(self):
self.assertRaises(TypeError, time.sleep, [])
self.assertRaises(TypeError, time.sleep, "a")
self.assertRaises(TypeError, time.sleep, complex(0, 0))

self.assertRaises(ValueError, time.sleep, -2)
self.assertRaises(ValueError, time.sleep, -1)
time.sleep(1.2)
self.assertRaises(ValueError, time.sleep, -0.1)

def test_sleep(self):
for value in [-0.0, 0, 0.0, 1e-6, 1, 1.2]:
with self.subTest(value=value):
time.sleep(value)

@unittest.skipIf(support.MS_WINDOWS, 'test only for non-Windows platforms')
def test_sleep_zero_posix(self):
# Test that time.sleep(0) does not accumulate delays.

N1 = 1000 # small number of samples for time.sleep(eps) with eps > 0
N2 = 100_000 # large number of samples for time.sleep(0)

# Compute how long time.sleep() takes for the 'time' clock resolution.
# We expect the result to be around 50 us for a nanosecond resolution.
eps = time.get_clock_info('time').resolution
max_dt_ns = self.stat_for_test_sleep(N1, time.sleep, eps)

# We expect a gap between time.sleep(0) and time.sleep(eps).
avg_dt_ns = self.stat_for_test_sleep(N2, time.sleep, 0)
self.assertLess(avg_dt_ns, max_dt_ns)

@staticmethod
def stat_for_test_sleep(n_samples, func, *args, **kwargs):
"""Compute the average (ns) time execution of func(*args, **kwargs)."""
samples = []
for _ in range(int(n_samples)):
t0 = time.monotonic_ns()
func(*args, **kwargs)
t1 = time.monotonic_ns()
samples.append(t1 - t0)
return math.fsum(samples) / n_samples

def test_epoch(self):
# bpo-43869: Make sure that Python use the same Epoch on all platforms:
Expand Down
0