use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.
the class Http2ClearTextExceptionTest method testProtobuf.
@Test
public void testProtobuf() throws InterruptedException {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setServer(serverConfig).setApplication(new ApplicationConfig().setAppName("serverApp")).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
try {
EchoResponse response = httpService.echoPb(request);
Assert.fail();
} catch (SofaRpcException e) {
Assert.assertTrue(e.getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaRpcException);
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
EchoResponse response = httpService3.echoPb(request);
Assert.assertNull(response);
ResponseFuture future = RpcInvokeContext.getContext().getFuture();
try {
future.get();
Assert.fail();
} catch (ExecutionException e) {
SofaRpcException re = (SofaRpcException) e.getCause();
Assert.assertTrue(re.getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
{
final Object[] result = new Object[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setTimeout(500).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
result[0] = throwable;
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException exception, String methodName, RequestBase request) {
result[0] = exception;
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
EchoResponse response = httpService4.echoPb(request);
Assert.assertNull(response);
latch.await(2000, TimeUnit.MILLISECONDS);
Throwable e = (Throwable) result[0];
Assert.assertTrue(e instanceof SofaRpcException);
Assert.assertTrue(((SofaRpcException) e).getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.
the class Http2ClearTextHessianTest method testHessian.
@Test
public void testHessian() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
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());
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService3.object(request);
Assert.assertNull(response);
ResponseFuture<ExampleObj> future = RpcInvokeContext.getContext().getFuture();
try {
response = future.get();
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
{
final ExampleObj[] result = new ExampleObj[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = (ExampleObj) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService4.object(request);
Assert.assertNull(response);
try {
latch.await(2000, TimeUnit.MILLISECONDS);
response = result[0];
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.
the class AsyncCallbackTest method testCallbackCallerHandleException.
@Test
public void testCallbackCallerHandleException() {
serverConfig = new ServerConfig().setPort(22223).setDaemon(false);
// RpcServer for C
CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(0)).setServer(serverConfig);
CProvider.export();
// RpcClient For B invoke C
Filter filter = new TestAsyncFilter();
BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(3000).setFilterRef(Arrays.asList(filter)).setRejectedExecutionPolicy(RejectedExecutionPolicy.CALLER_HANDLE_EXCEPTION.name()).setDirectUrl("bolt://127.0.0.1:22223");
HelloService helloService = BConsumer.refer();
int maxsize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_MAX);
int queuesize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_QUEUE);
int invokeCount = (maxsize + queuesize) * 2;
final CountDownLatch latch = new CountDownLatch(invokeCount);
AtomicInteger sofaExceptionCount = new AtomicInteger(0);
SofaResponseCallback callback = new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("B get result: {}", appResponse);
try {
Thread.sleep(100);
} catch (Exception e) {
}
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);
latch.countDown();
sofaExceptionCount.addAndGet(1);
}
};
for (int i = 0; i < invokeCount; i++) {
RpcInvokeContext.getContext().setResponseCallback(callback);
helloService.sayHello("" + i, 33);
}
try {
latch.await(100L * invokeCount, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
// Exception callbacks are triggered by IO threads after the thread pool is full, so the total number of responses received is equal to the total number of calls
assertEquals(0, latch.getCount());
// The number of exception callbacks triggered by IO threads must exist
assertTrue(sofaExceptionCount.get() > 0);
RpcInvokeContext.removeContext();
}
use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.
the class AsyncCallbackTest method testCallbackDiscard.
@Test
public void testCallbackDiscard() {
serverConfig = new ServerConfig().setPort(22225).setDaemon(false);
// RpcServer For C
CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(0)).setServer(serverConfig);
CProvider.export();
// RpcClient For B invoke C
Filter filter = new TestAsyncFilter();
BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(3000).setFilterRef(Arrays.asList(filter)).setRejectedExecutionPolicy(RejectedExecutionPolicy.DISCARD.name()).setDirectUrl("bolt://127.0.0.1:22225");
HelloService helloService = BConsumer.refer();
int maxsize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_MAX);
int queuesize = RpcConfigs.getIntValue(RpcOptions.ASYNC_POOL_QUEUE);
int invokeCount = (maxsize + queuesize) * 2;
final CountDownLatch latch = new CountDownLatch(invokeCount);
SofaResponseCallback callback = new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("B get result: {}", appResponse);
try {
Thread.sleep(100);
} catch (Exception e) {
}
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);
latch.countDown();
}
};
for (int i = 0; i < invokeCount; i++) {
RpcInvokeContext.getContext().setResponseCallback(callback);
helloService.sayHello("" + i, 33);
}
try {
latch.await(100L * invokeCount, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
LOGGER.info("Discard response callback: " + (invokeCount - latch.getCount()));
// The total number of callbacks received in discard mode must be less than the total number of requests
assertTrue(latch.getCount() > 0);
RpcInvokeContext.removeContext();
}
use of com.alipay.sofa.rpc.core.invoke.SofaResponseCallback in project sofa-rpc by sofastack.
the class AsyncCallbackTest method testAll.
@Test
public void testAll() {
serverConfig = new ServerConfig().setPort(22220).setDaemon(false);
// C服务的服务端
CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig);
CProvider.export();
// B调C的客户端
Filter filter = new TestAsyncFilter();
BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(50000).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22220");
HelloService helloService = BConsumer.refer();
final CountDownLatch latch = new CountDownLatch(1);
final String[] ret = { null };
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("B get result: {}", appResponse);
ret[0] = (String) 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);
latch.countDown();
}
});
String ret0 = helloService.sayHello("xxx", 22);
// 第一次返回null
Assert.assertNull(ret0);
try {
latch.await(60000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertNotNull(ret[0]);
// 过滤器生效
assertTrue(ret[0].endsWith("append by async filter"));
RpcInvokeContext.removeContext();
}
Aggregations