Skip to content

Commit f656083

Browse files
committed
Merge pull request #21988 from tszmytka
* gh-21988: Polish "Use Awaitility instead of Thread.sleep" Use Awaitility instead of Thread.sleep Closes gh-21988
2 parents 67604a5 + 41173f7 commit f656083

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.time.Duration;
2424
import java.util.ArrayList;
2525
import java.util.List;
26+
import java.util.Objects;
2627
import java.util.concurrent.CountDownLatch;
2728
import java.util.concurrent.TimeUnit;
2829

@@ -45,6 +46,7 @@
4546
import org.springframework.web.socket.handler.TextWebSocketHandler;
4647

4748
import static org.assertj.core.api.Assertions.assertThat;
49+
import static org.awaitility.Awaitility.await;
4850
import static org.hamcrest.Matchers.empty;
4951
import static org.hamcrest.Matchers.is;
5052
import 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

Comments
 (0)