8000 Enable worker tests by antocuni · Pull Request #1757 · pyscript/pyscript · GitHub
[go: up one dir, main page]

Skip to content

Enable worker tests #1757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Sep 27, 2023
Merged

Enable worker tests #1757

merged 19 commits into from
Sep 27, 2023

Conversation

antocuni
Copy link
Contributor
@antocuni antocuni commented Sep 25, 2023

This PR re-enables tests on workers.
Highlights:

  • by default, each test is run twice: the main thread version uses <script type="py">, the worker version automatically turn the tags into <script type="py" worker>
  • you can tweak the settings per-class by using the @with_execution_thread decorator. In particular, @with_execution_thread(None) is for those tests which don't care about it (e.g., test_py_config.py)
  • inside each class, there might be some test which should be run only in the main thread (because it doesn't make sense to test it in a worker). For those, I introduced the @only_main decorator
  • we might introduce @only_worker in the future, if needed
  • @skip_worker is for those tests which currently pass on main but not on workers. These are meant to be temporary, and eventually they should all be fixed

During the process, I tweaked/improved/fixed/deleted some of the existing tests. Some of them were at risk of being flaky and I made them more robust, others depended on some very precise implementation detail, and I made them more generic (for example, test_image_renders_correctly relied on pillow to render an image with a very specific string of bytes, and it broke due to the recent upgrade to pyodide 0.24.1)

I also renamed all the skip messages to start with NEXT, so that they are easier to grep.

Next steps

  1. re-enable automatic test run on all PRs (if someone wants to volunteer to do that, it would be welcome :))
  2. open an umbrella issue to examine/explain/group all the tests which are currently skipped on main
  3. open an umbrella issue to examine/explain/group all the tests which are currently skipped on worker

@antocuni antocuni marked this pull request as draft September 25, 2023 17:00
@antocuni
Copy link
Contributor Author

@JeffersGlass to continue the conversation started in the other PR, here is my WIP PR which among the other things fixes wait_for_pyscript.
This is the relevant commit: 1571841

At the moment it's a ugly hack because it hardcodes a console.debug() instead of using a hook, and the equivalent for the case of workers is even uglier (note that in this case it's python code, not JS code):

// XXX (antocuni): this is a hack, we need to find a better way to do this
const afterRunWorker = "import js; js.console.debug('---pyscript: done---')"
const workerHooks = {
codeBeforeRunWorker: () =>
[stdlib, ...hooks.codeBeforeRunWorker].map(dedent).join("\n"),
codeBeforeRunWorkerAsync: () =>
[stdlib, ...hooks.codeBeforeRunWorkerAsync].map(dedent).join("\n"),
codeAfterRunWorker: () =>
[...hooks.codeAfterRunWorker, afterRunWorker].map(dedent).join("\n"),
codeAfterRunWorkerAsync: () =>
[...hooks.codeAfterRunWorkerAsync, afterRunWorker].map(dedent).join("\n"),
};

@WebReflection is working on a better solution which uses hooks instead of hardcoding the prints.

@antocuni antocuni force-pushed the antocuni/worker-tests branch from 479e2ec to f554fdb Compare September 26, 2023 13:34
@antocuni antocuni changed the title WIP: enable worker tests Enable worker tests Sep 26, 2023
@antocuni antocuni marked this pull request as ready for review September 26, 2023 16:47
@pytest.mark.skip(
reason="FIXME: No banner and should also add a WARNING about CORS"
)
@pytest.mark.skip(reason="NEXT: No banner and should also add a WARNING about CORS")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it just CORS or all the worker related headers?

@@ -104,6 +110,7 @@ def test_python_exception(self):
assert tb_lines[0] == "Traceback (most recent call last):"
assert tb_lines[-1] == "Exception: this is an error"

@skip_worker("NEXT: py-click doesn't work inside workers")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. 10000 Learn more.

this is not really a skip, this is fundamentally impossible to relate to a worker from the main thread ... which worker? to start with ... and should we really offer this functionality when we have a @when decorator that works within Python code in workers too, as long as the ID exists on the main thread?

@@ -151,20 +159,24 @@ def test_execution_in_order(self):
"four",
]

@skip_worker("NEXT: something very weird happens here")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this info is a bit cryptic ... what is weird in this test?

