45
45
import org .springframework .web .socket .handler .TextWebSocketHandler ;
46
46
47
47
import static org .assertj .core .api .Assertions .assertThat ;
48
+ import static org .awaitility .Awaitility .await ;
48
49
import static org .hamcrest .Matchers .empty ;
49
50
import static org .hamcrest .Matchers .is ;
50
51
import static org .hamcrest .Matchers .not ;
@@ -88,17 +89,16 @@ void servesLivereloadJs() throws Exception {
88
89
void triggerReload () throws Exception {
89
90
LiveReloadWebSocketHandler handler = connect ();
90
91
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\" " ) );
94
95
}
95
96
96
97
@ Test
97
98
void pingPong () throws Exception {
98
99
LiveReloadWebSocketHandler handler = connect ();
99
100
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 ));
102
102
}
103
103
104
104
@ Test
@@ -117,8 +117,7 @@ private void awaitClosedException() throws InterruptedException {
117
117
void serverClose () throws Exception {
118
118
LiveReloadWebSocketHandler handler = connect ();
119
119
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 ));
122
121
}
123
122
124
123
private LiveReloadWebSocketHandler connect () throws Exception {
0 commit comments