Skip to content

Commit 4096b73

Browse files
committed
Tolerate race condition in httpRequestsAreTimed
Tomcat 11 will [1] send a response as soon as Content-Length bytes has been written. This initiates a race between the timer being registered on the server side and the test receiving the response and looking for the timer. Rather than sleeping for a fix period of time, we now use Awaitility to await the availability of the timer. Closes gh-48049 [1] apache/tomcat@69eff83
1 parent 58cdf71 commit 4096b73

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/metrics/JerseyServerMetricsAutoConfigurationTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.jersey.autoconfigure.metrics;
1818

1919
import java.net.URI;
20+
import java.time.Duration;
2021

2122
import io.micrometer.core.instrument.MeterRegistry;
2223
import io.micrometer.core.instrument.Timer;
@@ -26,6 +27,8 @@
2627
import jakarta.ws.rs.GET;
2728
import jakarta.ws.rs.Path;
2829
import jakarta.ws.rs.PathParam;
30+
import org.assertj.core.api.InstanceOfAssertFactories;
31+
import org.awaitility.Awaitility;
2932
import org.glassfish.jersey.micrometer.server.ObservationApplicationEventListener;
3033
import org.glassfish.jersey.server.ResourceConfig;
3134
import org.junit.jupiter.api.Test;
@@ -84,10 +87,14 @@ void shouldProvideAllNecessaryBeans() {
8487
void httpRequestsAreTimed() {
8588
this.webContextRunner.withUserConfiguration(MetricsConfiguration.class).run((context) -> {
8689
doRequest(context);
87-
Thread.sleep(500);
8890
MeterRegistry registry = context.getBean(MeterRegistry.class);
89-
Timer timer = registry.get("http.server.requests").tag("uri", "/users/{id}").timer();
90-
assertThat(timer.count()).isOne();
91+
// Response is sent before the timer is registered which triggers a race
92+
// condition.
93+
// https://github.com/apache/tomcat/commit/69eff83577f7c00cbaaca9384ab4b1989f516797
94+
Awaitility.await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
95+
Timer timer = registry.find("http.server.requests").tag("uri", "/users/{id}").timer();
96+
assertThat(timer).isNotNull().extracting(Timer::count, InstanceOfAssertFactories.LONG).isOne();
97+
});
9198
});
9299
}
93100

0 commit comments

Comments
 (0)