Skip to content

Commit 6559d55

Browse files
committed
test: Modify testRestRateLimit test to report when system is too slow.
Add an explicit check on the runtime and an error message that reports that the runtime was exceeded for the test to complete as written. testRestRateLimit requires that it finish within 3 seconds. Otherwise the number of remaining requests in the rate limit does not decrease on every call. If disk I/O is high, the anydbm version of this test can take > 3 seconds and result in a failed test. My other alternative was to measure the runtime and adjust the test to match the values that are returned. This seems like too much work and is unlikely to be an issue outside of a developers under powered system.
1 parent 2c14553 commit 6559d55

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

test/rest_common.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,12 @@ def testPagination(self):
10761076

10771077
def testRestRateLimit(self):
10781078

1079-
self.db.config['WEB_API_CALLS_PER_INTERVAL'] = 20
1080-
self.db.config['WEB_API_INTERVAL_IN_SEC'] = 60
1079+
calls_per_interval = 20
1080+
interval_sec = 60
1081+
wait_time_str = str(int(interval_sec/calls_per_interval))
1082+
1083+
self.db.config['WEB_API_CALLS_PER_INTERVAL'] = calls_per_interval
1084+
self.db.config['WEB_API_INTERVAL_IN_SEC'] = interval_sec
10811085

10821086
# Otk code never passes through the
10831087
# retry loop. Not sure why but I can force it
@@ -1090,17 +1094,22 @@ def testRestRateLimit(self):
10901094
# sqlite or anydbm. So don't need to exercise code.
10911095
pass
10921096

1093-
print("Now realtime start:", datetime.utcnow())
1097+
start_time = datetime.utcnow()
10941098
# don't set an accept header; json should be the default
10951099
# use up all our allowed api calls
1096-
for i in range(20):
1097-
# i is 0 ... 19
1100+
for i in range(calls_per_interval):
1101+
# i is 0 ... calls_per_interval
10981102
self.client_error_message = []
10991103
self.server.client.env.update({'REQUEST_METHOD': 'GET'})
11001104
results = self.server.dispatch('GET',
11011105
"/rest/data/user/%s/realname"%self.joeid,
11021106
self.empty_form)
11031107

1108+
loop_time = datetime.utcnow()
1109+
self.assertLess((loop_time-start_time).total_seconds(),
1110+
int(wait_time_str),
1111+
"Test system is too slow to complete test as configured")
1112+
11041113
# is successful
11051114
self.assertEqual(self.server.client.response_code, 200)
11061115
# does not have Retry-After header as we have
@@ -1136,11 +1145,12 @@ def testRestRateLimit(self):
11361145
59, delta=5)
11371146
self.assertEqual(
11381147
str(self.server.client.additional_headers["Retry-After"]),
1139-
"3") # check as string
1148+
wait_time_str) # check as string
11401149

11411150
print("Reset:", self.server.client.additional_headers["X-RateLimit-Reset"])
11421151
print("Now realtime pre-sleep:", datetime.utcnow())
1143-
sleep(3.1) # sleep as requested so we can do another login
1152+
# sleep as requested so we can do another login
1153+
sleep(float(wait_time_str) + 0.1)
11441154
print("Now realtime post-sleep:", datetime.utcnow())
11451155

11461156
# this should succeed
@@ -1181,11 +1191,13 @@ def testRestRateLimit(self):
11811191
self.assertEqual(self.server.client.response_code, 429)
11821192
self.assertEqual(
11831193
str(self.server.client.additional_headers["Retry-After"]),
1184-
"3") # check as string
1194+
wait_time_str) # check as string
11851195

11861196
json_dict = json.loads(b2s(results))
1187-
self.assertEqual(json_dict['error']['msg'],
1188-
"Api rate limits exceeded. Please wait: 3 seconds.")
1197+
self.assertEqual(
1198+
json_dict['error']['msg'],
1199+
"Api rate limits exceeded. Please wait: %s seconds." %
1200+
wait_time_str)
11891201

11901202
# reset rest params
11911203
self.db.config['WEB_API_CALLS_PER_INTERVAL'] = 0

0 commit comments

Comments
 (0)