Skip to content

Commit ce6a2c7

Browse files
committed
rearrange ClockEventLoop.advance_time so that extra iterations are not run unless needed
1 parent d757d19 commit ce6a2c7

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

pytest_asyncio/plugin.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,20 @@ async def advance_time(self, seconds):
213213
'''
214214
if seconds < 0:
215215
raise ValueError('cannot go backwards in time')
216-
# advance the clock and run the loop
216+
217+
# advance the clock by the given offset
217218
self._offset += seconds
218-
# Once advanced, new tasks may have just been scheduled for running
219-
# in the next loop, advance once more to start these handlers
220-
await asyncio.sleep(0)
221-
await asyncio.sleep(0)
219+
220+
# ensure waiting callbacks are run before advancing the clock
222221
await asyncio.sleep(0)
223222

223+
if seconds > 0:
224+
# Once the clock is adjusted, new tasks may have just been scheduled for running
225+
# in the next pass through the event loop and advance again for the task
226+
# that calls `advance_time`
227+
await asyncio.sleep(0)
228+
await asyncio.sleep(0)
229+
224230

225231
# maps marker to the name of the event loop fixture that will be available
226232
# to marked test functions

0 commit comments

Comments
 (0)