use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.
the class TestShutdownHookHandler method testShutdownHookHandlerCount.
@Test
public void testShutdownHookHandlerCount(@Mocked Response response) throws Exception {
Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", false);
ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
Assert.assertEquals(0, handler.getActiveCount());
// no reply
Invocation invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
}
}.getMockInstance();
handler.handle(invocation, asyncResp -> {
});
Assert.assertEquals(1, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
// normal
invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
asyncResp.handle(response);
}
}.getMockInstance();
handler.handle(invocation, asyncResp -> {
});
Assert.assertEquals(2, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
// next exception
invocation = new MockUp<Invocation>() {
@Mock
public void next(AsyncResponse asyncResp) throws Exception {
throw new Error();
}
}.getMockInstance();
try {
handler.handle(invocation, asyncResp -> {
});
Assert.assertFalse(true);
} catch (Throwable e) {
Assert.assertEquals(3, requestCounter.get());
Assert.assertEquals(1, handler.getActiveCount());
}
AtomicLong responseCounter = Deencapsulation.getField(ShutdownHookHandler.INSTANCE, "responseCounter");
responseCounter.incrementAndGet();
Assert.assertEquals(0, handler.getActiveCount());
// reply exception
// TODO: should be fixed
// try {
// handler.handle(invocation, asyncResp -> {
// throw new Error();
// });
//
// Assert.assertFalse(true);
// } catch (Throwable e) {
// Assert.assertEquals(3, requestCounter.get());
// Assert.assertEquals(1, handler.getActiveCount());
// }
}
use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.
the class TestInvokerUtils method testSyncInvokeInvocationWithException.
@Test
public void testSyncInvokeInvocationWithException() throws InterruptedException {
Invocation invocation = Mockito.mock(Invocation.class);
Response response = Mockito.mock(Response.class);
new MockUp<SyncResponseExecutor>() {
@Mock
public Response waitResponse() throws InterruptedException {
return Mockito.mock(Response.class);
}
};
Mockito.when(response.isSuccessed()).thenReturn(true);
OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
Mockito.when(operationMeta.getMicroserviceQualifiedName()).thenReturn("test");
try {
InvokerUtils.syncInvoke(invocation);
} catch (InvocationException e) {
Assert.assertEquals(490, e.getStatusCode());
}
}
use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.
the class ProducerInvocationMeter method onInvocationFinish.
@Override
public void onInvocationFinish(InvocationFinishEvent event) {
super.onInvocationFinish(event);
Invocation invocation = event.getInvocation();
executorQueueTimer.record(invocation.getStartExecutionTime() - invocation.getStartTime(), TimeUnit.NANOSECONDS);
executionTimer.record(event.getNanoCurrent() - invocation.getStartExecutionTime(), TimeUnit.NANOSECONDS);
}
use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.
the class TestAbstractRestInvocation method doInvoke.
@Test
public void doInvoke(@Mocked Endpoint endpoint, @Mocked OperationMeta operationMeta, @Mocked Object[] swaggerArguments, @Mocked SchemaMeta schemaMeta) throws Throwable {
Response response = Response.ok("ok");
Handler handler = new Handler() {
@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) {
asyncResp.complete(response);
}
};
List<Handler> handlerChain = Arrays.asList(handler);
Deencapsulation.setField(invocation, "handlerList", handlerChain);
Holder<Response> result = new Holder<>();
restInvocation = new AbstractRestInvocationForTest() {
@Override
protected void sendResponseQuietly(Response response) {
result.value = response;
}
};
restInvocation.invocation = invocation;
restInvocation.doInvoke();
Assert.assertSame(response, result.value);
}
use of org.apache.servicecomb.core.Invocation in project incubator-servicecomb-java-chassis by apache.
the class TracingFilterTest method testAfterReceiveRequestOnHeaderContainsTraceId.
@Test
public void testAfterReceiveRequestOnHeaderContainsTraceId() {
Invocation invocation = Mockito.mock(Invocation.class);
String traceId = "traceIdTest";
HttpServletRequestEx requestEx = Mockito.mock(HttpServletRequestEx.class);
Mockito.when(invocation.getContext(Const.TRACE_ID_NAME)).thenReturn(null);
Mockito.when(requestEx.getHeader(Const.TRACE_ID_NAME)).thenReturn(traceId);
FILTER.afterReceiveRequest(invocation, requestEx);
Mockito.verify(invocation).addContext(Const.TRACE_ID_NAME, traceId);
}
Aggregations