@@ -215,18 +215,8 @@ public void testMetricsSingleUseQuery() {
215
215
long gfeLatencyValue = getAggregatedValue (gfeLatencyMetricData , expectedAttributes );
216
216
assertEquals (fakeServerTiming .get (), gfeLatencyValue , 0 );
217
217
218
- MetricData afeLatencyMetricData =
219
- getMetricData (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME );
220
- long afeLatencyValue = getAggregatedValue (afeLatencyMetricData , expectedAttributes );
221
- assertEquals (fakeAFEServerTiming .get (), afeLatencyValue , 0 );
222
-
223
- MetricData gfeConnectivityMetricData =
224
- getMetricData (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME );
225
- assertThat (getAggregatedValue (gfeConnectivityMetricData , expectedAttributes )).isEqualTo (0 );
226
-
227
- MetricData afeConnectivityMetricData =
228
- getMetricData (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME );
229
- assertThat (getAggregatedValue (afeConnectivityMetricData , expectedAttributes )).isEqualTo (0 );
218
+ assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME ));
219
+ assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME ));
230
220
}
231
221
232
222
@ Test
@@ -406,10 +396,7 @@ public void testNoServerTimingHeader() throws IOException, InterruptedException
406
396
MetricData gfeConnectivityMetricData =
407
397
getMetricData (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME );
408
398
assertThat (getAggregatedValue (gfeConnectivityMetricData , expectedAttributes )).isEqualTo (1 );
409
-
410
- MetricData afeConnectivityMetricData =
411
- getMetricData (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME );
412
- assertThat (getAggregatedValue (afeConnectivityMetricData , expectedAttributes )).isEqualTo (1 );
399
+ assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME ));
413
400
spannerNoHeader .close ();
414
401
serverNoHeader .shutdown ();
415
402
serverNoHeader .awaitTermination ();
@@ -448,6 +435,27 @@ private MetricData getMetricData(InMemoryMetricReader reader, String metricName)
448
435
return null ;
449
436
}
450
437
438
+ private boolean checkIfMetricExists (InMemoryMetricReader reader , String metricName ) {
439
+ String fullMetricName = BuiltInMetricsConstant .METER_NAME + "/" + metricName ;
440
+
441
+ for (int attemptsLeft = 1000 ; attemptsLeft > 0 ; attemptsLeft --) {
442
+ boolean exists = reader .collectAllMetrics ().stream ()
443
+ .anyMatch (md -> md .getName ().equals (fullMetricName ));
444
+ if (exists ) {
445
+ return true ;
446
+ }
447
+ try {
448
+ Thread .sleep (1 );
449
+ } catch (InterruptedException e ) {
450
+ Thread .currentThread ().interrupt ();
451
+ throw new RuntimeException (e );
452
+ }
453
+ }
454
+
455
+ return false ;
456
+ }
457
+
458
+
451
459
private long getAggregatedValue (MetricData metricData , Attributes attributes ) {
452
460
switch (metricData .getType ()) {
453
461
case HISTOGRAM :
0 commit comments