From 06fa338d4257beaeb1fa54644f56c5222f3510bb Mon Sep 17 00:00:00 2001 From: Min Chen Date: Wed, 16 Jan 2013 21:56:25 -0800 Subject: [PATCH] Add some tests for api rate limit plugin. --- .../admin/ratelimit/ResetApiLimitCmd.java | 15 ++-- .../ratelimit/ApiRateLimitTest.java | 2 +- server/test/com/cloud/api/ListPerfTest.java | 75 ++++++++++++++++++- 3 files changed, 85 insertions(+), 7 deletions(-) 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 771b63a9e78..58cab186570 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"); } } } 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 850182d8f03..85eeaaf4223 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); diff --git a/server/test/com/cloud/api/ListPerfTest.java b/server/test/com/cloud/api/ListPerfTest.java index 350dde88b1c..c6fda9bf4eb 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 params = new HashMap(); + 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 params2 = new HashMap(); + params2.put("response", "json"); + params2.put("sessionkey", sessionKey); + String getResult = sendRequest("getApiLimit", params2); + //ApiLimitResponse loginResp = (ApiLimitResponse)fromSerializedString(getResult, ApiLimitResponse.class); + } }