Skip to content

Commit be69ff2

Browse files
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). (cherry picked from commit 2cf4c20) Co-authored-by: Victor Stinner <[email protected]>
1 parent 2d91a13 commit be69ff2

14 files changed

+68
-66
lines changed

Lib/pydoc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,14 +2213,14 @@ def _start_server(urlhandler, hostname, port):
22132213
Let the server do its thing. We just need to monitor its status.
22142214
Use time.sleep so the loop doesn't hog the CPU.
22152215
2216-
>>> starttime = time.time()
2216+
>>> starttime = time.monotonic()
22172217
>>> timeout = 1 #seconds
22182218
22192219
This is a short timeout for testing purposes.
22202220
22212221
>>> while serverthread.serving:
22222222
... time.sleep(.01)
2223-
... if serverthread.serving and time.time() - starttime > timeout:
2223+
... if serverthread.serving and time.monotonic() - starttime > timeout:
22242224
... serverthread.stop()
22252225
... break
22262226

Lib/test/_test_multiprocessing.py

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

159159
def __call__(self, *args, **kwds):
160-
t = time.time()
160+
t = time.monotonic()
161161
try:
162162
return self.func(*args, **kwds)
163163
finally:
164-
self.elapsed = time.time() - t
164+
self.elapsed = time.monotonic() - t
165165

166166
#
167167
# Base class for test cases
@@ -1036,9 +1036,9 @@ def test_no_import_lock_contention(self):
10361036

10371037
def test_timeout(self):
10381038
q = multiprocessing.Queue()
1039-
start = time.time()
1039+
start = time.monotonic()
10401040
self.assertRaises(pyqueue.Empty, q.get, True, 0.200)
1041-
delta = time.time() - start
1041+
delta = time.monotonic() - start
10421042
# bpo-30317: Tolerate a delta of 100 ms because of the bad clock
10431043
# resolution on Windows (usually 15.6 ms). x86 Windows7 3.x once
10441044
# failed because the delta was only 135.8 ms.
@@ -1434,9 +1434,9 @@ def _test_waitfor_timeout_f(cls, cond, state, success, sem):
14341434
sem.release()
14351435
with cond:
14361436
expected = 0.1
1437-
dt = time.time()
1437+
dt = time.monotonic()
14381438
result = cond.wait_for(lambda : state.value==4, timeout=expected)
1439-
dt = time.time() - dt
1439+
dt = time.monotonic() - dt
14401440
# borrow logic in assertTimeout() from test/lock_tests.py
14411441
if not result and expected * 0.6 < dt < expected * 10.0:
14421442
success.value = True
@@ -2527,7 +2527,7 @@ def test_map_no_failfast(self):
25272527
# process would fill the result queue (after the result handler thread
25282528
# terminated, hence not draining it anymore).
25292529

2530-
t_start = time.time()
2530+
t_start = time.monotonic()
25312531

25322532
with self.assertRaises(ValueError):
25332533
with self.Pool(2) as p:
@@ -2539,7 +2539,7 @@ def test_map_no_failfast(self):
25392539
p.join()
25402540

25412541
# check that we indeed waited for all jobs
2542-
self.assertGreater(time.time() - t_start, 0.9)
2542+
self.assertGreater(time.monotonic() - t_start, 0.9)
25432543

25442544
def test_release_task_refs(self):
25452545
# Issue #29861: task arguments and results should not be kept
@@ -4051,19 +4051,19 @@ def test_wait_timeout(self):
40514051
expected = 5
40524052
a, b = multiprocessing.Pipe()
40534053

4054-
start = time.time()
4054+
start = time.monotonic()
40554055
res = wait([a, b], expected)
4056-
delta = time.time() - start
4056+
delta = time.monotonic() - start
40574057

40584058
self.assertEqual(res, [])
40594059
self.assertLess(delta, expected * 2)
40604060
self.assertGreater(delta, expected * 0.5)
40614061

40624062
b.send(None)
40634063

4064-
start = time.time()
4064+
start = time.monotonic()
40654065
res = wait([a, b], 20)
4066-
delta = time.time() - start
4066+
delta = time.monotonic() - start
40674067

40684068
self.assertEqual(res, [a])
40694069
self.assertLess(delta, 0.4)
@@ -4087,28 +4087,28 @@ def test_wait_integer(self):
40874087
self.assertIsInstance(p.sentinel, int)
40884088
self.assertTrue(sem.acquire(timeout=20))
40894089

