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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

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

View File

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

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.function.web.function;
import java.net.URI;
import java.time.Duration;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@@ -37,6 +38,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 {
ConfigurableApplicationContext context = FunctionalSpringApplication.run(primaryAppConfig, "--server.port=0");
Thread.sleep(500);
String port = context.getEnvironment().getProperty("local.server.port");
if (port == null) {
Thread.sleep(500);
port = context.getEnvironment().getProperty("local.server.port");
assertThat(port).as("Unable to get 'local.server.port' - server may not have started up").isNotNull();
}
return Integer.valueOf(port);
await()
.pollDelay(Duration.ofMillis(500))
.pollInterval(Duration.ofMillis(500))
.atMost(Duration.ofSeconds(3))
.untilAsserted(() -> {
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(context.getEnvironment().getProperty("local.server.port"));
}
@SpringBootConfiguration