Search in sources :

Example 6 with Invocation

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());
// }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Mock(mockit.Mock) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Test(org.junit.Test)

Example 7 with Invocation

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());
    }
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MockUp(mockit.MockUp) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 8 with Invocation

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);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation)

Example 9 with Invocation

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);
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) Holder(javax.xml.ws.Holder) Handler(org.apache.servicecomb.core.Handler) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 10 with Invocation

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);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) HttpServletRequestEx(org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx) Test(org.junit.Test)

Aggregations

Invocation (org.apache.servicecomb.core.Invocation)66 Test (org.junit.Test)50 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)23 Response (org.apache.servicecomb.swagger.invocation.Response)19 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)16 MockUp (mockit.MockUp)11 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)9 ArrayList (java.util.ArrayList)9 Server (com.netflix.loadbalancer.Server)8 HashMap (java.util.HashMap)8 Holder (javax.xml.ws.Holder)8 Expectations (mockit.Expectations)7 Mock (mockit.Mock)7 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)5 AbstractSpan (org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan)5 Map (java.util.Map)4 AbstractConfiguration (org.apache.commons.configuration.AbstractConfiguration)4 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)4 Endpoint (org.apache.servicecomb.core.Endpoint)4 CseServer (org.apache.servicecomb.loadbalance.CseServer)4