Skip to content

Commit 45a91bd

Browse files
committed
xds: Add WRR metric test with real channel
1 parent 2bc4306 commit 45a91bd

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

xds/src/test/java/io/grpc/xds/WeightedRoundRobinLoadBalancerTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.google.common.collect.Maps;
3838
import com.google.protobuf.Duration;
3939
import io.grpc.Attributes;
40+
import io.grpc.CallOptions;
4041
import io.grpc.Channel;
4142
import io.grpc.ClientCall;
4243
import io.grpc.ConnectivityState;
@@ -52,14 +53,26 @@
5253
import io.grpc.LoadBalancer.SubchannelPicker;
5354
import io.grpc.LoadBalancer.SubchannelStateListener;
5455
import io.grpc.LongCounterMetricInstrument;
56+
import io.grpc.Metadata;
5557
import io.grpc.MetricRecorder;
58+
import io.grpc.MetricSink;
59+
import io.grpc.NoopMetricSink;
60+
import io.grpc.ServerCall;
61+
import io.grpc.ServerServiceDefinition;
5662
import io.grpc.Status;
5763
import io.grpc.SynchronizationContext;
64+
import io.grpc.inprocess.InProcessChannelBuilder;
65+
import io.grpc.inprocess.InProcessServerBuilder;
5866
import io.grpc.internal.FakeClock;
5967
import io.grpc.internal.GrpcUtil;
6068
import io.grpc.internal.TestUtils;
69+
import io.grpc.internal.testing.StreamRecorder;
6170
import io.grpc.services.InternalCallMetricRecorder;
6271
import io.grpc.services.MetricReport;
72+
import io.grpc.stub.ClientCalls;
73+
import io.grpc.stub.StreamObserver;
74+
import io.grpc.testing.GrpcCleanupRule;
75+
import io.grpc.testing.TestMethodDescriptors;
6376
import io.grpc.util.AbstractTestHelper;
6477
import io.grpc.util.MultiChildLoadBalancer.ChildLbState;
6578
import io.grpc.xds.WeightedRoundRobinLoadBalancer.StaticStrideScheduler;
@@ -100,6 +113,8 @@
100113
public class WeightedRoundRobinLoadBalancerTest {
101114
@Rule
102115
public final MockitoRule mockito = MockitoJUnit.rule();
116+
@Rule
117+
public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule();
103118

104119
private final TestHelper testHelperInstance;
105120
private final Helper helper;
@@ -1234,6 +1249,50 @@ public void metrics() {
12341249
verifyNoMoreInteractions(mockMetricRecorder);
12351250
}
12361251

1252+
@Test
1253+
public void metricWithRealChannel() throws Exception {
1254+
String serverName = "wrr-metrics";
1255+
grpcCleanupRule.register(
1256+
InProcessServerBuilder.forName(serverName)
1257+
.addService(ServerServiceDefinition.builder(
1258+
TestMethodDescriptors.voidMethod().getServiceName())
1259+
.addMethod(TestMethodDescriptors.voidMethod(), (call, headers) -> {
1260+
call.sendHeaders(new Metadata());
1261+
call.sendMessage(null);
1262+
call.close(Status.OK, new Metadata());
1263+
return new ServerCall.Listener<Void>() {};
1264+
})
1265+
.build())
1266+
.directExecutor()
1267+
.build()
1268+
.start());
1269+
MetricSink metrics = mock(MetricSink.class, delegatesTo(new NoopMetricSink()));
1270+
Channel channel = grpcCleanupRule.register(
1271+
InProcessChannelBuilder.forName(serverName)
1272+
.defaultServiceConfig(Collections.singletonMap(
1273+
"loadBalancingConfig", Arrays.asList(Collections.singletonMap(
1274+
"weighted_round_robin", Collections.emptyMap()))))
1275+
.addMetricSink(metrics)
1276+
.directExecutor()
1277+
.build());
1278+
1279+
// Ping-pong to wait for channel to fully start
1280+
StreamRecorder<Void> recorder = StreamRecorder.create();
1281+
StreamObserver<Void> requestObserver = ClientCalls.asyncClientStreamingCall(
1282+
channel.newCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT), recorder);
1283+
requestObserver.onCompleted();
1284+
assertThat(recorder.awaitCompletion(10, TimeUnit.SECONDS)).isTrue();
1285+
assertThat(recorder.getError()).isNull();
1286+
1287+
// Make sure at least one metric works. The other tests will make sure other metrics and the
1288+
// edge cases are working.
1289+
verify(metrics).addLongCounter(
1290+
argThat((instr) -> instr.getName().equals("grpc.lb.wrr.rr_fallback")),
1291+
eq(1L),
1292+
eq(Arrays.asList("directaddress:///wrr-metrics")),
1293+
eq(Arrays.asList("")));
1294+
}
1295+
12371296
// Verifies that the MetricRecorder has been called to record a long counter value of 1 for the
12381297
// given metric name, the given number of times
12391298
private void verifyLongCounterRecord(String name, int times, long value) {

0 commit comments

Comments
 (0)