diff --git a/plugins/api/rate-limit/src/main/java/org/apache/cloudstack/ratelimit/ApiRateLimitServiceImpl.java b/plugins/api/rate-limit/src/main/java/org/apache/cloudstack/ratelimit/ApiRateLimitServiceImpl.java index 9fe3cb23e98..3192727fbeb 100644 --- a/plugins/api/rate-limit/src/main/java/org/apache/cloudstack/ratelimit/ApiRateLimitServiceImpl.java +++ b/plugins/api/rate-limit/src/main/java/org/apache/cloudstack/ratelimit/ApiRateLimitServiceImpl.java @@ -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; diff --git a/plugins/api/rate-limit/src/test/java/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java b/plugins/api/rate-limit/src/test/java/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java index 872776070ef..6bfd201253f 100644 --- a/plugins/api/rate-limit/src/test/java/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java +++ b/plugins/api/rate-limit/src/test/java/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java @@ -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