Skip to content

Commit 1b237de

Browse files
committed
Use Awaitility in our own tests
Closes gh-18227
1 parent 568caa1 commit 1b237de

File tree

28 files changed

+403
-348
lines changed

28 files changed

+403
-348
lines changed

spring-boot-project/spring-boot-actuator/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@
345345
<artifactId>jersey-media-json-jackson</artifactId>
346346
<scope>test</scope>
347347
</dependency>
348+
<dependency>
349+
<groupId>org.awaitility</groupId>
350+
<artifactId>awaitility</artifactId>
351+
<scope>test</scope>
352+
</dependency>
348353
<dependency>
349354
<groupId>com.jayway.jsonpath</groupId>
350355
<artifactId>json-path</artifactId>

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/management/HeapDumpWebEndpointWebIntegrationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.time.Duration;
2122
import java.util.concurrent.TimeUnit;
2223

24+
import org.awaitility.Awaitility;
2325
import org.junit.jupiter.api.BeforeEach;
2426

2527
import org.springframework.boot.actuate.endpoint.web.test.WebEndpointTest;
@@ -31,7 +33,7 @@
3133
import org.springframework.test.web.reactive.server.WebTestClient;
3234
import org.springframework.util.FileCopyUtils;
3335

34-
import static org.assertj.core.api.Assertions.assertThat;
36+
import static org.hamcrest.Matchers.is;
3537

