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

@@ -1,6 +1,6 @@
import platform
from .support import PyScriptTest
from .support import PyScriptTest, skip_worker
class TestPyRepl(PyScriptTest):
@@ -79,6 +79,7 @@ class TestPyRepl(PyScriptTest):
# Shift-enter should not add a newline to the editor
assert self.page.locator(".cm-line").count() == 1
@skip_worker("FIXME: display()")
def test_display(self):
self.pyscript_run(
"""
@@ -92,6 +93,7 @@ class TestPyRepl(PyScriptTest):
out_div = self.page.wait_for_selector("#py-internal-0-repl-output")
assert out_div.inner_text() == "hello world"
@skip_worker("TIMEOUT")
def test_show_last_expression(self):
"""
Test that we display() the value of the last expression, as you would
@@ -109,6 +111,7 @@ class TestPyRepl(PyScriptTest):
out_div = self.page.wait_for_selector("#py-internal-0-repl-output")
assert out_div.inner_text() == "42"
@skip_worker("TIMEOUT")
def test_show_last_expression_with_output(self):
"""
Test that we display() the value of the last expression, as you would
@@ -130,6 +133,7 @@ class TestPyRepl(PyScriptTest):
out_div = self.page.wait_for_selector("#repl-target")
assert out_div.inner_text() == "42"
@skip_worker("FIXME: display()")
def test_run_clears_previous_output(self):
"""
Check that we clear the previous output of the cell before executing it
@@ -180,6 +184,7 @@ class TestPyRepl(PyScriptTest):
assert tb_lines[0] == "Traceback (most recent call last):"
assert tb_lines[-1] == "Exception: this is an error"
@skip_worker("FIXME: display()")
def test_multiple_repls(self):
"""
Multiple repls showing in the correct order in the page
@@ -202,6 +207,7 @@ class TestPyRepl(PyScriptTest):
self.page.wait_for_selector("#py-internal-1-repl-output")
assert self.page.inner_text("#py-internal-1-repl-output") == "second"
@skip_worker("FIXME: display()")
def test_python_exception_after_previous_output(self):
self.pyscript_run(
"""
@@ -223,6 +229,7 @@ class TestPyRepl(PyScriptTest):
assert "hello world" not in out_div.inner_text()
assert "ZeroDivisionError" in out_div.inner_text()
@skip_worker("FIXME: js.document")
def test_hide_previous_error_after_successful_run(self):
"""
this tests the fact that a new error div should be created once there's an
@@ -270,6 +277,7 @@ class TestPyRepl(PyScriptTest):
)
assert banner_content == expected
@skip_worker("TIMEOUT")
def test_auto_generate(self):
self.pyscript_run(
"""
@@ -300,6 +308,7 @@ class TestPyRepl(PyScriptTest):
out_texts = [el.inner_text() for el in self.iter_locator(outputs)]
assert out_texts == ["hello", "world", ""]
@skip_worker("FIXME: display()")
def test_multiple_repls_mixed_display_order(self):
"""
Displaying several outputs that don't obey the order in which the original
@@ -331,6 +340,7 @@ class TestPyRepl(PyScriptTest):
assert self.page.inner_text("#py-internal-1-1-repl-output") == "second children"
assert self.page.inner_text("#py-internal-0-1-repl-output") == "first children"
@skip_worker("FIXME: display()")
def test_repl_output_attribute(self):
# Test that output attribute sends stdout to the element
# with the given ID, but not display()
@@ -356,6 +366,7 @@ class TestPyRepl(PyScriptTest):
self.assert_no_banners()
@skip_worker("FIXME: js.document")
def test_repl_output_display_async(self):
# py-repls running async code are not expected to
# send display to element element
@@ -396,6 +407,7 @@ class TestPyRepl(PyScriptTest):
assert self.page.locator("#repl-target").text_content() == ""
self.assert_no_banners()
@skip_worker("FIXME: js.document")
def test_repl_stdio_dynamic_tags(self):
self.pyscript_run(
"""
@@ -498,6 +510,7 @@ class TestPyRepl(PyScriptTest):
assert self.page.wait_for_selector("#stderr-div").inner_text() == "one.\n"
self.assert_no_banners()
@skip_worker("TIMEOUT")
def test_repl_output_attribute_change(self):
# If the user changes the 'output' attribute of a <py-repl> tag mid-execution,
# Output should no longer go to the selected div and a warning should appear
@@ -535,6 +548,7 @@ class TestPyRepl(PyScriptTest):
alert_banner = self.page.wait_for_selector(".alert-banner")
assert expected_alert_banner_msg in alert_banner.inner_text()
@skip_worker("TIMEOUT")
def test_repl_output_element_id_change(self):
# If the user changes the ID of the targeted DOM element mid-execution,
# Output should no longer go to the selected element and a warning should appear
@@ -590,6 +604,7 @@ class TestPyRepl(PyScriptTest):
code = py_repl.inner_text()
assert "print('1')" in code
@skip_worker("TIMEOUT")
def test_repl_src_change(self):
self.writefile("loadReplSrc2.py", "2")
self.writefile("loadReplSrc3.py", "print('3')")