Skip to content

Commit d6f02fc

Browse files
committed
asyncio: Refactor test__run_once_logging() to not rely on the exact number of
calls to time.monotonic(). Use a "fast select" and a "slow select" instead.
1 parent 8d3e02e commit d6f02fc

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

Lib/test/test_asyncio/test_base_events.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,30 +240,23 @@ def test_set_debug(self):
240240
self.loop.set_debug(False)
241241
self.assertFalse(self.loop.get_debug())
242242

243-
@mock.patch('asyncio.base_events.time')
244243
@mock.patch('asyncio.base_events.logger')
245-
def test__run_once_logging(self, m_logger, m_time):
246-
# Log to INFO level if timeout > 1.0 sec.
247-
idx = -1
248-
data = [10.0, 10.0, 12.0, 13.0]
249-
250-
def monotonic():
251-
nonlocal data, idx
252-
idx += 1
253-
return data[idx]
244+
def test__run_once_logging(self, m_logger):
245+
def slow_select(timeout):
246+
time.sleep(1.0)
247+
return []
254248

255-
m_time.monotonic = monotonic
256-
257-
self.loop._scheduled.append(
258-
asyncio.TimerHandle(11.0, lambda: True, (), self.loop))
249+
# Log to INFO level if timeout > 1.0 sec.
250+
self.loop._selector.select = slow_select
259251
self.loop._process_events = mock.Mock()
260252
self.loop._run_once()
261253
self.assertEqual(logging.INFO, m_logger.log.call_args[0][0])
262254

263-
idx = -1
264-
data = [10.0, 10.0, 10.3, 13.0]
265-
self.loop._scheduled = [asyncio.TimerHandle(11.0, lambda: True, (),
266-
self.loop)]
255+
def fast_select(timeout):
256+
time.sleep(0.001)
257+
return []
258+
259+
self.loop._selector.select = fast_select
267260
self.loop._run_once()
268261
self.assertEqual(logging.DEBUG, m_logger.log.call_args[0][0])
269262

0 commit comments

Comments
 (0)