33
44import static java .util .concurrent .TimeUnit .MILLISECONDS ;
55import static org .assertj .core .api .Assertions .assertThat ;
6+ import static org .awaitility .Awaitility .await ;
67
78import com .microsoft .applicationinsights .agent .bootstrap .MicrometerUtil ;
89import io .micrometer .core .instrument .Clock ;
2728
2829class 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 );
@@ -197,6 +206,15 @@ void shouldCaptureLongTaskTimer() throws InterruptedException {
197206 assertThat (activeMeasurement .max ).isNull ();
198207 assertThat (activeMeasurement .namespace ).isNull ();
199208
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+
200218 AgentTestingMicrometerDelegate .Measurement durationMeasurement =
201219 getLastMeasurement ("test-long-task-timer_duration" );
202220 assertThat (durationMeasurement .value ).isGreaterThan (50 );
@@ -207,15 +225,16 @@ void shouldCaptureLongTaskTimer() throws InterruptedException {
207225 }
208226
209227 @ Test
210- void shouldCaptureFunctionCounter () throws InterruptedException {
228+ void shouldCaptureFunctionCounter () {
211229 // given
212230 CompositeMeterRegistry registry = Metrics .globalRegistry ;
213231
214232 // when
215233 FunctionCounter .builder ("test-function-counter" , "" , obj -> 6.6 ).register (registry );
216- Thread .sleep (SLEEP_MILLISECONDS );
217234
218235 // then
236+ await ().until (() -> getLastMeasurement ("test-function-counter" ) != null );
237+
219238 AgentTestingMicrometerDelegate .Measurement measurements =
220239 getLastMeasurement ("test-function-counter" );
221240 assertThat (measurements .value ).isEqualTo (6.6 );
@@ -226,16 +245,17 @@ void shouldCaptureFunctionCounter() throws InterruptedException {
226245 }
227246
228247 @ Test
229- void shouldCaptureFunctionTimer () throws InterruptedException {
248+ void shouldCaptureFunctionTimer () {
230249 // given
231250 CompositeMeterRegistry registry = Metrics .globalRegistry ;
232251
233252 // when
234253 FunctionTimer .builder ("test-function-timer" , "" , obj -> 2 , obj -> 4.4 , MILLISECONDS )
235254 .register (registry );
236- Thread .sleep (SLEEP_MILLISECONDS );
237255
238256 // then
257+ await ().until (() -> getLastMeasurement ("test-function-timer" ) != null );
258+
239259 AgentTestingMicrometerDelegate .Measurement measurement =
240260 getLastMeasurement ("test-function-timer" );
241261 assertThat (measurement .value ).isEqualTo (4.4 );
@@ -245,11 +265,14 @@ void shouldCaptureFunctionTimer() throws InterruptedException {
245265 assertThat (measurement .namespace ).isNull ();
246266 }
247267
248- public AgentTestingMicrometerDelegate .Measurement getLastMeasurement (String name ) {
268+ private static AgentTestingMicrometerDelegate .Measurement getLastMeasurement (String name ) {
249269 List <AgentTestingMicrometerDelegate .Measurement > measurements =
250270 delegate .getMeasurements ().stream ()
251271 .filter (measurement -> measurement .name .equals (name ) && measurement .value != 0 )
252272 .collect (Collectors .toList ());
273+ if (measurements .isEmpty ()) {
274+ return null ;
275+ }
253276 return measurements .get (measurements .size () - 1 );
254277 }
255278}
0 commit comments