Merge lp:~maxiberta/canonical-identity-provider/improve-throttle-tests into lp:canonical-identity-provider/release

Proposed by Maximiliano Bertacchini
Status: Merged
Approved by: Maximiliano Bertacchini
Approved revision: no longer in the source branch.
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: lp:~maxiberta/canonical-identity-provider/improve-throttle-tests
Merge into: lp:canonical-identity-provider/release
Diff against target: 68 lines (+11/-31)
1 file modified
src/api/v20/tests/test_handlers.py (+11/-31)
To merge this branch: bzr merge lp:~maxiberta/canonical-identity-provider/improve-throttle-tests
Reviewer Review Type Date Requested Status
Daniel Manrique (community) Approve
Review via email: mp+354892@code.launchpad.net

Commit message

Some cleanup of API throttling tests.

Description of the change

- De-duplicated code in a number of helper test functions (particularly `throttle_by_*`).
- Dropped single-line helper function (`patch_piston_cache`).
- Replaced literal `3600` (mocked request counter in cache) by `settings.THROTTLE_MAX_REQUESTS`.
- Replaced literal `42` (mocked request counter in cache) by `settings.THROTTLE_MAX_REQUESTS - 1`, which also prevents confusion with `42` as cache expiration time.
- Increased mocked expiration time from `time.time() + 42.1` to `time.time() + 42.99`, which should lower the chances of time-sensitive tests failing under load (with "AssertionError: '41' != u'42'") by allowing an extra 0.89 sec margin; but kept vila's FIXME note in case tests fail again. In that case we can resort to mocking `time()` as vila suggested, but I'd rather try this simple cleanup first.

To post a comment you must log in.
Revision history for this message
Daniel Manrique (roadmr) wrote :

nice, I guess 42 was overused as an universal constant.

Thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/api/v20/tests/test_handlers.py'
2--- src/api/v20/tests/test_handlers.py 2018-08-17 18:38:29 +0000
3+++ src/api/v20/tests/test_handlers.py 2018-09-13 20:30:42 +0000
4@@ -2164,46 +2164,26 @@
5 # entries and use them (instead of mocking them) -- vila 2015-11-13
6 class ThrottledTestCase(BaseTestCase):
7
8- def patch_piston_cache(self):
9- return self.patch('piston.utils.cache')
10-
11 def throttle_all(self):
12- mock_cache = self.patch_piston_cache()
13+ mock_cache = self.patch('piston.utils.cache')
14 # hit max number of requests allowed
15- mock_cache.get.return_value = (3600, time.time() + 42.1)
16+ mock_cache.get.return_value = (
17+ settings.THROTTLE_MAX_REQUESTS, time.time() + 42.99)
18
19 def throttle_by_openid(self, openid):
20- mock_cache = self.patch_piston_cache()
21+ mock_cache = self.patch('piston.utils.cache')
22
23 def mock_get(key, default=None, version=None):
24 # only look for openid in extra part
25 # the first part of the key is the requester username
26 _, extra = key.split(':', 1)
27 if openid in extra:
28- return (3600, time.time() + 42.1)
29- else:
30- return (42, time.time() + 42.1)
31- mock_cache.get = mock_get
32-
33- def throttle_by_email(self, email):
34- mock_cache = self.patch_piston_cache()
35-
36- def mock_get(key, default=None, version=None):
37- if email in key:
38- return (3600, time.time() + 42.1)
39- else:
40- return (42, time.time() + 42.1)
41- mock_cache.get = mock_get
42-
43- def throttle_by_token(self, token):
44- mock_cache = self.patch_piston_cache()
45-
46- def mock_get(key, default=None, version=None):
47- if token in key:
48- return (3600, time.time() + 42.1)
49- else:
50- return (42, time.time() + 42.1)
51- mock_cache.get = mock_get
52+ return settings.THROTTLE_MAX_REQUESTS, time.time() + 42.99
53+ else:
54+ return settings.THROTTLE_MAX_REQUESTS - 1, time.time() + 42.99
55+ mock_cache.get = mock_get
56+
57+ throttle_by_email = throttle_by_token = throttle_by_openid
58
59 def assert_handler_throttled(
60 self, request, token=None, throttle_func=None, throttled=True,
61@@ -2480,7 +2460,7 @@
62 with patch(
63 'api.v20.decorators.stats.increment', called_metrics.append):
64 with patch('piston.utils.cache') as mock_cache:
65- mock_cache.get.return_value = (0, time.time() + 42.1)
66+ mock_cache.get.return_value = (0, time.time() + 42.99)
67 self.get_response(request.path, method=request.method)
68 self.assertEqual([], called_metrics)
69