mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix weak bug
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.cache;
|
||||
|
||||
import cn.hutool.cache.impl.WeakCache;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -14,8 +15,31 @@ public class WeakCacheTest {
|
||||
|
||||
Assert.assertEquals(2, cache.size());
|
||||
|
||||
// 检查被MutableObj包装的key能否正常移除
|
||||
cache.remove("abc");
|
||||
|
||||
Assert.assertEquals(1, cache.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeByGcTest(){
|
||||
WeakCache<String, String> cache = new WeakCache<>(-1);
|
||||
cache.put("a", "1");
|
||||
cache.put("b", "2");
|
||||
Assert.assertEquals(2, cache.size());
|
||||
|
||||
// GC测试
|
||||
int i=0;
|
||||
while(true){
|
||||
if(2 == cache.size()){
|
||||
i++;
|
||||
Console.log("Object is alive for {} loops - ", i);
|
||||
System.gc();
|
||||
}else{
|
||||
Console.log("Object has been collected.");
|
||||
Console.log(cache.size());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user