Ejemplo n.º 1
0
 def test_in_memory_expiry(self):
     with hiro.Timeline().freeze() as timeline:
         storage = MemoryStorage()
         limiter = FixedWindowRateLimiter(storage)
         per_min = PER_MINUTE(10)
         for i in range(0, 10):
             self.assertTrue(limiter.hit(per_min))
         timeline.forward(60)
         # touch another key and yield
         limiter.hit(PER_SECOND(1))
         time.sleep(0.1)
         self.assertTrue(per_min.key_for() not in storage.storage)
Ejemplo n.º 2
0
 def test_in_memory_expiry(self):
     with hiro.Timeline().freeze() as timeline:
         storage = MemoryStorage()
         limiter = FixedWindowRateLimiter(storage)
         per_min = PER_MINUTE(10)
         for i in range(0,10):
             self.assertTrue(limiter.hit(per_min))
         timeline.forward(60)
         # touch another key and yield
         limiter.hit(PER_SECOND(1))
         time.sleep(0.1)
         self.assertTrue(per_min.key_for() not in storage.storage)
Ejemplo n.º 3
0
 def test_in_memory_expiry_moving_window(self):
     with hiro.Timeline().freeze() as timeline:
         storage = MemoryStorage()
         limiter = MovingWindowRateLimiter(storage)
         per_min = PER_MINUTE(10)
         per_sec = PER_SECOND(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()])
Ejemplo n.º 4
0
 def test_moving_window_in_memory(self):
     storage = MemoryStorage()
     limiter = MovingWindowRateLimiter(storage)
     with hiro.Timeline().freeze() as timeline:
         limit = PER_MINUTE(10)
         for i in range(0,5):
             self.assertTrue(limiter.hit(limit))
             self.assertTrue(limiter.hit(limit))
             timeline.forward(10)
         self.assertFalse(limiter.hit(limit))
         timeline.forward(20)
         self.assertTrue(limiter.hit(limit))
         self.assertTrue(limiter.hit(limit))
         self.assertFalse(limiter.hit(limit))
         self.assertEqual(len(storage.events[limit.key_for()]), 10)
Ejemplo n.º 5
0
 def test_in_memory(self):
     with hiro.Timeline().freeze() as timeline:
         storage = MemoryStorage()
         limiter = FixedWindowRateLimiter(storage)
         per_min = PER_MINUTE(10)
         for i in range(0, 10):
             self.assertTrue(limiter.hit(per_min))
         self.assertFalse(limiter.hit(per_min))
         timeline.forward(61)
         self.assertTrue(limiter.hit(per_min))
Ejemplo n.º 6
0
 def test_moving_window_in_memory(self):
     storage = MemoryStorage()
     limiter = MovingWindowRateLimiter(storage)
     with hiro.Timeline().freeze() as timeline:
         limit = PER_MINUTE(10)
         for i in range(0,5):
             self.assertTrue(limiter.hit(limit))
             self.assertTrue(limiter.hit(limit))
             self.assertEqual(
                 limiter.get_window_stats(limit)[1],
                 10 - ((i + 1) * 2)
             )
             timeline.forward(10)
         self.assertEqual(limiter.get_window_stats(limit)[1], 0)
         self.assertFalse(limiter.hit(limit))
         timeline.forward(20)
         self.assertEqual(limiter.get_window_stats(limit)[1], 2)
         self.assertEqual(limiter.get_window_stats(limit)[0], int(time.time() + 30))
         timeline.forward(31)
         self.assertEqual(limiter.get_window_stats(limit)[1], 10)