This commit is contained in:
Looly 2022-10-12 00:13:48 +08:00
parent 9eb29695bf
commit 9ecc1406b8

View File

@ -1,7 +1,6 @@
package cn.hutool.core.cache;
import cn.hutool.core.cache.impl.LRUCache;
import cn.hutool.core.lang.Console;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
@ -10,6 +9,7 @@ import org.junit.Ignore;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* <a href="https://github.com/dromara/hutool/issues/1895">https://github.com/dromara/hutool/issues/1895</a><br>
@ -69,13 +69,20 @@ public class LRUCacheTest {
@Test
public void issue2647Test(){
final AtomicInteger removeCount = new AtomicInteger();
final LRUCache<String, Integer> cache = CacheUtil.newLRUCache(3,1);
cache.setListener((key, value) -> Console.log("Start remove k-v, key:{}, value:{}", key, value));
cache.setListener((key, value) -> {
// 共移除7次
removeCount.incrementAndGet();
//Console.log("Start remove k-v, key:{}, value:{}", key, value);
});
for (int i = 0; i < 10; i++) {
cache.put(StrUtil.format("key-{}", i), i);
}
Assert.assertEquals(7, removeCount.get());
Assert.assertEquals(3, cache.size());
}
}