Skip to content

Commit

Permalink
add tests for get_wait_time method
Browse files Browse the repository at this point in the history
  • Loading branch information
tlinhart committed Jan 14, 2018
1 parent d9eb258 commit bda2296
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/rate_limit_test.py
Expand Up @@ -85,6 +85,31 @@ def test_limit_10_using_rate_limiter(self):
self.assertEqual(self.rate_limit.get_usage(), 11)
self.assertEqual(self.rate_limit.has_been_reached(), True)

def test_wait_time_limit_reached(self):
"""
Should report wait time approximately equal to expire after reaching
the limit without delay between requests.
"""
self.rate_limit = RateLimit(resource='test', client='localhost',
max_requests=10, expire=1)
self._make_10_requests()
with self.assertRaises(TooManyRequests):
with self.rate_limit:
pass
self.assertAlmostEqual(self.rate_limit.get_wait_time(), 1, places=2)

def test_wait_time_limit_expired(self):
"""
Should report wait time equal to expire / max_requests before any
requests were made and after the limit has expired.
"""
self.rate_limit = RateLimit(resource='test', client='localhost',
max_requests=10, expire=1)
self.assertEqual(self.rate_limit.get_wait_time(), 1./10)
self._make_10_requests()
time.sleep(1)
self.assertEqual(self.rate_limit.get_wait_time(), 1./10)


if __name__ == '__main__':
unittest.main()
Expand Down

0 comments on commit bda2296

Please sign in to comment.