async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]: if self.cmd_type is None: raise TypeError(f"No cmd_type specified for {self}") with connection.add_command_handler(self.cmd_type, self.handle): self.on_apply(connection) yield
async def do_ping_pong_test(alice_connection: ConnectionAPI, bob_connection: ConnectionAPI) -> None: got_ping = asyncio.Event() got_pong = asyncio.Event() async def _handle_ping(connection: ConnectionAPI, msg: Any) -> None: got_ping.set() bob_connection.get_base_protocol().send_pong() async def _handle_pong(connection: ConnectionAPI, msg: Any) -> None: got_pong.set() alice_connection.add_command_handler(Pong, _handle_pong) bob_connection.add_command_handler(Ping, _handle_ping) alice_connection.get_base_protocol().send_ping() await asyncio.wait_for(got_ping.wait(), timeout=1) await asyncio.wait_for(got_pong.wait(), timeout=1)
async def apply( self, connection: ConnectionAPI) -> AsyncIterator[asyncio.Future[None]]: """ See LogicAPI.apply() The future returned here will never be done as a CommandHandler doesn't run a background task. """ self.connection = connection with connection.add_command_handler(self.command_type, cast(HandlerFn, self.handle)): yield asyncio.Future()
async def apply( self, connection: ConnectionAPI) -> AsyncIterator[asyncio.Task[Any]]: """ See LogicAPI.apply() The future returned here will never be done as a CommandHandler doesn't run a background task. """ self.connection = connection with connection.add_command_handler(self.command_type, cast(HandlerFn, self.handle)): yield create_task( _never_ending_coro(), f'CommandHandler/{self.__class__.__name__}/apply')
async def apply(self, connection: ConnectionAPI) -> AsyncIterator[None]: self.connection = connection with connection.add_command_handler(self.command_type, cast(HandlerFn, self.handle)): yield