@@ -215,18 +215,8 @@ public void testMetricsSingleUseQuery() {
215215 long gfeLatencyValue = getAggregatedValue (gfeLatencyMetricData , expectedAttributes );
216216 assertEquals (fakeServerTiming .get (), gfeLatencyValue , 0 );
217217
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 ));
230220 }
231221
232222 @ Test
@@ -406,10 +396,7 @@ public void testNoServerTimingHeader() throws IOException, InterruptedException
406396 MetricData gfeConnectivityMetricData =
407397 getMetricData (metricReader , BuiltInMetricsConstant .GFE_CONNECTIVITY_ERROR_NAME );
408398 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 ));
413400 spannerNoHeader .close ();
414401 serverNoHeader .shutdown ();
415402 serverNoHeader .awaitTermination ();
@@ -448,6 +435,27 @@ private MetricData getMetricData(InMemoryMetricReader reader, String metricName)
448435 return null ;
449436 }
450437
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+
451459 private long getAggregatedValue (MetricData metricData , Attributes attributes ) {
452460 switch (metricData .getType ()) {
453461 case HISTOGRAM :
0 commit comments