Skip to content

Commit

Permalink
Cleanup event_loop fixture (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Sep 25, 2024
1 parent 2adbba2 commit 3734381
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
8 changes: 2 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio

from click.testing import CliRunner

from aiohttp_devtools.cli import cli
Expand All @@ -16,8 +14,7 @@ def test_cli_help():
assert 'Serve static files from a directory.' in result.output


def test_serve(mocker, event_loop):
asyncio.set_event_loop(event_loop)
def test_serve(mocker):
mock_run_app = mocker.patch('aiohttp_devtools.cli.run_app')
runner = CliRunner()
result = runner.invoke(cli, ['serve', '.'])
Expand Down Expand Up @@ -71,8 +68,7 @@ def test_runserver_error_verbose(mocker):


@forked
def test_runserver_no_args(event_loop):
asyncio.set_event_loop(event_loop)
def test_runserver_no_args():
runner = CliRunner()
result = runner.invoke(cli, ['runserver'])
assert result.exit_code == 2
Expand Down
2 changes: 1 addition & 1 deletion tests/test_runserver_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def test_load_simple_app(tmpworkdir):


@forked
async def test_create_app_wrong_name(tmpworkdir, event_loop):
async def test_create_app_wrong_name(tmpworkdir):
mktree(tmpworkdir, SIMPLE_APP)
config = Config(app_path='app.py', app_factory_name='missing')
with pytest.raises(AiohttpDevConfigError) as excinfo:
Expand Down
23 changes: 11 additions & 12 deletions tests/test_runserver_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async def check_callback(session):


@forked
def test_start_runserver_app_instance(tmpworkdir, event_loop):
def test_start_runserver_app_instance(tmpworkdir):
mktree(tmpworkdir, {
'app.py': """\
from aiohttp import web
Expand All @@ -116,7 +116,7 @@ async def hello(request):


@forked
def test_start_runserver_with_multi_app_modules(tmpworkdir, event_loop, capfd):
def test_start_runserver_with_multi_app_modules(tmpworkdir, capfd):
mktree(tmpworkdir, {
"app.py": f"""\
from aiohttp import web
Expand Down Expand Up @@ -194,11 +194,11 @@ async def test_aux_app(tmpworkdir, aiohttp_client):


@forked
async def test_serve_main_app(tmpworkdir, event_loop, mocker):
asyncio.set_event_loop(event_loop)
async def test_serve_main_app(tmpworkdir, mocker):
loop = asyncio.get_running_loop()
mktree(tmpworkdir, SIMPLE_APP)
mock_modify_main_app = mocker.patch('aiohttp_devtools.runserver.serve.modify_main_app')
event_loop.call_later(0.5, event_loop.stop)
loop.call_later(0.5, loop.stop)

config = Config(app_path="app.py", main_port=0)
runner = await create_main_app(config, config.import_app_factory())
Expand All @@ -210,7 +210,7 @@ async def test_serve_main_app(tmpworkdir, event_loop, mocker):


@forked
async def test_start_main_app_app_instance(tmpworkdir, event_loop, mocker):
async def test_start_main_app_app_instance(tmpworkdir, mocker):
mktree(tmpworkdir, {
'app.py': """\
from aiohttp import web
Expand All @@ -234,11 +234,10 @@ async def hello(request):


@pytest.fixture
def aux_cli(aiohttp_client, event_loop):
async def aux_cli(aiohttp_client):
app = create_auxiliary_app(static_path='.')
cli = event_loop.run_until_complete(aiohttp_client(app))
yield cli
event_loop.run_until_complete(cli.close())
async with await aiohttp_client(app) as cli:
yield cli


async def test_websocket_hello(aux_cli, smart_caplog):
Expand All @@ -256,7 +255,7 @@ async def test_websocket_hello(aux_cli, smart_caplog):
assert 'adev.server.aux WARNING: browser disconnected, appears no websocket connection was made' in smart_caplog


async def test_websocket_info(aux_cli, event_loop):
async def test_websocket_info(aux_cli):
assert len(aux_cli.server.app[WS]) == 0
ws = await aux_cli.session.ws_connect(aux_cli.make_url('/livereload'))
try:
Expand All @@ -282,7 +281,7 @@ async def test_websocket_bad(aux_cli, smart_caplog):
assert "adev.server.aux ERROR: unknown websocket message type binary, data: b'this is bytes'" in smart_caplog


async def test_websocket_reload(aux_cli, event_loop):
async def test_websocket_reload(aux_cli):
reloads = await src_reload(aux_cli.server.app, 'foobar')
assert reloads == 0
ws = await aux_cli.session.ws_connect(aux_cli.make_url('/livereload'))
Expand Down
2 changes: 1 addition & 1 deletion tests/test_runserver_serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async def test_aux_reload_runtime_error(smart_caplog):
assert 'adev.server.aux ERROR: Error broadcasting change to /foo/bar, RuntimeError: foobar\n' == smart_caplog


async def test_aux_cleanup(event_loop):
async def test_aux_cleanup():
aux_app = Application()
aux_app.on_cleanup.append(cleanup_aux_app)
ws = MagicMock()
Expand Down
12 changes: 6 additions & 6 deletions tests/test_runserver_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def __anext__(self):
return awatch_mock


async def test_single_file_change(event_loop, mocker):
async def test_single_file_change(mocker):
mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch')
mocked_awatch.side_effect = create_awatch_mock()
mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future())
Expand All @@ -53,7 +53,7 @@ async def test_single_file_change(event_loop, mocker):
await app_task._session.close()


async def test_multiple_file_change(event_loop, mocker):
async def test_multiple_file_change(mocker):
mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch')
mocked_awatch.side_effect = create_awatch_mock({('x', '/path/to/file'), ('x', '/path/to/file2')})
mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future())
Expand All @@ -71,7 +71,7 @@ async def test_multiple_file_change(event_loop, mocker):
await app_task._session.close()


async def test_python_no_server(event_loop, mocker):
async def test_python_no_server(mocker):
mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch')
mocked_awatch.side_effect = create_awatch_mock({('x', '/path/to/file.py')})

Expand Down Expand Up @@ -100,7 +100,7 @@ async def test_python_no_server(event_loop, mocker):
await app_task._session.close()


async def test_reload_server_running(event_loop, aiohttp_client, mocker):
async def test_reload_server_running(aiohttp_client, mocker):
app = Application()
ws: Set[Tuple[WebSocketResponse, str]] = set(((MagicMock(), "/foo"),))
app[WS] = ws
Expand All @@ -117,7 +117,7 @@ async def test_reload_server_running(event_loop, aiohttp_client, mocker):
await app_task._session.close()


async def test_livereload_task_single(event_loop, mocker):
async def test_livereload_task_single(mocker):
mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch')
mocked_awatch.side_effect = create_awatch_mock()
mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future())
Expand All @@ -130,7 +130,7 @@ async def test_livereload_task_single(event_loop, mocker):
mock_src_reload.assert_called_once_with(app, '/path/to/file')


async def test_livereload_task_multiple(event_loop, mocker):
async def test_livereload_task_multiple(mocker):
mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch')
mocked_awatch.side_effect = create_awatch_mock({('x', '/path/to/file'), ('x', '/path/to/file2')})
mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future())
Expand Down
13 changes: 5 additions & 8 deletions tests/test_serve.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import asyncio

import pytest
from pytest_toolbox import mktree

from aiohttp_devtools.runserver import serve_static


@pytest.fixture
def cli(event_loop, tmpworkdir, aiohttp_client):
asyncio.set_event_loop(event_loop)
async def cli(tmpworkdir, aiohttp_client):
args = serve_static(static_path=str(tmpworkdir), livereload=False)
yield event_loop.run_until_complete(aiohttp_client(args["app"]))
yield await aiohttp_client(args["app"])


async def test_simple_serve(cli, tmpworkdir):
Expand Down Expand Up @@ -39,7 +36,7 @@ async def test_file_missing(cli, tmpworkdir):
assert "baz/\n" in text


async def test_browser_cache(event_loop, aiohttp_client, tmpworkdir):
async def test_browser_cache(aiohttp_client, tmpworkdir):
args = serve_static(static_path=str(tmpworkdir), browser_cache=True)
assert args["port"] == 8000
cli = await aiohttp_client(args["app"])
Expand All @@ -49,7 +46,7 @@ async def test_browser_cache(event_loop, aiohttp_client, tmpworkdir):
assert "Cache-Control" not in r.headers


async def test_html_file_livereload(event_loop, aiohttp_client, tmpworkdir):
async def test_html_file_livereload(aiohttp_client, tmpworkdir):
args = serve_static(static_path=str(tmpworkdir), livereload=True)
assert args["port"] == 8000
cli = await aiohttp_client(args["app"])
Expand All @@ -68,7 +65,7 @@ async def test_html_file_livereload(event_loop, aiohttp_client, tmpworkdir):
assert text.startswith('(function e(t,n,r){')


async def test_serve_index(event_loop, aiohttp_client, tmpworkdir):
async def test_serve_index(aiohttp_client, tmpworkdir):
args = serve_static(static_path=str(tmpworkdir), livereload=False)
assert args["port"] == 8000
cli = await aiohttp_client(args["app"])
Expand Down

0 comments on commit 3734381

Please sign in to comment.