3030
3131package com .google .api .gax .grpc ;
3232
33+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
3334import static org .mockito .ArgumentMatchers .any ;
3435import static org .mockito .Mockito .mock ;
3536import static org .mockito .Mockito .spy ;
4445import io .grpc .Metadata ;
4546import io .grpc .MethodDescriptor ;
4647import io .grpc .Status ;
48+ import java .lang .reflect .Method ;
49+ import org .junit .jupiter .api .AfterEach ;
4750import org .junit .jupiter .api .Test ;
4851import org .junit .jupiter .api .extension .ExtendWith ;
4952import org .mockito .Mock ;
@@ -58,6 +61,11 @@ class GrpcLoggingInterceptorTest {
5861
5962 private static final MethodDescriptor <String , Integer > method = FakeMethodDescriptor .create ();
6063
64+ @ AfterEach
65+ void tearDown () throws Exception {
66+ setLoggingEnabled (false );
67+ }
68+
6169 @ Test
6270 void testInterceptor_basic () {
6371 when (channel .newCall (Mockito .<MethodDescriptor <String , Integer >>any (), any (CallOptions .class )))
@@ -101,4 +109,36 @@ void testInterceptor_responseListener() {
101109 Status status = Status .OK ;
102110 interceptor .currentListener .onClose (status , new Metadata ());
103111 }
112+
113+ @ Test
114+ void testInterceptor_skipsMetadataMaterializationWhenLoggingDisabled () throws Exception {
115+ setLoggingEnabled (false );
116+ when (channel .newCall (Mockito .<MethodDescriptor <String , Integer >>any (), any (CallOptions .class )))
117+ .thenReturn (call );
118+
119+ GrpcLoggingInterceptor interceptor = new GrpcLoggingInterceptor ();
120+ Channel intercepted = ClientInterceptors .intercept (channel , interceptor );
121+
122+ @ SuppressWarnings ("unchecked" )
123+ ClientCall .Listener <Integer > listener = mock (ClientCall .Listener .class );
124+
125+ Metadata requestHeaders = mock (Metadata .class );
126+ when (requestHeaders .keys ()).thenThrow (new AssertionError ("request headers should not be read" ));
127+ ClientCall <String , Integer > interceptedCall = intercepted .newCall (method , CallOptions .DEFAULT );
128+
129+ assertDoesNotThrow (() -> interceptedCall .start (listener , requestHeaders ));
130+
131+ Metadata responseHeaders = mock (Metadata .class );
132+ when (responseHeaders .keys ())
133+ .thenThrow (new AssertionError ("response headers should not be read" ));
134+
135+ assertDoesNotThrow (() -> interceptor .currentListener .onHeaders (responseHeaders ));
136+ }
137+
138+ private static void setLoggingEnabled (boolean enabled ) throws Exception {
139+ Class <?> loggingUtils = Class .forName ("com.google.api.gax.logging.LoggingUtils" );
140+ Method method = loggingUtils .getDeclaredMethod ("setLoggingEnabled" , boolean .class );
141+ method .setAccessible (true );
142+ method .invoke (null , enabled );
143+ }
104144}
0 commit comments