# retoor import queue import threading from tests.conftest import run_async from devplacepy.services.background import BackgroundQueue def _fill(direct_queue, count, executed): for _ in range(count): direct_queue.put_nowait((executed.append, ("x",), {})) def test_stop_without_start_returns_cleanly(): direct = BackgroundQueue() run_async(direct.stop()) assert direct.stats()["running"] is False def test_stop_full_queue_does_not_block(): direct = BackgroundQueue(maxsize=4) dead = threading.Thread(target=lambda: None) dead.start() dead.join() direct._thread = dead executed = [] _fill(direct._queue, 4, executed) run_async(direct.stop()) assert executed == ["x"] * 4 def test_stop_bounds_shutdown_drain_and_counts_dropped(): from devplacepy.services.background import SHUTDOWN_DRAIN_MAX direct = BackgroundQueue(maxsize=5000) executed = [] _fill(direct._queue, SHUTDOWN_DRAIN_MAX + 200, executed) run_async(direct.stop()) stats = direct.stats() assert len(executed) == SHUTDOWN_DRAIN_MAX assert stats["dropped"] == 200 def test_start_stop_roundtrip_executes_submitted_task(): direct = BackgroundQueue() run_async(direct.start()) try: executed = [] direct.submit(executed.append, "x") finally: run_async(direct.stop()) assert executed == ["x"] assert direct.stats()["running"] is False