Skip to content

add advance_time fixture and test (closes #83, #95, #96 #110) #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
simplify EventLoopClockAdvancer and clarify __call__ method
  • Loading branch information
derekbrokeit committed May 14, 2019
commit 6ddbbe3fda6dc1100df832ffb44c9bfb4380ffa0
12 changes: 8 additions & 4 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
@@ -168,6 +168,7 @@ def pytest_runtest_setup(item):

class EventLoopClockAdvancer:
__slots__ = ("offset", "_base_time",)

def __init__(self, loop):
self.offset = 0.0
self._base_time = loop.time
@@ -176,14 +177,17 @@ def __init__(self, loop):
def time(self):
return self._base_time() + self.offset

def __call__(self, seconds):
async def __call__(self, seconds):
# sleep so that the loop does everything currently waiting
await asyncio.sleep(0)

if seconds > 0:
# advance the clock by the given offset
self.offset += seconds

# Once the clock is adjusted, new tasks may have just been
# scheduled for running in the next pass through the event loop
return self.create_task(asyncio.sleep(0))
# Once the clock is adjusted, new tasks may have just been
# scheduled for running in the next pass through the event loop
await asyncio.sleep(0)


@pytest.yield_fixture