Skip to content

Commit b84ba0e

Browse files
committed
bpo-35707: improve tests for time.sleep()
1 parent e90cc9d commit b84ba0e

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Lib/test/test_time.py

+14-13
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ def test_conversions(self):
163163
def test_sleep(self):
164164
self.assertRaises(ValueError, time.sleep, -2)
165165
self.assertRaises(ValueError, time.sleep, -1)
166-
time.sleep(1.2)
167-
time.sleep(IndexLike(0))
168-
time.sleep(FloatLike(0))
166+
time.sleep(0.2)
169167

170168
def test_strftime(self):
171169
tt = time.gmtime(self.t)
@@ -468,21 +466,24 @@ def test_monotonic(self):
468466
self.assertGreaterEqual(t2, t1, "times=%s" % times)
469467
t1 = t2
470468

471-
# monotonic() includes time elapsed during a sleep
472-
t1 = time.monotonic()
473-
time.sleep(0.5)
474-
t2 = time.monotonic()
475-
dt = t2 - t1
476-
self.assertGreater(t2, t1)
477-
# bpo-20101: tolerate a difference of 50 ms because of bad timer
478-
# resolution on Windows
479-
self.assertTrue(0.450 <= dt)
480-
481469
# monotonic() is a monotonic but non adjustable clock
482470
info = time.get_clock_info('monotonic')
483471
self.assertTrue(info.monotonic)
484472
self.assertFalse(info.adjustable)
485473

474+
def test_monotic_sleep(self):
475+
# This tests both time.sleep() and time.monotonic(): we test various
476+
# types of input to time.sleep() and check using time.monotonic() that
477+
# we sleep sufficiently long.
478+
for T in (0.5, decimal.Decimal('0.5'), FloatLike(0.5), IndexLike(1)):
479+
t1 = time.monotonic()
480+
time.sleep(T)
481+
t2 = time.monotonic()
482+
dt = t2 - t1
483+
# bpo-20101: tolerate a difference of 50 ms because of bad timer
484+
# resolution on Windows
485+
self.assertGreaterEqual(dt, float(T) - 0.05)
486+
486487
def test_perf_counter(self):
487488
time.perf_counter()
488489

0 commit comments

Comments
 (0)