Search in sources :

Example 36 with SofaRpcException

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

the class RejectedTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(0).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(3000).setDirectUrl("bolt://127.0.0.1:22222").setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    final AtomicInteger success = new AtomicInteger();
    final AtomicInteger failure = new AtomicInteger();
    int times = 3;
    final CountDownLatch latch = new CountDownLatch(times);
    for (int i = 0; i < times; i++) {
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    helloService.sayHello("xxx", 22);
                    success.incrementAndGet();
                } catch (Exception e) {
                    if (e instanceof SofaRpcException) {
                        Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_BUSY);
                    }
                    failure.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            }
        }, "T1");
        thread1.start();
    }
    try {
        latch.await(10000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    Assert.assertEquals(success.get(), 2);
    Assert.assertEquals(failure.get(), 1);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) 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) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 37 with SofaRpcException

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

the class AbstractSerializerTest method buildDeserializeError.

@Test
public void buildDeserializeError() {
    RpcInternalContext old = RpcInternalContext.peekContext();
    try {
        RpcInternalContext.removeContext();
        SofaRpcException exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildDeserializeError("xx");
        Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, exception.getErrorType());
        RpcInternalContext.removeContext();
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.UNKNOWN, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(true);
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.SERVER_DESERIALIZE, exception.getErrorType());
        RpcInternalContext.getContext().setProviderSide(false);
        exception = serializer.buildDeserializeError("xx", new RuntimeException());
        Assert.assertEquals(RpcErrorType.CLIENT_DESERIALIZE, exception.getErrorType());
    } finally {
        RpcInternalContext.setContext(old);
    }
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 38 with SofaRpcException

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

the class ExceptionUtilsTest method testToString.

@Test
public void testToString() throws Exception {
    SofaRpcException exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, "111");
    String string = ExceptionUtils.toString(exception);
    Assert.assertNotNull(string);
    Pattern pattern = Pattern.compile("at");
    Matcher matcher = pattern.matcher(string);
    int count = 0;
    while (matcher.find()) {
        count++;
    }
    Assert.assertTrue(count > 1);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 39 with SofaRpcException

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

the class ExceptionUtilsTest method isClientException.

@Test
public void isClientException() throws Exception {
    SofaRpcException exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, "111");
    Assert.assertFalse(ExceptionUtils.isClientException(exception));
    exception = new SofaRpcException(RpcErrorType.CLIENT_TIMEOUT, "111");
    Assert.assertTrue(ExceptionUtils.isClientException(exception));
}
Also used : SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

Example 40 with SofaRpcException

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

the class ExceptionUtilsTest method isServerException.

@Test
public void isServerException() throws Exception {
    SofaRpcException exception = new SofaRpcException(RpcErrorType.SERVER_BUSY, "111");
    Assert.assertTrue(ExceptionUtils.isServerException(exception));
    exception = new SofaRpcException(RpcErrorType.CLIENT_TIMEOUT, "111");
    Assert.assertFalse(ExceptionUtils.isServerException(exception));
}
Also used : SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Test(org.junit.Test)

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