Skip to content

Commit

Permalink
add timeout to sofaRequest (#1224)
Browse files Browse the repository at this point in the history
* add timeout to sofaRequest

* add timeout to sofaRequest

Co-authored-by: liujianjun.ljj <[email protected]>
  • Loading branch information
EvenLjj and liujianjun.ljj authored Aug 9, 2022
1 parent 9a0478f commit 600ff15
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ protected SofaResponse doSendMsg(ProviderInfo providerInfo, ClientTransport tran
checkProviderVersion(providerInfo, request); // 根据服务端版本特殊处理
String invokeType = request.getInvokeType();
int timeout = resolveTimeout(request, consumerConfig, providerInfo);

request.setTimeout(timeout);
SofaResponse response = null;
// 同步调用
if (RpcConstants.INVOKER_TYPE_SYNC.equals(invokeType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -33,10 +36,12 @@
*/
public class AbstractClusterTest {

@Test
public void testDestroyWithDestroyHook() {
private static AbstractCluster abstractCluster;

@BeforeClass
public static void beforeClass() {
ConsumerConfig consumerConfig = new ConsumerConfig().setProtocol("test")
.setBootstrap("test");
.setBootstrap("test");
ConsumerBootstrap consumerBootstrap = new ConsumerBootstrap(consumerConfig) {
@Override
public Object refer() {
Expand Down Expand Up @@ -68,12 +73,16 @@ public boolean isSubscribed() {
return false;
}
};
AbstractCluster abstractCluster = new AbstractCluster(consumerBootstrap) {
abstractCluster = new AbstractCluster(consumerBootstrap) {
@Override
protected SofaResponse doInvoke(SofaRequest msg) throws SofaRpcException {
return null;
}
};
}

@Test
public void testDestroyWithDestroyHook() {
List<String> hookActionResult = new ArrayList<>(2);
Destroyable.DestroyHook destroyHook = new Destroyable.DestroyHook() {
@Override
Expand All @@ -91,4 +100,34 @@ public void postDestroy() {
Assert.assertEquals("preDestroy", hookActionResult.get(0));
Assert.assertEquals("postDestroy", hookActionResult.get(1));
}

@Test
public void testResolveTimeout() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method resolveTimeoutMethod = AbstractCluster.class.getDeclaredMethod("resolveTimeout", SofaRequest.class,
ConsumerConfig.class, ProviderInfo.class);
resolveTimeoutMethod.setAccessible(true);

SofaRequest sofaRequest = new SofaRequest();
ConsumerConfig consumerConfig = new ConsumerConfig();
ProviderInfo providerInfo = new ProviderInfo();
Integer defaultTimeout = (Integer) resolveTimeoutMethod.invoke(abstractCluster, sofaRequest, consumerConfig,
providerInfo);
Assert.assertTrue(defaultTimeout == 3000);

providerInfo.setStaticAttr(ProviderInfoAttrs.ATTR_TIMEOUT, "5000");
Integer providerTimeout = (Integer) resolveTimeoutMethod.invoke(abstractCluster, sofaRequest, consumerConfig,
providerInfo);
Assert.assertTrue(providerTimeout == 5000);

consumerConfig.setTimeout(2000);
Integer consumerTimeout = (Integer) resolveTimeoutMethod.invoke(abstractCluster, sofaRequest, consumerConfig,
providerInfo);
Assert.assertTrue(consumerTimeout == 2000);

sofaRequest.setTimeout(1000);
Integer invokeTimeout = (Integer) resolveTimeoutMethod.invoke(abstractCluster, sofaRequest, consumerConfig,
providerInfo);
Assert.assertTrue(invokeTimeout == 1000);

}
}

0 comments on commit 600ff15

Please sign in to comment.