async def test_acquire_when_too_long_ratelimit(self, compiled_route): with buckets.RESTBucket("spaghetti", compiled_route, 60) as rl: rl.reset_at = time.perf_counter() + 999999999999999999999999999 with mock.patch.object(buckets.RESTBucket, "is_rate_limited", return_value=True): with pytest.raises(errors.RateLimitTooLongError): await rl.acquire()
def test_resolve_when_not_unknown(self, compiled_route): with buckets.RESTBucket("spaghetti", compiled_route, float("inf")) as rl: with pytest.raises(RuntimeError, match=r"Cannot resolve known bucket"): rl.resolve("test") assert rl.name == "spaghetti"
async def test_acquire_when_unknown_bucket(self, compiled_route): with buckets.RESTBucket(buckets.UNKNOWN_HASH, compiled_route, float("inf")) as rl: rl._lock = mock.AsyncMock() with mock.patch.object(rate_limits.WindowedBurstRateLimiter, "acquire") as super_acquire: assert await rl.acquire() is None rl._lock.acquire.assert_awaited_once_with() super_acquire.assert_not_called()
async def test_acquire(self, compiled_route): with buckets.RESTBucket("spaghetti", compiled_route, float("inf")) as rl: rl._lock = mock.AsyncMock() with mock.patch.object(rate_limits.WindowedBurstRateLimiter, "acquire") as super_acquire: await rl.acquire() super_acquire.assert_awaited_once_with() rl._lock.acquire.assert_awaited_once_with()
async def test_async_context_manager(self, compiled_route): with mock.patch.object(buckets.RESTBucket, "acquire", new=mock.AsyncMock()) as acquire: with mock.patch.object(buckets.RESTBucket, "release") as release: async with buckets.RESTBucket("spaghetti", compiled_route, float("inf")): acquire.assert_awaited_once_with() release.assert_not_called() release.assert_called_once_with()
def test_update_rate_limit(self, compiled_route): with buckets.RESTBucket(__name__, compiled_route, float("inf")) as rl: rl.remaining = 1 rl.limit = 2 rl.reset_at = 3 rl.period = 2 with mock.patch.object(hikari_date, "monotonic", return_value=4.20): rl.update_rate_limit(9, 18, 27) assert rl.remaining == 9 assert rl.limit == 18 assert rl.reset_at == 27 assert rl.period == 27 - 4.20
async def test_acquire_when_too_long_ratelimit(self, compiled_route): stack = contextlib.ExitStack() rl = stack.enter_context( buckets.RESTBucket("spaghetti", compiled_route, 60)) rl._lock = mock.Mock(acquire=mock.AsyncMock()) rl.reset_at = time.perf_counter() + 999999999999999999999999999 stack.enter_context( mock.patch.object(buckets.RESTBucket, "is_rate_limited", return_value=True)) stack.enter_context(pytest.raises(errors.RateLimitTooLongError)) with stack: await rl.acquire() rl._lock.acquire.assert_awaited_once_with() rl._lock.release.assert_called_once_with()
def test_is_unknown(self, name, compiled_route): with buckets.RESTBucket(name, compiled_route, float("inf")) as rl: assert rl.is_unknown is (name == buckets.UNKNOWN_HASH)
def test_resolve(self, compiled_route): with buckets.RESTBucket(buckets.UNKNOWN_HASH, compiled_route, float("inf")) as rl: rl.resolve("test") assert rl.name == "test"
def test_drip(self, name, compiled_route): with buckets.RESTBucket(name, compiled_route) as rl: rl.remaining = 1 rl.drip() assert rl.remaining == 0 if name != buckets.UNKNOWN_HASH else 1