This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rhbz988202 - rate limit REST api plus test
- Loading branch information
Patrick Huang
committed
Mar 21, 2014
1 parent
5c625bf
commit 4d3448a
Showing
12 changed files
with
379 additions
and
9 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
functional-test/src/main/java/org/zanata/page/administration/ServerConfigurationPage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.zanata.page.administration; | ||
|
||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.support.FindBy; | ||
import org.zanata.page.BasePage; | ||
import org.zanata.util.WebElementUtil; | ||
|
||
/** | ||
* @author Patrick Huang | ||
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
public class ServerConfigurationPage extends BasePage { | ||
@FindBy(id = "serverConfigForm:urlField") | ||
private WebElement urlField; | ||
|
||
@FindBy(id = "serverConfigForm:rateLimitField:rateLimitEml") | ||
private WebElement rateLimitField; | ||
|
||
@FindBy(id = "serverConfigForm:save") | ||
private WebElement saveButton; | ||
|
||
public ServerConfigurationPage(WebDriver driver) { | ||
super(driver); | ||
} | ||
|
||
public ServerConfigurationPage inputRateLimit(int limit) { | ||
rateLimitField.clear(); | ||
rateLimitField.sendKeys(limit + ""); | ||
return this; | ||
} | ||
|
||
public String getRateLimit() { | ||
return rateLimitField.getAttribute("value"); | ||
} | ||
|
||
public AdministrationPage save() { | ||
saveButton.click(); | ||
return new AdministrationPage(getDriver()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
functional-test/src/test/java/org/zanata/feature/misc/RateLimitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package org.zanata.feature.misc; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
import org.junit.experimental.categories.Category; | ||
import org.zanata.feature.DetailedTest; | ||
import org.zanata.page.administration.AdministrationPage; | ||
import org.zanata.page.administration.ServerConfigurationPage; | ||
import org.zanata.util.AddUsersRule; | ||
import org.zanata.util.ZanataRestCaller; | ||
import org.zanata.workflow.BasicWorkFlow; | ||
import org.zanata.workflow.LoginWorkFlow; | ||
import com.google.common.base.Function; | ||
import com.google.common.base.Throwables; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Lists; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
/** | ||
* @author Patrick Huang <a | ||
* href="mailto:pahuang@redhat.com">pahuang@redhat.com</a> | ||
*/ | ||
@Category(DetailedTest.class) | ||
@Slf4j | ||
public class RateLimitTest { | ||
@ClassRule | ||
public static AddUsersRule addUsersRule = new AddUsersRule(); | ||
|
||
private static final String TRANSLATOR = "translator"; | ||
private static final String TRANSLATOR_API = | ||
"d83882201764f7d339e97c4b087f0806"; | ||
|
||
@Test | ||
public void canConfigureRateLimit() { | ||
new LoginWorkFlow().signIn("admin", "admin"); | ||
BasicWorkFlow basicWorkFlow = new BasicWorkFlow(); | ||
ServerConfigurationPage serverConfigPage = | ||
basicWorkFlow.goToPage("admin/server_configuration", | ||
ServerConfigurationPage.class); | ||
|
||
assertThat(serverConfigPage.getRateLimit(), Matchers.isEmptyString()); | ||
|
||
AdministrationPage administrationPage = | ||
serverConfigPage.inputRateLimit(1).save(); | ||
|
||
assertThat(administrationPage.getNotificationMessage(), | ||
Matchers.equalTo("Configuration was successfully updated.")); | ||
|
||
serverConfigPage = | ||
basicWorkFlow.goToPage("admin/server_configuration", | ||
ServerConfigurationPage.class); | ||
assertThat(serverConfigPage.getRateLimit(), Matchers.equalTo("1")); | ||
} | ||
|
||
@Test | ||
public void canRateLimitRestRequestsPerAPIKey() throws InterruptedException { | ||
new LoginWorkFlow().signIn("admin", "admin"); | ||
BasicWorkFlow basicWorkFlow = new BasicWorkFlow(); | ||
ServerConfigurationPage serverConfigPage = | ||
basicWorkFlow.goToPage("admin/server_configuration", | ||
ServerConfigurationPage.class); | ||
// adjust rate limit per second | ||
serverConfigPage.inputRateLimit(3).save(); | ||
|
||
// prepare to fire multiple REST requests | ||
final AtomicInteger atomicInteger = new AtomicInteger(1); | ||
// translator creates the project/version | ||
final String projectSlug = "project"; | ||
final String iterationSlug = "version"; | ||
new ZanataRestCaller(TRANSLATOR, TRANSLATOR_API) | ||
.createProjectAndVersion(projectSlug, iterationSlug, "gettext"); | ||
|
||
// requests from translator user | ||
final int translatorThreads = 4; | ||
Callable<Integer> translatorTask = new Callable<Integer>() { | ||
|
||
@Override | ||
public Integer call() { | ||
return invokeRestService(new ZanataRestCaller(TRANSLATOR, | ||
TRANSLATOR_API), projectSlug, iterationSlug, | ||
atomicInteger); | ||
} | ||
}; | ||
List<Callable<Integer>> translatorTasks = | ||
Collections.nCopies(translatorThreads, translatorTask); | ||
|
||
// requests from admin user | ||
int adminThreads = 3; | ||
Callable<Integer> adminTask = new Callable<Integer>() { | ||
@Override | ||
public Integer call() throws Exception { | ||
return invokeRestService(new ZanataRestCaller(), projectSlug, | ||
iterationSlug, atomicInteger); | ||
} | ||
}; | ||
|
||
List<Callable<Integer>> adminTasks = | ||
Collections.nCopies(adminThreads, adminTask); | ||
|
||
ExecutorService executorService = | ||
Executors.newFixedThreadPool(translatorThreads + adminThreads); | ||
|
||
List<Callable<Integer>> tasks = | ||
ImmutableList.<Callable<Integer>> builder() | ||
.addAll(translatorTasks).addAll(adminTasks).build(); | ||
|
||
List<Future<Integer>> futures = executorService.invokeAll(tasks); | ||
|
||
List<Integer> result = getResultStatusCodes(futures); | ||
|
||
// 1 request from translator should get 503 and fail | ||
log.info("result: {}", result); | ||
assertThat(result, Matchers.containsInAnyOrder(201, 201, 201, 201, 201, | ||
201, 403)); | ||
} | ||
|
||
private static Integer invokeRestService(ZanataRestCaller restCaller, | ||
String projectSlug, String iterationSlug, | ||
AtomicInteger atomicInteger) { | ||
try { | ||
int counter = atomicInteger.getAndIncrement(); | ||
return restCaller.postSourceDocResource(projectSlug, iterationSlug, | ||
ZanataRestCaller.buildSourceResource("doc" + counter, | ||
ZanataRestCaller.buildTextFlow("res" + counter, | ||
"content" + counter)), false); | ||
} catch (Exception e) { | ||
log.info("rest call failed: {}", e.getMessage()); | ||
return 500; | ||
} | ||
} | ||
|
||
private static List<Integer> getResultStatusCodes(List<Future<Integer>> futures) { | ||
return Lists.transform(futures, | ||
new Function<Future<Integer>, Integer>() { | ||
@Override | ||
public Integer apply(Future<Integer> input) { | ||
try { | ||
return input.get(); | ||
} | ||
catch (Exception e) { | ||
throw Throwables.propagate(e); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
zanata-war/src/main/java/org/zanata/annotation/RateLimiting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.zanata.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface RateLimiting { | ||
} |
15 changes: 15 additions & 0 deletions
15
zanata-war/src/main/java/org/zanata/annotation/RateLimitingResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.zanata.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.jboss.seam.annotations.intercept.Interceptors; | ||
import org.zanata.seam.interceptor.RateLimitingInterceptor; | ||
|
||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Interceptors(RateLimitingInterceptor.class) | ||
public @interface RateLimitingResource { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.