Add some tests for api rate limit plugin.

This commit is contained in:
Min Chen 2013-01-16 21:56:25 -08:00
parent bdcfa1919b
commit 06fa338d42
3 changed files with 85 additions and 7 deletions

View File

@ -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");
}
}
}

View File

@ -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);

View File

@ -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);
}
}