mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-09-28 21:19:02 +00:00
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] https://github.com/apache/tomcat/commit/69eff83577f7c00cbaaca9384ab4b1989f516797
This commit is contained in:
+10
-3
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.jersey.autoconfigure.metrics;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
@@ -26,6 +27,8 @@ import io.micrometer.observation.Observation.Context;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.PathParam;
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.awaitility.Awaitility;
|
||||
import org.glassfish.jersey.micrometer.server.ObservationApplicationEventListener;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -84,10 +87,14 @@ class JerseyServerMetricsAutoConfigurationTests {
|
||||
void httpRequestsAreTimed() {
|
||||
this.webContextRunner.withUserConfiguration(MetricsConfiguration.class).run((context) -> {
|
||||
doRequest(context);
|
||||
Thread.sleep(500);
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
Timer timer = registry.get("http.server.requests").tag("uri", "/users/{id}").timer();
|
||||
assertThat(timer.count()).isOne();
|
||||
// Response is sent before the timer is registered which triggers a race
|
||||
// condition.
|
||||
// https://github.com/apache/tomcat/commit/69eff83577f7c00cbaaca9384ab4b1989f516797
|
||||
Awaitility.await().atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
|
||||
Timer timer = registry.find("http.server.requests").tag("uri", "/users/{id}").timer();
|
||||
assertThat(timer).isNotNull().extracting(Timer::count, InstanceOfAssertFactories.LONG).isOne();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user