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);
}
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);
}
}
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);
}
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));
}
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));
}
Aggregations