diff --git a/tests/test_caption_pause.py b/tests/test_caption_pause.py new file mode 100644 index 0000000..ea5c7be --- /dev/null +++ b/tests/test_caption_pause.py @@ -0,0 +1,78 @@ +from __future__ import annotations + +import threading +import time +import unittest +from types import SimpleNamespace + +from scene_search.caption_jobs import CaptionJobManager + + +class CaptionPauseTests(unittest.TestCase): + def test_pause_waits_between_units_and_resume_continues(self) -> None: + manager = CaptionJobManager(SimpleNamespace()) # type: ignore[arg-type] + manager._state.update( + { + "running": True, + "stage": "captioning", + "frames_total": 10, + "frames_completed": 3, + } + ) + + paused = manager.pause() + self.assertTrue(paused["pause_requested"]) + + finished = threading.Event() + + def worker() -> None: + manager._wait_if_paused("captioning") + finished.set() + + thread = threading.Thread(target=worker) + thread.start() + + deadline = time.time() + 2 + while time.time() < deadline and not manager.status().get("paused"): + time.sleep(0.01) + + self.assertTrue(manager.status()["paused"]) + self.assertFalse(finished.is_set()) + + resumed = manager.resume() + self.assertFalse(resumed["pause_requested"]) + + thread.join(timeout=2) + self.assertTrue(finished.is_set()) + self.assertFalse(manager.status()["paused"]) + self.assertEqual(manager.status()["stage"], "captioning") + + def test_pause_during_routing_resumes_to_routing(self) -> None: + manager = CaptionJobManager(SimpleNamespace()) # type: ignore[arg-type] + manager._state.update({"running": True, "stage": "routing"}) + + manager.pause() + finished = threading.Event() + + thread = threading.Thread( + target=lambda: ( + manager._wait_if_paused("routing"), + finished.set(), + ) + ) + thread.start() + + deadline = time.time() + 2 + while time.time() < deadline and not manager.status().get("paused"): + time.sleep(0.01) + + self.assertEqual(manager.status()["stage"], "paused") + manager.resume() + thread.join(timeout=2) + + self.assertTrue(finished.is_set()) + self.assertEqual(manager.status()["stage"], "routing") + + +if __name__ == "__main__": + unittest.main()