修复StampedCache的get方法非原子问题

This commit is contained in:
Looly
2023-12-09 01:10:23 +08:00
parent dfc0b4ecf1
commit 6f4a115032
7 changed files with 168 additions and 115 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023. looly(loolly@aliyun.com)
* Hutool is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* https://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package org.dromara.hutool.core.cache;
import org.dromara.hutool.core.cache.impl.TimedCache;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.thread.ThreadUtil;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
/**
* https://gitee.com/dromara/hutool/issues/I8MEIX<br>
* get操作非原子
*/
public class IssueI8MEIXTest {
@Test
@Disabled
void getRemoveTest() {
final TimedCache<String, String> cache = new TimedCache<>(200);
cache.put("a", "123");
ThreadUtil.sleep(300);
// 测试时在get后的remove前加sleep测试在读取过程中put新值的问题
ThreadUtil.execute(() -> {
Console.log("get begin.");
Console.log(cache.get("a"));
});
ThreadUtil.execute(() -> {
ThreadUtil.sleep(200);
cache.put("a", "456");
Console.log("put ok.");
});
ThreadUtil.sleep(1000);
}
}

View File

@@ -32,6 +32,6 @@ public class IssueI5Q4HDTest {
wordTree.addWords(keyWordSet);
//DateUtil.beginOfHour()
final List<String> strings = wordTree.matchAll(content, -1, true, true);
Assertions.assertEquals("[站房, 站房建设, 面积较小, 不符合规范要求, 辅助设施, 站房]", strings.toString());
Assertions.assertEquals("[站房建设, 面积较小, 不符合规范要求, 辅助设施, 站房]", strings.toString());
}
}

View File

@@ -53,7 +53,7 @@ public class NFATest {
stopWatch.start("wordtree_char_find");
final List<String> ans2 = wordTree.matchAll(input, -1, true, true);
stopWatch.stop();
Assertions.assertEquals("she,he,her,say", String.join(",", ans2));
Assertions.assertEquals("she,her,say", String.join(",", ans2));
//Console.log(stopWatch.prettyPrint());
}
@@ -120,7 +120,7 @@ public class NFATest {
wordTreeLocal.addWords("say", "her", "he", "she", "shr");
final List<String> ans2 = wordTreeLocal.matchAll(input, -1, true, true);
stopWatch.stop();
Assertions.assertEquals("she,he,her,say", String.join(",", ans2));
Assertions.assertEquals("she,her,say", String.join(",", ans2));
//Console.log(stopWatch.prettyPrint());
}
@@ -157,8 +157,8 @@ public class NFATest {
final List<String> result1 = wordTreeLocal.matchAll(input, -1, true, true);
stopWatch.stop();
Assertions.assertEquals(3, result1.size());
Assertions.assertEquals(",赵啊,赵啊三", String.join(",", result1));
Assertions.assertEquals(1, result1.size());
Assertions.assertEquals("赵啊三", String.join(",", result1));
//Console.log(stopWatch.prettyPrint());
}
@@ -196,8 +196,8 @@ public class NFATest {
.collect(Collectors.toList());
stopWatch.stop();
Assertions.assertEquals(3, result1.size());
Assertions.assertEquals(",赵啊,赵啊三", String.join(",", result1));
Assertions.assertEquals(1, result1.size());
Assertions.assertEquals("赵啊三", String.join(",", result1));
//Console.log(stopWatch.prettyPrint());
}
@@ -233,7 +233,7 @@ public class NFATest {
stopWatch.stop();
Assertions.assertEquals(1, result1.size());
Assertions.assertEquals("", String.join(",", result1));
Assertions.assertEquals("啊三", String.join(",", result1));
//Console.log(stopWatch.prettyPrint());
}