Skip to content

Commit 0621391

Browse files
committed
test auto reconnect
1 parent 3477e2b commit 0621391

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

src/idom/server/sanic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ class SanicRenderServer(AbstractRenderServer[Sanic, Config]):
3636

3737
_loop: asyncio.AbstractEventLoop
3838
_dispatcher_type: Type[AbstractDispatcher]
39+
_did_stop: Event
3940

4041
def stop(self) -> None:
4142
"""Stop the running application"""
4243
self._loop.call_soon_threadsafe(self.application.stop)
44+
self._did_stop.wait(5)
4345

4446
def _create_config(self, config: Optional[Config]) -> Config:
4547
new_config: Config = {
@@ -59,6 +61,12 @@ def _setup_application(self, config: Config, app: Sanic) -> None:
5961

6062
self._setup_blueprint_routes(config, bp)
6163

64+
self._did_stop = did_stop = Event()
65+
66+
@app.listener("before_server_stop") # type: ignore
67+
async def server_did_stop(app: Sanic, loop: asyncio.AbstractEventLoop) -> None:
68+
did_stop.set()
69+
6270
cors_config = config["cors"]
6371
if cors_config: # pragma: no cover
6472
cors_params = cors_config if isinstance(cors_config, dict) else {}

src/idom/testing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def __exit__(
162162
self.server.stop()
163163
logging.getLogger().removeHandler(self._log_handler)
164164
self.raise_if_logged_exception()
165+
del self.mount, self.server
165166
return None
166167

167168

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import inspect
44
import logging
55
import os
6-
from typing import Iterator, List
6+
from typing import Any, Iterator, List
77

88
import pyalect.builtins.pytest # noqa
99
import pytest
@@ -89,10 +89,10 @@ def create_driver(driver_is_headless):
8989
"""A Selenium web driver"""
9090
drivers = []
9191

92-
def create():
92+
def create(**kwargs: Any):
9393
options = ChromeOptions()
9494
options.headless = driver_is_headless
95-
driver = create_simple_selenium_web_driver(driver_options=options)
95+
driver = create_simple_selenium_web_driver(driver_options=options, **kwargs)
9696
drivers.append(driver)
9797
return driver
9898

tests/test_client/test_app.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1-
def test_automatic_reconnect():
2-
assert False
1+
import idom
2+
from idom.testing import ServerMountPoint
3+
4+
5+
def test_automatic_reconnect(create_driver):
6+
# we need to wait longer here because the automatic reconnect is not instance
7+
driver = create_driver(implicit_wait_timeout=10)
8+
9+
@idom.component
10+
def OldComponent():
11+
return idom.html.p({"id": "old-component"}, "old")
12+
13+
mount_point = ServerMountPoint()
14+
15+
with mount_point:
16+
mount_point.mount(OldComponent)
17+
driver.get(mount_point.url())
18+
19+
# the server is disconnected but the last view state is still shown
20+
driver.find_element_by_id("old-component")
21+
22+
set_state = idom.Ref(None)
23+
24+
@idom.component
25+
def NewComponent():
26+
state, set_state.current = idom.hooks.use_state(0)
27+
return idom.html.p({"id": f"new-component-{state}"}, f"new-{state}")
28+
29+
with mount_point:
30+
mount_point.mount(NewComponent)
31+
32+
# Note the lack of a page refresh before looking up this new component. The
33+
# client should attempt to reconnect and display the new view automatically.
34+
driver.find_element_by_id("new-component-0")
35+
36+
# check that we can resume normal operation
37+
set_state.current(1)
38+
driver.find_element_by_id("new-component-1")

0 commit comments

Comments
 (0)