Skip to content

Commit 2845bbc

Browse files
committed
Add tests for census interceptor.
1 parent 4741f54 commit 2845bbc

File tree

1 file changed

+53
-79
lines changed

1 file changed

+53
-79
lines changed

census/src/test/java/io/grpc/census/CensusClientInterceptorTest.java

Lines changed: 53 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,39 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.common.collect.Iterables;
2122
import io.grpc.CallOptions;
2223
import io.grpc.Channel;
2324
import io.grpc.ClientCall;
2425
import io.grpc.ClientInterceptor;
2526
import io.grpc.ClientInterceptors;
2627
import io.grpc.ManagedChannel;
27-
import io.grpc.Metadata;
2828
import io.grpc.MethodDescriptor;
29-
import io.grpc.ServerCall;
30-
import io.grpc.ServerCallHandler;
31-
import io.grpc.ServerServiceDefinition;
32-
import io.grpc.Status;
3329
import io.grpc.inprocess.InProcessChannelBuilder;
34-
import io.grpc.inprocess.InProcessServerBuilder;
30+
import io.grpc.inprocess.InternalInProcess;
3531
import io.grpc.testing.GrpcCleanupRule;
36-
import io.opencensus.trace.Tracer;
37-
import io.opencensus.trace.propagation.BinaryFormat;
3832
import java.io.InputStream;
3933
import java.util.concurrent.atomic.AtomicReference;
4034
import org.junit.Before;
4135
import org.junit.Rule;
4236
import org.junit.Test;
4337
import org.junit.runner.RunWith;
4438
import org.junit.runners.JUnit4;
45-
import org.mockito.Mock;
46-
import org.mockito.junit.MockitoJUnit;
47-
import org.mockito.junit.MockitoRule;
4839

