3
3
4
4
import static java .util .concurrent .TimeUnit .MILLISECONDS ;
5
5
import static org .assertj .core .api .Assertions .assertThat ;
6
+ import static org .awaitility .Awaitility .await ;
6
7
7
8
import com .microsoft .applicationinsights .agent .bootstrap .MicrometerUtil ;
8
9
import io .micrometer .core .instrument .Clock ;
27
28
28
29
class MicrometerTest {
29
30
30
- private static final long SLEEP_MILLISECONDS = 6000 ;
31
-
32
31
private static final AgentTestingMicrometerDelegate delegate =
33
32
new AgentTestingMicrometerDelegate ();
34
33
@@ -57,15 +56,14 @@ void shouldNotDoubleRegisterAzureMonitorMeterRegistry() {
57
56
}
58
57
59
58
@ Test
60
- void shouldCaptureTimeGauge () throws InterruptedException {
59
+ void shouldCaptureTimeGauge () {
61
60
// given
62
61
CompositeMeterRegistry registry = Metrics .globalRegistry ;
63
62
TimeGauge .builder ("test-time-gauge" , "" , MILLISECONDS , obj -> 11.0 ).register (registry );
64
63
65
- // when
66
- Thread .sleep (SLEEP_MILLISECONDS );
67
-
68
64
// then
65
+ await ().until (() -> getLastMeasurement ("test-time-gauge" ) != null );
66
+
69
67
AgentTestingMicrometerDelegate .Measurement measurement = getLastMeasurement ("test-time-gauge" );
70
68
assertThat (measurement .value ).isEqualTo (11 );
71
69
assertThat (measurement .count ).isNull ();
@@ -75,15 +73,16 @@ void shouldCaptureTimeGauge() throws InterruptedException {
75
73
}
76
74
77
75
@ Test
78
- void shouldCaptureGauge () throws InterruptedException {
76
+ void shouldCaptureGauge () {
79
77
// given
80
78
CompositeMeterRegistry registry = Metrics .globalRegistry ;
81
79
82
80
// when
83
81
Gauge .builder ("test-gauge" , () -> 22.0 ).register (registry );
84
- Thread .sleep (SLEEP_MILLISECONDS );
85
82
86
83
// then
84
+ await ().until (() -> getLastMeasurement ("test-gauge" ) != null );
85
+
87
86
AgentTestingMicrometerDelegate .Measurement measurement = getLastMeasurement ("test-gauge" );
88
87
assertThat (measurement .value ).isEqualTo (22 );
89
88
assertThat (measurement .count ).isNull ();
@@ -94,16 +93,17 @@ void shouldCaptureGauge() throws InterruptedException {
94
93
95
94
@ Disabled
96
95
@ Test
97
- void shouldCaptureCounter () throws InterruptedException {
96
+ void shouldCaptureCounter () {
98
97
// given
99
98
CompositeMeterRegistry registry = Metrics .globalRegistry ;
100
99
101
100
// when
102
101
Counter counter = Counter .builder ("test-counter" ).register (registry );
103
102
counter .increment (3.3 );
104
- Thread .sleep (SLEEP_MILLISECONDS );
105
103
106
104
// then
105
+ await ().until (() -> getLastMeasurement ("test-counter" ) != null );
106
+
107
107
AgentTestingMicrometerDelegate .Measurement measurement = getLastMeasurement ("test-counter" );
108
108
assertThat (measurement .value ).isEqualTo (3.3 );
109
109
assertThat (measurement .count ).isNull ();
@@ -113,17 +113,18 @@ void shouldCaptureCounter() throws InterruptedException {
113
113
}
114
114
115
115
@ Test
116
- void shouldCaptureTimer () throws InterruptedException {
116
+ void shouldCaptureTimer () {
117
117
// given
118
118
CompositeMeterRegistry registry = Metrics .globalRegistry ;
119
119
Timer timer = Timer .builder ("test-timer" ).register (registry );
120
120
121
121
// when
122
122
timer .record (Duration .ofMillis (44 ));
123
123
timer .record (Duration .ofMillis (55 ));
124
- Thread .sleep (SLEEP_MILLISECONDS );
125
124
126
125
// then
126
+ await ().until (() -> getLastMeasurement ("test-timer" ) != null );
127
+
127
128
AgentTestingMicrometerDelegate .Measurement measurement = getLastMeasurement ("test-timer" );
128
129
assertThat (measurement .value ).isEqualTo (99 );
129
130
assertThat (measurement .count ).isEqualTo (2 );
@@ -134,7 +135,7 @@ void shouldCaptureTimer() throws InterruptedException {
134
135
}
135
136
136
137
@ Test
137
- void shouldCaptureDistributionSummary () throws InterruptedException {
138
+ void shouldCaptureDistributionSummary () {
138
139
// given
139
140
CompositeMeterRegistry registry = Metrics .globalRegistry ;
140
141
DistributionSummary distributionSummary =
@@ -143,9 +144,10 @@ void shouldCaptureDistributionSummary() throws InterruptedException {
143
144
// when
144
145
distributionSummary .record (4.4 );
145
146
distributionSummary .record (5.5 );
146
- Thread .sleep (SLEEP_MILLISECONDS );
147
147
148
148
// then
149
+ await ().until (() -> getLastMeasurement ("test-summary" ) != null );
150
+
149
151
AgentTestingMicrometerDelegate .Measurement measurement = getLastMeasurement ("test-summary" );
150
152
assertThat (measurement .value ).isEqualTo (9.9 );
151
153
assertThat (measurement .count ).isEqualTo (2 );
@@ -156,7 +158,7 @@ void shouldCaptureDistributionSummary() throws InterruptedException {
156
158
}
157
159
158
160
@ Test
159
- void shouldCaptureLongTaskTimer () throws InterruptedException {
161
+ void shouldCaptureLongTaskTimer () {
160
162
// given
161
163
CompositeMeterRegistry registry = Metrics .globalRegistry ;
162
164
@@ -186,9 +188,16 @@ void shouldCaptureLongTaskTimer() throws InterruptedException {
186
188
});
187
189
});
188
190
189
- Thread .sleep (SLEEP_MILLISECONDS );
190
-
191
191
// 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
+
192
201
AgentTestingMicrometerDelegate .Measurement activeMeasurement =
193
202
getLastMeasurement ("test-long-task-timer_active" );
194
203
assertThat (activeMeasurement .value ).isEqualTo (2 );
@@ -197,6 +206,15 @@ void shouldCaptureLongTaskTimer() throws InterruptedException {
197
206
assertThat (activeMeasurement .max ).isNull ();
198
207
assertThat (activeMeasurement .namespace ).isNull ();
199
208
209
+ await ()
210
+ .untilAsserted (
211
+ () -> {
212
+ AgentTestingMicrometerDelegate .Measurement durationMeasurement =
213
+ getLastMeasurement ("test-long-task-timer_duration" );
214
+ assertThat (durationMeasurement ).isNotNull ();
215
+ assertThat (durationMeasurement .value ).isGreaterThan (50 );
216
+ });
217
+
200
218
AgentTestingMicrometerDelegate .Measurement durationMeasurement =
201
219
getLastMeasurement ("test-long-task-timer_duration" );
202
220
assertThat (durationMeasurement .value ).isGreaterThan (50 );
@@ -207,15 +225,16 @@ void shouldCaptureLongTaskTimer() throws InterruptedException {
207
225
}
208
226
209
227
@ Test
210
- void shouldCaptureFunctionCounter () throws InterruptedException {
228
+ void shouldCaptureFunctionCounter () {
211
229
// given
212
230
CompositeMeterRegistry registry = Metrics .globalRegistry ;
213
231
214
232
// when
215
233
FunctionCounter .builder ("test-function-counter" , "" , obj -> 6.6 ).register (registry );
216
- Thread .sleep (SLEEP_MILLISECONDS );
217
234
218
235
// then
236
+ await ().until (() -> getLastMeasurement ("test-function-counter" ) != null );
237
+
219
238
AgentTestingMicrometerDelegate .Measurement measurements =
220
239
getLastMeasurement ("test-function-counter" );
221
240
assertThat (measurements .value ).isEqualTo (6.6 );
@@ -226,16 +245,17 @@ void shouldCaptureFunctionCounter() throws InterruptedException {
226
245
}
227
246
228
247
@ Test
229
- void shouldCaptureFunctionTimer () throws InterruptedException {
248
+ void shouldCaptureFunctionTimer () {
230
249
// given
231
250
CompositeMeterRegistry registry = Metrics .globalRegistry ;
232
251
233
252
// when
234
253
FunctionTimer .builder ("test-function-timer" , "" , obj -> 2 , obj -> 4.4 , MILLISECONDS )
235
254
.register (registry );
236
- Thread .sleep (SLEEP_MILLISECONDS );
237
255
238
256
// then
257
+ await ().until (() -> getLastMeasurement ("test-function-timer" ) != null );
258
+
239
259
AgentTestingMicrometerDelegate .Measurement measurement =
240
260
getLastMeasurement ("test-function-timer" );
241
261
assertThat (measurement .value ).isEqualTo (4.4 );
@@ -245,11 +265,14 @@ void shouldCaptureFunctionTimer() throws InterruptedException {
245
265
assertThat (measurement .namespace ).isNull ();
246
266
}
247
267
248
- public AgentTestingMicrometerDelegate .Measurement getLastMeasurement (String name ) {
268
+ private static AgentTestingMicrometerDelegate .Measurement getLastMeasurement (String name ) {
249
269
List <AgentTestingMicrometerDelegate .Measurement > measurements =
250
270
delegate .getMeasurements ().stream ()
251
271
.filter (measurement -> measurement .name .equals (name ) && measurement .value != 0 )
252
272
.collect (Collectors .toList ());
273
+ if (measurements .isEmpty ()) {
274
+ return null ;
275
+ }
253
276
return measurements .get (measurements .size () - 1 );
254
277
}
255
278
}
0 commit comments