Files
Vito a1f744708e fix: resolve rgthree Any Switch, show shared roles, choose ambiguous prompts in Inspect (#660)
* test(inspect): use a neutral unknown class in the pass-through fixtures

The #381 fixtures used rgthree's Any Switch as their example of an unknown
node. The table is about to describe that class (#657), so the stand-in
becomes PMTest Unknown Multi and any_switch_graph becomes
unknown_multi_graph. Resolver results are unchanged.

* feat(inspect): resolve rgthree Any Switch by its first proven input (#657)

rgthree's Any Switch outputs the first any_* input that is not None. The
inspection resolver treated it as an unknown node, so the model-compare
sweep (PromptManagerText into any_01, the same text joined after a LoRA
trigger into any_02) was ambiguous in Inspect.

The table gains a first-present selector strategy and lists the switch
under it. The resolver takes the first participating input when it is a
literal or its traced path proves a value (every node is a PromptManager,
code-traced, default-adapter or encoders/strings/conditioning/composites
class). Otherwise each input is one choice, unless all of them trace to
one text; limits and upstream divergences propagate, and no PromptManager
path is preferred. A missing or null input does not take part. The
browser resolver and its table compiler follow the same rules, with the
same strict entry check as the server.

Only graphs holding the class change their class projection, so only
their pending inspection items move; INSPECTION_RESOLVER_VERSION stays 2.

* fix(inspect): list a repeated Any Switch outcome once (#657)

A switch with three or more inputs can offer one text through two of
them (a, b, a). Each input was its own candidate, so one text read as a
choice between two, and two equal resolved candidates beside a failed
one would count as reviewable. Like an unknown node passing several
texts, a first-present divergence now collapses equal outcomes. The
browser resolver does the same. The table compiler test also checks the
128-character prefix bound in code points, as Python counts it.

* feat(inspect): show a role every candidate shares (#658)

An ambiguous file used to withhold both roles, even when every candidate
had the same negative. Inspect now judges each role on its own: a role is
shown resolved when every candidate is resolved with the same text,
stored recipe and left-out parts for it, using the first candidate's
text and source. A role that differs, or any unresolved candidate, keeps
the ambiguous line, and resolution.state stays ambiguous, so Send pair,
ingestion and Needs review behave as before. The browser-local resolver
applies the same rule; the footer Send to ComfyUI still needs both roles.

A multistage detailer run whose stages share one negative (fixture f19)
now shows that negative.

* feat(monitor): ask the ladder for PromptManager runs nothing else links

A PromptManager output whose metadata and tracked run cannot name its
prompt now takes the ladder path foreign outputs use: decided at settle
under the monitor policy, or after its run is sealed through the ladder
wait. Wait entries carry their run kind; the kind selects the settings
gate (SAVE_FOREIGN_RUNS for foreign entries, MONITORING_ENABLED for
PromptManager ones) and the marks of a created prompt. Keyless images,
placeholder batch entries and unreadable metadata never enter the wait.
Every final unlinked outcome hands the file to the review recorder.

Refs #659

* test(monitor): pin the final unlinked outcomes that reach Needs review

The sweep, joined-text and shared-prefix graphs move to a shared fixture
module. New tests pin when the monitor hands a file to its review
recorder: after the sealed re-check, a settle lookup that found the run's
record, a file outside ComfyUI's output folder and the keyless
re-check; never before the run is sealed, and never for a linked file.
A PromptManager output waiting for its run now logs that at INFO.

Refs #659

* feat(review): offer inspection choices for default-unresolved files

Decision.default_unresolved marks a file the default profile itself
calls unresolved (a blank positive does not count). With inspection on,
record_ambiguous_file now sends such a captured file with a prompt graph
straight to the inspection-profile branch: a reviewable ambiguous or
saver-anchored result is recorded, anything else is untraced. Apply is
unchanged; it re-resolves with the item's recorded profile.

Refs #659

* feat(review): one review dispatch and a review worker for monitor outputs

review_dispatch holds the scan's per-file Needs review decision and is
now also used by the monitor. The scan replaces its inline dispatch with
it (and offers default-unresolved files their inspection choices).

record_review_file runs the scan's per-file steps for one file, read
only: the scan-policy ladder with the sealed history and the recorded-run
check. A file the scan would link is skipped; otherwise the dispatch's
record outcome is the answer. The monitor queues paths through
queue_review_file onto one daemon worker (pm-review-recorder) with a
bounded queue that coalesces by path and a bounded deferred set it
replays once no job blocker remains. The package teardown stops it
right after the monitor and reports it as part review once started.

Refs #659

* docs(testing): the monitor queues what the scan would (#659)

Rewrite the Needs review paragraph for the 2026-11-19 decision: the
final-outcome points, the review worker and its bounds, the deferred
replay and the default-unresolved rule. Document the PromptManager-run
ladder path, the review step of the package teardown and the proven-value
allowlist every future semantics table entry must keep.

Refs #659

* fix(review): lock the untraced memory the scan and review worker share

Refs #659

* feat(inspect): name the file's Needs review item in the inspection responses

Inspect and the run-history current-file inspection gain a review field:
{"state": "pending", "item_id": ...} for the newest pending item at the
stat the inspection took, {"state": "decided"} once its item was decided,
and null otherwise. It is read only when a role is ambiguous and the file
is unlinked, by locator and stat (review_item_at), so Inspect never hashes
the file. A LoRA root, a failed read and any malformed value give null.

Refs #659

* feat(review): a single-item mode for the review controller and a shared option list

createReviewController({api, itemId}) holds one named item: it never reads
the queue, loads that item's detail, ends in phase decided (decided: true)
after a recorded decision or for an item already decided, and re-reads the
detail after a review-conflict, review-missing or restore refusal. The queue
mode is unchanged.

The Sampler context option list moves into renderReviewOptions
(review-options.mjs, re-exported by review.mjs), so Needs review and the
Inspect picker render the same options without Inspect importing the Needs
review page.

Refs #659

* feat(inspect): choose the prompt of an ambiguous file in Inspect

When the inspection names the file's pending Needs review item, the
inspector shows a Choose prompt section after its prompt blocks (Images
panel, viewer details, recorded run current file). It reads the item's
detail through the review controller's single-item mode: one Sampler
context option per candidate (unusable ones disabled with their reason),
the chosen option's prompt blocks with Copy and Send, Use this prompt and
Leave unresolved over the Needs review apply flow. A recorded decision
shows Needs review's toast and re-reads the inspection, so the file shows
its link; an item with nothing to choose offers Open in Needs review.

Refs #659

* fix(inspect): read the history review item only when Inspect asks

The run-history reader locates the file under the scan roots inside the
read, so an unambiguous or linked file costs no realpath of the output
folders. The docstring names the linked-subfolder limit, and the newest
pending item test pins the rowid tie-break.

Refs #659

* fix(inspect): end Choose prompt on another read and on a decision made elsewhere

The inspector disposes the section when it reads another file, fails or
defers, so a decision in flight never re-reads the next file. A decided
item hides the section and re-reads the inspection once, also when it was
decided in another tab or in Needs review.

Refs #659

* test(inspect): the rooted inspect shape gains the review facet

Refs #659

* docs(testing): first-present candidates and per-role results (#657, #658)

* test(review): keep the review cases ambiguous now that Any Switch resolves

Part 1 resolves rgthree's Any Switch, so the sweep fixture links. The
review and monitor tests that need a default-unresolved,
inspection-ambiguous file now use ambiguous_sweep_graph(): Part A's
any_switch_sweep_graph() with its switch class replaced by the
never-listed PMTest Unknown Multi (one sweep graph instead of two). A new
test pins that the real sweep links at settle from the inspector.

Refs #657 #659

* fix(review): start the review worker again after an unexpected error

* fix(inspect): pass the inspection refresh to Choose prompt by reference

* feat(review): queue an ambiguous file with one readable choice (#657)

An ambiguous result is reviewable when at least one candidate carries a
resolved prompt (MIN_CHOOSABLE = 1). The sweep with an LLM enhancer on
Any Switch any_01 now records an item, by the scan and the monitor, with
the run-time branch listed beside the readable one. A file with no
choosable candidate still records nothing.

* feat(review): collapse too many candidates into the most used prompts (#657)

More than 16 distinct candidates no longer make a limited item without
choices. The choosable ones are grouped by their positive and negative
texts, ordered by group size then first appearance, and the first 16
are kept, one real candidate each. The evidence names
collapsed: {shown, total, paths} and the item detail returns it.
Limited stays for evidence that cannot be projected or is over 8 MiB.

REVIEW_EVIDENCE_VERSION is folded into every item version (default items
now carry RESOLVER_VERSION + 1, inspection items fold it into their
seed), so the next scan supersedes pending, undecided items of the old
shape; decided items are kept. RESOLVER_VERSION, the ingestion's
provenance version, is unchanged.

* feat(inspect): record the Needs review item when Inspect finds none (#657)

When Inspect asks for the review facet (an ambiguous role, an unlinked
file under an output root) and no item exists at the inspected stat, the
images and History current-file inspections run record_review_file, the
review worker's own per-file path, in the executor and read the item
again. A file the scan would link or that offers nothing to choose
records nothing; a deferred or failed recording leaves review null and
a failure is logged, never failing the response.

* docs(testing): one readable choice, collapsed candidates, Inspect records (#657)

* feat(review): say when a file's prompts were shortened to the most used (#657)

The item detail now passes the stored evidence's optional `collapsed`
counts ({shown, total, paths}) through to the page, null when absent,
malformed, or when the detail was capped for size. Needs review and
Inspect's Choose prompt show one line under Sampler context:
"Showing the n most used of t prompts in this file."

* feat(review): list the image's own prompts first in Link to a prompt (#657)

With an empty search the dialog now lists the item's choosable
candidates under "From this image" (positive / negative, one per prompt
pair), then the Library's prompts under "Library". Choosing one of the
image's prompts and pressing Link image applies that candidate by its
hash, the same decision as Apply association (new
controller.applyCandidate); no negative is asked for, the candidate
carries its own. Typing searches the Library only. Items with no
choosable candidate show the dialog as before. A refusal stays in the
dialog with the server's sentence.

* feat(review): load more of the queue on scroll (#657)

The Needs review continuation now works like the Images page: an
IntersectionObserver (250 px ahead) loads the next page when the end of
the queue scrolls into view, and the line reads "40 of 45 images · more
load as you scroll" without a button. Nothing loads behind an open
dialog or the viewer; when one closes, focus returning to the page
checks again. A failed page shows the failure line and a Retry button.
Without IntersectionObserver the Load more images button stays.

* fix(review): ask the inspection profile when the default choice is partial (#657)

With one readable choice enough, a default result with one choice beside
unresolved branches was recorded as is, so the inspection profile, which
may read those branches (a Qwen image-edit sampler), was never asked and a
rescan replaced older inspection items with the poorer default ones. Such a
partial result now yields to the inspection result when that offers more
choosable candidates.

* fix(review): collapse only too many prompts, and apply any offered one (#657)

Collapsing triggered on more than 16 distinct candidates, sampler ids
included, so twenty samplers sharing three texts lost their run-time
branches behind a collapsed note. Now only more than 16 choosable texts
collapse; otherwise one candidate per text is kept and the branches
without a prompt fill the remaining places, as an ordinary item.

Apply's still-offered check compared the chosen hash with a fresh
collapse, so a reorder that changed the representatives refused Apply.
It now checks every projected, de-duplicated candidate hash
(candidate_hashes).

* fix(inspect): keep Inspect's recording quiet, bounded and current (#657)

A busy capture store (the worker copying a fresh output) is an expected
outcome: Inspect logs it at debug level and reads the item once more,
also after a failed recording. A file Inspect found unrecordable (the
scan would link it, or it offers nothing to choose) is remembered by its
locator, stat and review versions, at most 4,096 files, so a repeated
Inspect does not copy it again. A pending item of an older item version
is recorded again, so the current record supersedes it.

* docs(testing): partial default choices, bounded candidates, Inspect recording (#657)

* fix(review): Link to a prompt drops an image choice it no longer shows (#657)

Typing in the search box hid the "From this image" list but kept its
chosen prompt, so Link image applied a candidate the reviewer could no
longer see. Typing now drops that choice; Link image waits for a Library
prompt and sends a Library link. Emptying the box lists the image's
prompts again, none chosen.

When the item's detail is read again while the dialog is open, the list
follows what it offers now; a chosen prompt no longer offered is dropped
with "This prompt is no longer offered for this file. Choose again."
applyCandidate says the same when the hash cannot be chosen, and an
unanswered apply from the dialog asks to press Link image again.

* fix(review): keep the panel in place when Info brings it into view at one column (#657)

At one column, Info scrolls the panel into view past the queue, which
could bring the continuation into view and load the next page, pushing
the panel away. As on the Images page, loading now waits for the
reader's next wheel, touch move or key press.

* docs(review): say why the review worker's recorder is a lambda

* test(review): open the dialog of the D5 dialog test beside the queue (#657)

At one column (touch) the review panel follows the queue just above the
continuation, so scrolling Link to a prompt… into view brings the
continuation within the observer's 250 px margin. The next page then
loads before the dialog opens whenever the observer's callback runs
first (CI shard 4/4, touch), and the test found no continuation to
scroll to. That load is the page working as designed: the reader has
reached the end of the queue.

The test is about loading behind an open dialog, so it now runs at two
columns, where Link sits in the sticky panel beside the queue, and it
checks that the continuation is out of range before the dialog opens.
2026-11-21 03:07:38 +00:00

1112 lines
43 KiB
Python

"""Importing the extension package wires the queue hook to the shared database.
The package is loaded the way ComfyUI loads it (``__init__`` with relative
imports) with ComfyUI's ``server`` and ``folder_paths`` modules stubbed.
"""
import asyncio
import atexit
import contextlib
import importlib.util
import io
import logging
import json
import os
import sys
import tempfile
import threading
import types
import unittest
import unittest.mock as mock
from pathlib import Path
from aiohttp import web
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from utils.logging_config import get_logger_manager # noqa: E402
get_logger_manager()
try:
from tests.open_handles import open_handles_under # noqa: E402
except ImportError: # discovered with tests/ as the top-level directory
from open_handles import open_handles_under # noqa: E402
class FakePromptQueue:
def __init__(self):
self.results = []
def get(self, timeout=None):
return self.results.pop(0) if self.results else None
def fake_server_module(app=None, queue_task_done=False):
"""A ``server`` module whose PromptServer.instance accepts routes and hooks.
``app`` replaces the default app (one without aiohttp hooks);
``queue_task_done`` gives the queue a ``task_done``, so history registers.
"""
queue = FakePromptQueue()
if queue_task_done:
queue.task_done = lambda *a, **k: None
instance = types.SimpleNamespace(
routes=web.RouteTableDef(),
prompt_queue=queue,
handlers=[],
app=app if app is not None else types.SimpleNamespace(middlewares=[]),
)
instance.add_on_prompt_handler = instance.handlers.append
module = types.ModuleType("server")
module.PromptServer = type("PromptServer", (), {"instance": instance})
return module
def fake_folder_paths_module(output_dir):
module = types.ModuleType("folder_paths")
module.base_path = output_dir
module.models_dir = os.path.join(output_dir, "models")
module.get_output_directory = lambda: output_dir
module.get_folder_paths = lambda name: []
return module
def _import_package(name, db_path, output_dir, server=None):
"""Import ROOT/__init__.py as package ``name`` with ComfyUI stubbed.
Returns (package, server instance) to the scoped fixture.
"""
saved = {key: sys.modules.get(key) for key in ("server", "folder_paths")}
server = server or fake_server_module()
sys.modules["server"] = server
sys.modules["folder_paths"] = fake_folder_paths_module(output_dir)
try:
from py.config import PromptManagerConfig
config_path = os.path.join(output_dir, "promptmanager-test-config.json")
with open(config_path, "w", encoding="utf-8") as config:
json.dump({"database": {"default_path": db_path}}, config)
with (
mock.patch.object(PromptManagerConfig, "DEFAULT_DB_PATH", db_path),
mock.patch.dict(os.environ, {"PROMPT_MANAGER_CONFIG_PATH": config_path}),
):
spec = importlib.util.spec_from_file_location(
name,
os.path.join(ROOT, "__init__.py"),
submodule_search_locations=[ROOT],
)
package = importlib.util.module_from_spec(spec)
sys.modules[name] = package
try:
spec.loader.exec_module(package)
except BaseException:
cleanup_package(package, require_stopped=False)
raise
finally:
for key, module in saved.items():
if module is None:
sys.modules.pop(key, None)
else:
sys.modules[key] = module
return package, server.PromptServer.instance
def cleanup_package(package, require_stopped=True):
"""Stop fixture-owned workers before releasing its other resources.
The package's own teardown runs first (the reload case of #301); with
``require_stopped`` every part it reports must have stopped. The direct
stops after it also cover an import that failed before the teardown
existed, and a test that made a teardown step fail on purpose.
"""
teardown = getattr(package, "_stop_background_work", None)
if callable(teardown):
result = teardown()
if require_stopped:
assert all(result.values()), f"package teardown left work running: {result}"
monitor = getattr(package, "_global_image_monitor", None)
if monitor is not None:
assert monitor.stop_monitoring(), "package image monitor still running"
tracker = getattr(package, "_global_prompt_tracker", None)
if tracker is not None:
assert tracker.shutdown(), "package prompt cleanup thread still running"
registration = getattr(package, "_history_registration", None)
if registration is not None:
assert registration.dispose(3), "package history worker leaked"
close_every_database()
# The package's exit actions outlive it otherwise: at interpreter exit
# they would import from a package no longer in sys.modules (#580).
exit_signal = getattr(package, "_exit_signal", None)
if exit_signal is not None:
exit_signal.reset()
sys.modules.pop(package.__name__, None)
@contextlib.contextmanager
def load_package(name, db_path, output_dir, server=None, require_stopped=True):
"""Dispose package workers before closing its isolated logging handlers."""
root_logger = logging.getLogger("prompt_manager")
loggers = [root_logger] + [
logger
for key, logger in list(logging.Logger.manager.loggerDict.items())
if key.startswith("prompt_manager.") and isinstance(logger, logging.Logger)
]
previous = {
logger: (
logger.handlers[:],
logger.level,
logger.propagate,
logger.disabled,
logger.filters[:],
)
for logger in loggers
}
prior_handlers = {handler for state in previous.values() for handler in state[0]}
root_logger.handlers = []
try:
package, instance = _import_package(name, db_path, output_dir, server)
# A previously imported package may reuse its existing logger singleton.
if not root_logger.handlers:
root_logger.handlers = previous[root_logger][0][:]
try:
yield package, instance
finally:
cleanup_package(package, require_stopped=require_stopped)
finally:
current_loggers = [root_logger] + [
logger
for key, logger in list(logging.Logger.manager.loggerDict.items())
if key.startswith("prompt_manager.") and isinstance(logger, logging.Logger)
]
created_handlers = {
handler
for logger in current_loggers
for handler in logger.handlers
if handler not in prior_handlers
}
for logger in current_loggers:
(
logger.handlers,
level,
logger.propagate,
logger.disabled,
logger.filters,
) = previous.get(logger, ([], logging.NOTSET, True, False, []))
logger.setLevel(level)
for handler in created_handlers:
handler.close()
def close_every_database():
"""Close connections of every PromptModel registry in the process.
A package loaded under its own name imports its own copy of
database.models, whose PromptModel class keeps its own registry.
"""
for name, module in list(sys.modules.items()):
if name.endswith("database.models") and hasattr(module, "PromptModel"):
module.PromptModel.close_all_instances()
class PackageTestCase(unittest.TestCase):
package_name = "prompt_manager_pkg"
@classmethod
def setUpClass(cls):
cls.tmp = tempfile.TemporaryDirectory()
cls.addClassCleanup(cls.tmp.cleanup)
cls.addClassCleanup(cls.assert_no_open_handles)
cls.output_dir = os.path.join(cls.tmp.name, "output")
os.makedirs(cls.output_dir)
cls.db_path = cls.make_db_path(cls.tmp.name)
package_context = contextlib.ExitStack()
cls.addClassCleanup(package_context.close)
cls.package, cls.server = package_context.enter_context(
load_package(
cls.package_name, cls.db_path, cls.output_dir, server=cls.make_server()
)
)
@classmethod
def make_db_path(cls, tmp):
return os.path.join(tmp, "prompts.db")
@classmethod
def make_server(cls):
return fake_server_module()
@classmethod
def assert_no_open_handles(cls):
hits = open_handles_under(cls.tmp.name)
assert not hits, f"files still open before temp dir cleanup: {hits}"
class TestPackageWiring(PackageTestCase):
def test_nodes_are_registered(self):
self.assertEqual(
sorted(self.package.NODE_CLASS_MAPPINGS),
[
"PromptManager",
"PromptManagerFocused",
"PromptManagerPair",
"PromptManagerSplit",
"PromptManagerText",
"PromptSearchList",
],
)
def test_split_display_name_and_execution_contract_are_registered(self):
node = self.package.NODE_CLASS_MAPPINGS["PromptManagerSplit"]
self.assertEqual(
self.package.NODE_DISPLAY_NAME_MAPPINGS["PromptManagerSplit"],
"Prompt Manager (Split)",
)
focused = self.package.NODE_CLASS_MAPPINGS["PromptManagerFocused"]
self.assertTrue(issubclass(node, focused))
self.assertEqual(node.INPUT_TYPES(), focused.INPUT_TYPES())
def test_db_factory_returns_the_shared_database(self):
self.assertIsNotNone(self.package._global_db)
self.assertIs(self.package._db_factory(), self.package._global_db)
self.assertEqual(
os.path.normcase(os.path.realpath(self.package._global_db.model.db_path)),
os.path.normcase(os.path.realpath(self.db_path)),
)
def test_db_factory_raises_while_the_database_is_unavailable(self):
with mock.patch.object(self.package, "_global_db", None):
with self.assertRaisesRegex(RuntimeError, "database unavailable"):
self.package._db_factory()
def test_queue_hook_is_registered_and_the_queue_get_is_wrapped(self):
self.assertEqual(len(self.server.handlers), 1)
self.assertTrue(
getattr(self.server.prompt_queue.get, "_prompt_manager_dequeue_hook", False)
)
def test_image_monitor_watches_the_stubbed_output_directory(self):
status = self.package._global_image_monitor.get_status()
self.assertTrue(status["running"])
self.assertEqual(
[
os.path.normcase(os.path.realpath(d))
for d in status["monitored_directories"]
],
[os.path.normcase(os.path.realpath(self.output_dir))],
)
class TestPackageWithoutDatabase(PackageTestCase):
"""The database failing to initialise must not break the queue hook."""
package_name = "prompt_manager_pkg_nodb"
@classmethod
def make_db_path(cls, tmp):
blocker = os.path.join(tmp, "blocker")
with open(blocker, "w", encoding="utf-8") as f:
f.write("not a directory")
return os.path.join(blocker, "prompts.db") # parent is a file: init fails
def test_global_db_is_defined_but_unset(self):
self.assertIsNone(self.package._global_db)
with self.assertRaisesRegex(RuntimeError, "database unavailable"):
self.package._db_factory()
def test_queue_hook_still_registers_and_survives_a_dequeue(self):
self.assertEqual(len(self.server.handlers), 1)
request = self.server.handlers[0](
{"prompt": {"1": {"class_type": "PromptManager", "inputs": {"text": "x"}}}}
)
self.server.prompt_queue.results.append(
((0, request["prompt_id"], request["prompt"], {}, [], False), 1)
)
self.assertIsNotNone(self.server.prompt_queue.get())
class TestVersion(PackageTestCase):
def test_version_comes_from_pyproject(self):
import re
with open(os.path.join(ROOT, "pyproject.toml"), encoding="utf-8") as f:
expected = re.search(r'version\s*=\s*["\']([^"\']+)["\']', f.read())
self.assertEqual(self.package.get_version(), expected.group(1))
def test_unreadable_pyproject_gives_unknown(self):
with mock.patch.object(self.package, "Path", side_effect=OSError("denied")):
self.assertEqual(self.package.get_version(), "unknown")
missing = mock.Mock()
missing.parent.__truediv__ = lambda self_, name: mock.Mock(exists=lambda: False)
with mock.patch.object(self.package, "Path", return_value=missing):
self.assertEqual(self.package.get_version(), "unknown")
class TestPackageWhenHookRegistrationFails(PackageTestCase):
"""A server that rejects the hook must not break the rest of the package."""
package_name = "prompt_manager_pkg_nohook"
@classmethod
def make_server(cls):
module = fake_server_module()
instance = module.PromptServer.instance
def reject(handler):
raise RuntimeError("hooks unsupported")
instance.add_on_prompt_handler = reject
return module
def test_nodes_and_monitoring_still_come_up(self):
self.assertIn("PromptManager", self.package.NODE_CLASS_MAPPINGS)
self.assertIsNotNone(self.package._global_db)
self.assertTrue(self.package._global_image_monitor.is_monitoring)
self.assertEqual(self.server.handlers, [])
if __name__ == "__main__":
unittest.main()
class TestConsoleBanner(unittest.TestCase):
"""Loading the node must not depend on the console being able to print emoji."""
def test_package_loads_with_a_cp1252_console(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.addCleanup(close_every_database)
output_dir = os.path.join(tmp.name, "output")
os.makedirs(output_dir)
name = "prompt_manager_pkg_cp1252"
stream = io.TextIOWrapper(io.BytesIO(), encoding="cp1252", errors="strict")
package_context = contextlib.ExitStack()
self.addCleanup(package_context.close)
with contextlib.redirect_stdout(stream):
package, _ = package_context.enter_context(
load_package(name, os.path.join(tmp.name, "p.db"), output_dir)
)
self.addCleanup(sys.modules.pop, name, None)
monitor = getattr(package, "_global_image_monitor", None)
if monitor is not None:
self.addCleanup(monitor.stop_monitoring)
stream.flush()
banner = stream.buffer.getvalue().decode("cp1252")
lines = [
line
for line in banner.splitlines()
if "PromptManager" in line or "loaded" in line
]
self.assertEqual(len(lines), 1, banner)
self.assertRegex(
lines[0], r"\[ComfyUI-PromptManager\] Version:.* \S+ - \d+ tools loaded$"
)
self.assertNotIn("Loaded:", banner)
class TestFixtureExitActions(unittest.TestCase):
"""A disposed fixture package runs nothing at interpreter exit (#580)."""
def test_a_disposed_package_leaves_no_exit_action(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.addCleanup(close_every_database)
output_dir = os.path.join(tmp.name, "output")
os.makedirs(output_dir)
name = "prompt_manager_pkg_exit_actions"
self.addCleanup(sys.modules.pop, name, None)
with contextlib.redirect_stdout(io.StringIO()):
with load_package(name, os.path.join(tmp.name, "p.db"), output_dir) as (
package,
_,
):
signal = package._exit_signal
self.assertTrue(signal._on_exit, "the package installs exit actions")
self.assertEqual(signal._on_exit, [])
class PriorFileHandler(logging.FileHandler):
"""A caller-owned sink that stays closed once its owner closes it.
``FileHandler.emit`` reopens the file when ``stream`` is None, so a record
that selected this handler before ``close()`` would reopen prior.log after
it, and Windows then refuses the temp directory cleanup (WinError 32).
``Handler.handle`` and ``FileHandler.close`` hold the same handler lock, so
checking ``_closed`` in ``emit`` is race free (as in the product's
``_OwnedRotatingFileHandler``).
"""
def emit(self, record):
if not self._closed:
super().emit(record)
class TestPackageLoggerIsolation(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
# Runs after restore_logging, before the cleanup Windows refuses while
# prior.log is open; /proc makes Linux fail the same way.
self.addCleanup(self.assert_tmp_has_no_open_handles)
self.logger = logging.getLogger("prompt_manager")
self.previous = (
self.logger.handlers[:],
self.logger.level,
self.logger.propagate,
self.logger.disabled,
)
self.addCleanup(self.restore_logging)
self.path = os.path.join(self.tmp.name, "prior.log")
self.handler = PriorFileHandler(self.path)
self.logger.handlers = [self.handler]
self.logger.setLevel(logging.DEBUG)
self.logger.propagate = True
self.logger.disabled = True
self.child = logging.getLogger("prompt_manager.database")
self.child_previous = self.child.level
self.addCleanup(self.child.setLevel, self.child_previous)
self.child.setLevel(logging.ERROR)
self.saved_modules = {
key: sys.modules.get(key) for key in ("server", "folder_paths")
}
self.saved_config = os.environ.get("PROMPT_MANAGER_CONFIG_PATH")
self.server = fake_server_module()
self.server.PromptServer.instance.prompt_queue.task_done = lambda *a, **k: None
def restore_logging(self):
# Detach before closing, so new records stop selecting these handlers.
retired = [h for h in self.logger.handlers if h not in self.previous[0]]
(
self.logger.handlers,
self.logger.level,
self.logger.propagate,
self.logger.disabled,
) = self.previous
for handler in retired:
handler.close()
self.handler.close()
def assert_tmp_has_no_open_handles(self):
self.assertEqual(open_handles_under(self.tmp.name), [])
def assert_logging_restored(self, handlers, registration):
for key, module in self.saved_modules.items():
self.assertIs(sys.modules.get(key), module)
self.assertEqual(
os.environ.get("PROMPT_MANAGER_CONFIG_PATH"), self.saved_config
)
self.assertEqual(self.logger.handlers, [self.handler])
self.assertEqual(self.logger.level, logging.DEBUG)
self.assertTrue(self.logger.propagate)
self.assertTrue(self.logger.disabled)
self.assertEqual(self.child.level, logging.ERROR)
self.assertFalse(self.handler.stream.closed)
self.assertTrue(all(handler.stream is None for handler in handlers))
self.assertFalse(registration.writer.status().alive)
self.logger.disabled = False
self.logger.warning("prior logging still works")
self.logger.disabled = True
self.handler.flush()
with open(self.path, encoding="utf-8") as log:
self.assertIn("prior logging still works", log.read())
def test_package_scope_closes_its_handlers_after_worker_disposal(self):
with contextlib.redirect_stdout(io.StringIO()):
with load_package(
"prompt_manager_pkg_logger_scope",
os.path.join(self.tmp.name, "p.db"),
self.tmp.name,
self.server,
) as (package, _):
handlers = [
h
for h in self.logger.handlers
if isinstance(h, logging.FileHandler)
]
self.assertTrue(handlers)
registration = package._history_registration
original_dispose = registration.dispose
open_at_disposal = []
def dispose(timeout=2):
open_at_disposal.append(all(h.stream is not None for h in handlers))
return original_dispose(timeout)
registration.dispose = dispose
self.assertEqual(open_at_disposal, [True])
self.assert_logging_restored(handlers, registration)
def test_record_in_flight_at_restore_cannot_reopen_prior_log(self):
"""A record that selected prior.log before restore never reopens it.
Leaked daemon threads of other tests (image workers, watchdog) log
under ``prompt_manager.*``. One that reaches the prior handler while
restore_logging closes it would, with a plain FileHandler, reopen the
file on emit and keep it open through TemporaryDirectory.cleanup().
"""
message = "record selected before restore_logging"
selected = threading.Event()
resume = threading.Event()
errors = []
def pause_before_handler_lock(record):
if record.getMessage() == message:
selected.set()
if not resume.wait(5):
raise RuntimeError("logging thread was not released")
return True
def log_from_background_thread():
try:
logging.getLogger("prompt_manager.late_emitter").warning(message)
except Exception as error:
errors.append(error)
self.handler.addFilter(pause_before_handler_lock)
worker = threading.Thread(target=log_from_background_thread, daemon=True)
worker.start()
try:
self.assertTrue(selected.wait(5), "record did not select prior.log")
self.restore_logging()
self.assertIsNone(self.handler.stream)
finally:
resume.set()
worker.join(5)
self.assertFalse(worker.is_alive(), "logging thread did not finish")
self.assertEqual(errors, [])
self.assertIsNone(self.handler.stream)
self.assert_tmp_has_no_open_handles()
with open(self.path, encoding="utf-8") as log:
self.assertNotIn(message, log.read())
def test_failed_package_import_closes_handlers_and_restores_logging(self):
name = "prompt_manager_pkg_logger_failure"
captured = {}
original_print = print
def fail_banner(*args, **kwargs):
if any("tools loaded" in str(arg) for arg in args):
captured["package"] = sys.modules[name]
captured["handlers"] = [
h
for h in self.logger.handlers
if isinstance(h, logging.FileHandler)
]
raise RuntimeError("injected banner failure")
return original_print(*args, **kwargs)
with (
contextlib.redirect_stdout(io.StringIO()),
mock.patch("builtins.print", side_effect=fail_banner),
):
with self.assertRaisesRegex(RuntimeError, "injected banner failure"):
with load_package(
name,
os.path.join(self.tmp.name, "p.db"),
self.tmp.name,
self.server,
):
self.fail("failed import yielded a package")
self.assertNotIn(name, sys.modules)
self.assertTrue(captured["handlers"])
self.assert_logging_restored(
captured["handlers"], captured["package"]._history_registration
)
PACKAGE_THREAD_NAMES = ("PromptManagerPromptCleanup", "PromptManagerImageWorker")
def package_threads():
"""Live threads this design owns: the tracker, the image worker, and the
watchdog observer and emitters (which have no name of their own).
watchdog 7's InotifyFD, one daemon reader per process that no observer
stop ends, is not the package's."""
from watchdog.utils import BaseThread
return {
thread
for thread in threading.enumerate()
if thread.name in PACKAGE_THREAD_NAMES
or (isinstance(thread, BaseThread) and type(thread).__name__ != "InotifyFD")
}
def hooked_app():
return types.SimpleNamespace(
middlewares=[], on_cleanup=[], on_startup=[], on_shutdown=[]
)
WAIT_SECONDS = 30 # ceiling for every Event wait (TESTING.md test rules)
def start_busy_worker(package, folder, name="busy.png"):
"""The image worker alive in its processing delay."""
handler = package._global_image_monitor.handler
if handler is None:
raise AssertionError("the package's monitor did not start")
handler.processing_delay = 3600
# Outside the watched folder, so the observer does not schedule it first.
path = os.path.join(folder, name)
with open(path, "wb") as image:
image.write(b"x" * 16)
if not handler.schedule(path) or not handler._worker.is_alive():
raise AssertionError("the image worker did not start")
return handler._worker
class PackageTeardownCase(unittest.TestCase):
"""Each test loads its own package (#301): the teardown runs once."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.addCleanup(lambda: self.assertEqual(open_handles_under(self.tmp.name), []))
self.output_dir = os.path.join(self.tmp.name, "output")
os.makedirs(self.output_dir)
self.db_path = os.path.join(self.tmp.name, "prompts.db")
def load(self, server=None, require_stopped=True, suffix=""):
name = f"pm_teardown_{self._testMethodName}{suffix}"
stack = contextlib.ExitStack()
self.addCleanup(stack.close)
stack.enter_context(contextlib.redirect_stdout(io.StringIO()))
package, _ = stack.enter_context(
load_package(
name,
self.db_path,
self.output_dir,
server,
require_stopped=require_stopped,
)
)
return package
def busy_worker(self, package, name="busy.png"):
return start_busy_worker(package, self.tmp.name, name)
def hold_a_preview(self, package, *, advance, kill_releases):
"""A preview worker held inside a fake encode that tracks a fake
ffmpeg, and a package clock that the monitor step moves by
``advance`` seconds. Cleanups release the encode first."""
# Not the watched folder. Real path for both the file and the root:
# macOS /var vs /private/var, Windows short names.
previews = os.path.realpath(os.path.join(self.tmp.name, "previews"))
os.makedirs(previews)
from PIL import Image
image = os.path.join(previews, "held.png")
Image.new("RGB", (8, 8), "red").save(image)
held = types.SimpleNamespace(
entered=threading.Event(),
release=threading.Event(),
killed=threading.Event(),
stop_seen=[],
timeouts=[],
now=[100.0],
)
class FakeFfmpeg:
def poll(self): # _track keeps only processes still running
return None
def kill(self):
held.killed.set()
if kill_releases:
held.release.set()
def held_encode(src, dst, settings, is_video, *, stop, guard, track):
track(FakeFfmpeg())
held.entered.set()
held.release.wait(WAIT_SECONDS)
held.stop_seen.append(stop())
return {"action": "skipped"}
api = package.api
monitor = package._global_image_monitor
real_stop = monitor.stop_monitoring
real_close = api.close_auto_thumbnails
def stop_then_advance(timeout):
ok = real_stop(timeout)
held.now[0] += advance
return ok
def recorded_close(final=False, timeout=None):
held.timeouts.append(timeout)
return real_close(final=final, timeout=timeout)
for target, name, value in (
(api, "_get_all_output_dirs", lambda: [Path(previews)]),
(api, "_generate_one", held_encode),
(api, "close_auto_thumbnails", recorded_close),
(monitor, "stop_monitoring", stop_then_advance),
(package, "_clock", lambda: held.now[0]),
):
patcher = mock.patch.object(target, name, value)
patcher.start()
self.addCleanup(patcher.stop)
self.assertEqual(api.queue_capture_thumbnail(image), "queued")
self.addCleanup(held.release.set)
self.assertTrue(held.entered.wait(WAIT_SECONDS), "the encode never started")
held.thumbnailer = api._auto_thumbnailer # the teardown drops the API's
held.worker = held.thumbnailer._worker
# Cleanups run newest first: release the encode, then join the worker
# (even when an assertion failed first), then end the patches.
self.addCleanup(held.worker.join, WAIT_SECONDS)
self.addCleanup(held.release.set)
self.assertTrue(held.worker.is_alive())
return held
class TestPackageTeardown(PackageTeardownCase):
def test_an_image_in_hand_queues_no_preview_during_teardown(self):
package = self.load()
monitor = package._global_image_monitor
real_stop = monitor.stop_monitoring
image = os.path.join(self.output_dir, "in_hand.png")
from PIL import Image
Image.new("RGB", (8, 8), "red").save(image)
seen = {}
def stop_with_an_image_in_hand(timeout):
# The worker read ``thumbnail_queue`` before the detach and now
# hands its finished image over (at exit, Python 3.12+ refuses a
# new thread here and logs a traceback).
seen["queued"] = package.api.queue_capture_thumbnail(image)
seen["worker"] = package.api._auto_thumbnailer
return real_stop(timeout)
with (
mock.patch.object(
package.api,
"_get_all_output_dirs",
return_value=[Path(os.path.realpath(self.output_dir))],
),
mock.patch.object(
monitor, "stop_monitoring", side_effect=stop_with_an_image_in_hand
),
):
result = package._stop_background_work()
self.assertEqual(seen["queued"], "skipped")
self.assertIsNone(seen["worker"])
self.assertTrue(all(result.values()), result)
def test_teardown_stops_the_job_pool_once_a_job_used_it(self):
"""#523: the long-lived job pool is a teardown part of its own."""
package = self.load()
parallel = sys.modules[package.__name__ + ".utils.parallel"]
self.assertEqual(parallel.map_parallel(lambda n: n * 2, [1, 2], 2), [2, 4])
# This package's own pool (the suite's top-level one may also run).
threads = list(parallel._pool[1]._threads)
self.assertTrue(threads)
result = package._stop_background_work()
self.assertEqual(list(result), ["monitor", "thumbnails", "workers", "tracker"])
self.assertTrue(all(result.values()), result)
self.assertFalse(parallel.job_pool_started())
self.assertFalse(any(t.is_alive() for t in threads))
def test_teardown_stops_the_review_worker_after_the_monitor(self):
"""#659 C5: a started review worker is the part ``review``, stopped
right after the monitor."""
package = self.load()
api, monitor = package.api, package._global_image_monitor
self.assertEqual(monitor.review_recorder, api.queue_review_file)
done = threading.Event()
real_record = api.record_review_file
def record(path):
try:
return real_record(path)
finally:
done.set()
order = []
real_stop_monitor = monitor.stop_monitoring
real_stop_review = api.stop_review_worker
def stop_monitor(timeout):
order.append("monitor")
return real_stop_monitor(timeout)
def stop_review(timeout):
order.append("review")
return real_stop_review(timeout)
with (
mock.patch.object(api, "record_review_file", record),
mock.patch.object(monitor, "stop_monitoring", stop_monitor),
mock.patch.object(api, "stop_review_worker", stop_review),
):
self.assertEqual(
api.queue_review_file(os.path.join(self.tmp.name, "x.png")), "queued"
)
self.assertTrue(done.wait(WAIT_SECONDS))
thread = api._review_worker()._thread
result = package._stop_background_work()
self.assertEqual(order, ["monitor", "review"])
self.assertEqual(
list(result), ["monitor", "review", "thumbnails", "tracker"], result
)
self.assertTrue(all(result.values()), result)
self.assertFalse(thread.is_alive())
self.assertEqual(api.queue_review_file("late.png"), "stopped")
def test_teardown_reports_a_job_thread_past_its_bound(self):
package = self.load(require_stopped=False)
parallel = sys.modules[package.__name__ + ".utils.parallel"]
entered, release = threading.Event(), threading.Event()
def hold(_):
entered.set()
release.wait(30)
caller = threading.Thread(target=parallel.map_parallel, args=(hold, [1], 1))
caller.start()
self.addCleanup(caller.join, 30)
self.addCleanup(release.set)
self.assertTrue(entered.wait(30))
with (
mock.patch.object(parallel, "JOB_POOL_JOIN_SECONDS", 0.05),
self.assertLogs("prompt_manager.init", "WARNING") as logs,
):
result = package._stop_background_work()
self.assertFalse(result["workers"])
self.assertTrue(result["tracker"])
self.assertRegex("\n".join(logs.output), r"not stopped in time .*workers")
def test_teardown_kills_ffmpeg_and_stops_the_preview_worker(self):
package = self.load()
held = self.hold_a_preview(package, advance=4.0, kill_releases=True)
result = package._stop_background_work()
self.assertTrue(held.killed.is_set())
self.assertEqual(held.timeouts, [6.0]) # min(30, 10 - 4)
self.assertTrue(result["thumbnails"], result)
self.assertFalse(held.worker.is_alive())
self.assertEqual(held.stop_seen, [True])
def test_on_cleanup_runs_the_teardown(self):
app = hooked_app()
package = self.load(fake_server_module(app=app))
self.assertIs(app.on_cleanup[0], package._stop_background_work_on_cleanup)
self.assertIsNone(package._background_work_result)
async def run_cleanup():
for callback in list(app.on_cleanup):
await callback(app)
asyncio.run(run_cleanup())
self.assertEqual(
package._background_work_result,
{"monitor": True, "thumbnails": True, "tracker": True},
)
def test_teardown_runs_before_history_dispose_at_exit(self):
with mock.patch("atexit.register", wraps=atexit.register) as register:
package = self.load(fake_server_module(queue_task_done=True))
registered = [call.args[0] for call in register.call_args_list]
registration = package._history_registration
self.assertIsNotNone(registration, "history did not register")
self.assertIn(registration.dispose, registered)
self.assertIn(package._stop_background_work, registered)
# atexit runs newest first: the teardown before the history dispose.
self.assertGreater(
registered.index(package._stop_background_work),
registered.index(registration.dispose),
)
class ListHandler(logging.Handler):
def __init__(self):
super().__init__(logging.INFO)
self.lines = []
def emit(self, record):
self.lines.append(f"{record.levelname}:{record.getMessage()}")
class TestPackageTeardownOnce(PackageTestCase):
"""The tests that only observe one plain teardown share one package.
The teardown runs once, so it runs here in ``setUpClass`` (per-test
``assertLogs`` cannot wrap it); the tests read what it recorded.
"""
package_name = "pm_teardown_once"
@classmethod
def setUpClass(cls):
with contextlib.redirect_stdout(io.StringIO()):
super().setUpClass()
package = cls.package
cls.monitor = package._global_image_monitor
cls.observer = cls.monitor.observer
cls.tracker_thread = package._global_prompt_tracker.cleanup_thread
cls.worker = start_busy_worker(package, cls.tmp.name)
cls.recorder_before = cls.monitor.review_recorder
logger = logging.getLogger("prompt_manager.init")
handler, level = ListHandler(), logger.level
logger.addHandler(handler)
logger.setLevel(logging.INFO) # otherwise set by the config
try:
with mock.patch("atexit.unregister", wraps=atexit.unregister) as unregister:
cls.result = package._stop_background_work()
finally:
logger.removeHandler(handler)
logger.setLevel(level)
cls.log = "\n".join(handler.lines)
cls.unregister_calls = unregister.call_args_list
def test_teardown_stops_every_part_once(self):
expected = {"monitor": True, "thumbnails": True, "tracker": True}
self.assertEqual(self.result, expected)
self.assertEqual(list(self.result), ["monitor", "thumbnails", "tracker"])
self.assertIn("Background work stopped in", self.log)
self.assertIn("monitor=ok thumbnails=ok tracker=ok", self.log)
self.assertFalse(self.worker.is_alive())
self.assertFalse(self.observer.is_alive())
self.assertFalse(self.tracker_thread.is_alive())
copy = self.package._stop_background_work()
copy["monitor"] = False # a copy: the stored result is unchanged
with mock.patch.object(self.monitor, "stop_monitoring") as again:
self.assertEqual(self.package._stop_background_work(), expected)
again.assert_not_called()
def test_teardown_detaches_the_monitor_callbacks(self):
self.assertIsNotNone(self.recorder_before)
self.assertIsNone(self.monitor.review_recorder)
self.assertIsNone(self.monitor.thumbnail_queue)
self.assertIsNone(self.monitor.output_listener)
def test_teardown_removes_its_atexit_callback(self):
self.assertEqual(
self.unregister_calls, [mock.call(self.package._stop_background_work)]
)
class TestPackageTeardownFailures(PackageTeardownCase):
"""A failing step never skips the later ones (design D14, R2)."""
def assert_tracker_stops_after(self, error):
package = self.load(require_stopped=False)
monitor = package._global_image_monitor
tracker_thread = package._global_prompt_tracker.cleanup_thread
with (
mock.patch.object(monitor, "stop_monitoring", side_effect=error),
self.assertLogs("prompt_manager.init", "WARNING") as logs,
):
result = package._stop_background_work()
self.assertFalse(result["monitor"])
self.assertTrue(result["tracker"])
self.assertFalse(tracker_thread.is_alive())
text = "\n".join(logs.output)
self.assertIn("Background work not stopped in time", text)
self.assertRegex(text, r"not stopped in time \([0-9.]+ s\): monitor$")
self.assertIn("Traceback", text)
def test_teardown_step_failure_does_not_skip_later_steps(self):
self.assert_tracker_stops_after(RuntimeError("stop failed"))
def test_second_interrupt_does_not_skip_the_tracker(self):
self.assert_tracker_stops_after(KeyboardInterrupt())
def test_an_interrupt_between_steps_still_records_the_teardown(self):
package = self.load(require_stopped=False)
real_step = package._teardown_step
def interrupted_before_the_previews(name, stop, logger):
if name == "the preview worker":
raise KeyboardInterrupt # outside the step's own try
return real_step(name, stop, logger)
with (
mock.patch.object(
package, "_teardown_step", interrupted_before_the_previews
),
mock.patch("atexit.unregister", wraps=atexit.unregister) as unregister,
self.assertRaises(KeyboardInterrupt),
):
package._stop_background_work()
expected = {"monitor": True, "thumbnails": False, "tracker": False}
self.assertEqual(package._background_work_result, expected)
unregister.assert_called_once_with(package._stop_background_work)
with mock.patch.object(
package._global_image_monitor, "stop_monitoring"
) as again:
self.assertEqual(package._stop_background_work(), expected)
again.assert_not_called()
def test_teardown_reports_a_preview_worker_still_held(self):
package = self.load(require_stopped=False)
held = self.hold_a_preview(package, advance=9.95, kill_releases=False)
with self.assertLogs("prompt_manager.init", "WARNING") as logs:
result = package._stop_background_work()
self.assertTrue(held.killed.is_set())
self.assertEqual(len(held.timeouts), 1)
self.assertAlmostEqual(held.timeouts[0], 0.05, places=6)
self.assertTrue(held.thumbnailer.closed)
self.assertFalse(result["thumbnails"], result)
self.assertRegex("\n".join(logs.output), r"not stopped in time .*thumbnails")
held.release.set()
held.worker.join(WAIT_SECONDS)
self.assertFalse(held.worker.is_alive())
self.assertEqual(held.stop_seen, [True])
def test_teardown_shares_one_deadline(self):
package = self.load(require_stopped=False)
now = [100.0]
seen = []
def step(timeout, **_):
seen.append(timeout)
now[0] += 6.0
return True
with (
mock.patch.object(package, "_clock", lambda: now[0]),
mock.patch.object(
package._global_image_monitor, "stop_monitoring", side_effect=step
),
mock.patch.object(
package.api,
"close_auto_thumbnails",
side_effect=lambda final=False, timeout=None: step(timeout),
),
mock.patch.object(
package._global_prompt_tracker, "shutdown", side_effect=step
),
):
package._stop_background_work()
self.assertEqual(seen, [5.0, 4.0, 0.0])
class TestRepeatedLoads(PackageTeardownCase):
def test_repeated_loads_leave_no_threads(self):
for round_ in range(3):
before = package_threads()
with contextlib.redirect_stdout(io.StringIO()):
with load_package(
f"pm_teardown_repeat_{round_}",
self.db_path,
self.output_dir,
) as (package, _):
self.busy_worker(package, f"busy_{round_}.png")
started = package_threads() - before
self.assertTrue(started, "the package started no thread")
left = {t for t in package_threads() - before if t.is_alive()}
self.assertEqual(left, set(), f"round {round_}")