Skip to content

Commit c8d4381

Browse files
committed
Fix sporadically failing test
1 parent 0158aa8 commit c8d4381

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

agent/instrumentation/micrometer-1.0/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ dependencies {
4242

4343
tasks {
4444
withType<Test>().configureEach {
45-
jvmArgs("-Dapplicationinsights.internal.micrometer.step.millis=100")
45+
jvmArgs("-Dapplicationinsights.internal.micrometer.step.millis=1000")
4646
}
4747
}

agent/instrumentation/micrometer-1.0/src/test/java/MicrometerTest.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import static java.util.concurrent.TimeUnit.MILLISECONDS;
55
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.awaitility.Awaitility.await;
67

78
import com.microsoft.applicationinsights.agent.bootstrap.MicrometerUtil;
89
import io.micrometer.core.instrument.Clock;
@@ -27,8 +28,6 @@
2728

2829
class MicrometerTest {
2930

30-
private static final long SLEEP_MILLISECONDS = 6000;
31-
3231
private static final AgentTestingMicrometerDelegate delegate =
3332
new AgentTestingMicrometerDelegate();
3433

@@ -57,15 +56,14 @@ void shouldNotDoubleRegisterAzureMonitorMeterRegistry() {
5756
}
5857

5958
@Test
60-
void shouldCaptureTimeGauge() throws InterruptedException {
59+
void shouldCaptureTimeGauge() {
6160
// given
6261
CompositeMeterRegistry registry = Metrics.globalRegistry;
6362
TimeGauge.builder("test-time-gauge", "", MILLISECONDS, obj -> 11.0).register(registry);
6463

65-
// when
66-
Thread.sleep(SLEEP_MILLISECONDS);
67-
6864
// then
65+
await().until(() -> getLastMeasurement("test-time-gauge") != null);
66+
6967
AgentTestingMicrometerDelegate.Measurement measurement = getLastMeasurement("test-time-gauge");
7068
assertThat(measurement.value).isEqualTo(11);
7169
assertThat(measurement.count).isNull();
@@ -75,15 +73,16 @@ void shouldCaptureTimeGauge() throws InterruptedException {
7573
}
7674

7775
@Test
78-
void shouldCaptureGauge() throws InterruptedException {
76+
void shouldCaptureGauge() {
7977
// given
8078
CompositeMeterRegistry registry = Metrics.globalRegistry;
8179

8280
// when
8381
Gauge.builder("test-gauge", () -> 22.0).register(registry);
84-
Thread.sleep(SLEEP_MILLISECONDS);
8582

8683
// then
84+
await().until(() -> getLastMeasurement("test-gauge") != null);
85+
8786
AgentTestingMicrometerDelegate.Measurement measurement = getLastMeasurement("test-gauge");
8887
assertThat(measurement.value).isEqualTo(22);
8988
assertThat(measurement.count).isNull();
@@ -94,16 +93,17 @@ void shouldCaptureGauge() throws InterruptedException {
9493

9594
@Disabled
9695
@Test
97-
void shouldCaptureCounter() throws InterruptedException {
96+
void shouldCaptureCounter() {
9897
// given
9998
CompositeMeterRegistry registry = Metrics.globalRegistry;
10099

101100
// when
102101
Counter counter = Counter.builder("test-counter").register(registry);
103102
counter.increment(3.3);
104-
Thread.sleep(SLEEP_MILLISECONDS);
105103

106104
// then
105+
await().until(() -> getLastMeasurement("test-counter") != null);
106+
107107
AgentTestingMicrometerDelegate.Measurement measurement = getLastMeasurement("test-counter");
108108
assertThat(measurement.value).isEqualTo(3.3);
109109
assertThat(measurement.count).isNull();
@@ -113,17 +113,18 @@ void shouldCaptureCounter() throws InterruptedException {
113113
}
114114

115115
@Test
116-
void shouldCaptureTimer() throws InterruptedException {
116+
void shouldCaptureTimer() {
117117
// given
118118
CompositeMeterRegistry registry = Metrics.globalRegistry;
119119
Timer timer = Timer.builder("test-timer").register(registry);
120120

121121
// when
122122
timer.record(Duration.ofMillis(44));
123123
timer.record(Duration.ofMillis(55));
124-
Thread.sleep(SLEEP_MILLISECONDS);
125124

126125
// then
126+
await().until(() -> getLastMeasurement("test-timer") != null);
127+
127128
AgentTestingMicrometerDelegate.Measurement measurement = getLastMeasurement("test-timer");
128129
assertThat(measurement.value).isEqualTo(99);
129130
assertThat(measurement.count).isEqualTo(2);
@@ -134,7 +135,7 @@ void shouldCaptureTimer() throws InterruptedException {
134135
}
135136

136137
@Test
137-
void shouldCaptureDistributionSummary() throws InterruptedException {
138+
void shouldCaptureDistributionSummary() {
138139
// given
139140
CompositeMeterRegistry registry = Metrics.globalRegistry;
140141
DistributionSummary distributionSummary =
@@ -143,9 +144,10 @@ void shouldCaptureDistributionSummary() throws InterruptedException {
143144
// when
144145
distributionSummary.record(4.4);
145146
distributionSummary.record(5.5);
146-
Thread.sleep(SLEEP_MILLISECONDS);
147147

148148
// then
149+
await().until(() -> getLastMeasurement("test-summary") != null);
150+
149151
AgentTestingMicrometerDelegate.Measurement measurement = getLastMeasurement("test-summary");
150152
assertThat(measurement.value).isEqualTo(9.9);
151153
assertThat(measurement.count).isEqualTo(2);
@@ -156,7 +158,7 @@ void shouldCaptureDistributionSummary() throws InterruptedException {
156158
}
157159

158160
@Test
159-
void shouldCaptureLongTaskTimer() throws InterruptedException {
161+
void shouldCaptureLongTaskTimer() {
160162
// given
161163
CompositeMeterRegistry registry = Metrics.globalRegistry;
162164

@@ -186,9 +188,16 @@ void shouldCaptureLongTaskTimer() throws InterruptedException {
186188
});
187189
});
188190

189-
Thread.sleep(SLEEP_MILLISECONDS);
190-
191191
// then
192+
await()
193+
.untilAsserted(
194+
() -> {
195+
AgentTestingMicrometerDelegate.Measurement activeMeasurement =
196+
getLastMeasurement("test-long-task-timer_active");
197+
assertThat(activeMeasurement).isNotNull();
198+
assertThat(activeMeasurement.value).isEqualTo(2);
199+
});
200+
192201
AgentTestingMicrometerDelegate.Measurement activeMeasurement =
193202
getLastMeasurement("test-long-task-timer_active");
194203
assertThat(activeMeasurement.value).isEqualTo(2);
@@ -207,15 +216,16 @@ void shouldCaptureLongTaskTimer() throws InterruptedException {
207216
}
208217

209218
@Test
210-
void shouldCaptureFunctionCounter() throws InterruptedException {
219+
void shouldCaptureFunctionCounter() {
211220
// given
212221
CompositeMeterRegistry registry = Metrics.globalRegistry;
213222

214223
// when
215224
FunctionCounter.builder("test-function-counter", "", obj -> 6.6).register(registry);
216-
Thread.sleep(SLEEP_MILLISECONDS);
217225

218226
// then
227+
await().until(() -> getLastMeasurement("test-function-counter") != null);
228+
219229
AgentTestingMicrometerDelegate.Measurement measurements =
220230
getLastMeasurement("test-function-counter");
221231
assertThat(measurements.value).isEqualTo(6.6);
@@ -226,16 +236,17 @@ void shouldCaptureFunctionCounter() throws InterruptedException {
226236
}
227237

228238
@Test
229-
void shouldCaptureFunctionTimer() throws InterruptedException {
239+
void shouldCaptureFunctionTimer() {
230240
// given
231241
CompositeMeterRegistry registry = Metrics.globalRegistry;
232242

233243
// when
234244
FunctionTimer.builder("test-function-timer", "", obj -> 2, obj -> 4.4, MILLISECONDS)
235245
.register(registry);
236-
Thread.sleep(SLEEP_MILLISECONDS);
237246

238247
// then
248+
await().until(() -> getLastMeasurement("test-function-timer") != null);
249+
239250
AgentTestingMicrometerDelegate.Measurement measurement =
240251
getLastMeasurement("test-function-timer");
241252
assertThat(measurement.value).isEqualTo(4.4);
@@ -245,11 +256,14 @@ void shouldCaptureFunctionTimer() throws InterruptedException {
245256
assertThat(measurement.namespace).isNull();
246257
}
247258

248-
public AgentTestingMicrometerDelegate.Measurement getLastMeasurement(String name) {
259+
private static AgentTestingMicrometerDelegate.Measurement getLastMeasurement(String name) {
249260
List<AgentTestingMicrometerDelegate.Measurement> measurements =
250261
delegate.getMeasurements().stream()
251262
.filter(measurement -> measurement.name.equals(name) && measurement.value != 0)
252263
.collect(Collectors.toList());
264+
if (measurements.isEmpty()) {
265+
return null;
266+
}
253267
return measurements.get(measurements.size() - 1);
254268
}
255269
}

0 commit comments

Comments
 (0)