You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by da...@apache.org on 2022/12/16 07:49:26 UTC

[cloudstack] branch main updated: make api rate limit test more robust (#6984)

This is an automated email from the ASF dual-hosted git repository.

dahn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/main by this push:
     new 162af93e114 make api rate limit test more robust (#6984)
162af93e114 is described below

commit 162af93e114434f588f427bf0ac7036881ead56b
Author: dahn <da...@onecht.net>
AuthorDate: Thu Dec 15 23:49:17 2022 -0800

    make api rate limit test more robust (#6984)
    
    * make api rate limit test a little more robust
    
    * Update condition for time exeeded
---
 .../cloudstack/ratelimit/ApiRateLimitServiceImpl.java  | 17 +++++++++++++++++
 .../apache/cloudstack/ratelimit/ApiRateLimitTest.java  | 18 ++++++++++++++----
 2 files changed, 31 insertions(+), 4 deletions(-)

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));
         }
-
-        assertFalse("We should block >" + allowedRequests + " requests per second", 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();
+
+        // 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