Skip to content

Commit

Permalink
testing: Make test_hit_ratelimits deterministic.
Browse files Browse the repository at this point in the history
On slower systems or virtual environments, when you are running
tests in parallel mode, sometimes the time taken to send messages
in this test exceeds 1 second which resets the limit.
  • Loading branch information
umairwaheed committed May 6, 2017
1 parent 962b56e commit b8afd24
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions zerver/tests/test_external.py
Expand Up @@ -105,20 +105,23 @@ def test_hit_ratelimits(self):
user = get_user_profile_by_email(email)
clear_user_history(user)

start_time = time.time()
for i in range(6):
result = self.send_api_message(email, "some stuff %s" % (i,))
with mock.patch('time.time', return_value=(start_time + i * 0.1)):
result = self.send_api_message(email, "some stuff %s" % (i,))

self.assertEqual(result.status_code, 429)
json = ujson.loads(result.content)
self.assertEqual(json.get("result"), "error")
self.assertIn("API usage exceeded rate limit, try again in", json.get("msg"))
self.assertTrue('Retry-After' in result)
self.assertIn(result['Retry-After'], json.get("msg"))
self.assertEqual(result['Retry-After'], '0.5')

# We actually wait a second here, rather than force-clearing our history,
# to make sure the rate-limiting code automatically forgives a user
# after some time has passed.
with mock.patch('time.time', return_value=(time.time() + 1)):
with mock.patch('time.time', return_value=(start_time + 1.0)):
result = self.send_api_message(email, "Good message")

self.assert_json_success(result)

0 comments on commit b8afd24

Please sign in to comment.