From 4c563da8bde1c922b27038da29c20d8390f0c5bf Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 23 Oct 2025 11:16:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D`JschSessionPool.remove`?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../cn/hutool/core/lang/SimpleCacheTest.java | 37 ++++++++++++++++++- .../cn/hutool/extra/ssh/JschSessionPool.java | 14 ++++--- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49cace13d..4b47a10c4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ ### 🐞Bug修复 * 【jwt 】 修复verify方法在定义alg为`none`时验证失效问题(issue#4105@Github) +* 【extra 】 修复`JschSessionPool.remove`逻辑错误问题。 ------------------------------------------------------------------------------------------------------------- # 5.8.41(2025-10-12) diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java index 5ee4fabeb..644f622d6 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java @@ -6,8 +6,9 @@ import cn.hutool.core.thread.ThreadUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; public class SimpleCacheTest { @@ -61,4 +62,36 @@ public class SimpleCacheTest { assertEquals("aaaValue", cache.get("aaa")); IoUtil.close(tester); } + + @Test + void removeTest(){ + final SimpleCache cache = new SimpleCache<>(); + cache.put("key1", "value1"); + cache.get("key1"); + cache.put("key2", "value2"); + cache.get("key2"); + cache.put("key3", "value3"); + cache.get("key3"); + cache.put("key4", "value4"); + cache.get("key4"); + cache.get("key5", ()->"value5"); + + String key = null; + for (Map.Entry entry : cache) { + if ("value3".equals(entry.getValue())) { + key = entry.getKey(); + break; + } + } + + if(null != key){ + cache.remove(key); + } + + assertEquals("value1", cache.get("key1")); + assertEquals("value2", cache.get("key2")); + assertEquals("value4", cache.get("key4")); + assertEquals("value5", cache.get("key5")); + assertNull(cache.get("key3")); + } } diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschSessionPool.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschSessionPool.java index 4d312c8ec..a40073c76 100755 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschSessionPool.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschSessionPool.java @@ -4,7 +4,7 @@ import cn.hutool.core.lang.SimpleCache; import cn.hutool.core.util.StrUtil; import com.jcraft.jsch.Session; -import java.util.Iterator; +import java.util.Map; import java.util.Map.Entry; /** @@ -110,15 +110,17 @@ public enum JschSessionPool { */ public void remove(Session session) { if (null != session) { - final Iterator> iterator = this.cache.iterator(); - Entry entry; - while (iterator.hasNext()) { - entry = iterator.next(); + String key = null; + for (Map.Entry entry : cache) { if (session.equals(entry.getValue())) { - iterator.remove(); + key = entry.getKey(); break; } } + + if(null != key){ + cache.remove(key); + } } }