fix(worker): run discovery at most once per loop iteration
The scheduled-tick branch and the discover.requested one-shot branch both read the same stale per-iteration config snapshot, so an enabled+due sweep coinciding with a pending request ran discovery twice back-to-back (idempotent but wasteful). Extract maybe_run_discovery, which unifies both triggers into a single run and is now unit-tested. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from lyra_worker import main
|
||||
from lyra_worker.adapters.fakes import FakeMbBrowser, FakeSimilaritySource
|
||||
from lyra_worker.main import _run_discovery
|
||||
from lyra_worker.discovery import DiscoveryConfig
|
||||
from lyra_worker.main import _run_discovery, maybe_run_discovery
|
||||
from lyra_worker.similarity.base import SimilarArtist
|
||||
from tests.conftest import insert_watched_artist
|
||||
|
||||
@@ -33,3 +35,44 @@ def test_run_discovery_writes_result_and_clears_flag(conn):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('SELECT count(*) FROM "DiscoverySuggestion" WHERE kind = \'artist\'')
|
||||
assert cur.fetchone()[0] == 1
|
||||
|
||||
|
||||
def test_maybe_run_discovery_runs_once_when_due_and_requested(conn, monkeypatch):
|
||||
# A scheduled tick that is due AND a pending one-shot request in the same loop
|
||||
# iteration must trigger discovery exactly once, not twice.
|
||||
calls = []
|
||||
monkeypatch.setattr(main, "_run_discovery", lambda *a, **k: calls.append(1))
|
||||
config = {"discover.enabled": "true", "discover.requested": "true"}
|
||||
dcfg = DiscoveryConfig.from_config(config)
|
||||
new_tick = maybe_run_discovery(
|
||||
conn, [], FakeMbBrowser(), config, dcfg, now=10_000.0, last_discover_tick=0.0
|
||||
)
|
||||
assert calls == [1] # exactly one run
|
||||
assert new_tick == 10_000.0 # schedule tick advanced
|
||||
|
||||
|
||||
def test_maybe_run_discovery_requested_when_disabled_does_not_advance_tick(conn, monkeypatch):
|
||||
# A one-shot request runs even when discovery is disabled, but must not advance the
|
||||
# schedule tick (there is no active schedule).
|
||||
calls = []
|
||||
monkeypatch.setattr(main, "_run_discovery", lambda *a, **k: calls.append(1))
|
||||
config = {"discover.enabled": "false", "discover.requested": "true"}
|
||||
dcfg = DiscoveryConfig.from_config(config)
|
||||
new_tick = maybe_run_discovery(
|
||||
conn, [], FakeMbBrowser(), config, dcfg, now=10_000.0, last_discover_tick=42.0
|
||||
)
|
||||
assert calls == [1]
|
||||
assert new_tick == 42.0 # unchanged
|
||||
|
||||
|
||||
def test_maybe_run_discovery_idle_does_nothing(conn, monkeypatch):
|
||||
calls = []
|
||||
monkeypatch.setattr(main, "_run_discovery", lambda *a, **k: calls.append(1))
|
||||
config = {"discover.enabled": "true", "discover.requested": "false"}
|
||||
dcfg = DiscoveryConfig.from_config(config)
|
||||
# enabled but not yet due (tick was recent relative to now)
|
||||
new_tick = maybe_run_discovery(
|
||||
conn, [], FakeMbBrowser(), config, dcfg, now=100.0, last_discover_tick=99.0
|
||||
)
|
||||
assert calls == []
|
||||
assert new_tick == 99.0
|
||||
|
||||
Reference in New Issue
Block a user