4545import org .springframework .web .socket .handler .TextWebSocketHandler ;
4646
4747import static org .assertj .core .api .Assertions .assertThat ;
48+ import static org .awaitility .Awaitility .await ;
4849import static org .hamcrest .Matchers .empty ;
4950import static org .hamcrest .Matchers .is ;
5051import static org .hamcrest .Matchers .not ;
@@ -88,17 +89,16 @@ void servesLivereloadJs() throws Exception {
8889 void triggerReload () throws Exception {
8990 LiveReloadWebSocketHandler handler = connect ();
9091 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\" " );
92+ await (). atMost ( Duration . ofSeconds ( 1 )). until ( handler :: getMessages ,
93+ ( msgs ) -> msgs .get (0 ).contains ("http://livereload.com/protocols/official-7" )
94+ && msgs . get (1 ).contains ("command\" :\" reload\" " ) );
9495 }
9596
9697 @ Test
9798 void pingPong () throws Exception {
9899 LiveReloadWebSocketHandler handler = connect ();
99100 handler .sendMessage (new PingMessage ());
100- Thread .sleep (200 );
101- assertThat (handler .getPongCount ()).isEqualTo (1 );
101+ await ().atMost (Duration .ofSeconds (1 )).until (handler ::getPongCount , is (1 ));
102102 }
103103
104104 @ Test
@@ -117,8 +117,7 @@ private void awaitClosedException() throws InterruptedException {
117117 void serverClose () throws Exception {
118118 LiveReloadWebSocketHandler handler = connect ();
119119 this .server .stop ();
120- Thread .sleep (200 );
121- assertThat (handler .getCloseStatus ().getCode ()).isEqualTo (1006 );
120+ await ().atMost (Duration .ofSeconds (1 )).until (() -> handler .getCloseStatus ().getCode (), is (1006 ));
122121 }
123122
124123 private LiveReloadWebSocketHandler connect () throws Exception {
0 commit comments