"""Tests for frigate.util.builtin helpers.""" import unittest from unittest.mock import patch from frigate.util.builtin import EventsPerSecond class TestEventsPerSecond(unittest.TestCase): def test_eps_is_zero_before_any_events(self) -> None: eps = EventsPerSecond() with patch("frigate.util.builtin.time.monotonic", return_value=100.0): self.assertEqual(eps.eps(), 0.0) def test_eps_counts_events_in_window(self) -> None: eps = EventsPerSecond(last_n_seconds=10) clock = [1000.0] with patch("frigate.util.builtin.time.monotonic", side_effect=lambda: clock[0]): eps.start() # one event per second for five seconds for _ in range(5): clock[0] += 1.0 eps.update() # five events over the five seconds since start self.assertAlmostEqual(eps.eps(), 1.0) def test_old_timestamps_expire_from_window(self) -> None: eps = EventsPerSecond(last_n_seconds=10) clock = [0.0] with patch("frigate.util.builtin.time.monotonic", side_effect=lambda: clock[0]): eps.start() for _ in range(10): clock[0] += 1.0 eps.update() # jump well past the window so every timestamp ages out clock[0] += 100.0 self.assertEqual(eps.eps(), 0.0) def test_burst_after_start_is_not_divided_by_a_tiny_window(self) -> None: eps = EventsPerSecond(last_n_seconds=10) clock = [1000.0] with patch("frigate.util.builtin.time.monotonic", side_effect=lambda: clock[0]): eps.start() # eleven buffered frames arrive within 100 ms of starting for _ in range(11): clock[0] += 0.01 eps.update() # 11 events over less than a second is at most 11 per second self.assertLessEqual(eps.eps(), 11.0) def test_subsecond_window_keeps_its_rate(self) -> None: eps = EventsPerSecond(last_n_seconds=0.5) clock = [1000.0] with patch("frigate.util.builtin.time.monotonic", side_effect=lambda: clock[0]): eps.start() # twenty events per second for two seconds for _ in range(40): clock[0] += 0.05 eps.update() # read between events, so none sits exactly on the window edge clock[0] += 0.01 self.assertAlmostEqual(eps.eps(), 20.0) if __name__ == "__main__": unittest.main()