Search in sources :

Example 21 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)

Example 22 with AsyncResponse

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

the class TestVertxHttpMethod method testDoMethod.

@Test
public void testDoMethod(@Mocked HttpClient httpClient, @Injectable URIEndpointObject address) throws Exception {
    Context context = new MockUp<Context>() {

        @Mock
        public void runOnContext(Handler<Void> action) {
            action.handle(null);
        }
    }.getMockInstance();
    HttpClientWithContext httpClientWithContext = new HttpClientWithContext(httpClient, context);
    Invocation invocation = mock(Invocation.class);
    AsyncResponse asyncResp = mock(AsyncResponse.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);
    when(endpoint.getAddress()).thenReturn(address);
    when(request.exceptionHandler(Mockito.any())).then(answer -> null);
    Map<String, Object> map = new HashMap<>();
    when(invocation.getHandlerContext()).then(answer -> map);
    ;
    this.doMethod(httpClientWithContext, invocation, asyncResp);
    Assert.assertTrue(true);
}
Also used : Context(io.vertx.core.Context) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) Invocation(org.apache.servicecomb.core.Invocation) RestOperationMeta(org.apache.servicecomb.common.rest.definition.RestOperationMeta) HashMap(java.util.HashMap) Mock(mockit.Mock) Endpoint(org.apache.servicecomb.core.Endpoint) HttpClientWithContext(org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext) URLPathBuilder(org.apache.servicecomb.common.rest.definition.path.URLPathBuilder) URIEndpointObject(org.apache.servicecomb.foundation.common.net.URIEndpointObject) 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 23 with AsyncResponse

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

the class TestInvoker method completableFutureInvoke_normal.