def test_escaping_of_angle_brackets(self):
"""
Check that script tags escape angle brackets
"""
self.pyscript_run(
"""
<script type="py">import js; js.console.log("A", 1<2, 1>2)</script>
<script type="py">js.console.log("B <div></div>")</script>
<script type="py">import js; js.console.log("B <div></div>")</script>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only weird thing I see is that we use the worker global console while maybe we check/test the main thread one ... in that case it's from pyscript import window; window.console.log(...)

@@ -191,7 +203,7 @@ def test_packages(self):
"hello asciitree", # printed by us
]

@pytest.mark.skip("FIXME: No banner")
@pytest.mark.skip("NEXT: No banner")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if by banner we mean the DOM related to the error, it's a div.py-error from the error plugin.

@@ -301,7 +310,7 @@ def test_js_version(self):
)

# TODO: ... and we shouldn't: it's a module and we better don't leak in global
@pytest.mark.skip("DIFFERENT BEHAVIOUR: we don't expose pyscript on window")
@pytest.mark.skip("NEXT: we don't expose pyscript on window")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import pyscript works in either main (window?) or worker so I am not sure I understand this skip message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is about leaking globally pyscript on window, we dont' do that anymore, and likely we shouldn't ... as we land a module, and we discussed already, and filed issues and MRs, aroud not leaking on global.

@@ -68,6 +72,7 @@ def test_target_parameter(self):
mydiv = self.page.locator("#mydiv")
assert mydiv.inner_text() == "hello world"

@skip_worker("NEXT: display(target=...) does not work")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does and it's tested ... maybe it's what we try to grab as element that doesn't work?

@@ -87,6 +92,7 @@ def test_tag_target_attribute(self):
goodbye = self.page.locator("#goodbye")
assert goodbye.inner_text() == "goodbye world"

@skip_worker("NEXT: display target does not work properly")
Copy link
Contributor
@WebReflection WebReflection Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<script> can't be a visual target ... we talked about it and it's a display issue we need to fix, not a library issue ... there was a discussion around this, remember? if the ID is a <script> tag, then get its target.id instead ... this warning is misleading to me.

@@ -163,6 +171,7 @@ def say_hello():
assert py0.inner_text() == ""
assert py1.inner_text() == "hello"

@skip_worker("NEXT: py-click doesn't work")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and it cannot with workers ... see my previous comment.

# waiit_for_pyscript is broken: it waits until the python code is about to
# start, to until the python code has finished execution
@pytest.mark.skip("FIXME: wait_for_pyscript is broken")
@skip_worker("NEXT: matplotlib-pyodide backend does not work")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cute but it feels like, while we do care about matplotlib working from workers, and it's tested and it does work already, we're testing 3rd party gotchas in our unrelated project. This should (imho) be part of examples tests, not core functionalities.

@@ -30,7 +31,7 @@ def test_py_config_inline_pyscript(self):
)
assert self.console.log.lines[-1] == "config name: foobar"

@pytest.mark.skip("ERROR_SCRIPT: works with <py-script> not with <script>")
@pytest.mark.skip("NEXT: works with <py-script> not with <script>")
Copy link
Contributor
@WebReflection WebReflection Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is misleading ... you are accessing document.currentScript._pyodide.promise to grab the config and we landed already an explicit export of the config we pass along ... this needs a rewrite, happy to help if needed, you need to import { config as pyConfig } from 'core.js' and then globalThis.pyConfig = pyConfig and change any code that use internals wrongly to reach the config.

expected = (
"(PY1000): Invalid TOML\n"
"Expected DoubleQuote, Whitespace, or [a-z], [A-Z], "
'[0-9], "-", "_" but end of input found.'
)
assert banner.inner_text() == expected

@pytest.mark.skip("FIXME: emit a warning in case of multiple py-config")
@pytest.mark.skip("NEXT: emit a warning in case of multiple py-config")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -2,7 +2,7 @@

from .support import PyScriptTest, skip_worker

pytest.skip(reason="FIXME: entire stdio should be reviewed", allow_module_level=True)
pytest.skip(reason="NEXT: entire stdio should be reviewed", allow_module_level=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have an issue for this? I am not sure what this is about, thanks!

Copy link
Contributor
@WebReflection WebReflection left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed with @antocuni this MR should move forward so that we can re-enable CI in our PRs and branches. Nothing I've commented is really a blocker so I am going to approve this for the time being, but I'll keep those comments in mind for a possible follow-up MR.

@antocuni
Copy link
Contributor Author

@WebReflection thank you for the approval!
Yes, I plan to open subsequent issues where we can discuss how to deal with the currently skipped tests, so let's postpone the discussion until there

@antocuni antocuni merged commit abfc687 into main Sep 27, 2023
@antocuni antocuni deleted the antocuni/worker-tests branch September 27, 2023 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0