Search in sources :

Example 11 with AsyncResponse

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

the class TestVertxRestTransport method testSendException.

@Test
public void testSendException() {
    boolean validAssert;
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    URIEndpointObject endpoint = Mockito.mock(URIEndpointObject.class);
    Endpoint end = Mockito.mock(Endpoint.class);
    Mockito.when(invocation.getEndpoint()).thenReturn(end);
    Mockito.when(invocation.getEndpoint().getAddress()).thenReturn(endpoint);
    try {
        validAssert = true;
        instance.send(invocation, asyncResp);
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertFalse(validAssert);
}
Also used : Invocation(io.servicecomb.core.Invocation) Endpoint(io.servicecomb.core.Endpoint) URIEndpointObject(io.servicecomb.foundation.common.net.URIEndpointObject) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Example 12 with AsyncResponse

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

the class TestShutdownHookHandler method testShutdownHookHandlerCount.

@Test
public void testShutdownHookHandlerCount(@Mocked Response response) throws Exception {
    Deencapsulation.setField(ShutdownHookHandler.INSTANCE, "shuttingDown", false);
    ShutdownHookHandler handler = ShutdownHookHandler.INSTANCE;
    Assert.assertEquals(0, handler.getActiveCount());
    // no reply
    Invocation invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
        }
    }.getMockInstance();
    handler.handle(invocation, asyncResp -> {
    });
    Assert.assertEquals(1, requestCounter.get());
    Assert.assertEquals(1, handler.getActiveCount());
    // normal
    invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
            asyncResp.handle(response);
        }
    }.getMockInstance();
    handler.handle(invocation, asyncResp -> {
    });
    Assert.assertEquals(2, requestCounter.get());
    Assert.assertEquals(1, handler.getActiveCount());
    // next exception
    invocation = new MockUp<Invocation>() {

        @Mock
        public void next(AsyncResponse asyncResp) throws Exception {
            throw new Error();
        }
    }.getMockInstance();
    try {
        handler.handle(invocation, asyncResp -> {
        });
        Assert.assertFalse(true);
    } catch (Throwable e) {
        Assert.assertEquals(3, requestCounter.get());
        Assert.assertEquals(1, handler.getActiveCount());
    }
// reply exception
// TODO: should be fixed
//        try {
//            handler.handle(invocation, asyncResp -> {
//                throw new Error();
//            });
//
//            Assert.assertFalse(true);
//        } catch (Throwable e) {
//            Assert.assertEquals(3, requestCounter.get());
//            Assert.assertEquals(1, handler.getActiveCount());
//        }
}
Also used : Invocation(io.servicecomb.core.Invocation) MockUp(mockit.MockUp) AsyncResponse(io.servicecomb.core.AsyncResponse) Mock(mockit.Mock) InvocationException(io.servicecomb.core.exception.InvocationException) Test(org.junit.Test)

Example 13 with AsyncResponse

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

the class TestInvokerUtils method testReactiveInvoke.

