from unittest.mock import Mock import pytest from getstream_connector.throttling import BaseThrottler def test_init(): class TestThrottler(BaseThrottler): def _get_sleep_time(self, client) -> float: return 1 throttler = TestThrottler() assert throttler def test_init_negative_delay_threshold(): class TestThrottler(BaseThrottler): def _get_sleep_time(self, client) -> float: return 1 with pytest.raises(ValueError, match=r"scale_factor must be greater or equal than 0"): TestThrottler(delay_threshold=-1) @pytest.mark.parametrize( "delay_threshold, sleep_time, expected_is_delayed", ( (1, 1.1, True), (1, 0.9, False), ), ) def test_delay(delay_threshold, sleep_time, expected_is_delayed, mocker): mocker.patch("getstream_connector.throttling.base.time.sleep") class TestThrottler(BaseThrottler): def _get_sleep_time(self, client) -> float: return sleep_time test_throttler = TestThrottler(delay_threshold=delay_threshold) result = test_throttler.delay(Mock()) assert result == expected_is_delayed