|
40 | 40 | import java.lang.reflect.Method; |
41 | 41 | import java.lang.reflect.Proxy; |
42 | 42 | import java.util.ArrayList; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.Map; |
43 | 45 | import java.util.concurrent.TimeUnit; |
44 | 46 | import org.junit.jupiter.api.Test; |
45 | 47 | import org.junit.jupiter.api.extension.RegisterExtension; |
@@ -88,6 +90,7 @@ void instrumentsGrpcConnectionRequestAdvice() throws Exception { |
88 | 90 | void instrumentsRpcClientHandleServerRequestAdvice() throws Exception { |
89 | 91 | RpcClient rpcClient = mock(RpcClient.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); |
90 | 92 | setField(rpcClient, "serverRequestHandlers", new ArrayList<ServerRequestHandler>()); |
| 93 | + initializeRpcClientConfigIfPresent(rpcClient); |
91 | 94 | when(rpcClient.getCurrentServer()).thenReturn(new RpcClient.ServerInfo("127.0.0.1", 9848)); |
92 | 95 | rpcClient.registerServerRequestHandler(serverRequestHandler()); |
93 | 96 |
|
@@ -166,6 +169,62 @@ private static void setField(Object target, String fieldName, Object value) thro |
166 | 169 | field.set(target, value); |
167 | 170 | } |
168 | 171 |
|
| 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 | + |
169 | 228 | private static final class TestManagedChannel extends ManagedChannel { |
170 | 229 | private final String authority; |
171 | 230 |
|
|
0 commit comments