@Test
public void testReactiveInvoke() {
    Invocation invocation = Mockito.mock(Invocation.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    boolean validAssert;
    try {
        InvokerUtils.reactiveInvoke(invocation, asyncResp);
        validAssert = true;
    } catch (Exception e) {
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Invocation(io.servicecomb.core.Invocation) AsyncResponse(io.servicecomb.core.AsyncResponse) InvocationException(io.servicecomb.core.exception.InvocationException) Test(org.junit.Test)

Example 14 with AsyncResponse

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

the class VertxHttpMethod method handleResponse.

protected void handleResponse(Invocation invocation, HttpClientResponse httpResponse, RestOperationMeta restOperation, AsyncResponse asyncResp) {
    ProduceProcessor produceProcessor = null;
    String contentType = httpResponse.getHeader("Content-Type");
    if (contentType != null) {
        String contentTypeForFind = contentType;
        int idx = contentType.indexOf(';');
        if (idx != -1) {
            contentTypeForFind = contentType.substring(0, idx);
        }
        produceProcessor = restOperation.findProduceProcessor(contentTypeForFind);
    }
    if (produceProcessor == null) {
        String msg = String.format("path %s, statusCode %d, reasonPhrase %s, response content-type %s is not supported", restOperation.getAbsolutePath(), httpResponse.statusCode(), httpResponse.statusMessage(), contentType);
        Exception exception = ExceptionFactory.createConsumerException(new CommonExceptionData(msg));
        asyncResp.fail(invocation.getInvocationType(), exception);
        return;
    }
    ProduceProcessor finalProduceProcessor = produceProcessor;
    httpResponse.bodyHandler(responseBuf -> {
        invocation.getResponseExecutor().execute(() -> {
            try {
                ResponseMeta responseMeta = restOperation.getOperationMeta().findResponseMeta(httpResponse.statusCode());
                Object result = finalProduceProcessor.decodeResponse(responseBuf, responseMeta.getJavaType());
                Response response = Response.create(httpResponse.statusCode(), httpResponse.statusMessage(), result);
                for (String headerName : responseMeta.getHeaders().keySet()) {
                    List<String> headerValues = httpResponse.headers().getAll(headerName);
                    for (String headerValue : headerValues) {
                        response.getHeaders().addHeader(headerName, headerValue);
                    }
                }
                asyncResp.complete(response);
            } catch (Throwable e) {
                asyncResp.fail(invocation.getInvocationType(), e);
            }
        });
    });
}
Also used : AsyncResponse(io.servicecomb.core.AsyncResponse) HttpClientResponse(io.vertx.core.http.HttpClientResponse) Response(io.servicecomb.core.Response) ProduceProcessor(io.servicecomb.common.rest.codec.produce.ProduceProcessor) ResponseMeta(io.servicecomb.swagger.invocation.response.ResponseMeta) CommonExceptionData(io.servicecomb.core.exception.CommonExceptionData)

Example 15 with AsyncResponse

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

the class TestVertxGetMethod method testVertxGetMethod.

@Test
public void testVertxGetMethod() {
    HttpClient client = Mockito.mock(HttpClient.class);
    Invocation invocation = Mockito.mock(Invocation.class);
    IpPort ipPort = Mockito.mock(IpPort.class);
    Mockito.when(ipPort.getPort()).thenReturn(10);
    assertEquals(10, ipPort.getPort());
    Mockito.when(ipPort.getHostOrIp()).thenReturn("ever");
    assertEquals("ever", ipPort.getHostOrIp());
    RestOperationMeta operation = Mockito.mock(RestOperationMeta.class);
    AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
    HttpClientRequest obj = VertxGetMethod.INSTANCE.createRequest(client, invocation, ipPort, "good", operation, asyncResp);
    Assert.assertNull(obj);
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClient(io.vertx.core.http.HttpClient) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Aggregations

AsyncResponse (io.servicecomb.core.AsyncResponse)22 Invocation (io.servicecomb.core.Invocation)18 Test (org.junit.Test)16 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)6 Response (io.servicecomb.core.Response)6 IpPort (io.servicecomb.foundation.common.net.IpPort)6 Endpoint (io.servicecomb.core.Endpoint)5 OperationMeta (io.servicecomb.core.definition.OperationMeta)5 HttpClientRequest (io.vertx.core.http.HttpClientRequest)5 MockUp (mockit.MockUp)5 HttpClient (io.vertx.core.http.HttpClient)4 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)3 HttpClientWithContext (io.servicecomb.foundation.vertx.client.http.HttpClientWithContext)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ExecutionListener (com.netflix.loadbalancer.reactive.ExecutionListener)2 ProduceProcessor (io.servicecomb.common.rest.codec.produce.ProduceProcessor)2 URLPathBuilder (io.servicecomb.common.rest.definition.path.URLPathBuilder)2 InvocationException (io.servicecomb.core.exception.InvocationException)2 SyncResponseExecutor (io.servicecomb.core.provider.consumer.SyncResponseExecutor)2 URIEndpointObject (io.servicecomb.foundation.common.net.URIEndpointObject)2