Skip to content

Commit 2c39586

Browse files
committed
Simplify the timeout clock test
1 parent aa141a7 commit 2c39586

1 file changed

Lines changed: 9 additions & 17 deletions

File tree

Lib/test/test_interpreters/test_queues.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pickle
33
import threading
44
from textwrap import dedent
5+
import time
56
import unittest
67
from unittest import mock
78

@@ -356,26 +357,17 @@ def test_get_timeout(self):
356357
queue.get(HUGE_TIMEOUT, 0.1)
357358

358359
def test_timeout_uses_monotonic_clock(self):
359-
# gh-153005: the timeout deadline must be based on time.monotonic(),
360-
# not the wall clock, so adjusting the system clock during the call
361-
# cannot make get()/put() over- or under-wait.
362-
class FakeClock:
363-
def __init__(self):
364-
self.now = 1000.0
365-
def monotonic(self):
366-
return self.now
367-
def sleep(self, delay):
368-
self.now += delay
369-
def time(self):
370-
raise AssertionError('the wall clock must not be used')
371-
372-
with mock.patch.object(queues, 'time', FakeClock()):
373-
queue = queues.create(1)
360+
# gh-153005: the deadline must be computed from the monotonic clock,
361+
# since the wall clock can be adjusted while the call is blocked.
362+
queue = queues.create(1)
363+
with mock.patch.object(queues, 'time', wraps=time) as fake_time:
374364
with self.assertRaises(queues.QueueEmpty):
375-
queue.get(timeout=1, _delay=0.4)
365+
queue.get(timeout=0)
376366
queue.put(None)
377367
with self.assertRaises(queues.QueueFull):
378-
queue.put(None, timeout=1, _delay=0.4)
368+
queue.put(None, timeout=0)
369+
fake_time.monotonic.assert_called()
370+
fake_time.time.assert_not_called()
379371

380372
def test_get_nowait(self):
381373
queue = queues.create()

0 commit comments

Comments
 (0)