Patched versions of loop.time, loop.call_later, loop.call_at, and asyncio.sleep
Project description
aiofastfoward
Gives the ability to fast-forward time by providing patched versions of loop.call_later, loop.call_at, loop.time, and asyncio.sleep. This allows you to test asynchronous code synchronously.
Usage
loop.call_later
# Production code
async def schedule_callback(loop, callback):
loop.call_later(1, callback, 0)
loop.call_later(2, callback, 1)
# Test code
from unittest.mock import Mock, call
loop = asyncio.get_running_loop()
with aiofastfoward.FastForward(loop) as forward:
callback = Mock()
await schedule_callback(loop, callback)
await forward(1)
self.assertEqual(callback.mock_calls, [call(0)])
await forward(1)
self.assertEqual(callback.mock_calls, [call(0), call(1)])
asyncio.sleep
# Production code
async def sleeper(callback):
await asyncio.sleep(1)
await asyncio.sleep(2)
callback(0)
# Test code
loop = asyncio.get_running_loop()
callback = Mock()
with aiofastforward.FastForward(loop) as forward:
asyncio.create_task(sleeper())
await forward(3)
self.assertEqual(callback.mock_calls, [call(0)])
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiofastforward-0.0.1.tar.gz
(2.2 kB
view hashes)
Built Distribution
Close
Hashes for aiofastforward-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 168bbda46ea460fa74019f25d64d59cc6afee24db0e9963cedcc2b4d1ad8f44d |
|
MD5 | dfdaebb5ca5b3d291ff0e355bf6c9685 |
|
BLAKE2b-256 | de1a9c9321542ceed6d84d31a712320c78963051fb6188064a741bc9bdb4c9b6 |