def test_expiry(self): with hiro.Timeline().freeze() as timeline: limiter = FixedWindowRateLimiter(self.storage) per_min = RateLimitItemPerMinute(10) for i in range(0, 10): self.assertTrue(limiter.hit(per_min)) timeline.forward(60) # touch another key and yield limiter.hit(RateLimitItemPerSecond(1)) time.sleep(0.1) self.assertTrue(per_min.key_for() not in self.storage.storage)
def test_in_memory_expiry(self): with hiro.Timeline().freeze() as timeline: storage = MemoryStorage() limiter = FixedWindowRateLimiter(storage) per_min = RateLimitItemPerMinute(10) for i in range(0,10): self.assertTrue(limiter.hit(per_min)) timeline.forward(60) # touch another key and yield limiter.hit(RateLimitItemPerSecond(1)) time.sleep(0.1) self.assertTrue(per_min.key_for() not in storage.storage)
def test_in_memory_expiry_moving_window(self): with hiro.Timeline().freeze() as timeline: storage = MemoryStorage() limiter = MovingWindowRateLimiter(storage) per_min = RateLimitItemPerMinute(10) per_sec = RateLimitItemPerSecond(1) for i in range(0,2): for i in range(0,10): self.assertTrue(limiter.hit(per_min)) timeline.forward(60) self.assertTrue(limiter.hit(per_sec)) time.sleep(1) self.assertEqual([], storage.events[per_min.key_for()])
def test_in_memory_expiry_moving_window(self): with hiro.Timeline().freeze() as timeline: storage = MemoryStorage() limiter = MovingWindowRateLimiter(storage) per_min = RateLimitItemPerMinute(10) per_sec = RateLimitItemPerSecond(1) for i in range(0, 2): for i in range(0, 10): self.assertTrue(limiter.hit(per_min)) timeline.forward(60) self.assertTrue(limiter.hit(per_sec)) time.sleep(1) self.assertEqual([], storage.events[per_min.key_for()])