Skip to content

Commit

Permalink
Remove duplicate code. (sofastack#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjboy authored Jun 13, 2018
1 parent 2887cc0 commit 5d0ec1e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public void sendAppException(Throwable throwable) {
*/
@Override
public void sendSofaException(SofaRpcException sofaException) {
checkState();
checkState();
SofaResponse response = new SofaResponse();
response.setErrorMsg(sofaException.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,46 @@ public void testAll() {
.setDirectUrl("bolt://127.0.0.1:22223");
AsyncHelloService asyncHelloService = AConsumer.refer();

final CountDownLatch latch = new CountDownLatch(1);
final Object[] ret = { null };

RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("A get result: {}", appResponse);
ret[0] = appResponse;
latch.countDown();
}

@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
LOGGER.info("A get app exception: {}", throwable);
latch.countDown();
}

@Override
public void onSofaException(SofaRpcException sofaException, String methodName,
RequestBase request) {
LOGGER.info("A get sofa exception: {}", sofaException);
latch.countDown();
}
});
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);
Assert.assertNull(ret0); // 第一次返回null
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");
Assert.assertNull(ret0); // 第一次返回null
try {
latch.await(5000, TimeUnit.MILLISECONDS);
latch[0].await(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertTrue(ret[0] instanceof RuntimeException);

Assert.assertNotNull(ret[0]);
// 链路异步化调用--rpc异常
ret[0] = null;
latch[0] = new CountDownLatch(1);
RpcInvokeContext.getContext().setResponseCallback(buildCallback(ret, latch));
ret0 = asyncHelloService.rpcException("xxx");
Assert.assertNull(ret0); // 第一次返回null
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)
Expand All @@ -129,4 +134,30 @@ public void onSofaException(SofaRpcException sofaException, String methodName,
String s2 = syncHelloService.sayHello("yyy", 22);
Assert.assertNotNull(s2);
}

private SofaResponseCallback buildCallback(final Object[] ret, final CountDownLatch[] latch) {
return new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
LOGGER.info("A get result: {}", appResponse);
ret[0] = appResponse;
latch[0].countDown();
}

@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
LOGGER.info("A get app exception: ", throwable);
ret[0] = throwable;
latch[0].countDown();
}

@Override
public void onSofaException(SofaRpcException sofaException, String methodName,
RequestBase request) {
LOGGER.info("A get sofa exception: ", sofaException);
ret[0] = sofaException;
latch[0].countDown();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package com.alipay.sofa.rpc.test.async;

/**
* <p></p>
* <p>
*
*
* @author <a href=mailto:[email protected]>GengZhang</a>
*/
public interface AsyncHelloService {

String sayHello(String name, int age);

String appException(String name);

String rpcException(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.alipay.sofa.rpc.test.async;

import com.alipay.sofa.rpc.context.RpcInvokeContext;
import com.alipay.sofa.rpc.core.exception.RpcErrorType;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.RequestBase;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
Expand Down Expand Up @@ -61,4 +63,34 @@ public void onAppResponse(Object appResponse, String methodName, RequestBase req
}
return "hello async无效返回"; // 如果设置了AsyncProxyResponseCallback,则此处返回其实是无效。
}

@Override
public String appException(String name) {
RpcInvokeContext context = RpcInvokeContext.getContext();
context.setTimeout(2000);
context.setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
sendAppException(new RuntimeException("1234"));
}
});

helloService.sayHello(name, 1); // B-异步调用->C
return null;
}

@Override
public String rpcException(String name) {
RpcInvokeContext context = RpcInvokeContext.getContext();
context.setTimeout(2000);
context.setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
sendSofaException(new SofaRpcException(RpcErrorType.SERVER_BUSY, "bbb"));
}
});

helloService.sayHello(name, 1); // B-异步调用->C
return null;
}
}

0 comments on commit 5d0ec1e

Please sign in to comment.