tests: the leak census imports nothing when no starter could have been built, so it runs in jobs without invisible_core

This commit is contained in:
feder-cr committed 2026-11-15 00:26:20 +01:00
1 parent 91e69710eb
commit 163315c114
2 files changed
+41 -3

No files matched your search

+11 -3
View File
@@ -7,20 +7,28 @@ left 635). So the number of live starters, counted when the last test of a modul
Module level and not per test: a module-scoped browser (the e2e ones) is legitimately open between its tests and closed at the end.
"""
import sys
import time
import pytest
STARTER_NAME = "invpw-thread-starter"
_THREADS_MODULE = "invisible_playwright._juggler._threads"
# `stop` is asynchronous: it queues a None and the thread leaves on its next turn. A moment, not a wait for a leak that will not leave.
_JOIN_SECONDS = 5.0
def starters_alive():
"""The starters built and not ended, counted from their construction: `ThreadStarter` begins its thread without waiting for it, so
one that has not run yet is in no `threading.enumerate()` (a census of live threads missed 2 leaks on a Linux runner)."""
from invisible_playwright._juggler._threads import ThreadStarter
return ThreadStarter.open_starters()
one that has not run yet is in no `threading.enumerate()` (a census of live threads missed 2 leaks on a Linux runner).
NO IMPORT HERE. A module that never loaded `_threads` cannot have built a starter, so the answer is empty without importing anything:
the census runs from conftest in every job, including ones that install pytest alone and have no `invisible_core` (the `version`
job failed on `invisible_playwright._pin` when this function imported the package)."""
threads = sys.modules.get(_THREADS_MODULE)
if threads is None:
return []
return threads.ThreadStarter.open_starters()
def wait_until_back_to(before, seconds=_JOIN_SECONDS):
+30
View File
@@ -0,0 +1,30 @@
"""The leak census runs from conftest in every job, including the ones that install pytest alone. There it must import nothing of the
package: the `version` job of the release pull request failed with `No module named 'invisible_core'` when the census imported
`invisible_playwright._juggler._threads` (which pulls `invisible_playwright._pin`)."""
import subprocess
import sys
import textwrap
from pathlib import Path
TESTS = Path(__file__).resolve().parent
def test_a_census_in_a_process_that_never_loaded_the_package_imports_nothing_and_counts_nothing():
script = textwrap.dedent("""
import sys
sys.path.insert(0, %r)
class _NoPackage:
def find_spec(self, name, path=None, target=None):
if name == "invisible_core" or name.startswith(("invisible_core.", "invisible_playwright")):
raise ImportError("the census imported %%s" %% name)
return None
sys.meta_path.insert(0, _NoPackage())
import _browser_census as census
assert census.starters_alive() == [], census.starters_alive()
assert census.wait_until_back_to(0, seconds=0) == 0
loaded = sorted(m for m in sys.modules if m.startswith(("invisible_core", "invisible_playwright")))
assert not loaded, loaded
print("OK")
""") % str(TESTS)
done = subprocess.run([sys.executable, "-I", "-c", script], capture_output=True, text=True, timeout=60)
assert done.returncode == 0 and done.stdout.strip() == "OK", done.stdout + done.stderr