Skip to content

Commit 2cf4c20

Browse files
authored
bpo-35513: Replace time.time() with time.monotonic() in tests (GH-11182)
Replace time.time() with time.monotonic() in tests to measure time delta. test_zipfile64: display progress every minute (60 secs) rather than every 5 minutes (5*60 seconds).
1 parent 4e80f5c commit 2cf4c20

14 files changed

+68
-66
lines changed

Lib/pydoc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,14 +2231,14 @@ def _start_server(urlhandler, hostname, port):
22312231
Let the server do its thing. We just need to monitor its status.
22322232
Use time.sleep so the loop doesn't hog the CPU.
22332233
2234-
>>> starttime = time.time()
2234+
>>> starttime = time.monotonic()
22352235
>>> timeout = 1 #seconds
22362236
22372237
This is a short timeout for testing purposes.
22382238
22392239
>>> while serverthread.serving:
22402240
... time.sleep(.01)
2241-
... if serverthread.serving and time.time() - starttime > timeout:
2241+
... if serverthread.serving and time.monotonic() - starttime > timeout:
22422242
... serverthread.stop()
22432243
... break
22442244

Lib/test/_test_multiprocessing.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ def __init__(self, func):
155155
self.elapsed = None
156156

157157
def __call__(self, *args, **kwds):
158-
t = time.time()
158+
t = time.monotonic()
159159
try:
160160
return self.func(*args, **kwds)
161161
finally:
162-
self.elapsed = time.time() - t
162+
self.elapsed = time.monotonic() - t
163163

164164
#
165165
# Base class for test cases
@@ -1034,9 +1034,9 @@ def test_no_import_lock_contention(self):
10341034

10351035
def test_timeout(self):
10361036
q = multiprocessing.Queue()
1037-
start = time.time()
1037+
start = time.monotonic()
10381038
self.assertRaises(pyqueue.Empty, q.get, True, 0.200)
1039-
delta = time.time() - start
1039+
delta = time.monotonic() - start
10401040
# bpo-30317: Tolerate a delta of 100 ms because of the bad clock
10411041
# resolution on Windows (usually 15.6 ms). x86 Windows7 3.x once
10421042
# failed because the delta was only 135.8 ms.
@@ -1440,9 +1440,9 @@ def _test_waitfor_timeout_f(cls, cond, state, success, sem):
14401440
sem.release()
14411441
with cond:
14421442
expected = 0.1
1443-
dt = time.time()
1443+
dt = time.monotonic()
14441444
result = cond.wait_for(lambda : state.value==4, timeout=expected)
1445-
dt = time.time() - dt
1445+
dt = time.monotonic() - dt
14461446
# borrow logic in assertTimeout() from test/lock_tests.py
14471447
if not result and expected * 0.6 < dt < expected * 10.0:
14481448
success.value = True
@@ -2533,7 +2533,7 @@ def test_map_no_failfast(self):
25332533
# process would fill the result queue (after the result handler thread
25342534
# terminated, hence not draining it anymore).
25352535

2536-
t_start = time.time()
2536+
t_start = time.monotonic()
25372537

25382538
with self.assertRaises(ValueError):
25392539
with self.Pool(2) as p:
@@ -2545,7 +2545,7 @@ def test_map_no_failfast(self):
25452545
p.join()
25462546

25472547
# check that we indeed waited for all jobs
2548-
self.assertGreater(time.time() - t_start, 0.9)
2548+
self.assertGreater(time.monotonic() - t_start, 0.9)
25492549

25502550
def test_release_task_refs(self):
25512551
# Issue #29861: task arguments and results should not be kept
@@ -4108,19 +4108,19 @@ def test_wait_timeout(self):
41084108
expected = 5
41094109
a, b = multiprocessing.Pipe()
41104110

4111-
start = time.time()
4111+
start = time.monotonic()
41124112
res = wait([a, b], expected)
4113-
delta = time.time() - start
4113+
delta = time.monotonic() - start
41144114

41154115
self.assertEqual(res, [])
41164116
self.assertLess(delta, expected * 2)
41174117
self.assertGreater(delta, expected * 0.5)
41184118

41194119
b.send(None)
41204120

