Search in sources :

Example 11 with AsyncResponse

use of org.apache.servicecomb.swagger.invocation.AsyncResponse 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)

Example 12 with AsyncResponse

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

the class TestHighwayTransport method testSendException.

@Test
public void testSendException() throws Exception {
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    commonHighwayMock(invocation);
    Holder<Boolean> sended = new Holder<>(false);
    new MockUp<HighwayClient>() {

        @Mock
        public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
            sended.value = true;
        }
    };
    transport.send(invocation, asyncResp);
    Assert.assertTrue(sended.value);
}
Also used : Invocation(org.apache.servicecomb.core.Invocation) Holder(javax.xml.ws.Holder) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 13 with AsyncResponse

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

the class TestInvoker method completableFutureInvoke_failed.

@Test
public void completableFutureInvoke_failed(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
    Throwable error = new Error("failed");
    Response response = Response.createConsumerFail(error);
    new MockUp<InvokerUtils>() {

        @Mock
        void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
            asyncResp.handle(response);
            ;
        }
    };
    Invoker invoker = new Invoker("test", null, IPerson.class);
    CompletableFuture<Object> future = invoker.completableFutureInvoke(invocation, consumerOperation);
    future.whenComplete((result, ex) -> {
        Assert.assertEquals(null, result);
        Assert.assertSame(error, ex);
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 14 with AsyncResponse

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

the class TestVertxHttpMethod method testHandleResponse.

@Test
public void testHandleResponse() {
    boolean status = false;
    try {
        Invocation invocation = mock(Invocation.class);
        AsyncResponse asyncResp = mock(AsyncResponse.class);
        HttpClientResponse httpResponse = mock(HttpClientResponse.class);
        OperationMeta operationMeta = mock(OperationMeta.class);
        RestOperationMeta swaggerRestOperation = mock(RestOperationMeta.class);
        Endpoint endpoint = mock(Endpoint.class);
        when(invocation.getOperationMeta()).thenReturn(operationMeta);
        URLPathBuilder urlPathBuilder = mock(URLPathBuilder.class);
        when(swaggerRestOperation.getPathBuilder()).thenReturn(urlPathBuilder);
        operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
        when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
        when(invocation.getEndpoint()).thenReturn(endpoint);
        String contentType = httpResponse.getHeader("Content-Type");
        ProduceProcessor produceProcessor = mock(ProduceProcessor.class);
        when(swaggerRestOperation.findProduceProcessor(contentType)).thenReturn(produceProcessor);
        this.handleResponse(invocation, httpResponse, asyncResp);
    } catch (Exception ex) {
        status = true;
    }
    Assert.assertFalse(status);
}
Also used : ProduceProcessor(org.apache.servicecomb.common.rest.codec.produce.ProduceProcessor) Invocation(org.apache.servicecomb.core.Invocation) Endpoint(org.apache.servicecomb.core.Endpoint) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) HttpClientResponse(io.vertx.core.http.HttpClientResponse) URLPathBuilder(org.apache.servicecomb.common.rest.definition.path.URLPathBuilder) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 15 with AsyncResponse

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

the class TestVertxHttpMethod method testCreateRequest.

@Test
public void testCreateRequest() {
    HttpClient client = mock(HttpClient.class);
    Invocation invocation = mock(Invocation.class);
    OperationMeta operationMeta = mock(OperationMeta.class);
    Endpoint endpoint = mock(Endpoint.class);
    URIEndpointObject address = mock(URIEndpointObject.class);
    when(invocation.getEndpoint()).thenReturn(endpoint);
    when(endpoint.getAddress()).thenReturn(address);
    when(address.isSslEnabled()).thenReturn(false);
    when(invocation.getOperationMeta()).thenReturn(operationMeta);
    RestOperationMeta swaggerRestOperation = mock(RestOperationMeta.class);
    when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerRestOperation);
    IpPort ipPort = mock(IpPort.class);
    when(ipPort.getPort()).thenReturn(10);
    when(ipPort.getHostOrIp()).thenReturn("ever");
    AsyncResponse asyncResp = mock(AsyncResponse.class);
    List<HttpMethod> methods = new ArrayList<>(Arrays.asList(HttpMethod.GET, HttpMethod.PUT, HttpMethod.POST, HttpMethod.DELETE, HttpMethod.PATCH));
    for (HttpMethod method : methods) {
        when(swaggerRestOperation.getHttpMethod()).thenReturn(method.toString());
        HttpClientRequest obj = VertxHttpMethod.INSTANCE.createRequest(client, invocation, ipPort, "good", asyncResp);
        Assert.assertNull(obj);
    }
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) Invocation(org.apache.servicecomb.core.Invocation) Endpoint(org.apache.servicecomb.core.Endpoint) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) HttpClient(io.vertx.core.http.HttpClient) ArrayList(java.util.ArrayList) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) HttpMethod(io.vertx.core.http.HttpMethod) Test(org.junit.Test)

Aggregations

AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)17 Test (org.junit.Test)14 Invocation (org.apache.servicecomb.core.Invocation)13 Response (org.apache.servicecomb.swagger.invocation.Response)8 MockUp (mockit.MockUp)6 ArrayList (java.util.ArrayList)5 Mock (mockit.Mock)5 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 Holder (javax.xml.ws.Holder)4 IRule (com.netflix.loadbalancer.IRule)3 Server (com.netflix.loadbalancer.Server)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Expectations (mockit.Expectations)3 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)3 Endpoint (org.apache.servicecomb.core.Endpoint)3 VersionedCache (org.apache.servicecomb.foundation.common.cache.VersionedCache)3 CacheEndpoint (org.apache.servicecomb.serviceregistry.cache.CacheEndpoint)3 DynamicPropertyFactory (com.netflix.config.DynamicPropertyFactory)2