Search in sources :

Example 71 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class AsyncCallbackTest method testNoProviderException.

@Test
public void testNoProviderException() {
    // use bolt, so callback will throw connection closed exception
    serverConfig = new ServerConfig().setPort(22222).setDaemon(false).setProtocol("rest");
    serverConfig.buildIfAbsent().start();
    // B调C的客户端
    Filter filter = new TestAsyncFilter();
    BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(1000).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22222");
    HelloService helloService = BConsumer.refer();
    final CountDownLatch latch = new CountDownLatch(1);
    final String[] ret = { null };
    final boolean[] hasExp = { false };
    RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("B get result: {}", appResponse);
            latch.countDown();
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("B get app exception: {}", throwable);
            latch.countDown();
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("B get sofa exception: {}", sofaException);
            if ((sofaException instanceof SofaTimeOutException)) {
                hasExp[0] = false;
            } else {
                hasExp[0] = true;
            }
            latch.countDown();
        }
    });
    String ret0 = helloService.sayHello("xxx", 22);
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch.await(1500, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    // 一定是一个超时异常
    assertTrue(hasExp[0]);
    RpcInvokeContext.removeContext();
}
Also used : HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 72 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class AsyncCallbackTest method testTimeoutException.

@Test
public void testTimeoutException() {
    serverConfig = new ServerConfig().setPort(22221).setDaemon(false);
    // C服务的服务端
    CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(500)).setServer(serverConfig);
    CProvider.export();
    // B调C的客户端
    Filter filter = new TestAsyncFilter();
    BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(1).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22221");
    HelloService helloService = BConsumer.refer();
    final CountDownLatch latch = new CountDownLatch(1);
    final String[] ret = { null };
    final boolean[] hasExp = { false };
    RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("B get result: {}", appResponse);
            latch.countDown();
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("B get app exception: {}", throwable);
            latch.countDown();
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("B get sofa exception: {}", sofaException);
            if (sofaException instanceof SofaTimeOutException) {
                hasExp[0] = true;
            }
            latch.countDown();
        }
    });
    String ret0 = helloService.sayHello("xxx", 22);
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch.await(2000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    // 一定是一个超时异常
    assertTrue(hasExp[0]);
    RpcInvokeContext.removeContext();
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 73 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class FutureTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
    // 服务端
    ProviderConfig<HelloService> CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig2);
    CProvider.export();
    Filter filter = new TestAsyncFilter();
    // 客户端
    ConsumerConfig<HelloService> BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setTimeout(5000).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22222");
    HelloService helloService = BConsumer.refer();
    // 正常
    boolean error = false;
    try {
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture();
        ret = (String) future.get();
        Assert.assertNotNull(ret);
        // 过滤器生效
        Assert.assertTrue(ret.endsWith("append by async filter"));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
    // 客户端超时,设置 < 等待
    error = false;
    long start = System.currentTimeMillis();
    long end;
    try {
        RpcInvokeContext.getContext().setTimeout(300);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture(true);
        Assert.assertNull(RpcInvokeContext.getContext().getFuture());
        ret = (String) future.get(1000, TimeUnit.MILLISECONDS);
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof InvokeTimeoutException);
        error = true;
    } finally {
        end = System.currentTimeMillis();
        LOGGER.info("elapsed time " + (end - start) + "ms");
        Assert.assertTrue((end - start) < 400);
    }
    Assert.assertTrue(error);
    // 客户端超时,设置 > 等待
    error = false;
    start = System.currentTimeMillis();
    try {
        RpcInvokeContext.getContext().setTimeout(1000);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture(true);
        Assert.assertNull(RpcInvokeContext.getContext().getFuture());
        ret = (String) future.get(300, TimeUnit.MILLISECONDS);
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof TimeoutException);
        error = true;
    } finally {
        end = System.currentTimeMillis();
        LOGGER.info("elapsed time " + (end - start) + "ms");
        Assert.assertTrue((end - start) < 400);
    }
    Assert.assertTrue(error);
    // 客户端获取的时候  服务端还没返回
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(500);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Future future = SofaResponseFuture.getFuture();
        // 500ms 过去, 但是 1s 还没到,服务端还没返回
        ret = (String) future.get();
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof TimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
    // 客户端获取的时候 服务端已经返回但是超时了
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(500);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        // 1s 过去,被rpc设置超时了
        Thread.sleep(1500);
        Future future = SofaResponseFuture.getFuture();
        ret = (String) future.get();
        Assert.assertNotNull(ret);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof ExecutionException);
        Assert.assertTrue(e.getCause() instanceof InvokeTimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
    // 服务端超时
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(500);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Object ret1 = SofaResponseFuture.getResponse(300, true);
        Assert.assertNotNull(ret1);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaRpcException);
        Assert.assertTrue(e.getCause() instanceof TimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
    // 服务端超时
    error = false;
    try {
        RpcInvokeContext.getContext().setTimeout(300);
        String ret = helloService.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret);
        Object ret1 = SofaResponseFuture.getResponse(500, true);
        Assert.assertNotNull(ret1);
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaRpcException);
        Assert.assertTrue(e.getCause() instanceof InvokeTimeoutException);
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) SofaResponseFuture(com.alipay.sofa.rpc.api.future.SofaResponseFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) InvokeTimeoutException(com.alipay.remoting.rpc.exception.InvokeTimeoutException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 74 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class AsyncChainTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
    // C服务的服务端
    ProviderConfig<HelloService> CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig2);
    CProvider.export();
    // B调C的客户端
    ConsumerConfig<HelloService> BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(3000).setDirectUrl("bolt://127.0.0.1:22222");
    HelloService helloService = BConsumer.refer();
    // B服务的服务端
    ServerConfig serverConfig3 = new ServerConfig().setPort(22223).setDaemon(false);
    ProviderConfig<AsyncHelloService> BProvider = new ProviderConfig<AsyncHelloService>().setInterfaceId(AsyncHelloService.class.getName()).setRef(new AsyncHelloServiceImpl(helloService)).setServer(serverConfig3);
    BProvider.export();
    // A调B的客户端
    ConsumerConfig<AsyncHelloService> AConsumer = new ConsumerConfig<AsyncHelloService>().setInterfaceId(AsyncHelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(3000).setDirectUrl("bolt://127.0.0.1:22223");
    AsyncHelloService asyncHelloService = AConsumer.refer();
    final CountDownLatch[] latch = new CountDownLatch[1];
    latch[0] = new CountDownLatch(1);
    final Object[] ret = new Object[1];
    // 链路异步化调用--正常
    RpcInvokeContext.getContext().setResponseCallback(buildCallback(ret, latch));
    String ret0 = asyncHelloService.sayHello("xxx", 22);
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch[0].await(5000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    Assert.assertTrue(ret[0] instanceof String);
    // 链路异步化调用--业务异常
    ret[0] = null;
    latch[0] = new CountDownLatch(1);
    RpcInvokeContext.getContext().setResponseCallback(buildCallback(ret, latch));
    ret0 = asyncHelloService.appException("xxx");
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch[0].await(5000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    Assert.assertTrue(ret[0] instanceof RuntimeException);
    // 链路异步化调用--rpc异常
    ret[0] = null;
    latch[0] = new CountDownLatch(1);
    RpcInvokeContext.getContext().setResponseCallback(buildCallback(ret, latch));
    ret0 = asyncHelloService.rpcException("xxx");
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch[0].await(5000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    Assert.assertTrue(ret[0] instanceof SofaRpcException);
    Assert.assertTrue(((SofaRpcException) ret[0]).getMessage().contains("bbb"));
    // 非链路异步化调用--普通
    ConsumerConfig<AsyncHelloService> AConsumer2 = new ConsumerConfig<AsyncHelloService>().setInterfaceId(AsyncHelloService.class.getName()).setTimeout(3000).setDirectUrl("bolt://127.0.0.1:22223");
    AsyncHelloService syncHelloService = AConsumer2.refer();
    String s2 = syncHelloService.sayHello("yyy", 22);
    Assert.assertNotNull(s2);
}
Also used : HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 75 with SofaRpcException

use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.

the class AbstractHttp2ClientTransport method asyncSend.

@Override
public ResponseFuture asyncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    try {
        beforeSend(context, request);
        return doInvokeAsync(request, context, timeout);
    } catch (SofaRpcException e) {
        throw e;
    } catch (Exception e) {
        throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, e.getMessage(), e);
    } finally {
        afterSend(context, request);
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) TimeoutException(java.util.concurrent.TimeoutException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Aggregations

SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)91 Test (org.junit.Test)35 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)28 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)27 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)24 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)23 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)22 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)19 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)16 HelloService (com.alipay.sofa.rpc.test.HelloService)16 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)15 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)14 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)14 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)11 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)11 HashMap (java.util.HashMap)9 Filter (com.alipay.sofa.rpc.filter.Filter)8 InvokeTimeoutException (com.alipay.remoting.rpc.exception.InvokeTimeoutException)7