Browse Source

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] 69eff83577
pull/48101/head
Andy Wilkinson 3 months ago
parent
commit
4096b736c8
  1. 13
      module/spring-boot-jersey/src/test/java/org/springframework/boot/jersey/autoconfigure/metrics/JerseyServerMetricsAutoConfigurationTests.java

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

@ -17,6 +17,7 @@ @@ -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; @@ -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 { @@ -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();
});
});
}

Loading…
Cancel
Save