Skip to content

Commit

Permalink
test: make test more robust by non using globals and guessing the con…
Browse files Browse the repository at this point in the history
…text

We relied on a global variable to wait for the component to be called,
but this seems to not be very robust. Instead, we pass a unique ID
via a query parameter to not re-use the Event object, and store the
context that we actually use, instead of relying on the last used
context found.
  • Loading branch information
maartenbreddels committed Oct 5, 2023
1 parent 1c7acd5 commit 8eb41fa
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions solara/test/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import textwrap
import threading
import typing
import urllib.parse
import uuid
from io import BytesIO
from pathlib import Path
from typing import Any, Callable, Dict, Generator, List, Union
Expand All @@ -19,6 +21,7 @@
from IPython.display import display

import solara.server.app
import solara.server.kernel_context
import solara.server.server
import solara.server.settings
from solara.server import reload
Expand Down Expand Up @@ -151,36 +154,43 @@ def run(app: Union[solara.server.app.AppScript, str]):
return run


run_event = threading.Event()
run_calls = 0
run_events: Dict[str, threading.Event] = {}
used_contexts: Dict[str, solara.server.kernel_context.VirtualKernelContext] = {}


@solara.component
def SyncWrapper():
global run_calls
import reacton.ipywidgets as w
router = solara.use_router()
values = urllib.parse.parse_qs(router.search, keep_blank_values=True)
id = values.get("id", [None])[0] # type: ignore
if id is None:
solara.Error("No id found in url")
else:

run_calls += 1
run_event.set()
return w.VBox(children=[w.HTML(value="Test in solara"), w.VBox()])
import reacton.ipywidgets as w

used_contexts[id] = solara.server.kernel_context.get_current_context()
run_events[id].set()

return w.VBox(children=[w.HTML(value="Test in solara"), w.VBox()])


@pytest.fixture()
def solara_test(solara_server, solara_app, page_session: "playwright.sync_api.Page"):
global run_calls
with solara_app("solara.test.pytest_plugin:SyncWrapper"):
page_session.goto(solara_server.base_url)
id = str(uuid.uuid4())
run_events[id] = run_event = threading.Event()
page_session.goto(solara_server.base_url + f"?id={id}")
run_event.wait()
try:
assert run_calls == 1
keys = list(solara.server.kernel_context.contexts)
assert len(keys) == 1, "expected only one context, got %s" % keys
context = solara.server.kernel_context.contexts[keys[0]]
context = used_contexts[id]
with context:
test_output_warmup = widgets.Output()
test_output = widgets.Output()
try:
page_session.locator("text=Test in solara").wait_for()
assert context.container
context.container.children[0].children[1].children[1].children = [test_output_warmup] # type: ignore
with test_output_warmup:
warmup()
Expand All @@ -197,9 +207,9 @@ def solara_test(solara_server, solara_app, page_session: "playwright.sync_api.Pa
test_output.close()
test_output_warmup.close()
finally:
run_event.clear()
del run_events[id]
del used_contexts[id]
test_output = None
run_calls = 0


class ServerVoila(ServerBase):
Expand Down

0 comments on commit 8eb41fa

Please sign in to comment.