4121-
start = time.time()
4121+
start = time.monotonic()
41224122
res = wait([a, b], 20)
4123-
delta = time.time() - start
4123+
delta = time.monotonic() - start
41244124

41254125
self.assertEqual(res, [a])
41264126
self.assertLess(delta, 0.4)
@@ -4144,28 +4144,28 @@ def test_wait_integer(self):
41444144
self.assertIsInstance(p.sentinel, int)
41454145
self.assertTrue(sem.acquire(timeout=20))
41464146

4147-
start = time.time()
4147+
start = time.monotonic()
41484148
res = wait([a, p.sentinel, b], expected + 20)
4149-
delta = time.time() - start
4149+
delta = time.monotonic() - start
41504150

41514151
self.assertEqual(res, [p.sentinel])
41524152
self.assertLess(delta, expected + 2)
41534153
self.assertGreater(delta, expected - 2)
41544154

41554155
a.send(None)
41564156

4157-
start = time.time()
4157+
start = time.monotonic()
41584158
res = wait([a, p.sentinel, b], 20)
4159-
delta = time.time() - start
4159+
delta = time.monotonic() - start
41604160

41614161
self.assertEqual(sorted_(res), sorted_([p.sentinel, b]))
41624162
self.assertLess(delta, 0.4)
41634163

41644164
b.send(None)
41654165

4166-
start = time.time()
4166+
start = time.monotonic()
41674167
res = wait([a, p.sentinel, b], 20)
4168-
delta = time.time() - start
4168+
delta = time.monotonic() - start
41694169

41704170
self.assertEqual(sorted_(res), sorted_([a, p.sentinel, b]))
41714171
self.assertLess(delta, 0.4)
@@ -4176,9 +4176,9 @@ def test_wait_integer(self):
41764176
def test_neg_timeout(self):
41774177
from multiprocessing.connection import wait
41784178
a, b = multiprocessing.Pipe()
4179-
t = time.time()
4179+
t = time.monotonic()
41804180
res = wait([a], timeout=-1)
4181-
t = time.time() - t
4181+
t = time.monotonic() - t
41824182
self.assertEqual(res, [])
41834183
self.assertLess(t, 1)
41844184
a.close()

Lib/test/lock_tests.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def tearDown(self):
7474
support.reap_children()
7575

7676
def assertTimeout(self, actual, expected):
77-
# The waiting and/or time.time() can be imprecise, which
77+
# The waiting and/or time.monotonic() can be imprecise, which
7878
# is why comparing to the expected value would sometimes fail
7979
# (especially under Windows).
8080
self.assertGreaterEqual(actual, expected * 0.6)
@@ -190,16 +190,16 @@ def test_timeout(self):
190190
# TIMEOUT_MAX is ok
191191
lock.acquire(timeout=TIMEOUT_MAX)
192192
lock.release()
193-
t1 = time.time()
193+
t1 = time.monotonic()
194194
self.assertTrue(lock.acquire(timeout=5))
195-
t2 = time.time()
195+
t2 = time.monotonic()
196196
# Just a sanity test that it didn't actually wait for the timeout.
197197
self.assertLess(t2 - t1, 5)
198198
results = []
199199
def f():
200-
t1 = time.time()
200+
t1 = time.monotonic()
201201
results.append(lock.acquire(timeout=0.5))
202-
t2 = time.time()
202+
t2 = time.monotonic()
203203
results.append(t2 - t1)
204204
Bunch(f, 1).wait_for_finished()
205205
self.assertFalse(results[0])
@@ -382,9 +382,9 @@ def test_timeout(self):
382382
N = 5
383383
def f():
384384
results1.append(evt.wait(0.0))
385-
t1 = time.time()
385+
t1 = time.monotonic()
386386
r = evt.wait(0.5)
387-
t2 = time.time()
387+
t2 = time.monotonic()
388388
results2.append((r, t2 - t1))
389389
Bunch(f, N).wait_for_finished()
390390
self.assertEqual(results1, [False] * N)
@@ -545,9 +545,9 @@ def test_timeout(self):
545545
N = 5
546546
def f():
547547
cond.acquire()
548-
t1 = time.time()
548+
t1 = time.monotonic()
549549
result = cond.wait(0.5)
550-
t2 = time.time()
550+
t2 = time.monotonic()
551551
cond.release()
552552
results.append((t2 - t1, result))
553553
Bunch(f, N).wait_for_finished()
@@ -584,9 +584,9 @@ def test_waitfor_timeout(self):
584584
success = []
585585
def f():
586586
with cond:
587-
dt = time.time()
587+
dt = time.monotonic()
588588
result = cond.wait_for(lambda : state==4, timeout=0.1)
589-
dt = time.time() - dt
589+
dt = time.monotonic() - dt
590590
self.assertFalse(result)
591591
self.assertTimeout(dt, 0.1)
592592
success.append(None)
@@ -692,9 +692,9 @@ def test_acquire_timeout(self):
692692
self.assertFalse(sem.acquire(timeout=0.005))
693693
sem.release()
694694
self.assertTrue(sem.acquire(timeout=0.005))
695-
t = time.time()
695+
t = time.monotonic()
696696
self.assertFalse(sem.acquire(timeout=0.5))
697-
dt = time.time() - t
697+
dt = time.monotonic() - t
698698
self.assertTimeout(dt, 0.5)
699699

