diff --git a/dubbo-remoting/dubbo-remoting-api/pom.xml b/dubbo-remoting/dubbo-remoting-api/pom.xml
index 73e160136e72..b09d36c64c73 100644
--- a/dubbo-remoting/dubbo-remoting-api/pom.xml
+++ b/dubbo-remoting/dubbo-remoting-api/pom.xml
@@ -58,5 +58,10 @@
log4j-slf4j-impl
test
+
+ org.awaitility
+ awaitility
+ test
+
diff --git a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTaskTest.java b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTaskTest.java
index 6c758b372a94..2484853c52af 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTaskTest.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTaskTest.java
@@ -29,6 +29,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.remoting.Constants.HEARTBEAT_CHECK_TICK;
+import static org.awaitility.Awaitility.await;
class ReconnectTimerTaskTest {
@@ -37,12 +38,13 @@ class ReconnectTimerTaskTest {
private MockChannel channel;
private ReconnectTimerTask reconnectTimerTask;
+
private HashedWheelTimer reconnectTimer;
private boolean isConnected = false;
@BeforeEach
public void setup() throws Exception {
- long tickDuration = 1000;
+ long tickDuration = 30;
reconnectTimer = new HashedWheelTimer(tickDuration / HEARTBEAT_CHECK_TICK, TimeUnit.MILLISECONDS);
channel = new MockChannel() {
@Override
@@ -71,13 +73,19 @@ void testReconnect() throws Exception {
long now = System.currentTimeMillis();
url = url.addParameter(DUBBO_VERSION_KEY, "2.1.1");
- channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now - 1000);
- channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now - 1000);
+ channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now - 2000);
+ channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now - 2000);
+
+ await().atMost(2, TimeUnit.SECONDS)
+ .pollInterval(10, TimeUnit.MILLISECONDS)
+ .untilAsserted(() -> Assertions.assertTrue(channel.getReconnectCount() > 0));
- Thread.sleep(2000L);
- Assertions.assertTrue(channel.getReconnectCount() > 0);
isConnected = true;
- Thread.sleep(2000L);
- Assertions.assertTrue(channel.getReconnectCount() > 1);
+ channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now - 2000);
+ channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now - 2000);
+
+ await().atMost(2, TimeUnit.SECONDS)
+ .pollInterval(10, TimeUnit.MILLISECONDS)
+ .untilAsserted(() -> Assertions.assertTrue(channel.getReconnectCount() > 1));
}
}