3638
/**
3739
* Integration tests for {@link HeapDumpWebEndpoint} exposed by Jersey, Spring MVC, and
@@ -64,11 +66,7 @@ void getRequestShouldReturnHeapDumpInResponseBody(WebTestClient client) throws E
6466
}
6567

6668
private void assertHeapDumpFileIsDeleted() throws InterruptedException {
67-
long end = System.currentTimeMillis() + 5000;
68-
while (System.currentTimeMillis() < end && this.endpoint.file.exists()) {
69-
Thread.sleep(100);
70-
}
71-
assertThat(this.endpoint.file.exists()).isFalse();
69+
Awaitility.waitAtMost(Duration.ofSeconds(5)).until(this.endpoint.file::exists, is(false));
7270
}
7371

7472
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,11 @@
897897
<artifactId>tomcat-embed-jasper</artifactId>
898898
<scope>test</scope>
899899
</dependency>
900+
<dependency>
901+
<groupId>org.awaitility</groupId>
902+
<artifactId>awaitility</artifactId>
903+
<scope>test</scope>
904+
</dependency>
900905
<dependency>
901906
<groupId>org.hsqldb</groupId>
902907
<artifactId>hsqldb</artifactId>

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.net.URL;
2121
import java.net.URLClassLoader;
22+
import java.time.Duration;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.Collections;
@@ -27,6 +28,7 @@
2728
import java.util.List;
2829
import java.util.Map;
2930
import java.util.function.Consumer;
31+
import java.util.stream.Collectors;
3032

3133
import javax.persistence.EntityManager;
3234
import javax.persistence.EntityManagerFactory;
@@ -37,6 +39,7 @@
3739
import javax.transaction.UserTransaction;
3840

3941
import com.zaxxer.hikari.HikariDataSource;
42+
import org.awaitility.Awaitility;
4043
import org.hibernate.boot.model.naming.ImplicitNamingStrategy;
4144
import org.hibernate.boot.model.naming.PhysicalNamingStrategy;
4245
import org.hibernate.cfg.AvailableSettings;
@@ -77,6 +80,7 @@
7780

7881
import static org.assertj.core.api.Assertions.assertThat;
7982
import static org.assertj.core.api.Assertions.entry;
83+
import static org.hamcrest.Matchers.hasSize;
8084
import static org.mockito.Mockito.mock;
8185

8286
/**
@@ -387,12 +391,8 @@ void withAsyncBootstrappingAnApplicationListenerThatUsesJpaDoesNotTriggerABeanCu
387391
assertThat(context).hasNotFailed();
388392
EventCapturingApplicationListener listener = context
389393
.getBean(EventCapturingApplicationListener.class);
390-
long end = System.currentTimeMillis() + 30000;
391-
while ((System.currentTimeMillis() < end) && !dataSourceSchemaCreatedEventReceived(listener)) {
392-
Thread.sleep(100);
393-
}
394-
assertThat(listener.events.stream().filter(DataSourceSchemaCreatedEvent.class::isInstance))
395-
.hasSize(1);
394+
Awaitility.waitAtMost(Duration.ofSeconds(30))
395+
.until(() -> dataSourceSchemaCreatedEventsReceivedBy(listener), hasSize(1));
396396
});
397397
}
398398

@@ -408,13 +408,9 @@ void whenLocalContanerEntityManagerFactoryBeanHasNoJpaVendorAdapterAutoConfigura
408408
});
409409
}
410410

411-
private boolean dataSourceSchemaCreatedEventReceived(EventCapturingApplicationListener listener) {
412-
for (ApplicationEvent event : listener.events) {
413-
if (event instanceof DataSourceSchemaCreatedEvent) {
414-
return true;
415-
}
416-
}
417-
return false;
411+
private List<ApplicationEvent> dataSourceSchemaCreatedEventsReceivedBy(EventCapturingApplicationListener listener) {
412+
return listener.events.stream().filter(DataSourceSchemaCreatedEvent.class::isInstance)
413+
.collect(Collectors.toList());
418414
}
419415

420416
@Configuration(proxyBeanMethods = false)

spring-boot-project/spring-boot-devtools/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
<artifactId>HikariCP</artifactId>
131131
<scope>test</scope>
132132
</dependency>
133+
<dependency>
134+
<groupId>org.awaitility</groupId>
135+
<artifactId>awaitility</artifactId>
136+
<scope>test</scope>
137+
</dependency>
133138
<dependency>
134139
<groupId>org.springframework</groupId>
135140
<artifactId>spring-webmvc</artifactId>

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
2222
import java.net.URI;
23+
import java.time.Duration;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526
import java.util.concurrent.CountDownLatch;
2627
import java.util.concurrent.TimeUnit;
2728

2829
import org.apache.tomcat.websocket.WsWebSocketContainer;
30+
import org.awaitility.Awaitility;
2931
import org.junit.jupiter.api.AfterEach;
3032
import org.junit.jupiter.api.BeforeEach;
3133
import org.junit.jupiter.api.Disabled;
@@ -43,6 +45,9 @@
4345
import org.springframework.web.socket.handler.TextWebSocketHandler;
4446

4547
import static org.assertj.core.api.Assertions.assertThat;
48+
import static org.hamcrest.Matchers.empty;
49+
import static org.hamcrest.Matchers.is;
50+
import static org.hamcrest.Matchers.not;
4651

4752
/**
4853
* Tests for {@link LiveReloadServer}.
@@ -107,10 +112,7 @@ void clientClose() throws Exception {
107112
}
108113

109114
private void awaitClosedException() throws InterruptedException {
110-
long startTime = System.currentTimeMillis();
111-
while (this.server.getClosedExceptions().isEmpty() && System.currentTimeMillis() - startTime < 10000) {
112-
Thread.sleep(100);
113-
}
115+
Awaitility.waitAtMost(Duration.ofSeconds(10)).until(this.server::getClosedExceptions, is(not(empty())));
114116
}
115117

116118
@Test

spring-boot-project/spring-boot-tools/spring-boot-loader/pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
<optional>true</optional>
2222
</dependency>
2323
<!-- Test -->
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-test-support</artifactId>
27+
<scope>test</scope>
28+
</dependency>
2429
<dependency>
2530
<groupId>ch.qos.logback</groupId>
2631
<artifactId>logback-classic</artifactId>
2732
<scope>test</scope>
2833
</dependency>
2934
<dependency>
30-
<groupId>org.springframework</groupId>
31-
<artifactId>spring-webmvc</artifactId>
35+
<groupId>org.awaitility</groupId>
36+
<artifactId>awaitility</artifactId>
3237
<scope>test</scope>
3338
</dependency>
3439
<dependency>
@@ -39,8 +44,8 @@
3944
<scope>test</scope>
4045
</dependency>
4146
<dependency>
42-
<groupId>org.springframework.boot</groupId>
43-
<artifactId>spring-boot-test-support</artifactId>
47+
<groupId>org.springframework</groupId>
48+
<artifactId>spring-webmvc</artifactId>
4449
<scope>test</scope>
4550
</dependency>
4651
</dependencies>

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import java.io.IOException;
2222
import java.net.URL;
2323
import java.net.URLClassLoader;
24+
import java.time.Duration;
2425
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.List;
2728
import java.util.jar.Attributes;
2829
import java.util.jar.Manifest;
2930

3031
import org.assertj.core.api.Condition;
32+
import org.awaitility.Awaitility;
3133
import org.junit.jupiter.api.AfterEach;
3234
import org.junit.jupiter.api.BeforeEach;
3335
import org.junit.jupiter.api.Test;
@@ -44,6 +46,7 @@
4446

4547
import static org.assertj.core.api.Assertions.assertThat;
4648
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
49+
import static org.hamcrest.Matchers.containsString;
4750

4851
/**
4952
* Tests for {@link PropertiesLauncher}.
@@ -339,14 +342,7 @@ void encodedFileUrlLoaderPathIsHandledCorrectly() throws Exception {
339342
}
340343

341344
private void waitFor(String value) throws Exception {
342-
int count = 0;
343-
boolean timeout = false;
344-
while (!timeout && count < 100) {
345-
count++;
346-
Thread.sleep(50L);
347-
timeout = this.output.toString().contains(value);
348-
}
349-
assertThat(timeout).as("Timed out waiting for (" + value + ")").isTrue();
345+
Awaitility.waitAtMost(Duration.ofSeconds(5)).until(this.output::toString, containsString(value));
350346
}
351347

352348
private Condition<Archive> endingWith(String value) {

spring-boot-project/spring-boot/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@
421421
<artifactId>httpasyncclient</artifactId>
422422
<scope>test</scope>
423423
</dependency>
424+
<dependency>
425+
<groupId>org.awaitility</groupId>
426+
<artifactId>awaitility</artifactId>
427+
<scope>test</scope>
428+
</dependency>
424429
<dependency>
425430
<groupId>org.firebirdsql.jdbc</groupId>
426431
<artifactId>jaybird-jdk18</artifactId>

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/embedded/undertow/UndertowReactiveWebServerFactoryTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Arrays;
2424

2525
import io.undertow.Undertow;
26+
import org.awaitility.Awaitility;
2627
import org.junit.jupiter.api.Test;
2728
import org.junit.jupiter.api.io.TempDir;
2829
import org.mockito.InOrder;
@@ -36,6 +37,7 @@
3637

3738
import static org.assertj.core.api.Assertions.assertThat;
3839
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
40+
import static org.hamcrest.Matchers.is;
3941
import static org.mockito.ArgumentMatchers.any;
4042
import static org.mockito.Mockito.inOrder;
4143
import static org.mockito.Mockito.mock;
@@ -124,11 +126,8 @@ private void testAccessLog(String prefix, String suffix, String expectedFile)
124126
assertThat(accessLogDirectory.listFiles()).contains(accessLog);
125127
}
126128

127-
private void awaitFile(File file) throws InterruptedException {
128-
long end = System.currentTimeMillis() + 10000;
129-
while (!file.exists() && System.currentTimeMillis() < end) {
130-
Thread.sleep(100);
131-
}
129+
private void awaitFile(File file) {
130+
Awaitility.waitAtMost(Duration.ofSeconds(10)).until(file::exists, is(true));
132131
}
133132

134133
}

0 commit comments

Comments
 (0)