2929
3030import static org .apache .dubbo .common .constants .CommonConstants .DUBBO_VERSION_KEY ;
3131import static org .apache .dubbo .remoting .Constants .HEARTBEAT_CHECK_TICK ;
32+ import static org .awaitility .Awaitility .await ;
3233
3334class 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