1818
1919import static com .google .common .base .Preconditions .checkNotNull ;
2020import static com .google .common .truth .Truth .assertThat ;
21+ import static org .mockito .AdditionalAnswers .delegatesTo ;
2122import static org .mockito .ArgumentMatchers .any ;
2223import static org .mockito .ArgumentMatchers .anyString ;
2324import static org .mockito .ArgumentMatchers .argThat ;
5253import io .grpc .LoadBalancer .Subchannel ;
5354import io .grpc .LoadBalancer .SubchannelPicker ;
5455import io .grpc .LoadBalancer .SubchannelStateListener ;
55- import io .grpc .LongCounterMetricInstrument ;
5656import io .grpc .ManagedChannel ;
5757import io .grpc .ManagedChannelBuilder ;
5858import io .grpc .Metadata ;
6161import io .grpc .MetricInstrument ;
6262import io .grpc .MetricRecorder ;
6363import io .grpc .MetricRecorder .Registration ;
64+ import io .grpc .MetricSink ;
6465import io .grpc .NameResolver .ConfigOrError ;
6566import io .grpc .NoopMetricSink ;
6667import io .grpc .ServerCall ;
9495import java .util .Map ;
9596import java .util .concurrent .ScheduledExecutorService ;
9697import java .util .concurrent .TimeUnit ;
97- import java .util .concurrent .atomic .AtomicBoolean ;
9898import javax .annotation .Nonnull ;
9999import org .junit .After ;
100100import org .junit .Before ;
@@ -361,7 +361,7 @@ public void metricsWithRealChannel() throws Exception {
361361 .directExecutor ()
362362 .build ()
363363 .start ());
364- VerificationMetricSink metrics = new VerificationMetricSink ( );
364+ MetricSink metrics = mock ( MetricSink . class , delegatesTo ( new NoopMetricSink ()) );
365365 ManagedChannel channel = grpcCleanupRule .register (
366366 InternalManagedChannelBuilder .addMetricSink (
367367 InProcessChannelBuilder .forName ("fake-bigtable.googleapis.com" )
@@ -379,7 +379,12 @@ public void metricsWithRealChannel() throws Exception {
379379 assertThat (recorder .awaitCompletion (10 , TimeUnit .SECONDS )).isTrue ();
380380 assertThat (recorder .getError ()).isNull ();
381381
382- metrics .awaitCall ();
382+ verify (metrics ).addLongCounter (
383+ eqMetricInstrumentName ("grpc.lb.rls.default_target_picks" ),
384+ eq (1L ),
385+ eq (Arrays .asList ("directaddress:///fake-bigtable.googleapis.com" , "localhost:8972" ,
386+ "defaultTarget" , "complete" )),
387+ eq (Arrays .asList ("customvalue" )));
383388 }
384389
385390 @ Test
@@ -926,34 +931,4 @@ public void registerBackendResponse(boolean throttled) {
926931 }
927932 }
928933
929- private static final class VerificationMetricSink extends NoopMetricSink {
930- private final AtomicBoolean called =
931- new AtomicBoolean ();
932-
933- @ Override
934- public void addLongCounter (
935- LongCounterMetricInstrument metricInstrument ,
936- long value ,
937- List <String > requiredLabelValues ,
938- List <String > optionalLabelValues ) {
939- if (metricInstrument .getName ().equals ("grpc.lb.rls.default_target_picks" )
940- && value == 1L
941- && requiredLabelValues .equals (Arrays .asList (
942- "directaddress:///fake-bigtable.googleapis.com" , "localhost:8972" ,
943- "defaultTarget" , "complete" ))
944- && optionalLabelValues .equals (Arrays .asList ("customvalue" ))) {
945- called .set (true );
946- }
947- }
948-
949- public void awaitCall () throws InterruptedException {
950- long start = System .currentTimeMillis ();
951- while (!called .get ()) {
952- if (System .currentTimeMillis () - start > 5000 ) {
953- throw new AssertionError ("Timed out waiting for metric sink call" );
954- }
955- Thread .sleep (50 );
956- }
957- }
958- }
959934}
0 commit comments