修复JschSessionPool.remove逻辑错误问题。

This commit is contained in:
Looly 2025-10-23 11:16:12 +08:00
parent fb95caa7b9
commit 4c563da8bd
3 changed files with 44 additions and 8 deletions

View File

@ -11,6 +11,7 @@
### 🐞Bug修复 ### 🐞Bug修复
* 【jwt 】 修复verify方法在定义alg为`none`时验证失效问题issue#4105@Github * 【jwt 】 修复verify方法在定义alg为`none`时验证失效问题issue#4105@Github
* 【extra 】 修复`JschSessionPool.remove`逻辑错误问题。
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.41(2025-10-12) # 5.8.41(2025-10-12)

View File

@ -6,8 +6,9 @@ import cn.hutool.core.thread.ThreadUtil;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
public class SimpleCacheTest { public class SimpleCacheTest {
@ -61,4 +62,36 @@ public class SimpleCacheTest {
assertEquals("aaaValue", cache.get("aaa")); assertEquals("aaaValue", cache.get("aaa"));
IoUtil.close(tester); IoUtil.close(tester);
} }
@Test
void removeTest(){
final SimpleCache<String, String> 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<String, String> 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"));
}
} }

View File

@ -4,7 +4,7 @@ import cn.hutool.core.lang.SimpleCache;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
import java.util.Iterator; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
/** /**
@ -110,15 +110,17 @@ public enum JschSessionPool {
*/ */
public void remove(Session session) { public void remove(Session session) {
if (null != session) { if (null != session) {
final Iterator<Entry<String, Session>> iterator = this.cache.iterator(); String key = null;
Entry<String, Session> entry; for (Map.Entry<String, Session> entry : cache) {
while (iterator.hasNext()) {
entry = iterator.next();
if (session.equals(entry.getValue())) { if (session.equals(entry.getValue())) {
iterator.remove(); key = entry.getKey();
break; break;
} }
} }
if(null != key){
cache.remove(key);
}
} }
} }