Move pyodide to a web worker (#1333)

This PR adds support for optionally running pyodide in a web worker:

- add a new option config.execution_thread, which can be `main` or `worker`. The default is `main`

- improve the test machinery so that we run all tests twice, once for `main` and once for `worker`

- add a new esbuild target which builds the code for the worker

The support for workers is not complete and many features are still missing: there are 71 tests which are marked as `@skip_worker`, but we can fix them in subsequent PRs.

The vast majority of tests fail because js.document is unavailable: for it to run transparently, we need the "auto-syncify" feature of synclink.


Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
Co-authored-by: Madhur Tandon <20173739+madhur-tandon@users.noreply.github.com>
This commit is contained in:
Antonio Cuni
2023-04-14 10:55:31 +02:00
committed by GitHub
parent dfa116eb70
commit 8c5475f78f
28 changed files with 497 additions and 99 deletions

View File

@@ -2,11 +2,37 @@ import re
import pytest
from .support import JsErrors, PyScriptTest
from .support import JsErrors, PyScriptTest, skip_worker
class TestBasic(PyScriptTest):
def test_pyscript_hello(self):
self.pyscript_run(
"""
<py-script>
import js
js.console.log('hello pyscript')
</py-script>
"""
)
assert self.console.log.lines == ["hello pyscript"]
def test_execution_thread(self):
self.pyscript_run(
"""
<!-- we don't really need anything here, we just want to check that
pyscript starts -->
"""
)
assert self.execution_thread in ("main", "worker")
if self.execution_thread == "main":
where = "the main thread"
elif self.execution_thread == "worker":
where = "a web worker"
expected = f"[pyscript/main] Starting the interpreter in {where}"
assert expected in self.console.info.lines
def test_print(self):
self.pyscript_run(
"""
<py-script>
@@ -53,6 +79,9 @@ class TestBasic(PyScriptTest):
)
self.page.locator("button").click()
self.wait_for_console(
"Exception: this is an error inside handler", match_substring=True
)
## error in console
tb_lines = self.console.error.lines[-1].splitlines()
@@ -120,6 +149,7 @@ class TestBasic(PyScriptTest):
"hello asciitree", # printed by us
]
@skip_worker("FIXME: the banner doesn't appear")
def test_non_existent_package(self):
self.pyscript_run(
"""
@@ -139,6 +169,7 @@ class TestBasic(PyScriptTest):
alert_banner = self.page.wait_for_selector(".alert-banner")
assert expected_alert_banner_msg in alert_banner.inner_text()
@skip_worker("FIXME: the banner doesn't appear")
def test_no_python_wheel(self):
self.pyscript_run(
"""
@@ -240,6 +271,7 @@ class TestBasic(PyScriptTest):
is not None
)
@skip_worker("FIXME: showWarning()")
def test_assert_no_banners(self):
"""
Test that the DOM doesn't contain error/warning banners