-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: on_kernel_start callbacks acculumated after hot reload
Introduced in #471 We should remove the on_kernel_start callbacks on a hot reload, but not remove the ones added by the hot reload itself.
- Loading branch information
1 parent
0c33e33
commit bc84a1f
Showing
6 changed files
with
147 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import shutil | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import solara.lab | ||
import solara.server.kernel_context | ||
from solara.server import reload | ||
from solara.server.app import AppScript | ||
|
||
HERE = Path(__file__).parent | ||
|
||
kernel_start_path = HERE / "solara_test_apps" / "kernel_start.py" | ||
|
||
|
||
@pytest.mark.parametrize("as_module", [False, True]) | ||
def test_script_reload_component(tmpdir, kernel_context, extra_include_path, no_kernel_context, as_module): | ||
|
||
target = Path(tmpdir) / "kernel_start.py" | ||
shutil.copy(kernel_start_path, target) | ||
with extra_include_path(str(tmpdir)): | ||
on_kernel_start_callbacks = solara.server.kernel_context._on_kernel_start_callbacks.copy() | ||
callbacks_start = [k.callback for k in solara.server.kernel_context._on_kernel_start_callbacks] | ||
if as_module: | ||
app = AppScript(f"{target.stem}") | ||
else: | ||
app = AppScript(f"{target}") | ||
try: | ||
app.run() | ||
callback = app.routes[0].module.test_callback # type: ignore | ||
callbacks = [k.callback for k in solara.server.kernel_context._on_kernel_start_callbacks] | ||
assert callbacks == [*callbacks_start, callback] | ||
prev = callbacks.copy() | ||
reload.reloader.reload_event_next.clear() | ||
target.touch() | ||
# wait for the event to trigger | ||
reload.reloader.reload_event_next.wait() | ||
app.run() | ||
callback = app.routes[0].module.test_callback # type: ignore | ||
callbacks = [k[0] for k in solara.server.kernel_context._on_kernel_start_callbacks] | ||
assert callbacks != prev | ||
assert callbacks == [*callbacks_start, callback] | ||
finally: | ||
app.close() | ||
solara.server.kernel_context._on_kernel_start_callbacks.clear() | ||
solara.server.kernel_context._on_kernel_start_callbacks.extend(on_kernel_start_callbacks) | ||
|
||
|
||
def test_on_kernel_start_cleanup(kernel_context, no_kernel_context): | ||
def test_callback_cleanup(): | ||
pass | ||
|
||
cleanup = solara.lab.on_kernel_start(test_callback_cleanup) | ||
assert test_callback_cleanup in [k.callback for k in solara.server.kernel_context._on_kernel_start_callbacks] | ||
cleanup() | ||
assert test_callback_cleanup not in [k.callback for k in solara.server.kernel_context._on_kernel_start_callbacks] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import solara | ||
import solara.lab | ||
|
||
|
||
def test_callback(): | ||
pass | ||
|
||
|
||
solara.lab.on_kernel_start(test_callback) | ||
|
||
|
||
@solara.component | ||
def Page(): | ||
solara.Text("Hello, World!") |