Skip to content

Commit 65d2469

Browse files
committed
fix test_automatic_reconnect test
1 parent 16edba0 commit 65d2469

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

src/reactpy/testing/backend.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def __init__(
6060
app=self._app, host=self.host, port=self.port, loop="asyncio"
6161
)
6262
)
63+
self.webserver_thread: Thread
6364

6465
@property
6566
def log_records(self) -> list[logging.LogRecord]:
@@ -109,9 +110,10 @@ def list_logged_exceptions(
109110
async def __aenter__(self) -> BackendFixture:
110111
self._exit_stack = AsyncExitStack()
111112
self._records = self._exit_stack.enter_context(capture_reactpy_logs())
112-
Thread(target=self.webserver.run, daemon=True).start()
113113

114114
# Wait for the server to start
115+
self.webserver_thread: Thread = Thread(target=self.webserver.run, daemon=True)
116+
self.webserver_thread.start()
115117
await asyncio.sleep(1)
116118

117119
return self
@@ -131,7 +133,15 @@ async def __aexit__(
131133
msg = "Unexpected logged exception"
132134
raise LogAssertionError(msg) from logged_errors[0]
133135

134-
await asyncio.wait_for(self.webserver.shutdown(), timeout=5)
136+
await asyncio.wait_for(
137+
self.webserver.shutdown(), timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current
138+
)
139+
self.webserver_thread.join(timeout=REACTPY_TESTS_DEFAULT_TIMEOUT.current)
140+
141+
async def restart(self) -> None:
142+
"""Restart the server"""
143+
await self.__aexit__(None, None, None)
144+
await self.__aenter__()
135145

136146

137147
_MountFunc = Callable[["Callable[[], Any] | None"], None]

tests/test_client.py

+25-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from contextlib import AsyncExitStack
33
from pathlib import Path
44

5-
from playwright.async_api import Browser
5+
from playwright.async_api import Page
66

77
import reactpy
88
from reactpy.testing import BackendFixture, DisplayFixture, poll
@@ -13,13 +13,9 @@
1313
JS_DIR = Path(__file__).parent / "js"
1414

1515

16-
async def test_automatic_reconnect(browser: Browser):
17-
port = find_available_port("localhost")
18-
page = await browser.new_page()
19-
20-
# we need to wait longer here because the automatic reconnect is not instant
21-
page.set_default_timeout(10000)
22-
16+
async def test_automatic_reconnect(
17+
display: DisplayFixture, page: Page, server: BackendFixture
18+
):
2319
@reactpy.component
2420
def SomeComponent():
2521
count, incr_count = use_counter(0)
@@ -35,39 +31,34 @@ async def get_count():
3531
count = await page.wait_for_selector("#count")
3632
return await count.get_attribute("data-count")
3733

38-
async with AsyncExitStack() as exit_stack:
39-
server = await exit_stack.enter_async_context(BackendFixture(port=port))
40-
display = await exit_stack.enter_async_context(
41-
DisplayFixture(server, driver=page)
42-
)
43-
44-
await display.show(SomeComponent)
45-
46-
incr = await page.wait_for_selector("#incr")
34+
await display.show(SomeComponent)
4735

48-
for i in range(3):
49-
await poll(get_count).until_equals(str(i))
50-
await incr.click()
36+
await poll(get_count).until_equals("0")
37+
incr = await page.wait_for_selector("#incr")
38+
await incr.click()
5139

52-
# the server is disconnected but the last view state is still shown
53-
await page.wait_for_selector("#count")
40+
await poll(get_count).until_equals("1")
41+
incr = await page.wait_for_selector("#incr")
42+
await incr.click()
5443

55-
async with AsyncExitStack() as exit_stack:
56-
server = await exit_stack.enter_async_context(BackendFixture(port=port))
57-
display = await exit_stack.enter_async_context(
58-
DisplayFixture(server, driver=page)
59-
)
44+
await poll(get_count).until_equals("2")
45+
incr = await page.wait_for_selector("#incr")
46+
await incr.click()
6047

61-
# use mount instead of show to avoid a page refresh
62-
display.backend.mount(SomeComponent)
48+
await server.restart()
49+
await display.show(SomeComponent)
6350

64-
for i in range(3):
65-
await poll(get_count).until_equals(str(i))
51+
await poll(get_count).until_equals("0")
52+
incr = await page.wait_for_selector("#incr")
53+
await incr.click()
6654

67-
# need to refetch element because may unmount on reconnect
68-
incr = await page.wait_for_selector("#incr")
55+
await poll(get_count).until_equals("1")
56+
incr = await page.wait_for_selector("#incr")
57+
await incr.click()
6958

70-
await incr.click()
59+
await poll(get_count).until_equals("2")
60+
incr = await page.wait_for_selector("#incr")
61+
await incr.click()
7162

7263

7364
async def test_style_can_be_changed(display: DisplayFixture):

0 commit comments

Comments
 (0)