Search in sources :

Example 21 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class ProviderQpsFlowControlHandler method isLimitNewRequest.

private boolean isLimitNewRequest(QpsController qpsController, AsyncResponse asyncResp) {
    if (qpsController.isLimitNewRequest()) {
        CommonExceptionData errorData = new CommonExceptionData("rejected by qps flowcontrol");
        asyncResp.producerFail(new InvocationException(QpsConst.TOO_MANY_REQUESTS_STATUS, errorData));
        return true;
    } else {
        return false;
    }
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)

Example 22 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class TestBizkeeperCommand method testGetCacheKeyConsumer.

@Test
public void testGetCacheKeyConsumer() {
    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 ConsumerBizkeeperCommand("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(490);
    Assert.assertEquals(true, bizkeeperCommand.isFailedResponse(resp));
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) HystrixCommandProperties(com.netflix.hystrix.HystrixCommandProperties) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 23 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class TestConsumerQpsFlowControlHandler method testHandle.

@Test
public void testHandle() throws Exception {
    String key = "svc.schema.opr";
    QpsController qpsController = new QpsController("key", 12);
    Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
    Mockito.when(operationMeta.getMicroserviceQualifiedName()).thenReturn(key);
    setQpsController(key, qpsController);
    new MockUp<QpsController>() {

        @Mock
        public boolean isLimitNewRequest() {
            return true;
        }
    };
    new MockUp<QpsControllerManager>() {

        @Mock
        protected QpsController create(String qualifiedNameKey) {
            return qpsController;
        }
    };
    handler.handle(invocation, asyncResp);
    ArgumentCaptor<InvocationException> captor = ArgumentCaptor.forClass(InvocationException.class);
    Mockito.verify(asyncResp).consumerFail(captor.capture());
    InvocationException invocationException = captor.getValue();
    assertEquals(QpsConst.TOO_MANY_REQUESTS_STATUS, invocationException.getStatus());
    assertEquals("rejected by qps flowcontrol", ((CommonExceptionData) invocationException.getErrorData()).getMessage());
}
Also used : InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) MockUp(mockit.MockUp) Test(org.junit.Test)

Example 24 with InvocationException

use of org.apache.servicecomb.swagger.invocation.exception.InvocationException in project incubator-servicecomb-java-chassis by apache.

the class TestResponse method test.

@Test
public void test() {
    Response r = Response.create(200, "200", 2);
    Assert.assertEquals(200, r.getStatusCode());
    Assert.assertEquals(2, (int) r.getResult());
    Response r1 = r.build();
    Assert.assertEquals(r, r1);
    r = Response.create(300, "300", 3);
    Assert.assertEquals(300, r.getStatusCode());
    Assert.assertEquals("300", r.getReasonPhrase());
    Assert.assertEquals(3, ((InvocationException) r.getResult()).getErrorData());
    r = Response.createSuccess(Status.OK, 2);
    Assert.assertEquals(200, r.getStatusCode());
    Assert.assertEquals(2, (int) r.getResult());
    r = Response.success(2, Status.OK);
    Assert.assertEquals(200, r.getStatusCode());
    Assert.assertEquals(2, (int) r.getResult());
    r = Response.createFail(InvocationType.CONSUMER, "abc");
    Assert.assertEquals("CommonExceptionData [message=abc]", ((InvocationException) r.getResult()).getErrorData().toString());
    Assert.assertEquals(490, r.getStatusCode());
    r = Response.createFail(InvocationType.PRODUCER, "def");
    Assert.assertEquals("CommonExceptionData [message=def]", ((InvocationException) r.getResult()).getErrorData().toString());
    Assert.assertEquals(590, r.getStatusCode());
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) Test(org.junit.Test)

Aggregations

InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)24 Test (org.junit.Test)16 Response (org.apache.servicecomb.swagger.invocation.Response)8 CommonExceptionData (org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData)8 MockUp (mockit.MockUp)5 Invocation (org.apache.servicecomb.core.Invocation)5 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 Expectations (mockit.Expectations)3 RestParam (org.apache.servicecomb.common.rest.definition.RestParam)3 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)3 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)2 ArrayList (java.util.ArrayList)2 Holder (javax.xml.ws.Holder)2 AbstractTcpClientPackage (org.apache.servicecomb.foundation.vertx.client.tcp.AbstractTcpClientPackage)2 TcpResponseCallback (org.apache.servicecomb.foundation.vertx.client.tcp.TcpResponseCallback)2 Buffer (io.vertx.core.buffer.Buffer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Status (javax.ws.rs.core.Response.Status)1 Mock (mockit.Mock)1 WrapSchema (org.apache.servicecomb.codec.protobuf.utils.WrapSchema)1