Example #1
0
 def test_failed_redis_sentinel_lock(self):
     storage = RedisSentinelStorage("redis+sentinel://localhost:26379", service_name="localhost-redis-sentinel")
     limiter = MovingWindowRateLimiter(storage)
     limit = RateLimitItemPerSecond(1000)
     key = limit.key_for("test") + "/LOCK"
     storage.sentinel.master_for("localhost-redis-sentinel").setnx(key, 1)
     self.assertRaises(redis.lock.LockError, limiter.hit, limit, "test")
Example #2
0
 def test_failed_redis_lock(self):
     storage = RedisStorage("redis://localhost:6379")
     limiter = MovingWindowRateLimiter(storage)
     limit = RateLimitItemPerSecond(1000)
     key = limit.key_for("test") + "/LOCK"
     storage.storage.setnx(key, 1)
     self.assertRaises(redis.lock.LockError, limiter.hit, limit, "test")
Example #3
0
 def test_fixed_window_with_elastic_expiry_memcache_concurrency(self):
     storage = MemcachedStorage('memcached://localhost:11211')
     limiter = FixedWindowElasticExpiryRateLimiter(storage)
     start = int(time.time())
     limit = RateLimitItemPerSecond(100, 2)
     def _c():
         for i in range(0,50):
             limiter.hit(limit)
     t1, t2 = threading.Thread(target=_c), threading.Thread(target=_c)
     t1.start(), t2.start()
     [t1.join(), t2.join()]
     self.assertEqual(limiter.get_window_stats(limit)[1], 0)
     self.assertTrue(start + 2 <= limiter.get_window_stats(limit)[0] <= start + 3)
     self.assertEqual(storage.get(limit.key_for()), 100)
Example #4
0
    def test_fixed_window_with_elastic_expiry_memcache_concurrency(self):
        storage = MemcachedStorage('memcached://localhost:11211')
        limiter = FixedWindowElasticExpiryRateLimiter(storage)
        start = int(time.time())
        limit = RateLimitItemPerSecond(100, 2)

        def _c():
            for i in range(0, 50):
                limiter.hit(limit)

        t1, t2 = threading.Thread(target=_c), threading.Thread(target=_c)
        t1.start(), t2.start()
        [t1.join(), t2.join()]
        self.assertEqual(limiter.get_window_stats(limit)[1], 0)
        self.assertTrue(
            start + 2 <= limiter.get_window_stats(limit)[0] <= start + 3)
        self.assertEqual(storage.get(limit.key_for()), 100)