Skip to content

Commit 5293c05

Browse files
committed
feat: fix EnhancerTest
1 parent fcd3a9b commit 5293c05

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

core/src/test/java/com/taobao/arthas/core/advisor/EnhancerTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,24 @@ public void testEnhanceWithClassLoaderHash() throws Throwable {
111111
EqualsMatcher<String> methodNameMatcher = new EqualsMatcher<String>("print");
112112
EqualsMatcher<String> classNameMatcher = new EqualsMatcher<String>(MathGame.class.getName());
113113

114-
String targetClassLoaderHash = Integer.toHexString(MathGame.class.getClassLoader().hashCode());
114+
// Enhancer 会过滤与自身 ClassLoader 相同的类(认为是 Arthas 自身加载的类)。
115+
// 这里用另一个 ClassLoader 加载一份同名类,并用 classloader hash 精确指定只增强这一份。
116+
String targetClassLoaderHash = Integer.toHexString(anotherClassLoader.hashCode());
115117
Enhancer enhancer = new Enhancer(listener, false, false, classNameMatcher, null, methodNameMatcher, false,
116118
targetClassLoaderHash);
117119

118120
com.taobao.arthas.core.util.affect.EnhancerAffect affect = enhancer.enhance(instrumentation, 50);
119121

120-
String expectedMethodPrefix = ClassLoaderUtils.classLoaderHash(MathGame.class.getClassLoader()) + "|"
122+
String expectedMethodPrefix = ClassLoaderUtils.classLoaderHash(anotherClassLoader) + "|"
121123
+ MathGame.class.getName() + "#print|";
122-
String anotherMethodPrefix = ClassLoaderUtils.classLoaderHash(anotherClassLoader) + "|" + MathGame.class.getName()
124+
String nonTargetMethodPrefix = ClassLoaderUtils.classLoaderHash(MathGame.class.getClassLoader()) + "|" + MathGame.class.getName()
123125
+ "#print|";
124126

125127
Assertions.assertThat(affect.cCnt()).isEqualTo(1);
126128
Assertions.assertThat(affect.mCnt()).isEqualTo(1);
127129
Assertions.assertThat(affect.getMethods()).hasSize(1);
128130
Assertions.assertThat(affect.getMethods()).allMatch(m -> m.startsWith(expectedMethodPrefix));
129-
Assertions.assertThat(affect.getMethods()).noneMatch(m -> m.startsWith(anotherMethodPrefix));
131+
Assertions.assertThat(affect.getMethods()).noneMatch(m -> m.startsWith(nonTargetMethodPrefix));
130132
} finally {
131133
anotherClassLoader.close();
132134
}

0 commit comments

Comments
 (0)