async def get_address(self, loop: asyncio.AbstractEventLoop) -> SocketAddress: server = ThriftServer(Handler(), port=0) serve_task = loop.create_task(server.serve()) addy = await server.get_address() server.stop() await serve_task return addy
async def server_in_event_loop() -> typing.AsyncGenerator[SocketAddress, None]: server = ThriftServer(LeafServiceHandler(), ip="::1") serve_task = asyncio.get_event_loop().create_task(server.serve()) addr = await server.get_address() try: yield addr finally: server.stop() await serve_task
async def get_address(self, loop: asyncio.AbstractEventLoop) -> SocketAddress: # pyre-fixme[45]: Cannot instantiate abstract class `Handler`. server = ThriftServer(Handler(), port=0) serve_task = loop.create_task(server.serve()) addy = await server.get_address() server.stop() await serve_task return addy
async def server_in_event_loop(): server = ThriftServer(Handler(), ip="::1") serve_task = asyncio.get_event_loop().create_task(server.serve()) addr = await server.get_address() try: yield addr finally: server.stop() await serve_task
async def inner() -> None: # pyre-fixme[45]: Cannot instantiate abstract class `Handler`. handler = Handler() self.assertFalse(handler.on_start_serving_called) self.assertFalse(handler.on_stop_requested_called) server = ThriftServer(handler, port=0) serve_task = asyncio.get_event_loop().create_task(server.serve()) await server.get_address() self.assertTrue(handler.on_start_serving_called) server.stop() await serve_task self.assertTrue(handler.on_stop_requested_called)
def main(): parser = ArgumentParser() parser.add_argument('--port', default=1234, type=int, help='Port to run on') options = parser.parse_args() loop = asyncio.get_event_loop() handler = LoadTestHandler(loop) server = ThriftServer(handler, options.port, loop=loop) loop.add_signal_handler(signal.SIGINT, server.stop) loop.add_signal_handler(signal.SIGTERM, server.stop) print("Running Py3 server on port {}".format(options.port)) loop.run_until_complete(server.serve())
class TestServer: server: ThriftServer serve_task: asyncio.Task def __init__(self, *, ip: str, handler: thrift.py3.server.ServiceInterface) -> None: self.server = ThriftServer(handler, ip=ip, path=None) async def __aenter__(self) -> thrift.py3.server.SocketAddress: self.serve_task = asyncio.get_event_loop().create_task(self.server.serve()) return await self.server.get_address() # pyre-fixme[2]: Parameter must be annotated. async def __aexit__(self, *exc_info) -> None: self.server.stop() await self.serve_task
class TestServer: server: ThriftServer serve_task: asyncio.Task def __init__( self, ip: Optional[str] = None, handler: ServiceInterface = Handler(), # noqa: B008 ) -> None: self.server = ThriftServer(handler, ip=ip) async def __aenter__(self) -> SocketAddress: self.serve_task = asyncio.get_event_loop().create_task( self.server.serve()) return await self.server.get_address() # pyre-fixme[2]: Parameter must be annotated. async def __aexit__(self, *exc_info) -> None: self.server.stop() await self.serve_task