@Test
public void completableFutureInvoke_normal(@Mocked Invocation invocation, @Mocked SwaggerConsumerOperation consumerOperation, @Mocked ConsumerResponseMapper mapper) {
    Response response = Response.ok("1");
    new MockUp<InvokerUtils>() {

        @Mock
        void reactiveInvoke(Invocation invocation, AsyncResponse asyncResp) {
            asyncResp.handle(response);
            ;
        }
    };
    new Expectations() {

        {
            consumerOperation.getResponseMapper();
            result = mapper;
            mapper.mapResponse(response);
            result = 1;
        }
    };
    Invoker invoker = new Invoker("test", null, IPerson.class);
    CompletableFuture<Object> future = invoker.completableFutureInvoke(invocation, consumerOperation);
    future.whenComplete((result, ex) -> {
        Assert.assertEquals(1, result);
        Assert.assertEquals(null, ex);
    });
}
Also used : AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Response(org.apache.servicecomb.swagger.invocation.Response) Expectations(mockit.Expectations) Invocation(org.apache.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Test(org.junit.Test)

Example 24 with AsyncResponse

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

the class HighwayClient method send.

public void send(Invocation invocation, AsyncResponse asyncResp) throws Exception {
    invocation.getInvocationStageTrace().startGetConnection();
    HighwayClientConnection tcpClient = findClientPool(invocation);
    invocation.getInvocationStageTrace().startClientFiltersRequest();
    OperationProtobuf operationProtobuf = ProtobufManager.getOrCreateOperation(invocation);
    HighwayClientPackage clientPackage = createClientPackage(invocation, operationProtobuf);
    invocation.onStartSendRequest();
    tcpClient.send(clientPackage, ar -> {
        invocation.getInvocationStageTrace().finishWriteToBuffer(clientPackage.getFinishWriteToBuffer());
        invocation.getInvocationStageTrace().finishReceiveResponse();
        // 此时是在网络线程中,转换线程
        invocation.getResponseExecutor().execute(() -> {
            invocation.getInvocationStageTrace().startClientFiltersResponse();
            if (ar.failed()) {
                // 只会是本地异常
                invocation.getInvocationStageTrace().finishClientFiltersResponse();
                if (ar.cause() instanceof TimeoutException) {
                    // give an accurate cause for timeout exception
                    // The timeout period of 30000ms has been exceeded while executing GET /xxx for server 1.1.1.1:8080
                    // should not copy the message to invocationException to avoid leak server ip address
                    LOGGER.info("Request timeout, Details: {}.", ar.cause().getMessage());
                    asyncResp.consumerFail(new InvocationException(Status.REQUEST_TIMEOUT, new CommonExceptionData("Request Timeout.")));
                    return;
                }
                asyncResp.consumerFail(ar.cause());
                return;
            }
            // 处理应答
            try {
                Response response = HighwayCodec.decodeResponse(invocation, operationProtobuf, ar.result());
                invocation.getInvocationStageTrace().finishClientFiltersResponse();
                asyncResp.complete(response);
            } catch (Throwable e) {
                invocation.getInvocationStageTrace().finishClientFiltersResponse();
                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) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) CommonExceptionData(org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData) TimeoutException(java.util.concurrent.TimeoutException)

Example 25 with AsyncResponse

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

the class TestLoadBalanceHandler2 method testConfigEndpoint.

@Test
public void testConfigEndpoint() {
    ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
    InvocationRuntimeType invocationRuntimeType = Mockito.mock(InvocationRuntimeType.class);
    SchemaMeta schemaMeta = Mockito.mock(SchemaMeta.class);
    when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
    MicroserviceMeta microserviceMeta = Mockito.mock(MicroserviceMeta.class);
    when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
    when(schemaMeta.getMicroserviceName()).thenReturn("testMicroserviceName");
    when(microserviceMeta.getAppId()).thenReturn("testApp");
    when(referenceConfig.getVersionRule()).thenReturn("0.0.0+");
    when(referenceConfig.getTransport()).thenReturn("rest");
    Invocation invocation = new Invocation(referenceConfig, operationMeta, invocationRuntimeType, new HashMap<>());
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    InstanceCacheManager instanceCacheManager = Mockito.mock(InstanceCacheManager.class);
    TransportManager transportManager = Mockito.mock(TransportManager.class);
    Transport transport = Mockito.mock(Transport.class);
    ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", "false");
    // set up data
    MicroserviceInstance myself = new MicroserviceInstance();
    MicroserviceInstance findInstance = new MicroserviceInstance();
    List<String> findEndpoint = new ArrayList<>();
    findEndpoint.add("rest://localhost:9092");
    findInstance.setEndpoints(findEndpoint);
    findInstance.setInstanceId("findInstance");
    Map<String, MicroserviceInstance> data = new HashMap<>();
    DiscoveryTreeNode parent = new DiscoveryTreeNode().name("parent").data(data);
    scbEngine.setTransportManager(transportManager);
    SCBEngine.getInstance().setTransportManager(transportManager);
    LocalRegistryStore.INSTANCE.initSelfWithMocked(null, myself);
    mockUpInstanceCacheManager(instanceCacheManager);
    when(instanceCacheManager.getOrCreateVersionedCache("testApp", "testMicroserviceName", "0.0.0+")).thenReturn(parent);
    when(transportManager.findTransport("rest")).thenReturn(transport);
    data.put("findInstance", findInstance);
    parent.cacheVersion(1);
    LoadbalanceHandler handler = new LoadbalanceHandler();
    try {
        handler.handle(invocation, asyncResp);
    } catch (Exception e) {
    }
    Assert.assertEquals("rest://localhost:9092", invocation.getEndpoint().getEndpoint());
    // reset
    invocation.setEndpoint(null);
    // success
    invocation.addLocalContext("scb-endpoint", "rest://127.0.0.1:8080?sslEnabled=true&protocol=http2");
    try {
        handler.handle(invocation, asyncResp);
    } catch (Exception e) {
    }
    Assert.assertEquals("rest://127.0.0.1:8080?sslEnabled=true&protocol=http2", invocation.getEndpoint().getEndpoint());
    // reset
    invocation.setEndpoint(null);
    // endpoint format is not correct
    invocation.addLocalContext("scb-endpoint", "127.0.0.1:8080");
    try {
        handler.handle(invocation, asyncResp);
        Assert.assertEquals("endpoint's format is not correct, throw exception", " but not throw exception");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("Illegal character in scheme name"));
    }
    // reset
    invocation.setEndpoint(null);
    // transport is not find
    invocation.addLocalContext("scb-endpoint", "my://127.0.0.1:8080?sslEnabled=true&protocol=http2");
    try {
        handler.handle(invocation, asyncResp);
        Assert.assertEquals("endpoint's transport not found, throw exception", "but not throw exception");
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains("the endpoint's transport is not found."));
    }
}
Also used : NonSwaggerInvocation(org.apache.servicecomb.core.NonSwaggerInvocation) Invocation(org.apache.servicecomb.core.Invocation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) DiscoveryTreeNode(org.apache.servicecomb.registry.discovery.DiscoveryTreeNode) InvocationException(org.apache.servicecomb.swagger.invocation.exception.InvocationException) InstanceCacheManager(org.apache.servicecomb.registry.cache.InstanceCacheManager) ReferenceConfig(org.apache.servicecomb.core.provider.consumer.ReferenceConfig) SchemaMeta(org.apache.servicecomb.core.definition.SchemaMeta) MicroserviceMeta(org.apache.servicecomb.core.definition.MicroserviceMeta) InvocationRuntimeType(org.apache.servicecomb.core.definition.InvocationRuntimeType) OperationMeta(org.apache.servicecomb.core.definition.OperationMeta) TransportManager(org.apache.servicecomb.core.transport.TransportManager) AsyncResponse(org.apache.servicecomb.swagger.invocation.AsyncResponse) Transport(org.apache.servicecomb.core.Transport) Test(org.junit.Test)

Aggregations

AsyncResponse (org.apache.servicecomb.swagger.invocation.AsyncResponse)29 Invocation (org.apache.servicecomb.core.Invocation)20 Test (org.junit.Test)20 Response (org.apache.servicecomb.swagger.invocation.Response)16 MockUp (mockit.MockUp)10 ArrayList (java.util.ArrayList)9 Mock (mockit.Mock)8 List (java.util.List)7 Map (java.util.Map)6 Status (javax.ws.rs.core.Response.Status)6 Expectations (mockit.Expectations)6 Transport (org.apache.servicecomb.core.Transport)6 TransportManager (org.apache.servicecomb.core.transport.TransportManager)6 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)5 HashMap (java.util.HashMap)4 Holder (javax.xml.ws.Holder)4 Deencapsulation (mockit.Deencapsulation)4 Injectable (mockit.Injectable)4 Mocked (mockit.Mocked)4 RestOperationMeta (org.apache.servicecomb.common.rest.definition.RestOperationMeta)4