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