Skip to content

Commit 20d0a9e

Browse files
authored
Fix flakiness in JettyClientMetricsWithObservationTest.activeTimer() (#5894)
Signed-off-by: Johnny Lim <[email protected]>
1 parent fd6d438 commit 20d0a9e

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

micrometer-jetty12/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121
testImplementation project(":micrometer-test")
2222

2323
testImplementation libs.httpcomponents.client
24+
testImplementation 'org.awaitility:awaitility'
2425
}
2526

2627
java {

micrometer-jetty12/src/test/java/io/micrometer/jetty12/client/JettyClientMetricsWithObservationTest.java

+28-6
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@
1616
package io.micrometer.jetty12.client;
1717

1818
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
19+
import io.micrometer.core.instrument.LongTaskTimer;
1920
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
2021
import io.micrometer.observation.ObservationRegistry;
2122
import io.micrometer.observation.tck.TestObservationRegistry;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.junit.jupiter.api.Test;
2425

26+
import java.time.Duration;
27+
2528
import static com.github.tomakehurst.wiremock.client.WireMock.*;
2629
import static java.util.concurrent.TimeUnit.SECONDS;
2730
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.awaitility.Awaitility.await;
2832

2933
class JettyClientMetricsWithObservationTest extends JettyClientMetricsTest {
3034

@@ -47,13 +51,31 @@ protected void addInstrumentingListener() {
4751

4852
@Test
4953
void activeTimer(WireMockRuntimeInfo wmRuntimeInfo) throws Exception {
50-
stubFor(get("/ok").willReturn(ok()));
54+
stubFor(get("/ok").willReturn(ok().withFixedDelay(100)));
55+
56+
Thread thread = new Thread(() -> {
57+
try {
58+
httpClient.GET("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/ok");
59+
}
60+
catch (Exception ex) {
61+
throw new RuntimeException(ex);
62+
}
63+
});
64+
thread.start();
65+
66+
await().atMost(Duration.ofMillis(100))
67+
.pollDelay(Duration.ofMillis(10))
68+
.pollInterval(Duration.ofMillis(10))
69+
.untilAsserted(() -> {
70+
LongTaskTimer longTaskTimer = registry.find("jetty.client.requests.active")
71+
.tags("uri", "/ok", "method", "GET")
72+
.longTaskTimer();
73+
assertThat(longTaskTimer).isNotNull();
74+
assertThat(longTaskTimer.activeTasks()).isOne();
75+
});
76+
77+
thread.join();
5178

52-
httpClient.GET("http://localhost:" + wmRuntimeInfo.getHttpPort() + "/ok");
53-
assertThat(registry.get("jetty.client.requests.active")
54-
.tags("uri", "/ok", "method", "GET")
55-
.longTaskTimer()
56-
.activeTasks()).isOne();
5779
httpClient.stop();
5880

5981
assertThat(singleRequestLatch.await(10, SECONDS)).isTrue();

0 commit comments

Comments
 (0)