4090-
start = time.time()
4090+
start = time.monotonic()
40914091
res = wait([a, p.sentinel, b], expected + 20)
4092-
delta = time.time() - start
4092+
delta = time.monotonic() - start
40934093

40944094
self.assertEqual(res, [p.sentinel])
40954095
self.assertLess(delta, expected + 2)
40964096
self.assertGreater(delta, expected - 2)
40974097

40984098
a.send(None)
40994099

4100-
start = time.time()
4100+
start = time.monotonic()
41014101
res = wait([a, p.sentinel, b], 20)
4102-
delta = time.time() - start
4102+
delta = time.monotonic() - start
41034103

41044104
self.assertEqual(sorted_(res), sorted_([p.sentinel, b]))
41054105
self.assertLess(delta, 0.4)
41064106

41074107
b.send(None)
41084108

4109-
start = time.time()
4109+
start = time.monotonic()
41104110
res = wait([a, p.sentinel, b], 20)
4111-
delta = time.time() - start
4111+
delta = time.monotonic() - start
41124112

41134113
self.assertEqual(sorted_(res), sorted_([a, p.sentinel, b]))
41144114
self.assertLess(delta, 0.4)
@@ -4119,9 +4119,9 @@ def test_wait_integer(self):
41194119
def test_neg_timeout(self):
41204120
from multiprocessing.connection import wait
41214121
a, b = multiprocessing.Pipe()
4122-
t = time.time()
4122+
t = time.monotonic()
41234123
res = wait([a], timeout=-1)
4124-
t = time.time() - t
4124+
t = time.monotonic() - t
41254125
self.assertEqual(res, [])
41264126
self.assertLess(t, 1)
41274127
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
@@ -2219,11 +2219,11 @@ def start_threads(threads, unlock=None):
22192219
try:
22202220
if unlock:
22212221
unlock()
2222-
endtime = starttime = time.time()
2222+
endtime = starttime = time.monotonic()
22232223
for timeout in range(1, 16):
22242224
endtime += 60
22252225
for t in started:
2226-
t.join(max(endtime - time.time(), 0.01))
2226+
t.join(max(endtime - time.monotonic(), 0.01))
22272227
started = [t for t in started if t.isAlive()]
22282228
if not started:
22292229
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, loop=loop))

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
@@ -1013,12 +1013,12 @@ def my_url_handler(url, content_type):
10131013
serverthread = pydoc._start_server(my_url_handler, hostname='0.0.0.0', port=0)
10141014
self.assertIn('0.0.0.0', serverthread.docserver.address)
10151015

1016-
starttime = time.time()
1016+
starttime = time.monotonic()
10171017
timeout = 1 #seconds
10181018

10191019
while serverthread.serving:
10201020
time.sleep(.01)
1021-
if serverthread.serving and time.time() - starttime > timeout:
1021+
if serverthread.serving and time.monotonic() - starttime > timeout:
10221022
serverthread.stop()
10231023
break
10241024

Lib/test/test_signal.py

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

11241124
expected_sigs = 0
1125-
deadline = time.time() + 15.0
1125+
deadline = time.monotonic() + 15.0
11261126

11271127
while expected_sigs < N:
11281128
os.kill(os.getpid(), signal.SIGPROF)
11291129
expected_sigs += 1
11301130
# Wait for handlers to run to avoid signal coalescing
1131-
while len(sigs) < expected_sigs and time.time() < deadline:
1131+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
11321132
time.sleep(1e-5)
11331133

11341134
os.kill(os.getpid(), signal.SIGUSR1)
11351135
expected_sigs += 1
1136-
while len(sigs) < expected_sigs and time.time() < deadline:
1136+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
11371137
time.sleep(1e-5)
11381138

11391139
# All ITIMER_REAL signals should have been delivered to the
@@ -1156,7 +1156,7 @@ def handler(signum, frame):
11561156
self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL
11571157

11581158
expected_sigs = 0
1159-
deadline = time.time() + 15.0
1159+
deadline = time.monotonic() + 15.0
11601160

11611161
while expected_sigs < N:
11621162
# Hopefully the SIGALRM will be received somewhere during
@@ -1166,7 +1166,7 @@ def handler(signum, frame):
11661166

11671167
expected_sigs += 2
11681168
# Wait for handlers to run to avoid signal coalescing
1169-
while len(sigs) < expected_sigs and time.time() < deadline:
1169+
while len(sigs) < expected_sigs and time.monotonic() < deadline:
11701170
time.sleep(1e-5)
11711171

11721172
# 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)