Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_escaping_cancelled_error_from_cancelled_task():
"""Regression test for issue #88. No CancelledError should escape the outer scope."""
async with open_cancel_scope() as scope:
async with move_on_after(0.1):
await sleep(1)
await scope.cancel()
async def test_move_on_after(delay):
result = False
async with move_on_after(delay) as scope:
await sleep(1)
result = True
assert not result
assert scope.cancel_called
async def test_shielding():
async def cancel_when_ready():
await wait_all_tasks_blocked()
await tg.cancel_scope.cancel()
inner_sleep_completed = outer_sleep_completed = False
async with create_task_group() as tg:
await tg.spawn(cancel_when_ready)
async with move_on_after(10, shield=True) as inner_scope:
assert inner_scope.shield
await sleep(0.1)
inner_sleep_completed = True
await sleep(1)
outer_sleep_completed = True
assert inner_sleep_completed
assert not outer_sleep_completed
assert tg.cancel_scope.cancel_called
assert not inner_scope.cancel_called
async def test_move_on_after_no_timeout():
result = False
async with move_on_after(None) as scope:
assert scope.deadline == float('inf')
await sleep(0.1)
result = True
assert result
assert not scope.cancel_called
async def test_shielded_deadline():
async with move_on_after(10):
async with open_cancel_scope(shield=True):
async with move_on_after(1000):
assert await current_effective_deadline() - await current_time() > 900
async def test_nested_move_on_after():
sleep_completed = inner_scope_completed = False
async with move_on_after(0.1) as outer_scope:
assert await current_effective_deadline() == outer_scope.deadline
async with move_on_after(1) as inner_scope:
assert await current_effective_deadline() == outer_scope.deadline
await sleep(2)
sleep_completed = True
inner_scope_completed = True
assert not sleep_completed
assert not inner_scope_completed
assert outer_scope.cancel_called
assert not inner_scope.cancel_called
async def test_shielded_deadline():
async with move_on_after(10):
async with open_cancel_scope(shield=True):
async with move_on_after(1000):
assert await current_effective_deadline() - await current_time() > 900
async def test_vid_pid_transformation(backend):
scanner = HotplugDetector.for_device(vid="0402", pid="0x0204", backend=backend)
async with move_on_after(0.001):
async for event in scanner.events():
pass
assert backend.params["idVendor"] == 0x0402
assert backend.params["idProduct"] == 0x0204
async def test_receive_timeout(self):
def server():
conn, _ = sock.accept()
conn.close()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('127.0.0.1', 0))
sock.listen()
addr = sock.getsockname()
thread = Thread(target=server, daemon=True)
thread.start()
stream = await connect_tcp(*addr)
start_time = time.monotonic()
async with move_on_after(0.1):
while time.monotonic() - start_time < 0.3:
await stream.receive(1)
pytest.fail('The timeout was not respected')
async def test_deadline_reached_on_start():
async with move_on_after(0):
await sleep(0)
pytest.fail('Execution should not reach this point')