"""The publisher: no capture while unwatched, one slot, and giving up when dead.""" from __future__ import annotations import time import pytest from live_helpers import PHRASE, StubCapture from hhstream_live.publisher import (FPS_CAP, FAILURE_LIMIT, Stream, StreamRegistry, StreamSpec) def make_stream(capture, *, fps=10, phrase=PHRASE): return Stream(StreamSpec(phrase=phrase, output="hh-demo", fps=fps), capture) @pytest.mark.parametrize("bad", [0, -1, True, 1.5, FPS_CAP + 1]) def test_fps_must_be_a_positive_int_within_the_cap(bad): with pytest.raises(ValueError): make_stream(StubCapture(), fps=bad) def test_no_capture_while_nobody_is_watching(): """An enabled stream costs nothing until a request arrives.""" capture = StubCapture() stream = make_stream(capture) stream.start() try: time.sleep(0.4) assert capture.calls == 0 finally: stream.stop() def test_capture_starts_when_a_viewer_arrives_and_stops_when_it_leaves(): capture = StubCapture() stream = make_stream(capture) stream.start() try: stream.add_viewer() frame, seq = stream.wait_frame(0, timeout=5) assert frame and seq >= 1 stream.remove_viewer() time.sleep(0.3) settled = capture.calls time.sleep(0.4) assert capture.calls == settled # idle again finally: stream.stop() def test_snapshot_is_none_before_anyone_watches_and_bytes_after(): stream = make_stream(StubCapture()) stream.start() try: assert stream.snapshot() is None stream.add_viewer() frame, _ = stream.wait_frame(0, timeout=5) stream.remove_viewer() assert frame is not None assert stream.snapshot() == frame finally: stream.stop() def test_a_slow_viewer_does_not_queue_frames(): """One slot: a viewer that sleeps sees the latest frame, never a backlog.""" stream = make_stream(StubCapture(delay=0.01), fps=10) stream.start() try: stream.add_viewer() first, seq = stream.wait_frame(0, timeout=5) time.sleep(0.3) # let many frames go by unsent later, seq2 = stream.wait_frame(seq, timeout=5) assert later is not None # seq jumped by more than one: the frames in between were dropped, not # buffered for us. assert seq2 > seq + 1 finally: stream.stop() def test_a_dead_capture_gives_up_and_closes_viewers(): capture = StubCapture(fail_first=10_000) # never succeeds stream = make_stream(capture, fps=10) stream.start() try: stream.add_viewer() frame, _ = stream.wait_frame(0, timeout=10) assert frame is None assert stream.stopped assert capture.calls >= FAILURE_LIMIT finally: stream.stop() # ---- registry -------------------------------------------------------------- def test_registry_adds_and_finds_a_phrase_case_insensitively(): registry = StreamRegistry() try: registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture()) assert registry.get("Amber-Otter-Lantern-Harbor-7K2QP") is not None assert registry.get("nope-not-here-at-all") is None finally: registry.stop_all() def test_registry_refuses_a_duplicate_phrase(): registry = StreamRegistry() try: registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture()) with pytest.raises(ValueError): registry.add(StreamSpec(phrase=PHRASE, output="hh-other"), StubCapture()) finally: registry.stop_all() def test_registry_remove_stops_the_stream(): registry = StreamRegistry() stream = registry.add(StreamSpec(phrase=PHRASE, output="hh-demo"), StubCapture()) assert registry.remove(PHRASE) is stream assert registry.get(PHRASE) is None assert stream.stopped assert registry.remove(PHRASE) is None