Skip to content

Commit 3734381

Browse files
Cleanup event_loop fixture (#690)
1 parent 2adbba2 commit 3734381

File tree

6 files changed

+26
-34
lines changed

6 files changed

+26
-34
lines changed

tests/test_cli.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import asyncio
2-
31
from click.testing import CliRunner
42

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

1816

19-
def test_serve(mocker, event_loop):
20-
asyncio.set_event_loop(event_loop)
17+
def test_serve(mocker):
2118
mock_run_app = mocker.patch('aiohttp_devtools.cli.run_app')
2219
runner = CliRunner()
2320
result = runner.invoke(cli, ['serve', '.'])
@@ -71,8 +68,7 @@ def test_runserver_error_verbose(mocker):
7168

7269

7370
@forked
74-
def test_runserver_no_args(event_loop):
75-
asyncio.set_event_loop(event_loop)
71+
def test_runserver_no_args():
7672
runner = CliRunner()
7773
result = runner.invoke(cli, ['runserver'])
7874
assert result.exit_code == 2

tests/test_runserver_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async def test_load_simple_app(tmpworkdir):
1414

1515

1616
@forked
17-
async def test_create_app_wrong_name(tmpworkdir, event_loop):
17+
async def test_create_app_wrong_name(tmpworkdir):
1818
mktree(tmpworkdir, SIMPLE_APP)
1919
config = Config(app_path='app.py', app_factory_name='missing')
2020
with pytest.raises(AiohttpDevConfigError) as excinfo:

tests/test_runserver_main.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async def check_callback(session):
9393

9494

9595
@forked
96-
def test_start_runserver_app_instance(tmpworkdir, event_loop):
96+
def test_start_runserver_app_instance(tmpworkdir):
9797
mktree(tmpworkdir, {
9898
'app.py': """\
9999
from aiohttp import web
@@ -116,7 +116,7 @@ async def hello(request):
116116

117117

118118
@forked
119-
def test_start_runserver_with_multi_app_modules(tmpworkdir, event_loop, capfd):
119+
def test_start_runserver_with_multi_app_modules(tmpworkdir, capfd):
120120
mktree(tmpworkdir, {
121121
"app.py": f"""\
122122
from aiohttp import web
@@ -194,11 +194,11 @@ async def test_aux_app(tmpworkdir, aiohttp_client):
194194

195195

196196
@forked
197-
async def test_serve_main_app(tmpworkdir, event_loop, mocker):
198-
asyncio.set_event_loop(event_loop)
197+
async def test_serve_main_app(tmpworkdir, mocker):
198+
loop = asyncio.get_running_loop()
199199
mktree(tmpworkdir, SIMPLE_APP)
200200
mock_modify_main_app = mocker.patch('aiohttp_devtools.runserver.serve.modify_main_app')
201-
event_loop.call_later(0.5, event_loop.stop)
201+
loop.call_later(0.5, loop.stop)
202202

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

211211

212212
@forked
213-
async def test_start_main_app_app_instance(tmpworkdir, event_loop, mocker):
213+
async def test_start_main_app_app_instance(tmpworkdir, mocker):
214214
mktree(tmpworkdir, {
215215
'app.py': """\
216216
from aiohttp import web
@@ -234,11 +234,10 @@ async def hello(request):
234234

235235

236236
@pytest.fixture
237-
def aux_cli(aiohttp_client, event_loop):
237+
async def aux_cli(aiohttp_client):
238238
app = create_auxiliary_app(static_path='.')
239-
cli = event_loop.run_until_complete(aiohttp_client(app))
240-
yield cli
241-
event_loop.run_until_complete(cli.close())
239+
async with await aiohttp_client(app) as cli:
240+
yield cli
242241

243242

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

258257

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

284283

285-
async def test_websocket_reload(aux_cli, event_loop):
284+
async def test_websocket_reload(aux_cli):
286285
reloads = await src_reload(aux_cli.server.app, 'foobar')
287286
assert reloads == 0
288287
ws = await aux_cli.session.ws_connect(aux_cli.make_url('/livereload'))

tests/test_runserver_serve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def test_aux_reload_runtime_error(smart_caplog):
9898
assert 'adev.server.aux ERROR: Error broadcasting change to /foo/bar, RuntimeError: foobar\n' == smart_caplog
9999

100100

101-
async def test_aux_cleanup(event_loop):
101+
async def test_aux_cleanup():
102102
aux_app = Application()
103103
aux_app.on_cleanup.append(cleanup_aux_app)
104104
ws = MagicMock()

tests/test_runserver_watch.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def __anext__(self):
3131
return awatch_mock
3232

3333

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

5555

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

7373

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

@@ -100,7 +100,7 @@ async def test_python_no_server(event_loop, mocker):
100100
await app_task._session.close()
101101

102102

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

119119

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

132132

133-
async def test_livereload_task_multiple(event_loop, mocker):
133+
async def test_livereload_task_multiple(mocker):
134134
mocked_awatch = mocker.patch('aiohttp_devtools.runserver.watch.awatch')
135135
mocked_awatch.side_effect = create_awatch_mock({('x', '/path/to/file'), ('x', '/path/to/file2')})
136136
mock_src_reload = mocker.patch('aiohttp_devtools.runserver.watch.src_reload', return_value=create_future())

tests/test_serve.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import asyncio
2-
31
import pytest
42
from pytest_toolbox import mktree
53

64
from aiohttp_devtools.runserver import serve_static
75

86

97
@pytest.fixture
10-
def cli(event_loop, tmpworkdir, aiohttp_client):
11-
asyncio.set_event_loop(event_loop)
8+
async def cli(tmpworkdir, aiohttp_client):
129
args = serve_static(static_path=str(tmpworkdir), livereload=False)
13-
yield event_loop.run_until_complete(aiohttp_client(args["app"]))
10+
yield await aiohttp_client(args["app"])
1411

1512

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

4138

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

5148

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

7067

71-
async def test_serve_index(event_loop, aiohttp_client, tmpworkdir):
68+
async def test_serve_index(aiohttp_client, tmpworkdir):
7269
args = serve_static(static_path=str(tmpworkdir), livereload=False)
7370
assert args["port"] == 8000
7471
cli = await aiohttp_client(args["app"])

0 commit comments

Comments
 (0)