def test_run_app_cancels_failed_tasks(patched_loop): app = web.Application() task = None exc = RuntimeError("FAIL") async def fail(): try: await asyncio.sleep(1000) except asyncio.CancelledError: raise exc async def on_startup(app): nonlocal task loop = asyncio.get_event_loop() task = loop.create_task(fail()) await asyncio.sleep(0.01) app.on_startup.append(on_startup) exc_handler = mock.Mock() patched_loop.set_exception_handler(exc_handler) web._run_app(app, print=stopper(patched_loop)) assert task.done() msg = { 'message': 'unhandled exception during asyncio.run() shutdown', 'exception': exc, 'task': task, } exc_handler.assert_called_with(patched_loop, msg)
def test_run_app_context_vars(patched_loop): from contextvars import ContextVar count = 0 VAR = ContextVar('VAR', default='default') async def on_startup(app): nonlocal count assert 'init' == VAR.get() VAR.set('on_startup') count += 1 async def on_cleanup(app): nonlocal count assert 'on_startup' == VAR.get() count += 1 async def init(): nonlocal count assert 'default' == VAR.get() VAR.set('init') app = web.Application() app.on_startup.append(on_startup) app.on_cleanup.append(on_cleanup) count += 1 return app web._run_app(init(), print=stopper(patched_loop)) assert count == 3
def test_run_app_custom_backlog(patched_loop) -> None: app = web.Application() web._run_app(app, backlog=10, print=stopper(patched_loop)) patched_loop.create_server.assert_called_with( mock.ANY, '0.0.0.0', 8080, ssl=None, backlog=10, reuse_address=None, reuse_port=None)
def test_run_app_custom_backlog_unix(patched_loop) -> None: app = web.Application() web._run_app(app, path='/tmp/tmpsock.sock', backlog=10, print=stopper(patched_loop)) patched_loop.create_unix_server.assert_called_with( mock.ANY, '/tmp/tmpsock.sock', ssl=None, backlog=10)
def test_run_app_https(patched_loop) -> None: app = web.Application() ssl_context = ssl.create_default_context() web._run_app(app, ssl_context=ssl_context, print=stopper(patched_loop)) patched_loop.create_server.assert_called_with( mock.ANY, '0.0.0.0', 8443, ssl=ssl_context, backlog=128, reuse_address=None, reuse_port=None)
def test_run_app_http_unix_socket(patched_loop, tmpdir) -> None: app = web.Application() sock_path = str(tmpdir / 'socket.sock') printer = mock.Mock(wraps=stopper(patched_loop)) web._run_app(app, path=sock_path, print=printer) patched_loop.create_unix_server.assert_called_with(mock.ANY, sock_path, ssl=None, backlog=128) assert "http://unix:{}:".format(sock_path) in printer.call_args[0][0]
def test_run_app_mixed_bindings(run_app_kwargs, expected_server_calls, expected_unix_server_calls, patched_loop): app = web.Application() web._run_app(app, print=stopper(patched_loop), **run_app_kwargs) assert (patched_loop.create_unix_server.mock_calls == expected_unix_server_calls) assert (patched_loop.create_server.mock_calls == expected_server_calls)
def test_run_app_nondefault_host_port(patched_loop, aiohttp_unused_port) -> None: port = aiohttp_unused_port() host = '127.0.0.1' app = web.Application() web._run_app(app, host=host, port=port, print=stopper(patched_loop)) patched_loop.create_server.assert_called_with(mock.ANY, host, port, ssl=None, backlog=128, reuse_address=None, reuse_port=None)
def test_run_app_cancels_all_pending_tasks(patched_loop): app = web.Application() task = None async def on_startup(app): nonlocal task loop = asyncio.get_event_loop() task = loop.create_task(asyncio.sleep(1000)) app.on_startup.append(on_startup) web._run_app(app, print=stopper(patched_loop)) assert task.cancelled()
def test_run_app_abstract_linux_socket(patched_loop) -> None: sock_path = b"\x00" + uuid4().hex.encode('ascii') app = web.Application() web._run_app( app, path=sock_path.decode('ascii', 'ignore'), print=stopper(patched_loop)) patched_loop.create_unix_server.assert_called_with( mock.ANY, sock_path.decode('ascii'), ssl=None, backlog=128 )
def test_startup_cleanup_signals_even_on_failure(patched_loop) -> None: patched_loop.create_server = mock.Mock(side_effect=RuntimeError()) app = web.Application() startup_handler = make_mocked_coro() app.on_startup.append(startup_handler) cleanup_handler = make_mocked_coro() app.on_cleanup.append(cleanup_handler) with pytest.raises(RuntimeError): web._run_app(app, print=stopper(patched_loop)) startup_handler.assert_called_once_with(app) cleanup_handler.assert_called_once_with(app)
def test_run_app_preexisting_inet6_socket(patched_loop) -> None: app = web.Application() sock = socket.socket(socket.AF_INET6) with contextlib.closing(sock): sock.bind(('::', 0)) port = sock.getsockname()[1] printer = mock.Mock(wraps=stopper(patched_loop)) web._run_app(app, sock=sock, print=printer) patched_loop.create_server.assert_called_with( mock.ANY, sock=sock, backlog=128, ssl=None ) assert "http://[::]:{}".format(port) in printer.call_args[0][0]
def test_run_app_http(patched_loop) -> None: app = web.Application() startup_handler = make_mocked_coro() app.on_startup.append(startup_handler) cleanup_handler = make_mocked_coro() app.on_cleanup.append(cleanup_handler) web._run_app(app, print=stopper(patched_loop)) patched_loop.create_server.assert_called_with(mock.ANY, '0.0.0.0', 8080, ssl=None, backlog=128, reuse_address=None, reuse_port=None) startup_handler.assert_called_once_with(app) cleanup_handler.assert_called_once_with(app)
def test_run_app_preexisting_unix_socket(patched_loop, mocker) -> None: app = web.Application() sock_path = '/tmp/test_preexisting_sock1' sock = socket.socket(socket.AF_UNIX) with contextlib.closing(sock): sock.bind(sock_path) os.unlink(sock_path) printer = mock.Mock(wraps=stopper(patched_loop)) web._run_app(app, sock=sock, print=printer) patched_loop.create_server.assert_called_with( mock.ANY, sock=sock, backlog=128, ssl=None ) assert "http://unix:{}:".format(sock_path) in printer.call_args[0][0]
async def start(self) -> None: """Start the API server.""" ssl_generator = SSLGenerator() ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain(ssl_generator.certificate_path, ssl_generator.certificate_path_key) print('[Web API] Listening on https://localhost:8080') self.ignore_aiohttp_ssl_eror(asyncio.get_event_loop()) apps = [ web._run_app( self.app, host='0.0.0.0', port=8080, handle_signals=False, # pylint: disable=protected-access print=None, ssl_context=ssl_context) ] if self.config.type == NodeType.MASTER: print( '[Web API INSECURE] Listening on http://localhost:8079 (for Sonos media assets)' ) runner = web.AppRunner(self.insecure_app) thread = Thread(target=self.start_insecure_app, args=(runner, )) thread.start() await asyncio.gather(*apps)
def run(self, **kwargs): try: self._loop.create_task(self._session.run()) self._loop.run_until_complete(web._run_app(self._app, **kwargs)) except (web.GracefulExit, KeyboardInterrupt): pass finally: # remove every fastresume file in the directory for resume_data_file_name in os.listdir(self._resume_data_path): if not resume_data_file_name.endswith("fastresume"): continue try: resume_data_file_path = os.path.join( self._resume_data_path, resume_data_file_name ) os.unlink(resume_data_file_path) except IOError as e: print(e) # after the web server is stopped, shutdown the session self._loop.run_until_complete( self._session.shutdown(resume_data_path=self._resume_data_path) ) web._cancel_all_tasks(self._loop) self._loop.shutdown_asyncgens() self._loop.close()
async def init(self): await self.session["db_client"].connect() self.session["botovod"].clear_handlers() self.session["botovod"].add_handler(handler) await self.init_telegram() if self.session["app"] is None: return self.session["app"]["botovod"] = self.session["botovod"] self.session["app"]["db_client"] = self.session["db_client"] ssl_context = None if (self.settings.bot.certificate_path is not None and self.settings.bot.privatekey_path is not None): ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) ssl_context.load_cert_chain( self.settings.bot.certificate_path, self.settings.bot.privatekey_path, ) self.session["loop"].create_task( web._run_app( app=self.session["app"], port=self.settings.bot.port, ssl_context=ssl_context, ))
async def setup_server(cls, host='127.0.0.1', port=5000): ''' 建立网络服务端 :return: ''' # 路由 routes = web.RouteTableDef() # 操控待机/恢复的路径 @routes.view('/switch') class SwitchView(web.View): async def get(self): return web.json_response( data=bool(cls.endless_work_task and not cls.endless_work_task.done())) async def post(self): # 获得开关切换命令 # 如果有必要,前端可以post过来一些附加消息,比如操作人账号 alternate_msg = await self.request.post() # 关 if cls.endless_work_task and not cls.endless_work_task.done(): asyncio.create_task(cls.rest()) # 开 else: asyncio.create_task(cls.work()) return web.json_response(data=True) cls.app = web.Application() cls.app.add_routes(routes) # 启动网络服务 asyncio.create_task(web._run_app(app=cls.app, host=host, port=port))
async def run(self): with open("polls.yaml") as f: config = yaml.safe_load(f) self.allowed_domains = config.get("allowed_domains", "*") asyncio.ensure_future( web._run_app(self.app, host=config["host"], port=config["port"]))
def test_run_app_cancels_done_tasks(patched_loop): app = web.Application() task = None async def coro(): return 123 async def on_startup(app): nonlocal task loop = asyncio.get_event_loop() task = loop.create_task(coro()) app.on_startup.append(on_startup) web._run_app(app, print=stopper(patched_loop)) assert task.done()
async def on_start(self): await super().on_start() self.webapp = self.createApp() self.webtask = await self.app.scheduler.spawn( web._run_app(self.webapp, path=str(self.socketPath)))
def run_website(bot: RoleplayBot): app = Application() app['bot'] = bot aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(TEMPLATES_PATH), context_processors=[global_variables]) app.add_routes(routes) app.add_routes([web.static('/', STATIC_PATH)]) return _run_app(app, host='0.0.0.0', port=8081)
def run_webserver(self): async def response(*a): with open(__location__ + '/fixtures/eurofxref-hist-90d.xml') as f: x = f.read() return web.Response(text=x, content_type="text/xml") self.app = web.Application() self.app.add_routes([web.get('/', response)]) self.loop.create_task(_run_app(self.app, host='127.0.0.1', port=self.test_webserver_port))
def test_run_app_default_logger_setup_only_if_unconfigured(patched_loop): patched_loop.set_debug(True) logger = web.access_logger attrs = { 'hasHandlers.return_value': True, 'level': None, 'name': 'aiohttp.access', } mock_logger = mock.create_autospec(logger, name='mock_access_logger') mock_logger.configure_mock(**attrs) app = web.Application() web._run_app(app, print=stopper(patched_loop), access_log=mock_logger) mock_logger.setLevel.assert_not_called() mock_logger.hasHandlers.assert_called_with() mock_logger.addHandler.assert_not_called()
def test_run_app_default_logger_setup_requires_default_logger(patched_loop): patched_loop.set_debug(True) logger = web.access_logger attrs = { 'hasHandlers.return_value': False, 'level': logging.NOTSET, 'name': None, } mock_logger = mock.create_autospec(logger, name='mock_access_logger') mock_logger.configure_mock(**attrs) app = web.Application() web._run_app(app, print=stopper(patched_loop), access_log=mock_logger) mock_logger.setLevel.assert_not_called() mock_logger.hasHandlers.assert_not_called() mock_logger.addHandler.assert_not_called()
async def _run_websocket_server(self): mgr = socketio.AsyncRedisManager('redis://{}:{}'.format( settings.REDIS_HOST, settings.REDIS_PORT)) sio_settings = dict(client_manager=mgr, async_mode='aiohttp') if settings.ENABLE_CORS: sio_settings['cors_allowed_origins'] = '*' self.sio_server = socketio.AsyncServer(**sio_settings) self.loop.create_task( _run_app(app, host='127.0.0.1', port=self.socketioport))
async def main(run_scheduler=True): session = aiohttp.ClientSession() # todo close if run_scheduler: scheduler.start() scheduler.add_job(check, 'interval', (session, ), seconds=settings.interval, next_run_time=datetime.datetime.now()) asyncio.create_task( web._run_app(app, host=settings.host, port=settings.port))
def test_run_app_default_logger(monkeypatch, patched_loop): patched_loop.set_debug(True) logger = web.access_logger attrs = { 'hasHandlers.return_value': False, 'level': logging.NOTSET, 'name': 'aiohttp.access', } mock_logger = mock.create_autospec(logger, name='mock_access_logger') mock_logger.configure_mock(**attrs) app = web.Application() web._run_app(app, print=stopper(patched_loop), access_log=mock_logger) mock_logger.setLevel.assert_any_call(logging.DEBUG) mock_logger.hasHandlers.assert_called_with() assert isinstance(mock_logger.addHandler.call_args[0][0], logging.StreamHandler)
def test_run_app_multiple_preexisting_sockets(patched_loop) -> None: app = web.Application() sock1 = socket.socket() sock2 = socket.socket() with contextlib.closing(sock1), contextlib.closing(sock2): sock1.bind(('0.0.0.0', 0)) _, port1 = sock1.getsockname() sock2.bind(('0.0.0.0', 0)) _, port2 = sock2.getsockname() printer = mock.Mock(wraps=stopper(patched_loop)) web._run_app(app, sock=(sock1, sock2), print=printer) patched_loop.create_server.assert_has_calls([ mock.call(mock.ANY, sock=sock1, backlog=128, ssl=None), mock.call(mock.ANY, sock=sock2, backlog=128, ssl=None) ]) assert "http://0.0.0.0:{}".format(port1) in printer.call_args[0][0] assert "http://0.0.0.0:{}".format(port2) in printer.call_args[0][0]
def test_run_app_coro(patched_loop) -> None: startup_handler = cleanup_handler = None async def make_app(): nonlocal startup_handler, cleanup_handler app = web.Application() startup_handler = make_mocked_coro() app.on_startup.append(startup_handler) cleanup_handler = make_mocked_coro() app.on_cleanup.append(cleanup_handler) return app web._run_app(make_app(), print=stopper(patched_loop)) patched_loop.create_server.assert_called_with(mock.ANY, '0.0.0.0', 8080, ssl=None, backlog=128, reuse_address=None, reuse_port=None) startup_handler.assert_called_once_with(mock.ANY) cleanup_handler.assert_called_once_with(mock.ANY)
def build_webserver(self, hooks): ''' Build an async web server used by hooks ''' app = web.Application() # Always add a simple test endpoint async def ping(request): return web.Response(text='pong') app.add_routes([web.get('/ping', ping)]) # Add routes from hooks for hook in hooks: app.add_routes(hook.routes) # Finally build the webserver coroutine return web._run_app(app, port=self.http_port, print=logger.info)