Skip to content

Commit c2b607b

Browse files
committed
add test.
1 parent 673c9fe commit c2b607b

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcLoggingInterceptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
6666

6767
@Override
6868
public void start(Listener<RespT> responseListener, Metadata headers) {
69-
logRequestInfo(method, logDataBuilder);
69+
logRequestInfo(method, logDataBuilder, logger);
7070
recordRequestHeaders(headers, logDataBuilder);
7171
SimpleForwardingClientCallListener<RespT> responseLoggingListener =
7272
new SimpleForwardingClientCallListener<RespT>(responseListener) {
@@ -107,7 +107,7 @@ public void sendMessage(ReqT message) {
107107
// Helper methods for logging
108108
// some duplications with http equivalent to avoid exposing as public method for now
109109
<ReqT, RespT> void logRequestInfo(
110-
MethodDescriptor<ReqT, RespT> method, LogData.Builder logDataBuilder) {
110+
MethodDescriptor<ReqT, RespT> method, LogData.Builder logDataBuilder, Logger logger) {
111111
try {
112112
if (logger.isInfoEnabled()) {
113113
logDataBuilder.serviceName(method.getServiceName()).rpcName(method.getFullMethodName());

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/GrpcLoggingInterceptorTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030

3131
package com.google.api.gax.grpc;
3232

33+
import static org.junit.Assert.assertEquals;
3334
import static org.mockito.ArgumentMatchers.any;
3435
import static org.mockito.ArgumentMatchers.eq;
3536
import static org.mockito.Mockito.mock;
3637
import static org.mockito.Mockito.spy;
3738
import static org.mockito.Mockito.verify;
3839
import static org.mockito.Mockito.when;
3940

41+
import ch.qos.logback.classic.Level;
4042
import com.google.api.gax.grpc.testing.FakeMethodDescriptor;
4143
import com.google.api.gax.logging.LogData;
4244
import io.grpc.CallOptions;
@@ -46,11 +48,14 @@
4648
import io.grpc.Metadata;
4749
import io.grpc.MethodDescriptor;
4850
import io.grpc.Status;
51+
import org.junit.jupiter.api.Assertions;
4952
import org.junit.jupiter.api.BeforeEach;
5053
import org.junit.jupiter.api.Test;
5154
import org.mockito.Mock;
5255
import org.mockito.Mockito;
5356
import org.mockito.MockitoAnnotations;
57+
import org.slf4j.Logger;
58+
import org.slf4j.LoggerFactory;
5459

5560
class GrpcLoggingInterceptorTest {
5661
@Mock private Channel channel;
@@ -59,6 +64,7 @@ class GrpcLoggingInterceptorTest {
5964

6065
private static final MethodDescriptor<String, Integer> method = FakeMethodDescriptor.create();
6166

67+
private static final Logger LOGGER = LoggerFactory.getLogger(GrpcLoggingInterceptorTest.class);
6268
/** Sets up mocks. */
6369
@BeforeEach
6470
void setUp() {
@@ -111,4 +117,27 @@ void testInterceptor_responseListener() {
111117
verify(interceptor).recordResponsePayload(any(), any(LogData.Builder.class));
112118
verify(interceptor).logResponse(eq(status.getCode().toString()), any(LogData.Builder.class));
113119
}
120+
121+
@Test
122+
void testLogRequestInfo() {
123+
124+
TestAppender testAppender = setupTestLogger(GrpcLoggingInterceptorTest.class);
125+
GrpcLoggingInterceptor interceptor = new GrpcLoggingInterceptor();
126+
interceptor.logRequestInfo(method, LogData.builder(), LOGGER);
127+
128+
Assertions.assertEquals(1, testAppender.events.size());
129+
assertEquals(Level.INFO, testAppender.events.get(0).getLevel());
130+
assertEquals(
131+
"{\"serviceName\":\"FakeClient\",\"message\":\"Sending gRPC request\",\"rpcName\":\"FakeClient/fake-method\"}",
132+
testAppender.events.get(0).getMessage());
133+
testAppender.stop();
134+
}
135+
136+
private TestAppender setupTestLogger(Class<?> clazz) {
137+
TestAppender testAppender = new TestAppender();
138+
testAppender.start();
139+
Logger logger = LoggerFactory.getLogger(clazz);
140+
((ch.qos.logback.classic.Logger) logger).addAppender(testAppender);
141+
return testAppender;
142+
}
114143
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Google LLC nor the names of its
15+
* contributors may be used to endorse or promote products derived from
16+
* this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
package com.google.api.gax.grpc;
32+
33+
import ch.qos.logback.classic.spi.ILoggingEvent;
34+
import ch.qos.logback.core.AppenderBase;
35+
import java.util.ArrayList;
36+
import java.util.List;
37+
38+
/** Logback appender used to set up tests. */
39+
public class TestAppender extends AppenderBase<ILoggingEvent> {
40+
public List<ILoggingEvent> events = new ArrayList<>();
41+
42+
@Override
43+
protected void append(ILoggingEvent eventObject) {
44+
// triggering Logback to capture the current MDC context and store it with the log event
45+
eventObject.getMDCPropertyMap();
46+
events.add(eventObject);
47+
}
48+
49+
public void clearEvents() {
50+
events.clear();
51+
}
52+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<configuration>
2+
<appender name="TEST_APPENDER" class="com.google.api.gax.grpc.TestAppender" />
3+
<root level="INFO">
4+
<appender-ref ref="TEST_APPENDER" />
5+
</root>
6+
</configuration>

0 commit comments

Comments
 (0)