def test_run_app_abstract_linux_socket(loop, mocker): sock_path = b"\x00" + uuid4().hex.encode('ascii') loop.call_later(0.05, loop.stop) app = web.Application() web.run_app(app, path=sock_path.decode('ascii', 'ignore'), loop=loop, print=lambda *args: None) # New app run using same socket path with loop_context() as loop: mocker.spy(loop, 'create_unix_server') loop.call_later(0.05, loop.stop) app = web.Application() mocker.spy(app, 'startup') mocker.spy(os, 'remove') printed = StringIO() web.run_app(app, path=sock_path, print=printed.write, loop=loop) # Abstract paths don't exist on the file system, so no attempt should # be made to remove. assert mock.call([sock_path]) not in os.remove.mock_calls loop.create_unix_server.assert_called_with(mock.ANY, sock_path, ssl=None, backlog=128) app.startup.assert_called_once_with()
def test_info_login_api(app): with loop_context() as loop: with TestClient(app, loop=loop) as client: async def _test_info_login_api(): auth_header1 = { 'Authorization': 'Basic %s' % base64.b64encode(b'2014210761:2014210761').decode() } auth_header2 = { 'Authorization': 'Basic %s' % base64.b64encode(b'2014210761:fuckccnu').decode() } auth_header3 = {} resp = await client.get('/api/info/login/', headers=auth_header1) assert resp.status == 200 resp = await client.get('/api/info/login/', headers=auth_header2) assert resp.status == 403 resp = await client.get('/api/info/login/', headers=auth_header3) assert resp.status == 401 print('... test info login api [ok]') loop.run_until_complete(_test_info_login_api())
def test_query_success(self, mock_token, mock_env, variables): with loop_context() as loop: # Takes a query that will be used to determine which response is # generated by the FakeCrateDBCloud instance async def test_query(headers_query: dict): env = Configuration.get_env() token = Configuration.get_token() async with HttpSession( env, token, url="https://cratedb.local", conn=connector, headers= headers_query, # Determines the generated response ) as session: result = await session.fetch(me_query, variables) assert result["data"] == me_response fake_cloud = FakeCrateDBCloud(loop=loop) info = loop.run_until_complete(fake_cloud.start()) resolver = FakeResolver(info, loop=loop) connector = aiohttp.TCPConnector(loop=loop, resolver=resolver, ssl=True) query = {"query": "me"} loop.run_until_complete(test_query(query)) loop.run_until_complete(fake_cloud.stop())
def test_query_unauthorized(self, mock_print_error, mock_token, mock_env): with loop_context() as loop: # Takes a query that will be used to determine which response is # generated by the FakeCrateDBCloud instance async def test_query(headers_query: dict): env = Configuration.get_env() token = Configuration.get_token() with pytest.raises(SystemExit) as cm: async with HttpSession( env, token, url="https://cratedb.local", conn=connector, headers= headers_query, # Determines the generated response ) as session: await session.fetch(me_query, variables={}) mock_print_error.assert_called_once_with( "Unauthorized. Use `croud login` to login to CrateDB Cloud." ) assert cm.value.code == 1 fake_cloud = FakeCrateDBCloud(loop=loop) info = loop.run_until_complete(fake_cloud.start()) resolver = FakeResolver(info, loop=loop) connector = aiohttp.TCPConnector(loop=loop, resolver=resolver, ssl=True) query = {"query": "me"} loop.run_until_complete(test_query(query)) loop.run_until_complete(fake_cloud.stop())
def wrapper(*args, **kwargs): with loop_context() as loop: injector = Injector(loop=loop, fake_future=lambda: fake_future) asyncio.get_child_watcher().attach_loop(loop) asyncio.set_event_loop(loop) loop.run_until_complete(injector.call(f, *args, **kwargs))
def test_run_app_stale_unix_socket(loop, mocker, shorttmpdir): """Older asyncio event loop implementations are known to halt server creation when a socket path from a previous server bind still exists. """ skip_if_no_dict(loop) app = web.Application() sock_path = shorttmpdir.join('socket.sock') sock_path_string = str(sock_path) web.run_app(app, loop=loop, path=sock_path_string, print=stopper(loop)) assert not loop.is_closed() if sock_path.check(): # New app run using same socket path with loop_context() as loop: mocker.spy(loop, 'create_unix_server') app = web.Application() mocker.spy(app, 'startup') mocker.spy(os, 'remove') printer = mock.Mock(wraps=stopper(loop)) web.run_app(app, loop=loop, path=sock_path_string, print=printer) os.remove.assert_called_with(sock_path_string) loop.create_unix_server.assert_called_with(mock.ANY, sock_path_string, ssl=None, backlog=128) app.startup.assert_called_once_with() assert ("http://unix:{}:".format(sock_path) in printer.call_args[0][0])
def test_run_app_stale_unix_socket(loop, mocker, shorttmpdir): """Older asyncio event loop implementations are known to halt server creation when a socket path from a previous server bind still exists. """ loop.call_later(0.05, loop.stop) app = web.Application(loop=loop) sock_path = shorttmpdir.join('socket.sock') sock_path_string = str(sock_path) web.run_app(app, path=sock_path_string, print=lambda *args: None) assert loop.is_closed() if sock_path.check(): # New app run using same socket path with loop_context() as loop: mocker.spy(loop, 'create_unix_server') loop.call_later(0.05, loop.stop) app = web.Application(loop=loop) mocker.spy(app, 'startup') mocker.spy(os, 'remove') printed = StringIO() web.run_app(app, path=sock_path_string, print=printed.write) os.remove.assert_called_with(sock_path_string) loop.create_unix_server.assert_called_with(mock.ANY, sock_path_string, ssl=None, backlog=128) app.startup.assert_called_once_with() assert "http://unix:{}:".format(sock_path) in \ printed.getvalue()
def test_run_app_abstract_linux_socket(loop, mocker): sock_path = b"\x00" + uuid4().hex.encode('ascii') loop.call_later(0.05, loop.stop) app = web.Application(loop=loop) web.run_app( app, path=sock_path.decode('ascii', 'ignore'), print=lambda *args: None) assert loop.is_closed() # New app run using same socket path with loop_context() as loop: mocker.spy(loop, 'create_unix_server') loop.call_later(0.05, loop.stop) app = web.Application(loop=loop) mocker.spy(app, 'startup') mocker.spy(os, 'remove') printed = StringIO() web.run_app(app, path=sock_path, print=printed.write) # Abstract paths don't exist on the file system, so no attempt should # be made to remove. assert mock.call([sock_path]) not in os.remove.mock_calls loop.create_unix_server.assert_called_with( mock.ANY, sock_path, ssl=None, backlog=128 ) app.startup.assert_called_once_with()
def test_worker_run(): runner = CliRunner() with loop_context(): result = runner.invoke(cli, ['-A', 'tests.test_cli.darq', 'worker']) assert result.exit_code == 0 cli_output = 'Starting worker for 1 functions: tests.test_cli.foobar' assert cli_output in result.output
def test_grade_api(app): with loop_context() as loop: with TestClient(app, loop=loop) as client: async def get_headers(): nonlocal client auth_header = { 'Authorization': 'Basic %s' % base64.b64encode(b'2014210761:2014210761').decode() } async with aiohttp.ClientSession( headers=auth_header) as session: async with session.get( 'http://info_login_service:8080/api/info/login/' ) as resp: assert resp.status == 200 json_data = await resp.json() auth_header.update(json_data.get('cookie')) cookie_header = auth_header cookie_header['Sid'] = json_data.get('sid') return cookie_header async def _test_grade_get_api(): nonlocal client cookie_header = await get_headers() resp = await client.get('/api/grade/?xnm=2014&xqm=3', headers=cookie_header) assert resp.status == 200 print(".... grade get api [OK]") loop.run_until_complete(_test_grade_get_api())
def test_close(self): with loop_context() as loop: self.vane.hammertime.loop = loop self.vane.close(loop) self.vane.hammertime.close.assert_called_once_with() self.assertTrue(loop.is_closed())
def target() -> None: try: with loop_context() as loop: assert asyncio.get_event_loop_policy().get_event_loop() is loop loop.run_until_complete(test_subprocess_co(loop)) except Exception as exc: nonlocal child_exc child_exc = exc
def test_worker_check(): runner = CliRunner() with loop_context(): result = runner.invoke(cli, [ '-A', 'tests.test_cli.darq', 'worker', '--check', ]) assert result.exit_code == 1 expected = 'Health check failed: no health check sentinel value found' assert expected in result.output
def test_worker_queue(worker_mock): runner = CliRunner() custom_queue_name = 'my_queue' with loop_context(): result = runner.invoke(cli, [ '-A', 'tests.test_cli.darq', 'worker', '--queue', custom_queue_name, ]) assert result.exit_code == 0 worker_mock.assert_called_once_with(darq, queue_name=custom_queue_name)
def test_test_client_close_is_idempotent(): """ a test client, called multiple times, should not attempt to close the loop again. """ with loop_context() as loop: app = _create_example_app(loop) client = TestClient(app) client.close() client.close()
def test_worker_run_watch(mocker): darq.redis_pool = None mocker.patch('watchgod.awatch', return_value=mock_awatch()) runner = CliRunner() with loop_context(): result = runner.invoke(cli, [ '-A', 'tests.test_cli.darq', 'worker', '--watch', 'tests', ]) assert result.exit_code == 0 assert '1 files changed, reloading darq worker...' in result.output
def test_app_bad_argument(): runner = CliRunner() with loop_context(): result = runner.invoke(cli, ['-A', 'tests.test_cli.foobar', 'worker']) assert result.exit_code == 2 cli_output_re = ( r'Error: "APP" argument error\. <function foobar at .*> is not ' r'instance of <class \'darq\.app\.Darq\'>' ) assert re.search(cli_output_re, result.output)
def selector_loop() -> None: if sys.version_info >= (3, 8): policy = asyncio.WindowsSelectorEventLoopPolicy() else: policy = asyncio.DefaultEventLoopPolicy() asyncio.set_event_loop_policy(policy) with loop_context(policy.new_event_loop) as _loop: asyncio.set_event_loop(_loop) yield _loop
def function282(): with loop_context() as var2066: var2326 = function2617() with _TestClient(var2326, loop=var2066) as var292: @asyncio.coroutine def function2441(): var1051 = yield from var292.request('GET', '/') assert (var1051.status == 200) var29 = yield from var1051.var29() assert ('Hello, world' in var29) var2066.run_until_complete(function2441())
def test_server_with_create_test_teardown(): with loop_context() as loop: app = _create_example_app() with _TestClient(_TestServer(app, loop=loop), loop=loop) as client: async def test_get_route(): resp = await client.request("GET", "/") assert resp.status == 200 text = await resp.text() assert _hello_world_str == text loop.run_until_complete(test_get_route())
def test_no_jobs(darq, arq_redis, loop): darq.task(foobar) loop.run_until_complete( arq_redis.enqueue_job('tests.test_worker.foobar', [], {}), ) with loop_context(): worker = run_worker(darq) assert worker.jobs_complete == 1 assert str(worker) == ( '<Worker j_complete=1 j_failed=0 j_retried=0 j_ongoing=0>' )
def test_log_message_and_call_close_before_exiting_if_scan_cancelled(self): self.vane.scan_target = make_mocked_coro( raise_exception=asyncio.CancelledError) with loop_context() as loop: with patch("vane.core.custom_event_loop", MagicMock(return_value=loop)): self.vane.perform_action("scan", "http://localhost/") self.vane.output_manager.log_message.assert_any_call( "Scan interrupted.") self.assertTrue(loop.is_closed()) self.vane.output_manager.flush.assert_called_once_with()
def function509(): with loop_context() as var71: var1867 = function2617() with _TestClient(var1867, loop=var71) as var2177: @asyncio.coroutine def function2441(): nonlocal client var1595 = yield from var2177.request('GET', '/') assert (var1595.status == 200) var2663 = yield from var1595.var2663() assert ('Hello, world' in var2663) var71.run_until_complete(function2441())
def test_perform_action_dont_start_scan_if_database_failed_to_download_and_no_older_database_present( self): self.vane.database.database_directory = None self.vane.database._load_data = make_mocked_coro( raise_exception=ClientError()) self.vane.scan_target = make_mocked_coro() with loop_context() as loop, patch("vane.core.custom_event_loop", MagicMock(return_value=loop)): self.vane.perform_action(action="scan", url="test", verify_ssl=False) self.vane.scan_target.assert_not_called()
def run(self): try: backend = Backend(graph_path=str(TEST_GRAPH_PATH)) with backend: with loop_context() as loop: app = make_app(backend=backend, debug=True) client = TestClient(TestServer(app), loop=loop) loop.run_until_complete(client.start_server()) url = client.make_url("/graph/") self.q.put(url) loop.run_forever() except Exception as e: self.q.put(e)
def test_auto_gzip_decompress(): with loop_context() as loop: app = _create_example_app() with _TestClient(_TestServer(app, loop=loop), loop=loop) as client: async def test_get_route(): nonlocal client resp = await client.request("GET", "/gzip_hello") assert resp.status == 200 data = await resp.read() assert data == _hello_world_bytes loop.run_until_complete(test_get_route())
def test_scheduler_run(mocker): mocker.patch('darq.scheduler.poll', return_value=mock_awatch()) runner = CliRunner() darq.add_cron_jobs(cron(foobar, hour=1)) with loop_context(): result = runner.invoke(cli, ['-A', 'tests.test_cli.darq', 'scheduler']) assert result.exit_code == 0 cli_output = ( 'Starting cron scheduler for 1 cron jobs: \ntests.test_cli.foobar\n' ) assert cli_output in result.output
def test_server_with_create_test_teardown(): with loop_context() as loop: app = _create_example_app(loop) with _TestClient(app) as client: @asyncio.coroutine def test_get_route(): resp = yield from client.request("GET", "/") assert resp.status == 200 text = yield from resp.text() assert "Hello, world" in text loop.run_until_complete(test_get_route())
def test_full_server_scenario(): with loop_context() as loop: app = _create_example_app() with _TestClient(_TestServer(app, loop=loop), loop=loop) as client: @asyncio.coroutine def test_get_route(): nonlocal client resp = yield from client.request("GET", "/") assert resp.status == 200 text = yield from resp.text() assert _hello_world_str == text loop.run_until_complete(test_get_route())
def test_full_server_scenario(): with loop_context() as loop: app = _create_example_app() with _TestClient(app, loop=loop) as client: @asyncio.coroutine def test_get_route(): nonlocal client resp = yield from client.request("GET", "/") assert resp.status == 200 text = yield from resp.text() assert "Hello, world" in text loop.run_until_complete(test_get_route())
def loop(loop_factory, fast, loop_debug): """Return an instance of the event loop.""" with loop_context(loop_factory, fast=fast) as _loop: if loop_debug: _loop.set_debug(True) # pragma: no cover _loop._close = _loop.close _loop.close = lambda: None yield _loop # _loop._close() # TODO: Enable again when needed asyncio.set_event_loop(None)
def selector_loop(): if sys.version_info < (3, 7): policy = asyncio.get_event_loop_policy() policy._loop_factory = asyncio.SelectorEventLoop # type: ignore else: if sys.version_info >= (3, 8): policy = asyncio.WindowsSelectorEventLoopPolicy() # type: ignore else: policy = asyncio.DefaultEventLoopPolicy() asyncio.set_event_loop_policy(policy) with loop_context(policy.new_event_loop) as _loop: asyncio.set_event_loop(_loop) yield _loop
def test_auto_gzip_decompress(): with loop_context() as loop: app = _create_example_app() with _TestClient(_TestServer(app, loop=loop), loop=loop) as client: @asyncio.coroutine def test_get_route(): nonlocal client resp = yield from client.request("GET", "/gzip_hello") assert resp.status == 200 data = yield from resp.read() assert data == _hello_world_bytes loop.run_until_complete(test_get_route())
def test_mock_exists_custom_date_header(self): with loop_context() as loop: async def test(): async with TestClient(self.test_server, loop=loop) as client: # nonlocal client response = await client.get('/test-custom-date-header') self.assertEqual(response.status, 200) self.assertEqual(response.reason, 'OK') headers = response.headers self.assertEqual(headers['Date'], '20180201') loop.run_until_complete(test())
def test_run_app_stale_unix_socket(loop, mocker, shorttmpdir): """Older asyncio event loop implementations are known to halt server creation when a socket path from a previous server bind still exists. """ loop.call_later(0.05, loop.stop) app = web.Application() sock_path = shorttmpdir.join('socket.sock') sock_path_string = str(sock_path) web.run_app(app, loop=loop, path=sock_path_string, print=lambda *args: None) assert loop.is_closed() if sock_path.check(): # New app run using same socket path with loop_context() as loop: mocker.spy(loop, 'create_unix_server') loop.call_later(0.05, loop.stop) app = web.Application() mocker.spy(app, 'startup') mocker.spy(os, 'remove') printed = StringIO() web.run_app(app, loop=loop, path=sock_path_string, print=printed.write) os.remove.assert_called_with(sock_path_string) loop.create_unix_server.assert_called_with( mock.ANY, sock_path_string, ssl=None, backlog=128 ) app.startup.assert_called_once_with() assert "http://unix:{}:".format(sock_path) in \ printed.getvalue()
def test_run_app_stale_unix_socket(loop, mocker, shorttmpdir): """Older asyncio event loop implementations are known to halt server creation when a socket path from a previous server bind still exists. """ skip_if_no_dict(loop) app = web.Application() sock_path = shorttmpdir.join('socket.sock') sock_path_string = str(sock_path) web.run_app(app, loop=loop, path=sock_path_string, print=stopper(loop)) assert not loop.is_closed() if sock_path.check(): # New app run using same socket path with loop_context() as loop: mocker.spy(loop, 'create_unix_server') app = web.Application() mocker.spy(app, 'startup') mocker.spy(os, 'remove') printer = mock.Mock(wraps=stopper(loop)) web.run_app(app, loop=loop, path=sock_path_string, print=printer) os.remove.assert_called_with(sock_path_string) loop.create_unix_server.assert_called_with( mock.ANY, sock_path_string, ssl=None, backlog=128 ) app.startup.assert_called_once_with() assert ("http://unix:{}:".format(sock_path) in printer.call_args[0][0])
def loop(): with loop_context() as loop: yield loop
def loop(request): with loop_context(request.param) as loop: yield loop