Skip to content

Commit 54242c7

Browse files
authored
refactor(core): 优化InstanceOfCache缓存实现并完善测试异常处理
refactor(core): 优化InstanceOfCache缓存实现并完善测试异常处理
2 parents 9f60d3c + c20037a commit 54242c7

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

chainlet-test/src/test/java/com/rpamis/chainlet/test/DemoChainPipelineTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ public void testChainTypeReferenceStaticMethodsWithNull() {
780780
public void registerDuplicateChainIdShouldThrowException() {
781781
ChainTypeReference<DemoUser> reference = new ChainTypeReference<DemoUser>() {};
782782
// given
783-
ChainPipeline<DemoUser> demoChain = ChainPipelineFactory.createChain(reference)
783+
ChainPipelineFactory.createChain(reference)
784784
.chain("DuplicateTest")
785785
.addHandler(new AuthHandler())
786786
.strategy(Strategy.FULL)
@@ -992,8 +992,11 @@ public void testChainContextGetterSetter() {
992992

993993
// 测试其他setter方法
994994
context.setChain(null);
995+
Assert.assertNull(context.getChain());
995996
context.setStrategy(null);
997+
Assert.assertNull(context.getStrategy());
996998
context.setChainHandler(null);
999+
Assert.assertNull(context.getChainHandler());
9971000
List<ChainResult> newCheckResults = new ArrayList<>();
9981001
context.setCheckResults(newCheckResults);
9991002
Assert.assertEquals(newCheckResults, context.getCheckResults());
@@ -1024,6 +1027,7 @@ public void testChainStrategyContextGetterSetter() {
10241027

10251028
// 测试其他setter方法
10261029
context.setChain(null);
1030+
Assert.assertNull(context.getChain());
10271031
ChainResult newChainResult = new ChainResult(ValidateHandler.class, false, demoUser, "New test message");
10281032
context.setChainResult(newChainResult);
10291033
Assert.assertEquals(newChainResult, context.getChainResult());
@@ -1109,8 +1113,6 @@ public void testChainResultGetterSetter() {
11091113
@Test
11101114
@DisplayName("测试MethodMetaDataRegistry的方法")
11111115
public void testMethodMetaDataRegistry() {
1112-
// given
1113-
AuthHandler authHandler = new AuthHandler();
11141116
// when & then
11151117
// 测试getProcessKey方法
11161118
String processKey = MethodMetaDataRegistry.getProcessKey(AuthHandler.class, DemoUser.class);
@@ -1141,8 +1143,6 @@ public void testMethodMetaDataRegistry() {
11411143
@Test
11421144
@DisplayName("测试MethodRecord的方法")
11431145
public void testMethodRecord() throws Exception {
1144-
// 测试构造函数和getter方法
1145-
AuthHandler authHandler = new AuthHandler();
11461146
java.lang.reflect.Method method = AuthHandler.class.getMethod("process", DemoUser.class, ChainHandlerContext.class);
11471147
MethodRecord record = new MethodRecord(method, true);
11481148
Assert.assertEquals(method, record.getMethod());
@@ -1572,7 +1572,7 @@ public void testMethodMetaDataRegistryProcessMethods() {
15721572
Assert.assertTrue(record.isExist());
15731573
Assert.assertEquals(method, record.getMethod());
15741574
} catch (NoSuchMethodException e) {
1575-
e.printStackTrace();
1575+
throw new AssertionError("Failed to get process method", e);
15761576
}
15771577
}
15781578

@@ -1639,7 +1639,7 @@ class TestGenericClass extends ChainTypeReference<DemoUser> {
16391639
Assert.assertNotNull(clazz2);
16401640
Assert.assertEquals(DemoUser.class, clazz2);
16411641
} catch (Exception e) {
1642-
e.printStackTrace();
1642+
Assert.fail("Unexpected exception: " + e.getMessage());
16431643
}
16441644

16451645
// 测试getGenericType方法
@@ -1770,21 +1770,20 @@ public void testParallelChainPipelineImplConcurrentExecution() {
17701770

17711771
// 并发执行多次,验证不会抛出异常
17721772
for (int i = 0; i < 10; i++) {
1773-
final int index = i;
17741773
Thread thread = new Thread(() -> {
17751774
try {
17761775
CompleteChainResult chainResult = demoChain.apply(demoUser);
17771776
Assert.assertTrue(chainResult.isAllow());
17781777
} catch (Exception e) {
1779-
e.printStackTrace();
17801778
Assert.fail("并发执行失败: " + e.getMessage());
17811779
}
17821780
});
17831781
thread.start();
17841782
try {
17851783
thread.join();
17861784
} catch (InterruptedException e) {
1787-
e.printStackTrace();
1785+
Thread.currentThread().interrupt();
1786+
Assert.fail("Thread was interrupted: " + e.getMessage());
17881787
}
17891788
}
17901789
}

chainlet/src/main/java/com/rpamis/chainlet/core/support/InstanceOfCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.rpamis.chainlet.core.support;
1717

1818
import java.util.Map;
19-
import java.util.WeakHashMap;
19+
import java.util.concurrent.ConcurrentHashMap;
2020

2121
/**
2222
* 用于减少instanceof的次数
@@ -30,7 +30,7 @@ private InstanceOfCache() {
3030
throw new IllegalStateException("InstanceOfCache class prohibited instantiation");
3131
}
3232

33-
private static final Map<String, Boolean> CLASS_CACHE = new WeakHashMap<>();
33+
private static final Map<String, Boolean> CLASS_CACHE = new ConcurrentHashMap<>();
3434

3535
/**
3636
* 判断一个类是否是另一个类或其子类的实例

0 commit comments

Comments
 (0)