tests: the proof that a frame is in another process spins until one spin shows ticks, 30 s, and waits for the end of the spin instead of spin_ms + 800

This commit is contained in:
feder-cr committed 2026-11-16 01:15:37 +01:00
1 parent dbd490f023
commit 1dc5ea2a3f
3 files changed
+46 -17

No files matched your search

+16 -8
View File
@@ -161,7 +161,7 @@ def launch(binary, humanize=False, **kwargs):
def new_page(browser, **context_kwargs):
context = browser.new_context(**context_kwargs)
page = context.new_page()
page.set_default_timeout(8000)
page.set_default_timeout(30000)
page.set_default_navigation_timeout(30000)
return context, page
@@ -180,13 +180,13 @@ def spin_trial(observer, frame, spin_ms=SPIN_MS):
while (performance.now() < end) { /* spin */ }
window.__spin = [start, Date.now()];
}, 50); }""", spin_ms)
observer.page.wait_for_timeout(spin_ms + 800)
ticks = observer.evaluate("() => { clearInterval(window.__tick); return window.__ticks; }")
# until the frame says its spin is over (a condition with a deadline, not spin_ms + 800 ms): the ticks of the whole spin are in the list by then
window = wait_for_value(observer.page, lambda: frame.evaluate("window.__spin"))
ticks = observer.evaluate("() => { clearInterval(window.__tick); return window.__ticks; }")
return ticks, window
def wait_for_value(page, read, seconds=20.0):
def wait_for_value(page, read, seconds=30.0):
"""``read()`` once it is not None, with the client's events being read; None if it never is."""
end = time.monotonic() + seconds
value = read()
@@ -222,21 +222,29 @@ def other_process_proved(trials):
return any(ticks_inside(ticks, window) >= INSIDE_TICKS for ticks, window in trials)
#: The spins of the proof that two frames share ONE process (``test_fission_identity_parity_e2e.py`` strategy 0): a frame that shares the observer's process shows no
#: tick inside, in any spin (the absence is the proof), so a fixed number of spins is enough to say it, and there is no clock in it.
TRIALS = 6
#: The proof of ANOTHER process is one spin with ticks inside it, and a loaded machine starves the observer for a whole spin now and then: the spins go on until
#: one shows them, within this many seconds (the deadline of the wait; no healthy run comes near it).
PROOF_SECONDS = 30.0
def assert_other_process(observer, frame):
def assert_other_process(observer, frame, seconds=PROOF_SECONDS, clock=time.monotonic):
"""``frame`` runs in a process of its own, not the one of ``observer``: the observer's timer ticks while the frame
spins. Up to ``TRIALS`` spins (the first that shows ticks inside it is enough; a healthy machine needs one)."""
spins. Spins until one shows ticks inside it (a healthy machine needs one), for ``seconds``."""
trials = []
for _ in range(TRIALS):
end = clock() + seconds
while True:
trials.append(spin_trial(observer, frame))
if other_process_proved(trials):
return
if clock() >= end:
break
assert False, (
"the frame at %s shares the process of the observer at %s: in %d spins of %d ms the observer's 10 ms timer never "
"ticked inside a spin (a frame of another process leaves it free; here: ticks inside, ticks in all, spin ms) %s" % (
frame.url, observer.url, TRIALS, SPIN_MS,
frame.url, observer.url, len(trials), SPIN_MS,
[(ticks_inside(t, w), len(t), (w[1] - w[0]) if w else None) for t, w in trials]))
+11 -8
View File
@@ -61,7 +61,7 @@ from urllib.parse import urlsplit
import pytest
sys.path.insert(0, os.path.dirname(__file__))
from _fission_frames import TRIALS, other_process_proved, ticks_inside # noqa: E402
from _fission_frames import PROOF_SECONDS, TRIALS, other_process_proved, ticks_inside # noqa: E402
from _servers import LocalServer # noqa: E402
SEED = 4242
@@ -542,10 +542,10 @@ SPIN_PROBE_JS = """(spin) => new Promise(resolve => {
const ticks = [];
const id = setInterval(() => ticks.push(Date.now()), 10);
const done = (spun) => { clearInterval(id); removeEventListener('message', on); resolve([ticks, spun]); };
const on = e => { if (e.data && e.data.fission_spun) setTimeout(() => done(e.data.fission_spun), 200); };
const on = e => { if (e.data && e.data.fission_spun) done(e.data.fission_spun); };
addEventListener('message', on);
document.getElementById('oop2').contentWindow.postMessage({fission_busy: spin}, '*');
setTimeout(() => done(null), 8000);
setTimeout(() => done(null), 30000);
})"""
@@ -570,11 +570,14 @@ def _run(binary: str, seed: int, strategy: int) -> dict:
# strategy 1: until one spin shows ticks inside it (a healthy machine needs one). strategy 0: all of
# them, and none may show a tick inside (the proof of one process is the absence, every time)
trials = []
for _ in range(TRIALS):
ticks, spun = page.evaluate(SPIN_PROBE_JS, SPIN_MS)
trials.append((ticks, spun))
if strategy == 1 and other_process_proved(trials):
break
if strategy == 1:
# spins until one shows ticks inside it, for 30 s (a loaded machine starves the top for a whole spin now and then)
end = time.monotonic() + PROOF_SECONDS
while not other_process_proved(trials) and time.monotonic() < end:
trials.append(tuple(page.evaluate(SPIN_PROBE_JS, SPIN_MS)))
else:
for _ in range(TRIALS):
trials.append(tuple(page.evaluate(SPIN_PROBE_JS, SPIN_MS)))
finally:
server.shutdown()
return {"results": results, "spin_trials": trials, "port": port}
@@ -256,8 +256,9 @@ def test_a_frame_that_shares_the_process_keeps_the_assertion_red_and_one_that_do
url = "http://localhost:1/grid"
monkeypatch.setattr(ff, "spin_trial", lambda observer, frame: _same_process_trial(2500))
seconds = iter(range(0, 10_000, 7)) # a clock that moves 7 s at each look, so the 30 s of the proof are spent without waiting
with pytest.raises(AssertionError) as red:
ff.assert_other_process(Observer(), Frame())
ff.assert_other_process(Observer(), Frame(), clock=lambda: next(seconds))
assert "shares the process of the observer" in str(red.value) and "(0, " in str(red.value)
calls = []
@@ -268,3 +269,20 @@ def test_a_frame_that_shares_the_process_keeps_the_assertion_red_and_one_that_do
monkeypatch.setattr(ff, "spin_trial", starved_then_healthy)
ff.assert_other_process(Observer(), Frame())
assert len(calls) == 3
# a machine that starves the observer for more spins than the fixed count of the old proof is still waited for, until the deadline
calls.clear()
def starved_for_a_long_time(observer, frame):
calls.append(1)
return _other_process_trial(starved=len(calls) <= 2 * ff.TRIALS)
monkeypatch.setattr(ff, "spin_trial", starved_for_a_long_time)
ff.assert_other_process(Observer(), Frame(), clock=lambda: 0)
assert len(calls) == 2 * ff.TRIALS + 1
def test_the_proof_of_another_process_has_no_fixed_pause_and_no_fixed_count():
import inspect
spin = inspect.getsource(ff.spin_trial)
assert "wait_for_timeout" not in spin, "the spin is waited for as a condition (its end), not for spin_ms + 800 ms"
assert "range(TRIALS)" not in inspect.getsource(ff.assert_other_process), "the proof of another process is waited for until a deadline"