Search in sources :

Example 6 with OperationMeta

use of org.apache.servicecomb.core.definition.OperationMeta in project incubator-servicecomb-java-chassis by apache.

the class TestOperationProtobuf method testOperationProtobuf.

@Test
public void testOperationProtobuf() throws Exception {
    UnitTestMeta meta = new UnitTestMeta();
    SchemaMeta schemaMeta = meta.getOrCreateSchemaMeta(Impl.class);
    OperationMeta operationMeta = schemaMeta.findOperation("test");
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
    Assert.assertEquals(operationMeta, operationProtobuf.getOperationMeta());
    Assert.assertEquals(ArgsNotWrapSchema.class, operationProtobuf.getRequestSchema().getClass());
    Assert.assertEquals(NormalWrapSchema.class, operationProtobuf.getResponseSchema().getClass());
    WrapSchema responseSchema = operationProtobuf.findResponseSchema(200);
    Assert.assertEquals(operationProtobuf.getResponseSchema(), responseSchema);
    responseSchema = operationProtobuf.findResponseSchema(300);
    Assert.assertNotNull(responseSchema);
    Assert.assertNotEquals(operationProtobuf.getResponseSchema(), responseSchema);
}
Also used : UnitTestMeta(org.apache.servicecomb.core.unittest.UnitTestMeta) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) ArgsNotWrapSchema(org.apache.servicecomb.codec.protobuf.utils.schema.ArgsNotWrapSchema) NormalWrapSchema(org.apache.servicecomb.codec.protobuf.utils.schema.NormalWrapSchema) WrapSchema(org.apache.servicecomb.codec.protobuf.utils.WrapSchema) Test(org.junit.Test)

Example 7 with OperationMeta

use of org.apache.servicecomb.core.definition.OperationMeta in project incubator-servicecomb-java-chassis by apache.

the class TestProviderQpsFlowControlHandler method testHandleIsLimitNewRequestAsFalse.

@Test
public void testHandleIsLimitNewRequestAsFalse() throws Exception {
    Mockito.when(invocation.getContext(Const.SRC_MICROSERVICE)).thenReturn("test");
    OperationMeta mockOperationMeta = QpsControllerManagerTest.getMockOperationMeta("pojo", "server", "opr");
    Mockito.when(invocation.getOperationMeta()).thenReturn(mockOperationMeta);
    new MockUp<QpsController>() {

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

        @Mock
        protected QpsController create(String qualifiedNameKey) {
            return new QpsController(qualifiedNameKey, 12);
        }
    };
    handler.handle(invocation, asyncResp);
    Mockito.verify(invocation).next(asyncResp);
}
Also used : MockUp(mockit.MockUp) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 8 with OperationMeta

use of org.apache.servicecomb.core.definition.OperationMeta in project incubator-servicecomb-java-chassis by apache.

the class TestProviderQpsFlowControlHandler method testHandle.

@Test
public void testHandle() throws Exception {
    Mockito.when(invocation.getContext(Const.SRC_MICROSERVICE)).thenReturn("test");
    OperationMeta mockOperationMeta = QpsControllerManagerTest.getMockOperationMeta("pojo", "server", "opr");
    Mockito.when(invocation.getOperationMeta()).thenReturn(mockOperationMeta);
    new MockUp<QpsController>() {

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

        @Mock
        protected QpsController create(String qualifiedNameKey) {
            return new QpsController(qualifiedNameKey, 12);
        }
    };
    handler.handle(invocation, asyncResp);
    ArgumentCaptor<InvocationException> captor = ArgumentCaptor.forClass(InvocationException.class);
    Mockito.verify(asyncResp).producerFail(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) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) Test(org.junit.Test)

Example 9 with OperationMeta

use of org.apache.servicecomb.core.definition.OperationMeta in project incubator-servicecomb-java-chassis by apache.

the class DefaultHttpClientFilter method afterReceiveResponse.

