From c13385ea7471783c5e50afd5c504dd42a662927e Mon Sep 17 00:00:00 2001 From: Tomek Szmytka Date: Tue, 16 Jun 2020 20:34:48 +0200 Subject: [PATCH 1/2] Use Awaitility instead of Thread.sleep See gh-21988 --- .../devtools/livereload/LiveReloadServerTests.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java index b7d0aef8c3..96449d36d0 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java @@ -45,6 +45,7 @@ import org.springframework.web.socket.client.standard.StandardWebSocketClient; import org.springframework.web.socket.handler.TextWebSocketHandler; import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -88,17 +89,16 @@ class LiveReloadServerTests { void triggerReload() throws Exception { LiveReloadWebSocketHandler handler = connect(); this.server.triggerReload(); - Thread.sleep(200); - assertThat(handler.getMessages().get(0)).contains("http://livereload.com/protocols/official-7"); - assertThat(handler.getMessages().get(1)).contains("command\":\"reload\""); + await().atMost(Duration.ofSeconds(1)).until(handler::getMessages, + (msgs) -> msgs.get(0).contains("http://livereload.com/protocols/official-7") + && msgs.get(1).contains("command\":\"reload\"")); } @Test void pingPong() throws Exception { LiveReloadWebSocketHandler handler = connect(); handler.sendMessage(new PingMessage()); - Thread.sleep(200); - assertThat(handler.getPongCount()).isEqualTo(1); + await().atMost(Duration.ofSeconds(1)).until(handler::getPongCount, is(1)); } @Test @@ -117,8 +117,7 @@ class LiveReloadServerTests { void serverClose() throws Exception { LiveReloadWebSocketHandler handler = connect(); this.server.stop(); - Thread.sleep(200); - assertThat(handler.getCloseStatus().getCode()).isEqualTo(1006); + await().atMost(Duration.ofSeconds(1)).until(() -> handler.getCloseStatus().getCode(), is(1006)); } private LiveReloadWebSocketHandler connect() throws Exception { From 41173f7c4019e69400c0789def787e29be543e29 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 19 Jun 2020 08:17:35 +0100 Subject: [PATCH 2/2] Polish "Use Awaitility instead of Thread.sleep" See gh-21988 --- .../livereload/LiveReloadServerTests.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java index 96449d36d0..8b15e1e152 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/livereload/LiveReloadServerTests.java @@ -23,6 +23,7 @@ import java.net.URI; import java.time.Duration; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -89,16 +90,18 @@ class LiveReloadServerTests { void triggerReload() throws Exception { LiveReloadWebSocketHandler handler = connect(); this.server.triggerReload(); - await().atMost(Duration.ofSeconds(1)).until(handler::getMessages, - (msgs) -> msgs.get(0).contains("http://livereload.com/protocols/official-7") - && msgs.get(1).contains("command\":\"reload\"")); + List messages = await().atMost(Duration.ofSeconds(10)).until(handler::getMessages, + (msgs) -> msgs.size() == 2); + assertThat(messages.get(0)).contains("http://livereload.com/protocols/official-7"); + assertThat(messages.get(1)).contains("command\":\"reload\""); + } @Test void pingPong() throws Exception { LiveReloadWebSocketHandler handler = connect(); handler.sendMessage(new PingMessage()); - await().atMost(Duration.ofSeconds(1)).until(handler::getPongCount, is(1)); + await().atMost(Duration.ofSeconds(10)).until(handler::getPongCount, is(1)); } @Test @@ -117,7 +120,9 @@ class LiveReloadServerTests { void serverClose() throws Exception { LiveReloadWebSocketHandler handler = connect(); this.server.stop(); - await().atMost(Duration.ofSeconds(1)).until(() -> handler.getCloseStatus().getCode(), is(1006)); + CloseStatus closeStatus = await().atMost(Duration.ofSeconds(10)).until(handler::getCloseStatus, + Objects::nonNull); + assertThat(closeStatus.getCode()).isEqualTo(1006); } private LiveReloadWebSocketHandler connect() throws Exception {