Skip to content

Commit 7e763c9

Browse files
committed
fix test
1 parent e3e1b13 commit 7e763c9

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

instrumentation/nacos-client-2.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/nacosclient/v2_0/NacosClientAgentInstrumentationTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import java.lang.reflect.Method;
4141
import java.lang.reflect.Proxy;
4242
import java.util.ArrayList;
43+
import java.util.Collections;
44+
import java.util.Map;
4345
import java.util.concurrent.TimeUnit;
4446
import org.junit.jupiter.api.Test;
4547
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -88,6 +90,7 @@ void instrumentsGrpcConnectionRequestAdvice() throws Exception {
8890
void instrumentsRpcClientHandleServerRequestAdvice() throws Exception {
8991
RpcClient rpcClient = mock(RpcClient.class, withSettings().defaultAnswer(CALLS_REAL_METHODS));
9092
setField(rpcClient, "serverRequestHandlers", new ArrayList<ServerRequestHandler>());
93+
initializeRpcClientConfigIfPresent(rpcClient);
9194
when(rpcClient.getCurrentServer()).thenReturn(new RpcClient.ServerInfo("127.0.0.1", 9848));
9295
rpcClient.registerServerRequestHandler(serverRequestHandler());
9396

@@ -166,6 +169,62 @@ private static void setField(Object target, String fieldName, Object value) thro
166169
field.set(target, value);
167170
}
168171

172+
private static void initializeRpcClientConfigIfPresent(RpcClient rpcClient) throws Exception {
173+
try {
174+
Field field = RpcClient.class.getDeclaredField("rpcClientConfig");
175+
field.setAccessible(true);
176+
if (field.get(rpcClient) != null) {
177+
return;
178+
}
179+
Class<?> configType = field.getType();
180+
Object config =
181+
mock(
182+
configType,
183+
invocation -> {
184+
Method method = invocation.getMethod();
185+
String methodName = method.getName();
186+
if ("name".equals(methodName)) {
187+
return "test-rpc-client";
188+
}
189+
Class<?> returnType = method.getReturnType();
190+
if (returnType == boolean.class) {
191+
return false;
192+
}
193+
if (returnType == int.class) {
194+
return 0;
195+
}
196+
if (returnType == long.class) {
197+
return 0L;
198+
}
199+
if (returnType == double.class) {
200+
return 0d;
201+
}
202+
if (returnType == float.class) {
203+
return 0f;
204+
}
205+
if (returnType == short.class) {
206+
return (short) 0;
207+
}
208+
if (returnType == byte.class) {
209+
return (byte) 0;
210+
}
211+
if (returnType == char.class) {
212+
return (char) 0;
213+
}
214+
if (returnType == String.class) {
215+
return "";
216+
}
217+
if (returnType == Map.class) {
218+
return Collections.emptyMap();
219+
}
220+
return null;
221+
});
222+
field.set(rpcClient, config);
223+
} catch (NoSuchFieldException ignored) {
224+
// nacos-client 2.0.0 does not expose rpcClientConfig on RpcClient
225+
}
226+
}
227+
169228
private static final class TestManagedChannel extends ManagedChannel {
170229
private final String authority;
171230

0 commit comments

Comments
 (0)