fix weak bug

This commit is contained in:
Looly
2022-04-10 18:30:07 +08:00
parent 477657ffb8
commit af977ac3d4
11 changed files with 106 additions and 122 deletions

View File

@@ -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;
}
}
}
}