use of com.predic8.membrane.core.interceptor.apimanagement.rateLimiter.AMRateLimiter in project service-proxy by membrane.
the class AMRateLimitInterceptorTest method testHandleRequestRateLimit5SecondConcurrency.
@Test
public void testHandleRequestRateLimit5SecondConcurrency() throws Exception {
final Exchange exc = new Exchange(null);
exc.setResponse(Response.ResponseBuilder.newInstance().build());
exc.setProperty(Exchange.API_KEY, "junit");
exc.setRule(new ServiceProxy());
exc.getRule().setName("junit API");
final AMRateLimiter rli = new AMRateLimiter();
ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
rli.setAmc(amc);
ArrayList<Thread> threads = new ArrayList<Thread>();
final AtomicInteger continues = new AtomicInteger();
final AtomicInteger returns = new AtomicInteger();
for (int i = 0; i < 1000; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Outcome out = rli.handleRequest(exc);
if (out == Outcome.CONTINUE) {
continues.incrementAndGet();
} else if (out == Outcome.RETURN) {
returns.incrementAndGet();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads.add(t);
t.start();
}
for (Thread t : threads) {
t.join();
}
assertEquals(5, continues.get());
assertEquals(995, returns.get());
Thread.sleep(2000);
assertEquals(Outcome.CONTINUE, rli.handleRequest(exc));
}
Aggregations