rate_limiter: Fix flakiness of tests in test_external.

I noticed RateLimitTests.test_hit_ratelimits fails when run as an
individual test, but never when run after other tests. That's due to the
first API request in a run of tests taking a long time, as detailed in
the comment on the change to the setUp method.
This commit is contained in:
Mateusz Mandera 2020-08-22 15:32:37 +02:00 committed by Tim Abbott
parent 934bdb9651
commit 4e8b087c82
1 changed files with 13 additions and 0 deletions

View File

@ -45,6 +45,19 @@ class RateLimitTests(ZulipTestCase):
def setUp(self) -> None:
super().setUp()
# Some tests here can be somewhat timing-sensitive in a way
# that can't be eliminated, e.g. due to testing things that rely
# on redis' internal timing mechanism which we can't mock.
# The first API request when running a suite of tests is slow
# and can take multiple seconds. This is not a problem when running
# multiple tests, but if an individual, time-sensitive test from this class
# is run, the first API request it makes taking a lot of time can throw things off
# and cause the test to fail. Thus we do a dummy API request here to warm up
# the system and allow the tests to assume their requests won't take multiple seconds.
user = self.example_user('hamlet')
self.api_get(user, "/api/v1/messages")
settings.RATE_LIMITING = True
add_ratelimit_rule(1, 5)