@Override
public Response afterReceiveResponse(Invocation invocation, HttpServletResponseEx responseEx) {
    OperationMeta operationMeta = invocation.getOperationMeta();
    ResponseMeta responseMeta = operationMeta.findResponseMeta(responseEx.getStatus());
    RestOperationMeta swaggerRestOperation = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
    ProduceProcessor produceProcessor = findProduceProcessor(swaggerRestOperation, responseEx);
    if (produceProcessor == null) {
        String msg = String.format("method %s, path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", swaggerRestOperation.getHttpMethod(), swaggerRestOperation.getAbsolutePath(), responseEx.getStatus(), responseEx.getStatusType().getReasonPhrase(), responseEx.getHeader(HttpHeaders.CONTENT_TYPE));
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        return Response.consumerFailResp(exception);
    }
    Object result = null;
    try {
        result = produceProcessor.decodeResponse(responseEx.getBodyBuffer(), responseMeta.getJavaType());
    } catch (Exception e) {
        return Response.consumerFailResp(e);
    }
    Response response = Response.create(responseEx.getStatusType(), result);
    for (String headerName : responseEx.getHeaderNames()) {
        Collection<String> headerValues = responseEx.getHeaders(headerName);
        for (String headerValue : headerValues) {
            response.getHeaders().addHeader(headerName, headerValue);
        }
    }
    return response;
}
Also used : Response(org.apache.servicecomb.swagger.invocation.Response) ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) ResponseMeta(org.apache.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta)

Example 10 with OperationMeta

use of org.apache.servicecomb.core.definition.OperationMeta in project incubator-servicecomb-java-chassis by apache.

the class HighwayClient method send.

public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    HighwayClientConnectionPool tcpClientPool = clientMgr.findClientPool(invocation.isSync());
    OperationMeta operationMeta = invocation.getOperationMeta();
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(operationMeta);
    HighwayClientConnection tcpClient = tcpClientPool.findOrCreateClient(invocation.getEndpoint().getEndpoint());
    HighwayClientPackage clientPackage = new HighwayClientPackage(invocation, operationProtobuf, tcpClient);
    LOGGER.debug("Sending request by highway, qualifiedName={}, endpoint={}.", invocation.getMicroserviceQualifiedName(), invocation.getEndpoint().getEndpoint());
    tcpClient.send(clientPackage, ar -> {
        // 此时是在网络线程中,转换线程
        invocation.getResponseExecutor().execute(() -> {
            if (ar.failed()) {
                // 只会是本地异常
                asyncResp.consumerFail(ar.cause());
                return;
            }
            // 处理应答
            try {
                Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, ar.result(), tcpClient.getProtobufFeature());
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.consumerFail(e);
            }
        });
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) OperationProtobuf(org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta)

Aggregations

OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)33 Test (org.junit.Test)14 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)12 Invocation (org.apache.servicecomb.core.Invocation)9 SchemaMeta (org.apache.servicecomb.core.definition.SchemaMeta)8 MockUp (mockit.MockUp)6 AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)6 Endpoint (org.apache.servicecomb.core.Endpoint)5 MicroserviceMeta (org.apache.servicecomb.core.definition.MicroserviceMeta)5 OperationProtobuf (org.apache.servicecomb.codec.protobuf.definition.OperationProtobuf)4 Response (org.apache.servicecomb.swagger.invocation.Response)4 HttpClientRequest (io.vertx.core.http.HttpClientRequest)3 Holder (javax.xml.ws.Holder)3 WrapSchema (org.apache.servicecomb.codec.protobuf.utils.WrapSchema)3 ProduceProcessor (org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor)3 URLPathBuilder (org.apache.servicecomb.common.rest.definition.path.URLPathBuilder)3 ReactiveExecutor (org.apache.servicecomb.core.executor.ReactiveExecutor)3 URIEndpointObject (org.apache.servicecomb.foundation.common.net.URIEndpointObject)3 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)3 Buffer (io.vertx.core.buffer.Buffer)2