use of com.predic8.membrane.core.interceptor.Outcome in project service-proxy by membrane.
the class AMQuotaInterceptorTest method testAMQuota.
@Test
public void testAMQuota() throws IOException, InterruptedException {
final Exchange exc = new Exchange(null);
exc.setRequest(new Request.Builder().header("Test", "test").body("hello").build());
exc.setResponse(new Response.ResponseBuilder().header("Test2", "test2").body("Hello back!").build());
exc.setProperty(Exchange.API_KEY, "junit");
exc.setRule(new ServiceProxy());
exc.getRule().setName("junit API");
ApiManagementConfiguration amc = new ApiManagementConfiguration(System.getProperty("user.dir"), "src\\test\\resources\\apimanagement\\api.yaml");
long reqSize = exc.getRequest().getHeader().toString().getBytes().length + exc.getRequest().getHeader().getContentLength();
long respSize = exc.getResponse().getHeader().toString().getBytes().length + exc.getResponse().getHeader().getContentLength();
assertEquals(31 + 5, reqSize);
assertEquals(34 + 11, respSize);
final AMQuota amq = new AMQuota();
amq.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 = amq.handleRequest(exc);
if (out == Outcome.CONTINUE) {
continues.incrementAndGet();
} else if (out == Outcome.RETURN) {
returns.incrementAndGet();
}
amq.handleResponse(exc);
} catch (Exception e) {
e.printStackTrace();
}
}
});
threads.add(t);
// t.start();
// doing sync because else we cant predictably count request/response pairs
t.run();
}
for (Thread t : threads) {
t.join();
}
// the limit is ( or should be ) 120B
// 31+5 ( Req ) + 34+11 ( Resp ) = 81 for every completed exchange
// the second request adds another 31+5 -> 81 + 36 = 117 < 120B -> after the second request it should block because the limit is 120b and the following response would bring it over the limit ( responses never block, only requests )
assertEquals(2, continues.get());
assertEquals(998, returns.get());
Thread.sleep(2000);
assertEquals(Outcome.CONTINUE, amq.handleRequest(exc));
}
use of com.predic8.membrane.core.interceptor.Outcome in project service-proxy by membrane.
the class AbortExchangeTest method setup.
@Before
public void setup() throws Exception {
router = new HttpRouter();
LimitedMemoryExchangeStore es = new LimitedMemoryExchangeStore();
router.setExchangeStore(es);
router.getTransport().getInterceptors().add(2, new ExchangeStoreInterceptor(es));
ServiceProxy sp2 = new ServiceProxy(new ServiceProxyKey("*", "*", ".*", 3031), "", -1);
sp2.getInterceptors().add(new AbstractInterceptor() {
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
exc.getRequest().readBody();
exc.setResponse(Response.ok("").body(new InputStream() {
int l = 0;
@Override
public int read() throws IOException {
if (l >= 2000000)
return -1;
return 0;
}
}, true).build());
return Outcome.RETURN;
}
});
router.getRuleManager().addProxyAndOpenPortIfNew(sp2);
router.init();
}
use of com.predic8.membrane.core.interceptor.Outcome in project service-proxy by membrane.
the class RequestInterceptor method handleRequest.
@Override
public Outcome handleRequest(Exchange exc) throws Exception {
boolean logDebug = log.isDebugEnabled();
for (Interceptor i : getInterceptors()) {
EnumSet<Flow> f = i.getFlow();
if (!f.contains(Flow.REQUEST))
continue;
if (logDebug)
log.debug("Invoking request handler: " + i.getDisplayName() + " on exchange: " + exc);
Outcome o = i.handleRequest(exc);
if (o != Outcome.CONTINUE)
return o;
}
return Outcome.CONTINUE;
}
Aggregations