2323import java .time .Duration ;
2424import java .util .ArrayList ;
2525import java .util .List ;
26+ import java .util .Objects ;
2627import java .util .concurrent .CountDownLatch ;
2728import java .util .concurrent .TimeUnit ;
2829
4546import org .springframework .web .socket .handler .TextWebSocketHandler ;
4647
4748import static org .assertj .core .api .Assertions .assertThat ;
49+ import static org .awaitility .Awaitility .await ;
4850import static org .hamcrest .Matchers .empty ;
4951import static org .hamcrest .Matchers .is ;
5052import static org .hamcrest .Matchers .not ;
@@ -88,17 +90,18 @@ void servesLivereloadJs() throws Exception {
8890 void triggerReload () throws Exception {
8991 LiveReloadWebSocketHandler handler = connect ();
9092 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+
9498 }
9599
96100 @ Test
97101 void pingPong () throws Exception {
98102 LiveReloadWebSocketHandler handler = connect ();
99103 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 ));
102105 }
103106
104107 @ Test
@@ -117,8 +120,9 @@ private void awaitClosedException() throws InterruptedException {
117120 void serverClose () throws Exception {
118121 LiveReloadWebSocketHandler handler = connect ();
119122 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 );
122126 }
123127
124128 private LiveReloadWebSocketHandler connect () throws Exception {
0 commit comments