add retryTimes check (#6594)

This commit is contained in:
孙继峰 2021-08-09 10:05:42 +08:00 committed by GitHub
parent 0b15090bab
commit 4c1175acf0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 9 deletions

View File

@ -784,6 +784,7 @@ public abstract class RpcClient implements Closeable {
exceptionToThrow = e; exceptionToThrow = e;
} }
retryTimes++;
} }
if (rpcClientStatus.compareAndSet(RpcClientStatus.RUNNING, RpcClientStatus.UNHEALTHY)) { if (rpcClientStatus.compareAndSet(RpcClientStatus.RUNNING, RpcClientStatus.UNHEALTHY)) {

View File

@ -47,6 +47,7 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
@ -60,10 +61,10 @@ public class RpcClientTest {
Field reconnectionSignalField; Field reconnectionSignalField;
Field lastActiveTimeStampField;
Field retryTimesField; Field retryTimesField;
Field timeoutMillsField;
Method resolveServerInfoMethod; Method resolveServerInfoMethod;
Answer<?> runAsSync; Answer<?> runAsSync;
@ -107,17 +108,17 @@ public class RpcClientTest {
modifiersField1.setAccessible(true); modifiersField1.setAccessible(true);
modifiersField1.setInt(reconnectionSignalField, reconnectionSignalField.getModifiers() & ~Modifier.FINAL); modifiersField1.setInt(reconnectionSignalField, reconnectionSignalField.getModifiers() & ~Modifier.FINAL);
lastActiveTimeStampField = RpcClient.class.getDeclaredField("lastActiveTimeStamp");
lastActiveTimeStampField.setAccessible(true);
Field modifiersField2 = Field.class.getDeclaredField("modifiers");
modifiersField2.setAccessible(true);
modifiersField2.setInt(reconnectionSignalField, reconnectionSignalField.getModifiers() & ~Modifier.FINAL);
retryTimesField = RpcClient.class.getDeclaredField("RETRY_TIMES"); retryTimesField = RpcClient.class.getDeclaredField("RETRY_TIMES");
retryTimesField.setAccessible(true); retryTimesField.setAccessible(true);
Field modifiersField3 = Field.class.getDeclaredField("modifiers"); Field modifiersField3 = Field.class.getDeclaredField("modifiers");
modifiersField3.setAccessible(true); modifiersField3.setAccessible(true);
modifiersField3.setInt(reconnectionSignalField, reconnectionSignalField.getModifiers() & ~Modifier.FINAL); modifiersField3.setInt(retryTimesField, retryTimesField.getModifiers() & ~Modifier.FINAL);
timeoutMillsField = RpcClient.class.getDeclaredField("DEFAULT_TIMEOUT_MILLS");
timeoutMillsField.setAccessible(true);
Field modifiersField4 = Field.class.getDeclaredField("modifiers");
modifiersField4.setAccessible(true);
modifiersField4.setInt(timeoutMillsField, timeoutMillsField.getModifiers() & ~Modifier.FINAL);
resolveServerInfoMethod = RpcClient.class.getDeclaredMethod("resolveServerInfo", String.class); resolveServerInfoMethod = RpcClient.class.getDeclaredMethod("resolveServerInfo", String.class);
resolveServerInfoMethod.setAccessible(true); resolveServerInfoMethod.setAccessible(true);
@ -360,4 +361,32 @@ public class RpcClientTest {
Assert.assertNotNull(exception); Assert.assertNotNull(exception);
Assert.assertEquals(RpcClientStatus.UNHEALTHY, rpcClient.rpcClientStatus.get()); Assert.assertEquals(RpcClientStatus.UNHEALTHY, rpcClient.rpcClientStatus.get());
} }
@Test(expected = NacosException.class)
public void testRequestFutureWhenClientAlreadyShutDownThenThrowException() throws NacosException {
rpcClient.rpcClientStatus.set(RpcClientStatus.SHUTDOWN);
rpcClient.currentConnection = connection;
rpcClient.requestFuture(null);
}
@Test
public void testRequestFutureWhenRetryReachMaxRetryTimesThenSwitchServer() throws NacosException, IllegalAccessException {
timeoutMillsField.set(rpcClient, 5000L);
retryTimesField.set(rpcClient, 3);
rpcClient.rpcClientStatus.set(RpcClientStatus.RUNNING);
rpcClient.currentConnection = connection;
doThrow(NacosException.class).when(connection).requestFuture(any());
Exception exception = null;
try {
rpcClient.requestFuture(null);
} catch (NacosException e) {
exception = e;
}
verify(connection, times(3)).requestFuture(any());
verify(rpcClient).switchServerAsyncOnRequestFail();
Assert.assertNotNull(exception);
Assert.assertEquals(RpcClientStatus.UNHEALTHY, rpcClient.rpcClientStatus.get());
}
} }