23
23
import java .time .Duration ;
24
24
import java .util .ArrayList ;
25
25
import java .util .List ;
26
+ import java .util .Objects ;
26
27
import java .util .concurrent .CountDownLatch ;
27
28
import java .util .concurrent .TimeUnit ;
28
29
45
46
import org .springframework .web .socket .handler .TextWebSocketHandler ;
46
47
47
48
import static org .assertj .core .api .Assertions .assertThat ;
49
+ import static org .awaitility .Awaitility .await ;
48
50
import static org .hamcrest .Matchers .empty ;
49
51
import static org .hamcrest .Matchers .is ;
50
52
import static org .hamcrest .Matchers .not ;
@@ -88,17 +90,18 @@ void servesLivereloadJs() throws Exception {
88
90
void triggerReload () throws Exception {
89
91
LiveReloadWebSocketHandler handler = connect ();
90
92
this .server .triggerReload ();
91
- Thread .sleep (200 );
92
- assertThat (handler .getMessages ().get (0 )).contains ("http://livereload.com/protocols/official-7" );
93
- assertThat (handler .getMessages ().get (1 )).contains ("command\" :\" reload\" " );
93
+ List <String > messages = await ().atMost (Duration .ofSeconds (10 )).until (handler ::getMessages ,
94
+ (msgs ) -> msgs .size () == 2 );
95
+ assertThat (messages .get (0 )).contains ("http://livereload.com/protocols/official-7" );
96
+ assertThat (messages .get (1 )).contains ("command\" :\" reload\" " );
97
+
94
98
}
95
99
96
100
@ Test
97
101
void pingPong () throws Exception {
98
102
LiveReloadWebSocketHandler handler = connect ();
99
103
handler .sendMessage (new PingMessage ());
100
- Thread .sleep (200 );
101
- assertThat (handler .getPongCount ()).isEqualTo (1 );
104
+ await ().atMost (Duration .ofSeconds (10 )).until (handler ::getPongCount , is (1 ));
102
105
}
103
106
104
107
@ Test
@@ -117,8 +120,9 @@ private void awaitClosedException() throws InterruptedException {
117
120
void serverClose () throws Exception {
118
121
LiveReloadWebSocketHandler handler = connect ();
119
122
this .server .stop ();
120
- Thread .sleep (200 );
121
- assertThat (handler .getCloseStatus ().getCode ()).isEqualTo (1006 );
123
+ CloseStatus closeStatus = await ().atMost (Duration .ofSeconds (10 )).until (handler ::getCloseStatus ,
124
+ Objects ::nonNull );
125
+ assertThat (closeStatus .getCode ()).isEqualTo (1006 );
122
126
}
123
127
124
128
private LiveReloadWebSocketHandler connect () throws Exception {
0 commit comments