@@ -209,18 +209,8 @@ public void testMetricsSingleUseQuery() {
209
209
long gfeLatencyValue = getAggregatedValue (gfeLatencyMetricData , expectedAttributes );
210
210
assertEquals (fakeServerTiming .get (), gfeLatencyValue , 0 );
211
211
212
- MetricData afeLatencyMetricData =
213
- getMetricData (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME );
214
- long afeLatencyValue = getAggregatedValue (afeLatencyMetricData , expectedAttributes );
215
- assertEquals (fakeAFEServerTiming .get (), afeLatencyValue , 0 );
216
-
217
- MetricData gfeConnectivityMetricData =
218
- getMetricData (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME );
219
- assertThat (getAggregatedValue (gfeConnectivityMetricData , expectedAttributes )).isEqualTo (0 );
220
-
221
- MetricData afeConnectivityMetricData =
222
- getMetricData (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME );
223
- assertThat (getAggregatedValue (afeConnectivityMetricData , expectedAttributes )).isEqualTo (0 );
212
+ assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_LATENCIES_NAME ));
213
+ assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME ));
224
214
}
225
215
226
216
@ Test
@@ -400,10 +390,7 @@ public void testNoServerTimingHeader() throws IOException, InterruptedException
400
390
MetricData gfeConnectivityMetricData =
401
391
getMetricData (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME );
402
392
assertThat (getAggregatedValue (gfeConnectivityMetricData , expectedAttributes )).isEqualTo (1 );
403
-
404
- MetricData afeConnectivityMetricData =
405
- getMetricData (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME );
406
- assertThat (getAggregatedValue (afeConnectivityMetricData , expectedAttributes )).isEqualTo (1 );
393
+ assertFalse (checkIfMetricExists (metricReader , BuiltInMetricsConstant .AFE_CONNECTIVITY_ERROR_NAME ));
407
394
spannerNoHeader .close ();
408
395
serverNoHeader .shutdown ();
409
396
serverNoHeader .awaitTermination ();
@@ -442,6 +429,27 @@ private MetricData getMetricData(InMemoryMetricReader reader, String metricName)
442
429
return null ;
443
430
}
444
431
432
+ private boolean checkIfMetricExists (InMemoryMetricReader reader , String metricName ) {
433
+ String fullMetricName = BuiltInMetricsConstant .METER_NAME + "/" + metricName ;
434
+
435
+ for (int attemptsLeft = 1000 ; attemptsLeft > 0 ; attemptsLeft --) {
436
+ boolean exists = reader .collectAllMetrics ().stream ()
437
+ .anyMatch (md -> md .getName ().equals (fullMetricName ));
438
+ if (exists ) {
439
+ return true ;
440
+ }
441
+ try {
442
+ Thread .sleep (1 );
443
+ } catch (InterruptedException e ) {
444
+ Thread .currentThread ().interrupt ();
445
+ throw new RuntimeException (e );
446
+ }
447
+ }
448
+
449
+ return false ;
450
+ }
451
+
452
+
445
453
private long getAggregatedValue (MetricData metricData , Attributes attributes ) {
446
454
switch (metricData .getType ()) {
447
455
case HISTOGRAM :
0 commit comments