Skip to content

Commit 615d25b

Browse files
test: optimize ReconnectTimerTaskTest execution time
1 parent 70133d3 commit 615d25b

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

dubbo-remoting/dubbo-remoting-api/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,10 @@
5858
<artifactId>log4j-slf4j-impl</artifactId>
5959
<scope>test</scope>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.awaitility</groupId>
63+
<artifactId>awaitility</artifactId>
64+
<scope>test</scope>
65+
</dependency>
6166
</dependencies>
6267
</project>

dubbo-remoting/dubbo-remoting-api/src/test/java/org/apache/dubbo/remoting/exchange/support/header/ReconnectTimerTaskTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
3131
import static org.apache.dubbo.remoting.Constants.HEARTBEAT_CHECK_TICK;
32+
import static org.awaitility.Awaitility.await;
3233

3334
class ReconnectTimerTaskTest {
3435

@@ -37,12 +38,13 @@ class ReconnectTimerTaskTest {
3738
private MockChannel channel;
3839

3940
private ReconnectTimerTask reconnectTimerTask;
41+
4042
private HashedWheelTimer reconnectTimer;
4143
private boolean isConnected = false;
4244

4345
@BeforeEach
4446
public void setup() throws Exception {
45-
long tickDuration = 1000;
47+
long tickDuration = 30;
4648
reconnectTimer = new HashedWheelTimer(tickDuration / HEARTBEAT_CHECK_TICK, TimeUnit.MILLISECONDS);
4749
channel = new MockChannel() {
4850
@Override
@@ -71,13 +73,19 @@ void testReconnect() throws Exception {
7173
long now = System.currentTimeMillis();
7274

7375
url = url.addParameter(DUBBO_VERSION_KEY, "2.1.1");
74-
channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now - 1000);
75-
channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now - 1000);
76+
channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now - 2000);
77+
channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now - 2000);
78+
79+
await().atMost(2, TimeUnit.SECONDS)
80+
.pollInterval(10, TimeUnit.MILLISECONDS)
81+
.untilAsserted(() -> Assertions.assertTrue(channel.getReconnectCount() > 0));
7682

77-
Thread.sleep(2000L);
78-
Assertions.assertTrue(channel.getReconnectCount() > 0);
7983
isConnected = true;
80-
Thread.sleep(2000L);
81-
Assertions.assertTrue(channel.getReconnectCount() > 1);
84+
channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now - 2000);
85+
channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now - 2000);
86+
87+
await().atMost(2, TimeUnit.SECONDS)
88+
.pollInterval(10, TimeUnit.MILLISECONDS)
89+
.untilAsserted(() -> Assertions.assertTrue(channel.getReconnectCount() > 1));
8290
}
8391
}

0 commit comments

Comments
 (0)