You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by mc...@apache.org on 2013/01/18 20:15:35 UTC

[17/24] git commit: Add some tests for api rate limit plugin.

Add some tests for api rate limit plugin.

Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/06fa338d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/06fa338d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/06fa338d

Branch: refs/heads/master
Commit: 06fa338d4257beaeb1fa54644f56c5222f3510bb
Parents: bdcfa19
Author: Min Chen <mi...@citrix.com>
Authored: Wed Jan 16 21:56:25 2013 -0800
Committer: Min Chen <mi...@citrix.com>
Committed: Wed Jan 16 21:56:25 2013 -0800

----------------------------------------------------------------------
 .../command/admin/ratelimit/ResetApiLimitCmd.java  |   15 ++-
 .../cloudstack/ratelimit/ApiRateLimitTest.java     |    2 +-
 server/test/com/cloud/api/ListPerfTest.java        |   75 ++++++++++++++-
 3 files changed, 85 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/06fa338d/plugins/api/rate-limit/src/org/apache/cloudstack/api/command/admin/ratelimit/ResetApiLimitCmd.java
----------------------------------------------------------------------
diff --git a/plugins/api/rate-limit/src/org/apache/cloudstack/api/command/admin/ratelimit/ResetApiLimitCmd.java b/plugins/api/rate-limit/src/org/apache/cloudstack/api/command/admin/ratelimit/ResetApiLimitCmd.java
index 771b63a..58cab18 100644
--- a/plugins/api/rate-limit/src/org/apache/cloudstack/api/command/admin/ratelimit/ResetApiLimitCmd.java
+++ b/plugins/api/rate-limit/src/org/apache/cloudstack/api/command/admin/ratelimit/ResetApiLimitCmd.java
@@ -16,14 +16,19 @@
 // under the License.
 package org.apache.cloudstack.api.command.admin.ratelimit;
 
-import org.apache.cloudstack.api.*;
+import org.apache.cloudstack.api.ACL;
+import org.apache.cloudstack.api.APICommand;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.ApiErrorCode;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.Parameter;
+import org.apache.cloudstack.api.PlugService;
+import org.apache.cloudstack.api.ServerApiException;
 import org.apache.cloudstack.api.response.AccountResponse;
 import org.apache.cloudstack.api.response.ApiLimitResponse;
 import org.apache.cloudstack.api.response.SuccessResponse;
-import org.apache.log4j.Logger;
-
-import org.apache.cloudstack.api.APICommand;
 import org.apache.cloudstack.ratelimit.ApiRateLimitService;
+import org.apache.log4j.Logger;
 
 import com.cloud.user.Account;
 import com.cloud.user.UserContext;
@@ -88,7 +93,7 @@ public class ResetApiLimitCmd extends BaseCmd {
             SuccessResponse response = new SuccessResponse(getCommandName());
             this.setResponseObject(response);
         } else {
-            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to reset api limit counter");
+            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to reset api limit counter");
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/06fa338d/plugins/api/rate-limit/test/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java
----------------------------------------------------------------------
diff --git a/plugins/api/rate-limit/test/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java b/plugins/api/rate-limit/test/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java
index 850182d..85eeaaf 100644
--- a/plugins/api/rate-limit/test/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java
+++ b/plugins/api/rate-limit/test/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java
@@ -100,7 +100,7 @@ public class ApiRateLimitTest {
 
     @Test
     public void canDoReasonableNumberOfApiAccessPerSecond() throws Exception {
-        int allowedRequests = 50000;
+        int allowedRequests = 200;
         _limitService.setMaxAllowed(allowedRequests);
         _limitService.setTimeToLive(1);
 

http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/06fa338d/server/test/com/cloud/api/ListPerfTest.java
----------------------------------------------------------------------
diff --git a/server/test/com/cloud/api/ListPerfTest.java b/server/test/com/cloud/api/ListPerfTest.java
index 350dde8..c6fda9b 100644
--- a/server/test/com/cloud/api/ListPerfTest.java
+++ b/server/test/com/cloud/api/ListPerfTest.java
@@ -171,7 +171,73 @@ public class ListPerfTest extends APITest {
     }
 
     @Test
-    public void testMultiListAccounts() throws Exception {
+    public void testNoApiLimitOnRootAdmin() throws Exception {
+        // issue list Accounts calls
+        final HashMap<String, String> params = new HashMap<String, String>();
+        params.put("response", "json");
+        params.put("listAll", "true");
+        params.put("sessionkey", sessionKey);
+        // assuming ApiRateLimitService set api.throttling.max = 25
+        int clientCount = 26;
+        Runnable[] clients = new Runnable[clientCount];
+        final boolean[] isUsable = new boolean[clientCount];
+
+        final CountDownLatch startGate = new CountDownLatch(1);
+
+        final CountDownLatch endGate = new CountDownLatch(clientCount);
+
+
+        for (int i = 0; i < isUsable.length; ++i) {
+            final int j = i;
+            clients[j] = new Runnable() {
+
+                /**
+                 * {@inheritDoc}
+                 */
+                @Override
+                public void run() {
+                    try {
+                        startGate.await();
+
+                        sendRequest("listAccounts", params);
+
+                        isUsable[j] = true;
+
+                    } catch (CloudRuntimeException e){
+                        isUsable[j] = false;
+                        e.printStackTrace();
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    } finally {
+                        endGate.countDown();
+                    }
+                }
+            };
+        }
+
+        ExecutorService executor = Executors.newFixedThreadPool(clientCount);
+
+        for (Runnable runnable : clients) {
+            executor.execute(runnable);
+        }
+
+        startGate.countDown();
+
+        endGate.await();
+
+        int rejectCount = 0;
+        for ( int i = 0; i < isUsable.length; ++i){
+            if ( !isUsable[i])
+                rejectCount++;
+        }
+
+        assertEquals("No request should be rejected!", 0, rejectCount);
+
+    }
+
+
+    @Test
+    public void testApiLimitOnUser() throws Exception {
         // log in using normal user
         login("demo", "password");
         // issue list Accounts calls
@@ -235,6 +301,13 @@ public class ListPerfTest extends APITest {
 
         assertEquals("Only one request should be rejected!", 1, rejectCount);
 
+        // issue get api limit calls
+        final HashMap<String, String> params2 = new HashMap<String, String>();
+        params2.put("response", "json");
+        params2.put("sessionkey", sessionKey);
+        String getResult =  sendRequest("getApiLimit", params2);
+        //ApiLimitResponse loginResp = (ApiLimitResponse)fromSerializedString(getResult, ApiLimitResponse.class);
+
     }
 
 }