Skip to content

Commit c13385e

Browse files
tszmytkawilkinsona
authored andcommitted
Use Awaitility instead of Thread.sleep
See gh-21988
1 parent 67604a5 commit c13385e

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.web.socket.handler.TextWebSocketHandler;
4646

4747
import static org.assertj.core.api.Assertions.assertThat;
48+
import static org.awaitility.Awaitility.await;
4849
import static org.hamcrest.Matchers.empty;
4950
import static org.hamcrest.Matchers.is;
5051
import 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

Comments
 (0)