From 4e8b087c824eea16f8ddea09137d8ef408370df0 Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Sat, 22 Aug 2020 15:32:37 +0200 Subject: [PATCH] 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. --- zerver/tests/test_external.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/zerver/tests/test_external.py b/zerver/tests/test_external.py index 41273b6439..1cae60ea55 100644 --- a/zerver/tests/test_external.py +++ b/zerver/tests/test_external.py @@ -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)