mirror of https://github.com/apache/cloudstack.git
make api rate limit test more robust (#6984)
* make api rate limit test a little more robust * Update condition for time exeeded
This commit is contained in:
parent
fc5bd85ecb
commit
162af93e11
|
|
@ -230,6 +230,23 @@ public class ApiRateLimitServiceImpl extends AdapterBase implements APIChecker,
|
|||
this.timeToLive = timeToLive;
|
||||
}
|
||||
|
||||
protected int getTimeToLive() {
|
||||
return this.timeToLive;
|
||||
}
|
||||
|
||||
protected int getMaxAllowed() {
|
||||
return this.maxAllowed;
|
||||
}
|
||||
|
||||
protected int getIssued(Long accountId) {
|
||||
int ammount = 0;
|
||||
StoreEntry entry = _store.get(accountId);
|
||||
if (entry != null) {
|
||||
ammount = entry.getCounter();
|
||||
}
|
||||
return ammount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMaxAllowed(int max) {
|
||||
maxAllowed = max;
|
||||
|
|
|
|||
|
|
@ -112,15 +112,25 @@ public static void setUp() throws ConfigurationException {
|
|||
public void canDoReasonableNumberOfApiAccessPerSecond() throws Exception {
|
||||
int allowedRequests = 200;
|
||||
s_limitService.setMaxAllowed(allowedRequests);
|
||||
s_limitService.setTimeToLive(1);
|
||||
s_limitService.setTimeToLive(5);
|
||||
long startTime = System.nanoTime();
|
||||
|
||||
User key = createFakeUser();
|
||||
|
||||
for (int i = 0; i < allowedRequests; i++) {
|
||||
assertTrue("We should allow " + allowedRequests + " requests per second, but failed at request " + i, isUnderLimit(key));
|
||||
assertTrue(String.format("We should allow %d requests per second, but failed at request %d.", allowedRequests, i), isUnderLimit(key));
|
||||
}
|
||||
// we cannot really say more about this test
|
||||
boolean underLimit = isUnderLimit(key);
|
||||
long endTime = System.nanoTime();
|
||||
System.out.println("time elapsed " + (endTime - startTime)/1000/1000 + " ms");
|
||||
int issued = s_limitService.getIssued(key.getAccountId());
|
||||
int timeToLive = s_limitService.getTimeToLive();
|
||||
|
||||
assertFalse("We should block >" + allowedRequests + " requests per second", isUnderLimit(key));
|
||||
// this assertion is really invalid as we don´t know if we exceeded the time to live for the amount of api calls (for sure)
|
||||
// so only fail if timeToLive is not exeeded and we didn´t get the requested number of calls
|
||||
assertFalse(String.format("We should block >%d requests per %d seconds (managed %d, time elapsed %d ns)",
|
||||
s_limitService.getMaxAllowed(), timeToLive, issued, endTime - startTime), ((endTime - startTime)/1000000000 < timeToLive) && underLimit);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue