Use Awaitility when waiting on local port

This commit is contained in:
onobc
2022-03-15 10:11:01 -05:00
committed by Oleg Zhurakousky
parent 0de3a5e7ea
commit da58b0904e
4 changed files with 34 additions and 19 deletions

View File

@@ -55,6 +55,11 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -41,6 +41,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MimeTypeUtils; import org.springframework.util.MimeTypeUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
@@ -306,14 +307,15 @@ public class GrpcInteractionTests {
} }
private int patientlyGetPort(ConfigurableApplicationContext context) throws InterruptedException { private int patientlyGetPort(ConfigurableApplicationContext context) throws InterruptedException {
Thread.sleep(500); await()
String port = context.getEnvironment().getProperty("local.grpc.server.port"); .pollDelay(Duration.ofMillis(500))
if (port == null) { .pollInterval(Duration.ofMillis(500))
Thread.sleep(500); .atMost(Duration.ofSeconds(3))
port = context.getEnvironment().getProperty("local.grpc.server.port"); .untilAsserted(() -> {
assertThat(port).as("Unable to get 'local.grpc.server.port' - server may not have started up").isNotNull(); String port = context.getEnvironment().getProperty("local.grpc.server.port");
} assertThat(port).as("Unable to get 'local.grpc.server.port' - server may not have started up").isNotEmpty();
return Integer.valueOf(port); });
return Integer.valueOf(context.getEnvironment().getProperty("local.grpc.server.port"));
} }
@EnableAutoConfiguration @EnableAutoConfiguration

View File

@@ -40,6 +40,11 @@
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId> <artifactId>spring-cloud-function-context</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>commons-logging</groupId> <groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
@@ -57,9 +62,9 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.awaitility</groupId>
<artifactId>spring-boot-configuration-processor</artifactId> <artifactId>awaitility</artifactId>
<optional>true</optional> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.function.web.function; package org.springframework.cloud.function.web.function;
import java.net.URI; import java.net.URI;
import java.time.Duration;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -37,6 +38,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
/** /**
* *
@@ -106,14 +108,15 @@ public class FunctionEndpointInitializerTests {
private int startServerAndWaitForPort(Class<?> primaryAppConfig) throws InterruptedException { private int startServerAndWaitForPort(Class<?> primaryAppConfig) throws InterruptedException {
ConfigurableApplicationContext context = FunctionalSpringApplication.run(primaryAppConfig, "--server.port=0"); ConfigurableApplicationContext context = FunctionalSpringApplication.run(primaryAppConfig, "--server.port=0");
Thread.sleep(500); await()
String port = context.getEnvironment().getProperty("local.server.port"); .pollDelay(Duration.ofMillis(500))
if (port == null) { .pollInterval(Duration.ofMillis(500))
Thread.sleep(500); .atMost(Duration.ofSeconds(3))
port = context.getEnvironment().getProperty("local.server.port"); .untilAsserted(() -> {
assertThat(port).as("Unable to get 'local.server.port' - server may not have started up").isNotNull(); String port = context.getEnvironment().getProperty("local.server.port");
} assertThat(port).as("Unable to get 'local.server.port' - server may not have started up").isNotEmpty();
return Integer.valueOf(port); });
return Integer.valueOf(context.getEnvironment().getProperty("local.server.port"));
} }
@SpringBootConfiguration @SpringBootConfiguration