tests: the second window session runs in a thread of its own (no sync client inside another one's loop)
This commit is contained in:
1 parent
a9ec63caed
commit
2b9858570a
3 files changed
+87
-11
No files matched your search
@@ -4,10 +4,18 @@ The sessions are HEADED on purpose. ``headless=True`` gives every session a disp
|
||||
Windows), so two of them never see each other; the focus and occlusion tests need two windows on ONE display, which is what two headed sessions of the
|
||||
same run have (the Linux e2e runs under ``xvfb-run``). Both sessions use the same seed, so both windows are the same declared window and the second one
|
||||
covers the first completely.
|
||||
|
||||
The first session is the one of the test's own thread; the second one lives in a thread of its own (``in_a_thread_of_its_own``, the pattern of
|
||||
``test_popups_reach_the_client_e2e.py``): an open sync client keeps its asyncio loop marked as running in the thread that started it, and a second sync client
|
||||
refuses to start there ("It looks like you are using Playwright Sync API inside the asyncio loop"). A sync client's objects belong to the thread that made
|
||||
them, so everything the test does with the second session is handed to that thread (``run``).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable, Dict, Optional
|
||||
import contextlib
|
||||
import queue
|
||||
import threading
|
||||
from typing import Any, Callable, Dict, Iterator, Optional
|
||||
|
||||
from _waits import Watch, watch
|
||||
|
||||
@@ -31,3 +39,61 @@ def wait_for_condition(page, sample: Callable[[], Any], done: Callable[[Any], An
|
||||
result = watch(sample, done, seconds, lambda: page.wait_for_timeout(100))
|
||||
assert result.ok, "%s; seen: %s" % (what, result.render())
|
||||
return result
|
||||
|
||||
|
||||
class SecondSession:
|
||||
"""A session that lives in a thread of its own: ``run(fn)`` calls ``fn(session)`` in that thread and gives back its result or raises its error."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._jobs: "queue.Queue[Optional[tuple]]" = queue.Queue()
|
||||
|
||||
def run(self, fn: Callable[[Any], Any]) -> Any:
|
||||
box: Dict[str, Any] = {}
|
||||
done = threading.Event()
|
||||
self._jobs.put((fn, box, done))
|
||||
assert done.wait(300), "the second session did not answer within 300 s"
|
||||
if "error" in box:
|
||||
raise box["error"]
|
||||
return box.get("value")
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def in_a_thread_of_its_own(firefox_binary: str, *, extra_prefs: Optional[Dict[str, Any]] = None) -> Iterator[SecondSession]:
|
||||
"""A second headed session on the same display, started and closed in a thread with no asyncio loop; the test keeps the first one in its own thread."""
|
||||
handle = SecondSession()
|
||||
up = threading.Event()
|
||||
failure: Dict[str, BaseException] = {}
|
||||
|
||||
def serve() -> None:
|
||||
try:
|
||||
with launch(firefox_binary, extra_prefs=extra_prefs) as session:
|
||||
up.set()
|
||||
while True:
|
||||
job = handle._jobs.get()
|
||||
if job is None:
|
||||
return
|
||||
fn, box, done = job
|
||||
try:
|
||||
box["value"] = fn(session)
|
||||
except BaseException as exc: # handed back to the test's thread by ``run``
|
||||
box["error"] = exc
|
||||
finally:
|
||||
done.set()
|
||||
except BaseException as exc: # the launch or the close of the session
|
||||
failure["error"] = exc
|
||||
finally:
|
||||
up.set()
|
||||
|
||||
worker = threading.Thread(target=serve, name="second-session", daemon=True)
|
||||
worker.start()
|
||||
assert up.wait(300), "the second session did not start within 300 s"
|
||||
if "error" in failure:
|
||||
raise failure["error"]
|
||||
try:
|
||||
yield handle
|
||||
finally:
|
||||
handle._jobs.put(None)
|
||||
worker.join(120)
|
||||
assert not worker.is_alive(), "the second session did not close within 120 s"
|
||||
if "error" in failure:
|
||||
raise failure["error"]
|
||||
@@ -20,7 +20,7 @@ from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from _window_sessions import launch, wait_for_condition
|
||||
from _window_sessions import in_a_thread_of_its_own, launch, wait_for_condition
|
||||
|
||||
pytestmark = pytest.mark.e2e
|
||||
|
||||
@@ -36,6 +36,13 @@ EVENTS = "() => window.__events"
|
||||
TWO_FRAMES = "() => new Promise(done => requestAnimationFrame(() => requestAnimationFrame(done)))"
|
||||
|
||||
|
||||
def takes_the_focus(b):
|
||||
"""Runs in the second session's own thread: opens a window and waits until its page has the focus."""
|
||||
taker = b.new_page()
|
||||
taker.goto("about:blank")
|
||||
wait_for_condition(taker, lambda: taker.evaluate(HAS_FOCUS), lambda focused: focused is True, "the second session's page never took the focus")
|
||||
|
||||
|
||||
def another_session_takes_the_display(firefox_binary, keeps, with_own_page=False):
|
||||
"""What A's page reads once B's window has the focus, and (``with_own_page``) once A has opened a page of its own."""
|
||||
extra = None if keeps else {PREF: False}
|
||||
@@ -44,10 +51,8 @@ def another_session_takes_the_display(firefox_binary, keeps, with_own_page=False
|
||||
first.goto("about:blank")
|
||||
first.evaluate(RECORD)
|
||||
wait_for_condition(first, lambda: first.evaluate(HAS_FOCUS), lambda focused: focused is True, "the page never had the focus of its own window")
|
||||
with launch(firefox_binary) as b:
|
||||
taker = b.new_page()
|
||||
taker.goto("about:blank")
|
||||
wait_for_condition(taker, lambda: taker.evaluate(HAS_FOCUS), lambda focused: focused is True, "the second session's page never took the focus")
|
||||
with in_a_thread_of_its_own(firefox_binary) as b:
|
||||
b.run(takes_the_focus)
|
||||
if keeps:
|
||||
first.evaluate(TWO_FRAMES)
|
||||
else:
|
||||
|
||||
@@ -21,7 +21,7 @@ from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from _window_sessions import launch, wait_for_condition
|
||||
from _window_sessions import in_a_thread_of_its_own, launch, wait_for_condition
|
||||
|
||||
pytestmark = pytest.mark.e2e
|
||||
|
||||
@@ -40,18 +40,23 @@ CHANGES = "() => window.__visibility"
|
||||
TWO_FRAMES = "() => new Promise(done => requestAnimationFrame(() => requestAnimationFrame(done)))"
|
||||
|
||||
|
||||
def opens_the_covering_window(second):
|
||||
"""Runs in the second session's own thread: opens a window on the display and waits for two painted frames."""
|
||||
cover = second.new_page()
|
||||
cover.goto("about:blank")
|
||||
cover.evaluate(TWO_FRAMES)
|
||||
|
||||
|
||||
def second_window_over_the_first(firefox_binary, tracking):
|
||||
"""(state, changes) of the first session's page after a second session's window is up over it.
|
||||
|
||||
``tracking`` is None for the product as it launches, or the value both tracking prefs are given."""
|
||||
extra = None if tracking is None else {name: tracking for name in TRACKING_PREFS}
|
||||
with launch(firefox_binary, extra_prefs=extra) as first, launch(firefox_binary) as second:
|
||||
with launch(firefox_binary, extra_prefs=extra) as first, in_a_thread_of_its_own(firefox_binary) as second:
|
||||
covered = first.new_page()
|
||||
covered.goto("about:blank")
|
||||
assert covered.evaluate(WATCH) == "visible", "the page was not visible before anything covered it"
|
||||
cover = second.new_page()
|
||||
cover.goto("about:blank")
|
||||
cover.evaluate(TWO_FRAMES)
|
||||
second.run(opens_the_covering_window)
|
||||
if tracking:
|
||||
wait_for_condition(covered, lambda: covered.evaluate(STATE), lambda state: state == "hidden",
|
||||
"a window that another one covers never turned hidden with occlusion tracking enabled")
|
||||
|
||||
Reference in new issue
Block a user