Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Dec 7, 2023
1 parent 982e01a commit eefa7ff
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion tests/test_runserver_watch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import time
from functools import partial
from typing import Set, Tuple
from unittest.mock import MagicMock, call
from unittest.mock import AsyncMock, MagicMock, call

from aiohttp import ClientSession
from aiohttp.web import Application, WebSocketResponse
Expand Down Expand Up @@ -193,3 +194,34 @@ async def test_stop_process_dirty(mocker):
await app_task._stop_dev_server()
assert mock_kill.call_args_list == [call(321, 2)]
assert process_mock.kill.called_once()


async def test_restart_after_connection_loss(mocker):
mocked_awatch = mocker.patch("aiohttp_devtools.runserver.watch.awatch", autospec=True, spec_set=True)
mocked_awatch.side_effect = create_awatch_mock({("x", "/path/to/file.py")})
app_task = AppTask(MagicMock())
start_mock = mocker.patch.object(app_task, "_start_dev_server", autospec=True, spec_set=True)
mock_reload = mocker.patch.object(app_task, "_src_reload_when_live", autospec=True, spec_set=True)
mocker.patch.object(app_task, "_stop_dev_server", autospec=True, spec_set=True)

app = mocker.create_autospec(Application, spec_set=True, instance=True)
# Simulate connection lost from recent restart.
d = {WS: set(), LAST_RELOAD: [1, time.time()]}
app.__getitem__.side_effect = lambda k: d.get(k, MagicMock())

def update_ws(i):
d[WS].add(MagicMock(spec_set=()))
return AsyncMock()

sleep_mock = mocker.patch("asyncio.sleep", autospec=True, spec_set=True)
sleep_mock.side_effect = update_ws

await app_task.start(app)
assert app_task._task is not None
await app_task._task
assert sleep_mock.call_count < 5
assert call(0.1) in sleep_mock.call_args_list
mock_reload.assert_called_once()
assert start_mock.call_count == 2
assert app_task._session is not None
await app_task._session.close()

0 comments on commit eefa7ff

Please sign in to comment.