Skip to content

Commit 9dca999

Browse files
authored
fix test case (trpc-group#99)
* feat: 修复线程池泄露问题,并增加相应测试。 * docs: 将测试类注释从中文改为英文 * test: 新增测试验证 unregisterMBean 对 null 参数的异常处理 * docs: 将部分代码注释从中文改为英文 * style: 移除 DefTimeoutManager 类 watch 方法多余减号注释行 * style: 统一 ConfigManagerTest 测试方法缩进格式 * style: 移除 ConfigManager.reset() 空行并设置默认值 false * fix: 修正日志标点及异常参数顺序并补充异常对象输出 * docs(test): 将测试类注释及方法注释等更新为英文并增强测试用例
1 parent 9b35b99 commit 9dca999

File tree

2 files changed

+63
-42
lines changed

2 files changed

+63
-42
lines changed

trpc-core/src/test/java/com/tencent/trpc/core/management/support/MBeanRegistryHelperTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ public void testUnregisterMBeanWithInvalidPattern() throws Exception {
108108
MBeanRegistryHelper.unregisterMBean(objectName);
109109
}
110110

111+
/**
112+
* Test unregisterMBean method exception handling by creating a scenario that triggers exception
113+
* This test covers the exception handling branch in unregisterMBean method
114+
*/
115+
@Test
116+
public void testUnregisterMBeanException() throws Exception {
117+
// Test with null ObjectName which should cause RuntimeOperationsException
118+
// in the isRegistered(null) call, which will trigger the exception handling branch
119+
120+
// This should not throw any exception - the exception should be caught and logged
121+
try {
122+
MBeanRegistryHelper.unregisterMBean(null);
123+
// If we reach here, it means the exception was properly caught and handled
124+
} catch (Exception e) {
125+
// If any exception escapes, the test should fail
126+
Assert.fail("Exception should have been caught and logged, but was thrown: " + e.getMessage());
127+
}
128+
129+
// The test passes if no exception is thrown (exception is caught and logged)
130+
// The logger.warn("unregister mbean exception: ", e) line should be executed
131+
// This covers the exception handling branch that was previously untested
132+
}
133+
111134
/**
112135
* Test MBean interface for testing purposes
113136
*/

trpc-proto/trpc-proto-http/src/test/java/com/tencent/trpc/proto/http/client/AbstractConsumerInvokerTest.java

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/*
2-
3-
42
* Tencent is pleased to support the open source community by making tRPC available.
53
*
64
* Copyright (C) 2023 Tencent.
@@ -42,7 +40,7 @@
4240
import org.junit.Test;
4341

4442
/**
45-
* 测试 AbstractConsumerInvoker 的 ShutdownListener 功能
43+
* Test the ShutdownListener functionality of AbstractConsumerInvoker
4644
*/
4745
public class AbstractConsumerInvokerTest {
4846

@@ -58,32 +56,32 @@ public class AbstractConsumerInvokerTest {
5856

5957
@Before
6058
public void setUp() {
61-
// 创建 mock 对象
59+
// Create mock objects
6260
mockClient = mock(AbstractRpcClient.class);
6361
mockConfig = mock(ConsumerConfig.class);
6462
mockProtocolConfig = mock(ProtocolConfig.class);
6563
mockBackendConfig = mock(BackendConfig.class);
6664
mockWorkerPool = mock(WorkerPool.class);
6765

68-
// 设置 mock 对象的行为
66+
// Configure mock object behavior
6967
when(mockConfig.getBackendConfig()).thenReturn(mockBackendConfig);
7068
when(mockBackendConfig.getWorkerPoolObj()).thenReturn(mockWorkerPool);
7169
when(mockProtocolConfig.getIp()).thenReturn("127.0.0.1");
7270
when(mockProtocolConfig.getPort()).thenReturn(8080);
7371
when(mockProtocolConfig.getExtMap()).thenReturn(new HashMap<String, Object>());
7472

75-
// 创建测试实例
73+
// Create test instance
7674
testInvoker = new TestConsumerInvoker(mockClient, mockConfig, mockProtocolConfig);
7775
}
7876

7977
@After
8078
public void tearDown() {
81-
// 重置静态状态
79+
// Reset static state
8280
AbstractConsumerInvoker.reset();
8381
}
8482

8583
/**
86-
* 测试 ShutdownListener 不为空
84+
* Test that ShutdownListener is not null
8785
*/
8886
@Test
8987
public void testShutdownListenerNotNull() {
@@ -92,33 +90,33 @@ public void testShutdownListenerNotNull() {
9290
}
9391

9492
/**
95-
* 测试 onShutdown 方法的日志输出和执行
93+
* Test the log output and execution of onShutdown method
9694
*/
9795
@Test
9896
public void testOnShutdownExecution() {
99-
// 获取 ShutdownListener
97+
// Get ShutdownListener
10098
ShutdownListener shutdownListener = testInvoker.getShutdownListener();
10199
assertNotNull("ShutdownListener should not be null", shutdownListener);
102100

103-
// 测试 onShutdown 方法不会抛出异常
101+
// Test that onShutdown method does not throw exceptions
104102
try {
105103
shutdownListener.onShutdown();
106-
// 如果没有异常,测试通过
104+
// If no exception occurs, test passes
107105
assertTrue("onShutdown method should execute without exceptions", true);
108106
} catch (Exception e) {
109107
throw new AssertionError("onShutdown method should not throw exceptions", e);
110108
}
111109
}
112110

113111
/**
114-
* 测试多次调用 onShutdown 方法的安全性
112+
* Test the safety of calling onShutdown method multiple times
115113
*/
116114
@Test
117115
public void testMultipleOnShutdownCalls() {
118116
ShutdownListener shutdownListener = testInvoker.getShutdownListener();
119117
assertNotNull("ShutdownListener should not be null", shutdownListener);
120118

121-
// 多次调用 onShutdown 方法,确保不会出现异常
119+
// Call onShutdown method multiple times to ensure no exceptions occur
122120
try {
123121
shutdownListener.onShutdown();
124122
shutdownListener.onShutdown();
@@ -130,26 +128,26 @@ public void testMultipleOnShutdownCalls() {
130128
}
131129

132130
/**
133-
* 测试 ShutdownListener 的类型
131+
* Test the type of ShutdownListener
134132
*/
135133
@Test
136134
public void testShutdownListenerType() {
137135
ShutdownListener shutdownListener = testInvoker.getShutdownListener();
138136
assertNotNull("ShutdownListener should not be null", shutdownListener);
139137

140-
// 验证 ShutdownListener 是内部类的实例
138+
// Verify that ShutdownListener is an instance of the inner class
141139
String className = shutdownListener.getClass().getSimpleName();
142140
assertTrue("ShutdownListener should be InternalShutdownListener",
143141
className.contains("InternalShutdownListener"));
144142
}
145143

146144
/**
147-
* 测试静态方法 stop() reset() 的调用
145+
* Test the invocation of static methods stop() and reset()
148146
*/
149147
@Test
150148
public void testStaticMethods() {
151149
try {
152-
// 测试静态方法调用不会抛出异常
150+
// Test that static method calls do not throw exceptions
153151
AbstractConsumerInvoker.stop();
154152
AbstractConsumerInvoker.reset();
155153
assertTrue("Static methods should execute without exceptions", true);
@@ -159,18 +157,18 @@ public void testStaticMethods() {
159157
}
160158

161159
/**
162-
* 测试 HTTP 协议的默认配置(不包含 keystore 配置)
160+
* Test default configuration for HTTP protocol (without keystore configuration)
163161
*/
164162
@Test
165163
public void testHttpSchemeWithoutKeystore() throws Exception {
166-
// 创建不包含 keystore 配置的 extMap
164+
// Create extMap without keystore configuration
167165
Map<String, Object> extMap = new HashMap<>();
168166
when(mockProtocolConfig.getExtMap()).thenReturn(extMap);
169167

170-
// 创建新的测试实例
168+
// Create new test instance
171169
TestConsumerInvoker httpInvoker = new TestConsumerInvoker(mockClient, mockConfig, mockProtocolConfig);
172170

173-
// 通过反射获取 scheme 字段来验证
171+
// Verify by accessing the scheme field via reflection
174172
Field schemeField = AbstractConsumerInvoker.class.getDeclaredField("scheme");
175173
schemeField.setAccessible(true);
176174
String scheme = (String) schemeField.get(httpInvoker);
@@ -179,20 +177,20 @@ public void testHttpSchemeWithoutKeystore() throws Exception {
179177
}
180178

181179
/**
182-
* 测试 HTTPS 协议的配置(包含 keystore 配置)
180+
* Test HTTPS protocol configuration (with keystore configuration)
183181
*/
184182
@Test
185183
public void testHttpsSchemeWithKeystore() throws Exception {
186-
// 创建包含 keystore 配置的 extMap
184+
// Create extMap with keystore configuration
187185
Map<String, Object> extMap = new HashMap<>();
188186
extMap.put(KEYSTORE_PATH, "/path/to/keystore.jks");
189187
extMap.put(KEYSTORE_PASS, "password123");
190188
when(mockProtocolConfig.getExtMap()).thenReturn(extMap);
191189

192-
// 创建新的测试实例
190+
// Create new test instance
193191
TestConsumerInvoker httpsInvoker = new TestConsumerInvoker(mockClient, mockConfig, mockProtocolConfig);
194192

195-
// 通过反射获取 scheme 字段来验证
193+
// Verify by accessing the scheme field via reflection
196194
Field schemeField = AbstractConsumerInvoker.class.getDeclaredField("scheme");
197195
schemeField.setAccessible(true);
198196
String scheme = (String) schemeField.get(httpsInvoker);
@@ -201,19 +199,19 @@ public void testHttpsSchemeWithKeystore() throws Exception {
201199
}
202200

203201
/**
204-
* 测试只有 KEYSTORE_PATH 但没有 KEYSTORE_PASS 的情况(应该使用 HTTP
202+
* Test case with only KEYSTORE_PATH but no KEYSTORE_PASS (should use HTTP)
205203
*/
206204
@Test
207205
public void testHttpSchemeWithOnlyKeystorePath() throws Exception {
208-
// 创建只包含 KEYSTORE_PATH 的 extMap
206+
// Create extMap with only KEYSTORE_PATH
209207
Map<String, Object> extMap = new HashMap<>();
210208
extMap.put(KEYSTORE_PATH, "/path/to/keystore.jks");
211209
when(mockProtocolConfig.getExtMap()).thenReturn(extMap);
212210

213-
// 创建新的测试实例
211+
// Create new test instance
214212
TestConsumerInvoker httpInvoker = new TestConsumerInvoker(mockClient, mockConfig, mockProtocolConfig);
215213

216-
// 通过反射获取 scheme 字段来验证
214+
// Verify by accessing the scheme field via reflection
217215
Field schemeField = AbstractConsumerInvoker.class.getDeclaredField("scheme");
218216
schemeField.setAccessible(true);
219217
String scheme = (String) schemeField.get(httpInvoker);
@@ -222,19 +220,19 @@ public void testHttpSchemeWithOnlyKeystorePath() throws Exception {
222220
}
223221

224222
/**
225-
* 测试只有 KEYSTORE_PASS 但没有 KEYSTORE_PATH 的情况(应该使用 HTTP
223+
* Test case with only KEYSTORE_PASS but no KEYSTORE_PATH (should use HTTP)
226224
*/
227225
@Test
228226
public void testHttpSchemeWithOnlyKeystorePass() throws Exception {
229-
// 创建只包含 KEYSTORE_PASS 的 extMap
227+
// Create extMap with only KEYSTORE_PASS
230228
Map<String, Object> extMap = new HashMap<>();
231229
extMap.put(KEYSTORE_PASS, "password123");
232230
when(mockProtocolConfig.getExtMap()).thenReturn(extMap);
233231

234-
// 创建新的测试实例
232+
// Create new test instance
235233
TestConsumerInvoker httpInvoker = new TestConsumerInvoker(mockClient, mockConfig, mockProtocolConfig);
236234

237-
// 通过反射获取 scheme 字段来验证
235+
// Verify by accessing the scheme field via reflection
238236
Field schemeField = AbstractConsumerInvoker.class.getDeclaredField("scheme");
239237
schemeField.setAccessible(true);
240238
String scheme = (String) schemeField.get(httpInvoker);
@@ -243,27 +241,27 @@ public void testHttpSchemeWithOnlyKeystorePass() throws Exception {
243241
}
244242

245243
/**
246-
* 测试 URI 构建在不同协议下的正确性
244+
* Test URI construction correctness under different protocols
247245
*/
248246
@Test
249247
public void testUriConstructionWithDifferentSchemes() throws Exception {
250-
// 设置 mock 对象的基本配置
248+
// Configure basic settings for mock objects
251249
when(mockConfig.getBackendConfig().getBasePath()).thenReturn("/api");
252250

253-
// 创建 mock request invocation
251+
// Create mock request and invocation
254252
Request mockRequest = mock(Request.class);
255253
com.tencent.trpc.core.rpc.RpcInvocation mockInvocation = mock(com.tencent.trpc.core.rpc.RpcInvocation.class);
256254
when(mockRequest.getInvocation()).thenReturn(mockInvocation);
257255
when(mockInvocation.getFunc()).thenReturn("/test");
258256

259-
// 测试 HTTP 协议的 URI
257+
// Test URI for HTTP protocol
260258
Map<String, Object> httpExtMap = new HashMap<>();
261259
when(mockProtocolConfig.getExtMap()).thenReturn(httpExtMap);
262260
TestConsumerInvoker httpInvoker = new TestConsumerInvoker(mockClient, mockConfig, mockProtocolConfig);
263261
URI httpUri = httpInvoker.getUri(mockRequest);
264262
assertEquals("HTTP URI scheme should be http", HTTP_SCHEME, httpUri.getScheme());
265263

266-
// 测试 HTTPS 协议的 URI
264+
// Test URI for HTTPS protocol
267265
Map<String, Object> httpsExtMap = new HashMap<>();
268266
httpsExtMap.put(KEYSTORE_PATH, "/path/to/keystore.jks");
269267
httpsExtMap.put(KEYSTORE_PASS, "password123");
@@ -274,7 +272,7 @@ public void testUriConstructionWithDifferentSchemes() throws Exception {
274272
}
275273

276274
/**
277-
* 测试用的 ConsumerInvoker 实现类
275+
* Test ConsumerInvoker implementation class for testing purposes
278276
*/
279277
private static class TestConsumerInvoker extends AbstractConsumerInvoker<TestService> {
280278

@@ -285,13 +283,13 @@ public TestConsumerInvoker(AbstractRpcClient client, ConsumerConfig<TestService>
285283

286284
@Override
287285
public Response send(Request request) throws Exception {
288-
// 简单的测试实现
286+
// Simple test implementation
289287
return null;
290288
}
291289
}
292290

293291
/**
294-
* 测试用的服务接口
292+
* Test service interface for testing purposes
295293
*/
296294
private interface TestService {
297295
String testMethod(String input);

0 commit comments

Comments
 (0)