Search in sources :

Example 1 with InvocationException

use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.

the class TestShutdownHookHandler method testShutdownHookHandlerReject.

@Test
public void testShutdownHookHandlerReject() throws Exception {
    Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", true);
    Holder<InvocationType> typeHolder = new Holder<>(InvocationType.PRODUCER);
    Invocation invocation = new MockUp<Invocation>() {

        @Mock
        public InvocationType getInvocationType() {
            return typeHolder.value;
        }
    }.getMockInstance();
    ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
    handler.handle(invocation, asyncResp -> {
        InvocationException e = asyncResp.getResult();
        Assert.assertEquals(((CommonExceptionData) e.getErrorData()).getMessage(), "shutting down in progress");
        Assert.assertEquals(e.getStatusCode(), 590);
    });
    typeHolder.value = InvocationType.CONSUMER;
    handler.handle(invocation, asyncResp -> {
        InvocationException e = asyncResp.getResult();
        Assert.assertEquals(((CommonExceptionData) e.getErrorData()).getMessage(), "shutting down in progress");
        Assert.assertEquals(e.getStatusCode(), 490);
    });
}
Also used : Invocation(io.servicecomb.core.Invocation) InvocationException(io.servicecomb.core.exception.InvocationException) Holder(javax.xml.ws.Holder) InvocationType(io.servicecomb.core.invocation.InvocationType) Mock(mockit.Mock) Test(org.junit.Test)

Example 2 with InvocationException

use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.

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 : Response(io.servicecomb.core.Response) AsyncResponse(io.servicecomb.core.AsyncResponse) Invocation(io.servicecomb.core.Invocation) InvocationException(io.servicecomb.core.exception.InvocationException) MockUp(mockit.MockUp) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 3 with InvocationException

use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.

the class ConsumerQpsFlowControlHandler method handle.

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    if (!Config.INSTANCE.isConsumerEnabled()) {
        invocation.next(asyncResp);
        return;
    }
    OperationMeta operationMeta = invocation.getOperationMeta();
    QpsController qpsController = qpsControllerMgr.getOrCreate(operationMeta);
    if (qpsController.isLimitNewRequest()) {
        // 429
        CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol");
        asyncResp.consumerFail(new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData));
        return;
    }
    invocation.next(asyncResp);
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData) OperationMeta(io.servicecomb.core.definition.OperationMeta)

Example 4 with InvocationException

use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.

the class ProviderQpsFlowControlHandler method handle.

@Override
public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    if (!Config.INSTANCE.isProviderEnabled()) {
        invocation.next(asyncResp);
        return;
    }
    String microServiceName = (String) invocation.getContext(Const.SRC_MICROSERVICE);
    if (microServiceName != null && !microServiceName.isEmpty()) {
        QpsController qpsController = qpsControllerMgr.getOrCreate(microServiceName);
        if (qpsController.isLimitNewRequest()) {
            // 429
            CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol");
            asyncResp.producerFail(new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData));
            return;
        }
    }
    invocation.next(asyncResp);
}
Also used : InvocationException(io.servicecomb.core.exception.InvocationException) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData)

Example 5 with InvocationException

use of io.servicecomb.core.exception.InvocationException in project java-chassis by ServiceComb.

the class TestBizkeeperCommand method testGetCacheKeyProvider.

@Test
public void testGetCacheKeyProvider() {
    Invocation invocation = Mockito.mock(Invocation.class);
    Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
    Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");
    HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter().withRequestCacheEnabled(true).withRequestLogEnabled(false);
    BizkeeperCommand bizkeeperCommand = new ProviderBizkeeperCommand("groupname", invocation, HystrixObservableCommand.Setter.withGroupKey(CommandKey.toHystrixCommandGroupKey("groupname", invocation)).andCommandKey(CommandKey.toHystrixCommandKey("groupname", invocation)).andCommandPropertiesDefaults(setter));
    String str = bizkeeperCommand.getCacheKey();
    Assert.assertNull(str);
    Response resp = Mockito.mock(Response.class);
    Mockito.when(resp.isFailed()).thenReturn(false);
    Assert.assertEquals(false, bizkeeperCommand.isFailedResponse(resp));
    Mockito.when(resp.isFailed()).thenReturn(true);
    InvocationException excp = Mockito.mock(InvocationException.class);
    Mockito.when(resp.getResult()).thenReturn(excp);
    Mockito.when(excp.getStatusCode()).thenReturn(400);
    Assert.assertEquals(false, bizkeeperCommand.isFailedResponse(resp));
    Mockito.when(resp.getResult()).thenReturn(excp);
    Mockito.when(excp.getStatusCode()).thenReturn(590);
    Assert.assertEquals(true, bizkeeperCommand.isFailedResponse(resp));
}
Also used : Response(io.servicecomb.core.Response) Invocation(io.servicecomb.core.Invocation) InvocationException(io.servicecomb.core.exception.InvocationException) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) OperationMeta(io.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Aggregations

InvocationException (io.servicecomb.core.exception.InvocationException)20 Test (org.junit.Test)13 CommonExceptionData (io.servicecomb.core.exception.CommonExceptionData)8 Invocation (io.servicecomb.core.Invocation)6 OperationMeta (io.servicecomb.core.definition.OperationMeta)6 Response (io.servicecomb.core.Response)4 RestParam (io.servicecomb.common.rest.definition.RestParam)3 MicroserviceMeta (io.servicecomb.core.definition.MicroserviceMeta)3 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)2 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)2 ArrayList (java.util.ArrayList)2 Holder (javax.xml.ws.Holder)2 Expectations (mockit.Expectations)2 MockUp (mockit.MockUp)2 WrapSchema (io.servicecomb.codec.protobuf.utils.WrapSchema)1 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)1 OperationLocator (io.servicecomb.common.rest.locator.OperationLocator)1 ServicePathManager (io.servicecomb.common.rest.locator.ServicePathManager)1 AsyncResponse (io.servicecomb.core.AsyncResponse)1 Endpoint (io.servicecomb.core.Endpoint)1