mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
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.
This commit is contained in:
parent
8d3e02ef5a
commit
d6f02fc649
1 changed files with 11 additions and 18 deletions
|
|
@ -240,30 +240,23 @@ class BaseEventLoopTests(unittest.TestCase):
|
||||||
self.loop.set_debug(False)
|
self.loop.set_debug(False)
|
||||||
self.assertFalse(self.loop.get_debug())
|
self.assertFalse(self.loop.get_debug())
|
||||||
|
|
||||||
@mock.patch('asyncio.base_events.time')
|
|
||||||
@mock.patch('asyncio.base_events.logger')
|
@mock.patch('asyncio.base_events.logger')
|
||||||
def test__run_once_logging(self, m_logger, m_time):
|
def test__run_once_logging(self, m_logger):
|
||||||
|
def slow_select(timeout):
|
||||||
|
time.sleep(1.0)
|
||||||
|
return []
|
||||||
|
|
||||||
# Log to INFO level if timeout > 1.0 sec.
|
# Log to INFO level if timeout > 1.0 sec.
|
||||||
idx = -1
|
self.loop._selector.select = slow_select
|
||||||
data = [10.0, 10.0, 12.0, 13.0]
|
|
||||||
|
|
||||||
def monotonic():
|
|
||||||
nonlocal data, idx
|
|
||||||
idx += 1
|
|
||||||
return data[idx]
|
|
||||||
|
|
||||||
m_time.monotonic = monotonic
|
|
||||||
|
|
||||||
self.loop._scheduled.append(
|
|
||||||
asyncio.TimerHandle(11.0, lambda: True, (), self.loop))
|
|
||||||
self.loop._process_events = mock.Mock()
|
self.loop._process_events = mock.Mock()
|
||||||
self.loop._run_once()
|
self.loop._run_once()
|
||||||
self.assertEqual(logging.INFO, m_logger.log.call_args[0][0])
|
self.assertEqual(logging.INFO, m_logger.log.call_args[0][0])
|
||||||
|
|
||||||
idx = -1
|
def fast_select(timeout):
|
||||||
data = [10.0, 10.0, 10.3, 13.0]
|
time.sleep(0.001)
|
||||||
self.loop._scheduled = [asyncio.TimerHandle(11.0, lambda: True, (),
|
return []
|
||||||
self.loop)]
|
|
||||||
|
self.loop._selector.select = fast_select
|
||||||
self.loop._run_once()
|
self.loop._run_once()
|
||||||
self.assertEqual(logging.DEBUG, m_logger.log.call_args[0][0])
|
self.assertEqual(logging.DEBUG, m_logger.log.call_args[0][0])
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue