本文整理汇总了Python中trio.fail_after方法的典型用法代码示例。如果您正苦于以下问题:Python trio.fail_after方法的具体用法?Python trio.fail_after怎么用?Python trio.fail_after使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类trio
的用法示例。
在下文中一共展示了trio.fail_after方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _reql_timeout
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def _reql_timeout(seconds):
"""
Run a block with a timeout, raising `ReqlTimeoutError` if the block
execution exceeds the timeout.
:param float seconds: A timeout in seconds. If None, then no timeout is
enforced.
:raises ReqlTimeoutError: If execution time exceeds the timeout.
"""
if seconds is None:
yield
else:
try:
with trio.fail_after(seconds):
yield
except trio.TooSlow:
raise ReqlTimeoutError()
示例2: _wait_for_winfsp_ready
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def _wait_for_winfsp_ready(mountpoint_path, timeout=1.0):
trio_mountpoint_path = trio.Path(mountpoint_path)
# Polling for `timeout` seconds until winfsp is ready
with trio.fail_after(timeout):
while True:
try:
if await trio_mountpoint_path.exists():
return
await trio.sleep(0.01)
# Looks like a revoked workspace has been mounted
except PermissionError:
return
# Might be another OSError like errno 113 (No route to host)
except OSError:
return
示例3: test_device_invite_same_name_different_organizations
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_device_invite_same_name_different_organizations(
backend, apiv1_alice_backend_sock, apiv1_otheralice_backend_sock, alice, otheralice, alice_nd_id
):
with backend.event_bus.listen() as spy, trio.fail_after(1):
async with device_invite(
apiv1_alice_backend_sock, invited_device_name=alice_nd_id.device_name
) as prep:
await spy.wait(MetaEvent.EVENT_CONNECTED, {"event_type": BackendEvent.DEVICE_CLAIMED})
backend.event_bus.send(
BackendEvent.DEVICE_CLAIMED,
organization_id=otheralice.organization_id,
device_id=alice_nd_id,
encrypted_claim=b"<from OtherOrg>",
)
await trio.sleep(0)
backend.event_bus.send(
BackendEvent.DEVICE_CLAIMED,
organization_id=alice.organization_id,
device_id=alice_nd_id,
encrypted_claim=b"<from CoolOrg>",
)
assert prep[0] == {"status": "ok", "encrypted_claim": b"<from CoolOrg>"}
示例4: test_concurrent_user_invite
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_concurrent_user_invite(
backend, apiv1_alice_backend_sock, apiv1_adam_backend_sock, alice, adam, mallory
):
with backend.event_bus.listen() as spy, trio.fail_after(1):
async with user_invite(apiv1_alice_backend_sock, user_id=mallory.user_id) as prep1:
await spy.wait(MetaEvent.EVENT_CONNECTED, {"event_type": BackendEvent.USER_CLAIMED})
async with user_invite(apiv1_adam_backend_sock, user_id=mallory.user_id) as prep2:
spy.clear()
await spy.wait(MetaEvent.EVENT_CONNECTED, {"event_type": BackendEvent.USER_CLAIMED})
backend.event_bus.send(
BackendEvent.USER_CLAIMED,
organization_id=mallory.organization_id,
user_id=mallory.user_id,
encrypted_claim=b"<good>",
)
assert prep1[0] == {"status": "ok", "encrypted_claim": b"<good>"}
assert prep2[0] == {"status": "ok", "encrypted_claim": b"<good>"}
示例5: test_user_invite_claim_cancel_invitation
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_user_invite_claim_cancel_invitation(monitor, running_backend, backend, alice):
new_device_id = DeviceID("zack@pc1")
token = generate_invitation_token()
invite_and_claim_cancel_scope = None
async def _from_alice():
nonlocal invite_and_claim_cancel_scope
with trio.CancelScope() as invite_and_claim_cancel_scope:
await invite_and_create_user(alice, new_device_id.user_id, is_admin=False, token=token)
async def _cancel_invite_and_claim():
invite_and_claim_cancel_scope.cancel()
await _invite_and_claim(running_backend, _from_alice, _cancel_invite_and_claim)
# Now make sure the invitation cannot be used
with trio.fail_after(1):
with pytest.raises(InviteClaimError) as exc:
await claim_user(alice.organization_addr, new_device_id, token=token)
assert (
str(exc.value)
== "Cannot retrieve invitation creator: User `zack` doesn't exist in backend"
)
示例6: test_process_while_offline
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_process_while_offline(
autojump_clock, running_backend, alice_core, bob_user_fs, alice, bob
):
assert alice_core.backend_status == BackendConnStatus.READY
with running_backend.offline():
with alice_core.event_bus.listen() as spy:
# Force wakeup of the sync monitor
alice_core.event_bus.send(CoreEvent.FS_ENTRY_UPDATED, id=alice.user_manifest_id)
assert not alice_core.are_monitors_idle()
with trio.fail_after(60): # autojump, so not *really* 60s
await spy.wait(
CoreEvent.BACKEND_CONNECTION_CHANGED,
{"status": BackendConnStatus.LOST, "status_exc": spy.ANY},
)
await alice_core.wait_idle_monitors()
assert alice_core.backend_status == BackendConnStatus.LOST
示例7: test_unmount_with_fusermount
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_unmount_with_fusermount(base_mountpoint, alice, alice_user_fs, event_bus):
wid = await alice_user_fs.workspace_create("w")
workspace = alice_user_fs.get_workspace(wid)
await workspace.touch("/bar.txt")
async with mountpoint_manager_factory(
alice_user_fs, event_bus, base_mountpoint
) as mountpoint_manager:
with event_bus.listen() as spy:
mountpoint_path = await mountpoint_manager.mount_workspace(wid)
command = f"fusermount -u {mountpoint_path}".split()
expected = {"mountpoint": mountpoint_path, "workspace_id": wid, "timestamp": None}
completed_process = await trio.run_process(command)
with trio.fail_after(1):
# fusermount might fail for some reasons
while completed_process.returncode:
completed_process = await trio.run_process(command)
await spy.wait(CoreEvent.MOUNTPOINT_STOPPED, expected)
assert not await trio.Path(mountpoint_path / "bar.txt").exists()
# Mountpoint path should be removed on umounting
assert not await trio.Path(mountpoint_path).exists()
示例8: test_init_online_backend_late_reply
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_init_online_backend_late_reply(
server_factory, core_config, alice, event_bus, backend
):
can_serve_client = trio.Event()
async def _handle_client(stream):
await can_serve_client.wait()
return await backend.handle_client(stream)
async with server_factory(_handle_client, alice.organization_addr):
with trio.fail_after(1):
async with logged_core_factory(
config=core_config, device=alice, event_bus=event_bus
) as core:
# We don't want for backend to reply finish core init
with core.event_bus.listen() as spy:
can_serve_client.set()
# Now backend reply, monitor should send events accordingly
await spy.wait(
CoreEvent.BACKEND_CONNECTION_CHANGED,
kwargs={"status": BackendConnStatus.READY, "status_exc": None},
)
示例9: _start_serving
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def _start_serving(
self, nursery: trio_typing.Nursery, ipc_path: pathlib.Path
) -> None:
if not self.is_running:
raise LifecycleError("Cannot start server if endpoint is not running")
elif self.is_stopped:
raise LifecycleError("Endpoint has already been run and stopped")
elif self.is_serving:
raise LifecycleError("Endpoint is already serving")
elif self.is_server_stopped:
raise LifecycleError("Endpoint server already ran and was stopped")
self.ipc_path = ipc_path
nursery.start_soon(self._run_server)
# Wait until the ipc socket has appeared and is accepting connections.
with trio.fail_after(constants.IPC_WAIT_SECONDS):
await _wait_for_path(trio.Path(ipc_path))
await self._socket_bound.wait()
示例10: heartbeat
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def heartbeat(ws, timeout, interval):
'''
Send periodic pings on WebSocket ``ws``.
Wait up to ``timeout`` seconds to send a ping and receive a pong. Raises
``TooSlowError`` if the timeout is exceeded. If a pong is received, then
wait ``interval`` seconds before sending the next ping.
This function runs until cancelled.
:param ws: A WebSocket to send heartbeat pings on.
:param float timeout: Timeout in seconds.
:param float interval: Interval between receiving pong and sending next
ping, in seconds.
:raises: ``ConnectionClosed`` if ``ws`` is closed.
:raises: ``TooSlowError`` if the timeout expires.
:returns: This function runs until cancelled.
'''
while True:
with trio.fail_after(timeout):
await ws.ping()
await trio.sleep(interval)
示例11: test_server_handler_exit
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_server_handler_exit(nursery, autojump_clock):
async def handler(request):
server_ws = await request.accept()
await trio.sleep(1)
server = await nursery.start(
partial(serve_websocket, handler, HOST, 0, ssl_context=None))
# connection should close when server handler exists
with trio.fail_after(2):
async with open_websocket(
HOST, server.port, '/', use_ssl=False) as connection:
with pytest.raises(ConnectionClosed) as exc_info:
await connection.get_message()
exc = exc_info.value
assert exc.reason.name == 'NORMAL_CLOSURE'
示例12: test_datagram_receiver
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_datagram_receiver(socket_pair):
sending_socket, receiving_socket = socket_pair
receiver_address = receiving_socket.getsockname()
sender_address = sending_socket.getsockname()
send_channel, receive_channel = trio.open_memory_channel(1)
async with background_trio_service(DatagramReceiver(receiving_socket, send_channel)):
data = b"some packet"
await sending_socket.sendto(data, receiver_address)
with trio.fail_after(0.5):
received_datagram = await receive_channel.receive()
assert received_datagram.datagram == data
assert received_datagram.sender_endpoint.ip_address == inet_aton(sender_address[0])
assert received_datagram.sender_endpoint.port == sender_address[1]
示例13: test_datagram_sender
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_datagram_sender(socket_pair):
sending_socket, receiving_socket = socket_pair
receiver_endpoint = receiving_socket.getsockname()
sender_endpoint = sending_socket.getsockname()
send_channel, receive_channel = trio.open_memory_channel(1)
async with background_trio_service(DatagramSender(receive_channel, sending_socket)):
outgoing_datagram = OutgoingDatagram(
b"some packet",
Endpoint(inet_aton(receiver_endpoint[0]), receiver_endpoint[1]),
)
await send_channel.send(outgoing_datagram)
with trio.fail_after(0.5):
data, sender = await receiving_socket.recvfrom(1024)
assert data == outgoing_datagram.datagram
assert sender == sender_endpoint
示例14: test_packet_decoder
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_packet_decoder():
datagram_send_channel, datagram_receive_channel = trio.open_memory_channel(1)
packet_send_channel, packet_receive_channel = trio.open_memory_channel(1)
service = PacketDecoder(datagram_receive_channel, packet_send_channel)
async with background_trio_service(service):
packet = AuthTagPacketFactory()
sender_endpoint = EndpointFactory()
await datagram_send_channel.send(IncomingDatagram(
datagram=packet.to_wire_bytes(),
sender_endpoint=sender_endpoint,
))
with trio.fail_after(0.5):
incoming_packet = await packet_receive_channel.receive()
assert incoming_packet.packet == packet
assert incoming_packet.sender_endpoint.ip_address == sender_endpoint.ip_address
assert incoming_packet.sender_endpoint.port == sender_endpoint.port
示例15: test_packet_encoder
# 需要导入模块: import trio [as 别名]
# 或者: from trio import fail_after [as 别名]
def test_packet_encoder():
packet_send_channel, packet_receive_channel = trio.open_memory_channel(1)
datagram_send_channel, datagram_receive_channel = trio.open_memory_channel(1)
service = PacketEncoder(packet_receive_channel, datagram_send_channel)
async with background_trio_service(service):
receiver_endpoint = EndpointFactory()
outgoing_packet = OutgoingPacket(
packet=AuthTagPacketFactory(),
receiver_endpoint=receiver_endpoint,
)
await packet_send_channel.send(outgoing_packet)
with trio.fail_after(0.5):
outgoing_datagram = await datagram_receive_channel.receive()
assert outgoing_datagram.datagram == outgoing_packet.packet.to_wire_bytes()
assert outgoing_datagram.receiver_endpoint.ip_address == receiver_endpoint.ip_address
assert outgoing_datagram.receiver_endpoint.port == receiver_endpoint.port