4940
/**
50-
* Test for {@link CensusClientInterceptor} and {@link CensusTracingModule}.
41+
* Test for {@link CensusClientInterceptor}.
5142
*/
5243
@RunWith(JUnit4.class)
5344
public class CensusClientInterceptorTest {
5445

55-
@Rule
56-
public final MockitoRule mocks = MockitoJUnit.rule();
5746
@Rule
5847
public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule();
5948

6049
private static final CallOptions.Key<String> CUSTOM_OPTION =
6150
CallOptions.Key.createWithDefault("option", "default");
6251
private static final CallOptions CALL_OPTIONS =
6352
CallOptions.DEFAULT.withOption(CUSTOM_OPTION, "customvalue");
53+
6454
private static class StringInputStream extends InputStream {
6555
final String string;
6656

@@ -97,92 +87,76 @@ public String parse(InputStream stream) {
9787
.setFullMethodName("package1.service2/method3")
9888
.build();
9989

100-
@Mock
101-
private Tracer tracer;
102-
@Mock
103-
private BinaryFormat mockTracingPropagationHandler;
104-
@Mock
105-
private ClientCall.Listener<String> mockClientCallListener;
106-
@Mock
107-
private ServerCall.Listener<String> mockServerCallListener;
90+
private final AtomicReference<CallOptions> callOptionsCaptor = new AtomicReference<>();
10891

10992
private ManagedChannel channel;
11093

94+
95+
@SuppressWarnings("unchecked")
11196
@Before
11297
public void setUp() {
113-
String serverName = InProcessServerBuilder.generateName();
114-
grpcCleanupRule.register(
115-
InProcessServerBuilder.forName(serverName)
116-
.addService(
117-
ServerServiceDefinition.builder("package1.service2")
118-
.addMethod(method, new ServerCallHandler<String, String>() {
119-
@Override
120-
public ServerCall.Listener<String> startCall(
121-
ServerCall<String, String> call, Metadata headers) {
122-
call.sendHeaders(new Metadata());
123-
call.sendMessage("Hello");
124-
call.close(
125-
Status.PERMISSION_DENIED.withDescription("No you don't"), new Metadata());
126-
return mockServerCallListener;
127-
}
128-
}).build())
129-
.directExecutor()
130-
.build());
131-
channel =
132-
grpcCleanupRule.register(
133-
InProcessChannelBuilder.forName(serverName).directExecutor().build());
134-
}
135-
136-
@Test
137-
public void disableDefaultClientStatsByInterceptor() {
138-
ClientInterceptor interceptor =
139-
CensusClientInterceptor.newBuilder().setStatsEnabled(false).build();
140-
testDisableDefaultCensus(interceptor);
141-
}
142-
143-
@Test
144-
public void disableDefaultClientTracingByInterceptor() {
145-
146-
}
147-
148-
private void testDisableDefaultCensus(ClientInterceptor interceptor) {
149-
final AtomicReference<CallOptions> capturedCallOptions = new AtomicReference<>();
150-
ClientInterceptor callOptionsCaptureInterceptor = new ClientInterceptor() {
98+
ClientInterceptor callOptionCaptureInterceptor = new ClientInterceptor() {
15199
@Override
152100
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
153101
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
154-
capturedCallOptions.set(callOptions);
102+
callOptionsCaptor.set(callOptions);
155103
return next.newCall(method, callOptions);
156104
}
157105
};
158-
Channel interceptedChannel =
159-
ClientInterceptors.intercept(channel, interceptor, callOptionsCaptureInterceptor);
160-
ClientCall<String, String> call = interceptedChannel.newCall(method, CALL_OPTIONS);
161-
assertThat(capturedCallOptions.get().getStreamTracerFactories()).isEmpty();
106+
InProcessChannelBuilder builder =
107+
InProcessChannelBuilder.forName("non-existing server").directExecutor();
108+
InternalInProcess.setTestInterceptor(builder, callOptionCaptureInterceptor);
109+
channel = grpcCleanupRule.register(builder.build());
162110
}
163111

164112
@Test
165-
public void stats_starts_finishes_realTime() {
166-
167-
}
168-
169-
@Test
170-
public void stats_starts_finishes_noReaLTime() {
171-
113+
public void usingCensusInterceptorDisablesDefaultCensus() {
114+
ClientInterceptor interceptor =
115+
CensusClientInterceptor.newBuilder().build();
116+
Channel interceptedChannel = ClientInterceptors.intercept(channel, interceptor);
117+
interceptedChannel.newCall(method, CALL_OPTIONS);
118+
assertThat(callOptionsCaptor.get().getStreamTracerFactories()).isEmpty();
172119
}
173120

174121
@Test
175-
public void stats_starts_noFinishes_noRealTime() {
176-
122+
public void customStatsConfig() {
123+
ClientInterceptor interceptor =
124+
CensusClientInterceptor.newBuilder()
125+
.setStatsEnabled(true)
126+
.setRecordStartedRpcs(false)
127+
.setRecordFinishedRpcs(false)
128+
.setRecordRealTimeMetrics(true)
129+
.build();
130+
Channel interceptedChannel = ClientInterceptors.intercept(channel, interceptor);
131+
interceptedChannel.newCall(method, CALL_OPTIONS);
132+
CensusStatsModule.ClientCallTracer callTracer =
133+
(CensusStatsModule.ClientCallTracer) Iterables.getOnlyElement(
134+
callOptionsCaptor.get().getStreamTracerFactories());
135+
assertThat(callTracer.module.recordStartedRpcs).isEqualTo(false);
136+
assertThat(callTracer.module.recordFinishedRpcs).isEqualTo(false);
137+
assertThat(callTracer.module.recordRealTimeMetrics).isEqualTo(true);
177138
}
178139

179140
@Test
180-
public void stats_noStarts_finishes_noRealTime() {
181-
141+
public void onlyEnableTracing() {
142+
ClientInterceptor interceptor =
143+
CensusClientInterceptor.newBuilder().setTracingEnabled(true).build();
144+
Channel interceptedChannel = ClientInterceptors.intercept(channel, interceptor);
145+
interceptedChannel.newCall(method, CALL_OPTIONS);
146+
assertThat(Iterables.getOnlyElement(callOptionsCaptor.get().getStreamTracerFactories()))
147+
.isInstanceOf(CensusTracingModule.ClientCallTracer.class);
182148
}
183149

184150
@Test
185-
public void stats_noStarts_noFinishes_noRealTime() {
186-
151+
public void enableStatsAndTracing() {
152+
ClientInterceptor interceptor =
153+
CensusClientInterceptor.newBuilder().setStatsEnabled(true).setTracingEnabled(true).build();
154+
Channel interceptedChannel = ClientInterceptors.intercept(channel, interceptor);
155+
interceptedChannel.newCall(method, CALL_OPTIONS);
156+
assertThat(callOptionsCaptor.get().getStreamTracerFactories()).hasSize(2);
157+
assertThat(callOptionsCaptor.get().getStreamTracerFactories().get(0))
158+
.isInstanceOf(CensusTracingModule.ClientCallTracer.class);
159+
assertThat(callOptionsCaptor.get().getStreamTracerFactories().get(1))
160+
.isInstanceOf(CensusStatsModule.ClientCallTracer.class);
187161
}
188-
}
162+
}

0 commit comments

Comments
 (0)