From 4c1175acf077ab94eada8977967b926afefe4676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E7=BB=A7=E5=B3=B0?= Date: Mon, 9 Aug 2021 10:05:42 +0800 Subject: [PATCH] add retryTimes check (#6594) --- .../nacos/common/remote/client/RpcClient.java | 1 + .../common/remote/client/RpcClientTest.java | 47 +++++++++++++++---- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java index d5dfdac26..2d71b71b2 100644 --- a/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java +++ b/common/src/main/java/com/alibaba/nacos/common/remote/client/RpcClient.java @@ -784,6 +784,7 @@ public abstract class RpcClient implements Closeable { exceptionToThrow = e; } + retryTimes++; } if (rpcClientStatus.compareAndSet(RpcClientStatus.RUNNING, RpcClientStatus.UNHEALTHY)) { diff --git a/common/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java b/common/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java index e0dcecf36..0a209b346 100644 --- a/common/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/remote/client/RpcClientTest.java @@ -47,6 +47,7 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @RunWith(MockitoJUnitRunner.class) @@ -60,10 +61,10 @@ public class RpcClientTest { Field reconnectionSignalField; - Field lastActiveTimeStampField; - Field retryTimesField; + Field timeoutMillsField; + Method resolveServerInfoMethod; Answer runAsSync; @@ -107,17 +108,17 @@ public class RpcClientTest { modifiersField1.setAccessible(true); 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.setAccessible(true); Field modifiersField3 = Field.class.getDeclaredField("modifiers"); 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.setAccessible(true); @@ -360,4 +361,32 @@ public class RpcClientTest { Assert.assertNotNull(exception); 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()); + } } \ No newline at end of file