18
18
19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
21
+ import com .google .common .collect .Iterables ;
21
22
import io .grpc .CallOptions ;
22
23
import io .grpc .Channel ;
23
24
import io .grpc .ClientCall ;
24
25
import io .grpc .ClientInterceptor ;
25
26
import io .grpc .ClientInterceptors ;
26
27
import io .grpc .ManagedChannel ;
27
- import io .grpc .Metadata ;
28
28
import io .grpc .MethodDescriptor ;
29
- import io .grpc .ServerCall ;
30
- import io .grpc .ServerCallHandler ;
31
- import io .grpc .ServerServiceDefinition ;
32
- import io .grpc .Status ;
33
29
import io .grpc .inprocess .InProcessChannelBuilder ;
34
- import io .grpc .inprocess .InProcessServerBuilder ;
30
+ import io .grpc .inprocess .InternalInProcess ;
35
31
import io .grpc .testing .GrpcCleanupRule ;
36
- import io .opencensus .trace .Tracer ;
37
- import io .opencensus .trace .propagation .BinaryFormat ;
38
32
import java .io .InputStream ;
39
33
import java .util .concurrent .atomic .AtomicReference ;
40
34
import org .junit .Before ;
41
35
import org .junit .Rule ;
42
36
import org .junit .Test ;
43
37
import org .junit .runner .RunWith ;
44
38
import org .junit .runners .JUnit4 ;
45
- import org .mockito .Mock ;
46
- import org .mockito .junit .MockitoJUnit ;
47
- import org .mockito .junit .MockitoRule ;
48
39
49
40
/**
50
- * Test for {@link CensusClientInterceptor} and {@link CensusTracingModule} .
41
+ * Test for {@link CensusClientInterceptor}.
51
42
*/
52
43
@ RunWith (JUnit4 .class )
53
44
public class CensusClientInterceptorTest {
54
45
55
- @ Rule
56
- public final MockitoRule mocks = MockitoJUnit .rule ();
57
46
@ Rule
58
47
public final GrpcCleanupRule grpcCleanupRule = new GrpcCleanupRule ();
59
48
60
49
private static final CallOptions .Key <String > CUSTOM_OPTION =
61
50
CallOptions .Key .createWithDefault ("option" , "default" );
62
51
private static final CallOptions CALL_OPTIONS =
63
52
CallOptions .DEFAULT .withOption (CUSTOM_OPTION , "customvalue" );
53
+
64
54
private static class StringInputStream extends InputStream {
65
55
final String string ;
66
56
@@ -97,92 +87,76 @@ public String parse(InputStream stream) {
97
87
.setFullMethodName ("package1.service2/method3" )
98
88
.build ();
99
89
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 <>();
108
91
109
92
private ManagedChannel channel ;
110
93
94
+
95
+ @ SuppressWarnings ("unchecked" )
111
96
@ Before
112
97
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 () {
151
99
@ Override
152
100
public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
153
101
MethodDescriptor <ReqT , RespT > method , CallOptions callOptions , Channel next ) {
154
- capturedCallOptions .set (callOptions );
102
+ callOptionsCaptor .set (callOptions );
155
103
return next .newCall (method , callOptions );
156
104
}
157
105
};
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 () );
162
110
}
163
111
164
112
@ 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 ();
172
119
}
173
120
174
121
@ 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 );
177
138
}
178
139
179
140
@ 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 );
182
148
}
183
149
184
150
@ 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 );
187
161
}
188
- }
162
+ }
0 commit comments