def test_one_rule_no_persistence(self): name = 'ut' config = self._test_config() config[name].pop('persistence') rate_limiter = RateLimiter.get(name, config=config, user='******') for i in range(5): self.assertEqual(0, rate_limiter()) wait_time = rate_limiter(waits=False) self.assertLess(0.9, wait_time) self.assertGreater(1.1, wait_time)
def test_single_rate_limit_loaded_data(self): name = 'ut' config = self._test_config() index = RamKV() index.set_kv('rl:docido:ut-user:ut', '1462177808756|5') rate_limiter = RateLimiter.get(name, config=config, user='******', persistence_kwargs=dict(index=index)) for i in range(1, 6): self.assertEqual(0, rate_limiter()) self.assertEqual(str(i), index._kv['rl:docido:ut-user:ut'].split('|')[1]) wait_time = rate_limiter(waits=False) self.assertLess(0.9, wait_time) self.assertGreater(1.1, wait_time)
def _single_rate_limit(self, runtime=False): name = 'ut' config = self._test_config() index = RamKV() ctor_kwargs, call_kwargs = self._runtime_expr(runtime) rate_limiter = RateLimiter.get(name, config=config, persistence_kwargs=dict(index=index), **ctor_kwargs) for i in range(1, 6): self.assertEqual(0, rate_limiter(**call_kwargs)) self.assertEqual(str(i), index._kv['rl:docido:ut-user:ut'].split('|')[1]) wait_time = rate_limiter(waits=False, **call_kwargs) self.assertLess(0.9, wait_time) self.assertGreater(1.1, wait_time)