@@ -209,18 +209,8 @@ public void testMetricsSingleUseQuery() {
209209 long gfeLatencyValue = getAggregatedValue (gfeLatencyMetricData , expectedAttributes );
210210 assertEquals (fakeServerTiming .get (), gfeLatencyValue , 0 );
211211
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 ));
224214 }
225215
226216 @ Test
@@ -400,10 +390,7 @@ public void testNoServerTimingHeader() throws IOException, InterruptedException
400390 MetricData gfeConnectivityMetricData =
401391 getMetricData (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME );
402392 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 ));
407394 spannerNoHeader .close ();
408395 serverNoHeader .shutdown ();
409396 serverNoHeader .awaitTermination ();
@@ -442,6 +429,27 @@ private MetricData getMetricData(InMemoryMetricReader reader, String metricName)
442429 return null ;
443430 }
444431
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+
445453 private long getAggregatedValue (MetricData metricData , Attributes attributes ) {
446454 switch (metricData .getType ()) {
447455 case HISTOGRAM :
0 commit comments