700700
def test_default_value(self):

Lib/test/support/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,11 +2259,11 @@ def start_threads(threads, unlock=None):
22592259
try:
22602260
if unlock:
22612261
unlock()
2262-
endtime = starttime = time.time()
2262+
endtime = starttime = time.monotonic()
22632263
for timeout in range(1, 16):
22642264
endtime += 60
22652265
for t in started:
2266-
t.join(max(endtime - time.time(), 0.01))
2266+
t.join(max(endtime - time.monotonic(), 0.01))
22672267
started = [t for t in started if t.isAlive()]
22682268
if not started:
22692269
break

Lib/test/test_asyncio/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ async def once():
107107

108108

109109
def run_until(loop, pred, timeout=30):
110-
deadline = time.time() + timeout
110+
deadline = time.monotonic() + timeout
111111
while not pred():
112112
if timeout is not None:
113-
timeout = deadline - time.time()
113+
timeout = deadline - time.monotonic()
114114
if timeout <= 0:
115115
raise futures.TimeoutError()
116116
loop.run_until_complete(tasks.sleep(0.001))

Lib/test/test_asyncore.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def capture_server(evt, buf, serv):
7070
pass
7171
else:
7272
n = 200
73-
start = time.time()
74-
while n > 0 and time.time() - start < 3.0:
73+
start = time.monotonic()
74+
while n > 0 and time.monotonic() - start < 3.0:
7575
r, w, e = select.select([conn], [], [], 0.1)
7676
if r:
7777
n -= 1

Lib/test/test_dummy_thread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ def delay_unlock(to_unlock, delay):
7070
to_unlock.release()
7171

