Search in sources :

Example 31 with SofaRpcException

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

the class BoltDirectUrlTest method testAll.

// @Test
// FIXME 目前bolt的IO线程关闭时未释放,暂不支持本测试用例
public void testAll() {
    // 只有2个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
    HelloService helloService = consumerConfig.refer();
    Assert.assertNotNull(helloService.sayHello("xx", 22));
    serverConfig.getServer().stop();
    // 关闭后再调用一个抛异常
    try {
        helloService.sayHello("xx", 22);
    } catch (Exception e) {
        // 应该抛出异常
        Assert.assertTrue(e instanceof SofaRpcException);
    }
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 40));
    serverConfig.getServer().start();
    // 等待客户端重连服务端
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 60));
    Assert.assertNotNull(helloService.sayHello("xx", 22));
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Callable(java.util.concurrent.Callable)

Example 32 with SofaRpcException

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

the class H2cDirectUrlTest method testAll.

@Test
public void testAll() throws InterruptedException {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setBootstrap("h2c").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    final ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setBootstrap("h2c").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
    HttpService httpService = consumerConfig.refer();
    ExampleObj request = new ExampleObj();
    request.setId(200);
    request.setName("yyy");
    ExampleObj response = httpService.object(request);
    Assert.assertEquals(200, response.getId());
    Assert.assertEquals("yyyxx", response.getName());
    serverConfig.getServer().stop();
    // 关闭后再调用一个抛异常
    try {
        httpService.object(request);
    } catch (Exception e) {
        // 应该抛出异常
        Assert.assertTrue(e instanceof SofaRpcException);
    }
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 40));
    serverConfig.getServer().start();
    // 等待客户端重连服务端
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 60));
    response = httpService.object(request);
    Assert.assertEquals(200, response.getId());
    Assert.assertEquals("yyyxx", response.getName());
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ExampleObj(com.alipay.sofa.rpc.server.http.ExampleObj) HttpService(com.alipay.sofa.rpc.server.http.HttpService) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 33 with SofaRpcException

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

the class HystrixFilterAsyncTest method testHystrixTimeout.

@Test
public void testHystrixTimeout() {
    providerConfig = defaultServer(2000);
    providerConfig.export();
    consumerConfig = defaultClient().setTimeout(10000);
    HystrixService HystrixService = consumerConfig.refer();
    long start = System.currentTimeMillis();
    try {
        HystrixService.sayHello("abc", 24);
        Future future = SofaResponseFuture.getFuture();
        Assert.assertFalse(future.isDone());
        Assert.assertFalse(future.isCancelled());
        SofaResponseFuture.getResponse(10000, true);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e instanceof SofaRpcException);
        final Throwable cause = e.getCause();
        Assert.assertTrue(cause.getMessage(), cause instanceof HystrixRuntimeException);
        Assert.assertTrue((System.currentTimeMillis() - start) > HYSTRIX_DEFAULT_TIMEOUT);
    }
}
Also used : SofaResponseFuture(com.alipay.sofa.rpc.api.future.SofaResponseFuture) Future(java.util.concurrent.Future) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 34 with SofaRpcException

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

the class RestDirectUrlTest method testAll.

@Test
public void testAll() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    final ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setDirectUrl("rest://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
    RestService restService = consumerConfig.refer();
    Assert.assertEquals(restService.query(11), "hello world !null");
    serverConfig.getServer().stop();
    // 关闭后再调用一个抛异常
    try {
        restService.query(11);
    } catch (Exception e) {
        // 应该抛出异常
        Assert.assertTrue(e instanceof SofaRpcException);
    }
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 40));
    serverConfig.getServer().start();
    // 等待客户端重连服务端
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 60));
    Assert.assertEquals(restService.query(11), "hello world !null");
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RestServiceImpl(com.alipay.sofa.rpc.server.rest.RestServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RestService(com.alipay.sofa.rpc.server.rest.RestService) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 35 with SofaRpcException

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

the class NotFoundInvokerTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(100).setCoreThreads(10).setMaxThreads(10);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1500)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setFilterRef(Collections.<Filter>singletonList(new Filter() {

        @Override
        public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
            // 造一个假方法
            request.setMethodName(request.getMethodName() + "_unknown");
            return invoker.invoke(request);
        }
    })).setRegister(false);
    HelloService helloService = consumerConfig.refer();
    boolean ok = true;
    try {
        // 找不到方法
        helloService.sayHello("xxx", 22);
    } catch (Exception e) {
        if (e instanceof SofaRpcException) {
            Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        } else if (e instanceof UndeclaredThrowableException) {
            Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        }
        ok = false;
    }
    Assert.assertFalse(ok);
    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setRegister(false);
    EchoService echoService = consumerConfig2.refer();
    ok = true;
    try {
        echoService.echoStr("xx");
    } catch (Exception e) {
        if (e instanceof SofaRpcException) {
            Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        } else if (e instanceof UndeclaredThrowableException) {
            Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        }
        ok = false;
    }
    Assert.assertFalse(ok);
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

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