forked from apache/skywalking-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSWDescriptionStrategyCacheTest.java
More file actions
239 lines (201 loc) · 10.6 KB
/
SWDescriptionStrategyCacheTest.java
File metadata and controls
239 lines (201 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.bytebuddy.agent.builder;
import org.junit.Test;
import org.junit.Assert;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URLClassLoader;
import java.net.URL;
import java.util.Map;
/**
* Tests the behavior of the WeakHashMap caching mechanism in SWDescriptionStrategy.
*/
public class SWDescriptionStrategyCacheTest {
@Test
public void testWeakHashMapCacheCleanup() throws Exception {
// Get static cache field
Field cacheField = SWDescriptionStrategy.SWTypeDescriptionWrapper.class
.getDeclaredField("CLASS_LOADER_TYPE_CACHE");
cacheField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<ClassLoader, Map<String, SWDescriptionStrategy.TypeCache>> cache =
(Map<ClassLoader, Map<String, SWDescriptionStrategy.TypeCache>>) cacheField.get(null);
// Record initial cache size
int initialCacheSize = cache.size();
// Create test ClassLoader
URLClassLoader testClassLoader = new URLClassLoader(new URL[0], null);
String testTypeName = "com.test.DynamicClass";
// Create SWTypeDescriptionWrapper instance
SWDescriptionStrategy.SWTypeDescriptionWrapper wrapper =
new SWDescriptionStrategy.SWTypeDescriptionWrapper(
null, "test", testClassLoader, testTypeName);
// Call getTypeCache method via reflection to trigger caching
Method getTypeCacheMethod = wrapper.getClass()
.getDeclaredMethod("getTypeCache");
getTypeCacheMethod.setAccessible(true);
SWDescriptionStrategy.TypeCache typeCache =
(SWDescriptionStrategy.TypeCache) getTypeCacheMethod.invoke(wrapper);
// Verify that the ClassLoader exists in cache
Assert.assertTrue("Cache should contain the test ClassLoader",
cache.containsKey(testClassLoader));
Assert.assertNotNull("TypeCache should be created", typeCache);
Assert.assertEquals("Cache size should increase by 1",
initialCacheSize + 1, cache.size());
// Clear ClassLoader references, prepare for GC test
testClassLoader = null;
wrapper = null;
typeCache = null;
// Force garbage collection
System.gc();
Thread.sleep(100);
System.gc();
Thread.sleep(100);
// WeakHashMap should automatically clean up garbage collected ClassLoader entries
int attempts = 0;
int currentCacheSize = cache.size();
while (currentCacheSize > initialCacheSize && attempts < 20) {
System.gc();
Thread.sleep(50);
currentCacheSize = cache.size();
attempts++;
}
System.out.println("Cache size after GC: " + currentCacheSize +
" (initial: " + initialCacheSize + ", attempts: " + attempts + ")");
// Verify that WeakHashMap cleanup mechanism works properly
Assert.assertTrue("WeakHashMap should clean up entries or attempts should be reasonable",
currentCacheSize <= initialCacheSize + 1 && attempts < 20);
}
@Test
public void testBootstrapClassLoaderHandling() throws Exception {
// Get Bootstrap ClassLoader cache field
Field bootstrapCacheField = SWDescriptionStrategy.SWTypeDescriptionWrapper.class
.getDeclaredField("BOOTSTRAP_TYPE_CACHE");
bootstrapCacheField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, SWDescriptionStrategy.TypeCache> bootstrapCache =
(Map<String, SWDescriptionStrategy.TypeCache>) bootstrapCacheField.get(null);
int initialBootstrapCacheSize = bootstrapCache.size();
// Test Bootstrap ClassLoader (null) handling
String testTypeName = "test.BootstrapClass";
SWDescriptionStrategy.SWTypeDescriptionWrapper wrapper =
new SWDescriptionStrategy.SWTypeDescriptionWrapper(
null, "test", null, testTypeName);
// Call getTypeCache method via reflection
Method getTypeCacheMethod = wrapper.getClass()
.getDeclaredMethod("getTypeCache");
getTypeCacheMethod.setAccessible(true);
SWDescriptionStrategy.TypeCache typeCache =
(SWDescriptionStrategy.TypeCache) getTypeCacheMethod.invoke(wrapper);
// Verify Bootstrap ClassLoader cache handling
Assert.assertNotNull("TypeCache should be created for bootstrap classloader", typeCache);
Assert.assertTrue("Bootstrap cache should contain the type",
bootstrapCache.containsKey(testTypeName));
Assert.assertEquals("Bootstrap cache size should increase by 1",
initialBootstrapCacheSize + 1, bootstrapCache.size());
}
@Test
public void testMultipleClassLoadersIndependentCache() throws Exception {
Field cacheField = SWDescriptionStrategy.SWTypeDescriptionWrapper.class
.getDeclaredField("CLASS_LOADER_TYPE_CACHE");
cacheField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<ClassLoader, Map<String, SWDescriptionStrategy.TypeCache>> cache =
(Map<ClassLoader, Map<String, SWDescriptionStrategy.TypeCache>>) cacheField.get(null);
int initialCacheSize = cache.size();
// Create two different ClassLoaders
URLClassLoader classLoader1 = new URLClassLoader(new URL[0], null);
URLClassLoader classLoader2 = new URLClassLoader(new URL[0], null);
String testTypeName = "com.test.SameClassName";
// Create TypeCache with same class name for both ClassLoaders
SWDescriptionStrategy.SWTypeDescriptionWrapper wrapper1 =
new SWDescriptionStrategy.SWTypeDescriptionWrapper(
null, "test", classLoader1, testTypeName);
SWDescriptionStrategy.SWTypeDescriptionWrapper wrapper2 =
new SWDescriptionStrategy.SWTypeDescriptionWrapper(
null, "test", classLoader2, testTypeName);
// Call getTypeCache method via reflection
Method getTypeCacheMethod =
SWDescriptionStrategy.SWTypeDescriptionWrapper.class.getDeclaredMethod("getTypeCache");
getTypeCacheMethod.setAccessible(true);
SWDescriptionStrategy.TypeCache typeCache1 =
(SWDescriptionStrategy.TypeCache) getTypeCacheMethod.invoke(wrapper1);
SWDescriptionStrategy.TypeCache typeCache2 =
(SWDescriptionStrategy.TypeCache) getTypeCacheMethod.invoke(wrapper2);
// Verify that the two ClassLoaders have independent cache entries
Assert.assertNotNull("TypeCache1 should be created", typeCache1);
Assert.assertNotNull("TypeCache2 should be created", typeCache2);
Assert.assertNotSame("TypeCaches should be different instances", typeCache1, typeCache2);
// Verify cache structure
Assert.assertEquals("Cache should contain both classloaders",
initialCacheSize + 2, cache.size());
Assert.assertTrue("Cache should contain classloader1", cache.containsKey(classLoader1));
Assert.assertTrue("Cache should contain classloader2", cache.containsKey(classLoader2));
// Verify each ClassLoader has independent type cache
Map<String, SWDescriptionStrategy.TypeCache> typeCacheMap1 = cache.get(classLoader1);
Map<String, SWDescriptionStrategy.TypeCache> typeCacheMap2 = cache.get(classLoader2);
Assert.assertNotNull("ClassLoader1 should have type cache map", typeCacheMap1);
Assert.assertNotNull("ClassLoader2 should have type cache map", typeCacheMap2);
Assert.assertNotSame("Type cache maps should be different", typeCacheMap1, typeCacheMap2);
Assert.assertTrue("ClassLoader1 cache should contain the type",
typeCacheMap1.containsKey(testTypeName));
Assert.assertTrue("ClassLoader2 cache should contain the type",
typeCacheMap2.containsKey(testTypeName));
}
@Test
public void testConcurrentAccess() throws Exception {
// Test concurrent access scenario
final String testTypeName = "com.test.ConcurrentClass";
final int threadCount = 10;
final Thread[] threads = new Thread[threadCount];
final URLClassLoader[] classLoaders = new URLClassLoader[threadCount];
final SWDescriptionStrategy.TypeCache[] results = new SWDescriptionStrategy.TypeCache[threadCount];
// Create multiple threads to access cache simultaneously
for (int i = 0; i < threadCount; i++) {
final int index = i;
classLoaders[i] = new URLClassLoader(new URL[0], null);
threads[i] = new Thread(() -> {
try {
SWDescriptionStrategy.SWTypeDescriptionWrapper wrapper =
new SWDescriptionStrategy.SWTypeDescriptionWrapper(
null, "test", classLoaders[index], testTypeName);
Method getTypeCacheMethod = wrapper.getClass()
.getDeclaredMethod("getTypeCache");
getTypeCacheMethod.setAccessible(true);
results[index] = (SWDescriptionStrategy.TypeCache)
getTypeCacheMethod.invoke(wrapper);
} catch (Exception e) {
Assert.fail("Concurrent access should not throw exception: " + e.getMessage());
}
});
}
// Start all threads
for (Thread thread : threads) {
thread.start();
}
// Wait for all threads to complete
for (Thread thread : threads) {
thread.join(1000); // Wait at most 1 second
}
// Verify all results
for (int i = 0; i < threadCount; i++) {
Assert.assertNotNull("Result " + i + " should not be null", results[i]);
}
System.out.println("Concurrent access test completed successfully with " + threadCount + " threads");
}
}