7272
self.lock.acquire()
73-
start_time = int(time.time())
73+
start_time = int(time.monotonic())
7474
_thread.start_new_thread(delay_unlock,(self.lock, DELAY))
7575
if support.verbose:
7676
print()
7777
print("*** Waiting for thread to release the lock "\
7878
"(approx. %s sec.) ***" % DELAY)
7979
self.lock.acquire()
80-
end_time = int(time.time())
80+
end_time = int(time.monotonic())
8181
if support.verbose:
8282
print("done")
8383
self.assertGreaterEqual(end_time - start_time, DELAY,

Lib/test/test_ossaudiodev.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ def play_sound_file(self, data, rate, ssize, nchannels):
7777
# set parameters based on .au file headers
7878
dsp.setparameters(AFMT_S16_NE, nchannels, rate)
7979
self.assertTrue(abs(expected_time - 3.51) < 1e-2, expected_time)
80-
t1 = time.time()
80+
t1 = time.monotonic()
8181
dsp.write(data)
8282
dsp.close()
83-
t2 = time.time()
83+
t2 = time.monotonic()
8484
elapsed_time = t2 - t1
8585

8686
percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100

Lib/test/test_pydoc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,12 +1131,12 @@ def my_url_handler(url, content_type):
11311131
serverthread = pydoc._start_server(my_url_handler, hostname='0.0.0.0', port=0)
11321132
self.assertIn('0.0.0.0', serverthread.docserver.address)
11331133

1134-
starttime = time.time()
1134+
starttime = time.monotonic()
11351135
timeout = 1 #seconds
11361136

11371137
while serverthread.serving:
11381138
time.sleep(.01)
1139-
if serverthread.serving and time.time() - starttime > timeout:
1139+
if serverthread.serving and time.monotonic() - starttime > timeout:
11401140
serverthread.stop()
11411141
break
11421142

Lib/test/test_signal.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,18 +1163,18 @@ def second_handler(signum=None, frame=None):
11631163
self.setsig(signal.SIGALRM, second_handler) # for ITIMER_REAL
11641164

11651165
expected_sigs = 0
1166-
deadline = time.time() + 15.0
1166+
deadline = time.monotonic() + 15.0
11671167

11681168
while expected_sigs < N:
11691169
os.kill(os.getpid(), signal.SIGPROF)
11701170
expected_sigs += 1
11711171
# Wait for handlers to run to avoid signal coalescing
1172-
while len(sigs) < expected_sigs and time.time() < deadline:
1172+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
11731173
time.sleep(1e-5)
11741174

11751175
os.kill(os.getpid(), signal.SIGUSR1)
11761176
expected_sigs += 1
1177-
while len(sigs) < expected_sigs and time.time() < deadline:
1177+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
11781178
time.sleep(1e-5)
11791179

11801180
# All ITIMER_REAL signals should have been delivered to the
@@ -1197,7 +1197,7 @@ def handler(signum, frame):
11971197
self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL
11981198

11991199
expected_sigs = 0
1200-
deadline = time.time() + 15.0
1200+
deadline = time.monotonic() + 15.0
12011201

12021202
while expected_sigs < N:
12031203
# Hopefully the SIGALRM will be received somewhere during
@@ -1207,7 +1207,7 @@ def handler(signum, frame):
12071207

12081208
expected_sigs += 2
12091209
# Wait for handlers to run to avoid signal coalescing
1210-
while len(sigs) < expected_sigs and time.time() < deadline:
1210+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
12111211
time.sleep(1e-5)
12121212

12131213
# All ITIMER_REAL signals should have been delivered to the

Lib/test/test_threadsignals.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def test_lock_acquire_interruption(self):
9595
lock = thread.allocate_lock()
9696
lock.acquire()
9797
signal.alarm(1)
98-
t1 = time.time()
98+
t1 = time.monotonic()
9999
self.assertRaises(KeyboardInterrupt, lock.acquire, timeout=5)
100-
dt = time.time() - t1
100+
dt = time.monotonic() - t1
101101
# Checking that KeyboardInterrupt was raised is not sufficient.
102102
# We want to assert that lock.acquire() was interrupted because
103103
# of the signal, not that the signal handler was called immediately
@@ -136,9 +136,9 @@ def other_thread():
136136
rlock.release()
137137
time.sleep(0.01)
138138
signal.alarm(1)
139-
t1 = time.time()
139+
t1 = time.monotonic()
140140
self.assertRaises(KeyboardInterrupt, rlock.acquire, timeout=5)
141-
dt = time.time() - t1
141+
dt = time.monotonic() - t1
142142
# See rationale above in test_lock_acquire_interruption
143143
self.assertLess(dt, 3.0)
144144
finally:
@@ -203,9 +203,9 @@ def my_handler(signum, frame):
203203
old_handler = signal.signal(signal.SIGUSR1, my_handler)
204204
try:
205205
def timed_acquire():
206-
self.start = time.time()
206+
self.start = time.monotonic()
207207
lock.acquire(timeout=0.5)
208-
self.end = time.time()
208+
self.end = time.monotonic()
209209
def send_signals():
210210
for _ in range(40):
211211
time.sleep(0.02)

Lib/test/test_timeout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ def _sock_operation(self, count, timeout, method, *args):
127127
self.sock.settimeout(timeout)
128128
method = getattr(self.sock, method)
129129
for i in range(count):
130-
t1 = time.time()
130+
t1 = time.monotonic()
131131
try:
132132
method(*args)
133133
except socket.timeout as e:
134-
delta = time.time() - t1
134+
delta = time.monotonic() - t1
135135
break
136136
else:
137137
self.fail('socket.timeout was not raised')

0 commit comments

Comments
 (0)