Skip to content

Commit

Permalink
[type:fix] remove closed session from the NAMESPACE_SESSION_MAP (#5734)
Browse files Browse the repository at this point in the history
add test for create non singleton spi object in multi thread

Co-authored-by: aias00 <[email protected]>
  • Loading branch information
eye-gu and Aias00 authored Oct 29, 2024
1 parent b066eb7 commit 586d571
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ private static synchronized void sendMessageBySession(final Session session, fin

private void clearSession(final Session session) {
SESSION_SET.remove(session);
String namespaceId = getNamespaceId(session);
if (StringUtils.isNotBlank(namespaceId)) {
NAMESPACE_SESSION_MAP.getOrDefault(namespaceId, Sets.newConcurrentHashSet()).remove(session);
}
ThreadLocalUtils.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
Expand Down Expand Up @@ -296,7 +298,31 @@ public void loadResourcesIOException()
assertThat(expect.getTargetException().getMessage(), containsString("load extension resources error"));
}
}


@Test
public void testMultiThreadNonSingleton() throws InterruptedException {
int threadNum = Runtime.getRuntime().availableProcessors();
if (threadNum <= 1) {
threadNum = 2;
}
int loop = 10000;
ConcurrentHashMap<ListSPI, Boolean> cache = new ConcurrentHashMap<>();
List<Thread> threads = new ArrayList<>(threadNum);
for (int i = 0; i < threadNum; i++) {
Thread arrayList = new Thread(() -> {
for (int j = 0; j < loop; j++) {
cache.put(ExtensionLoader.getExtensionLoader(ListSPI.class).getJoin("arrayList"), true);
}
});
arrayList.start();
threads.add(arrayList);
}
for (Thread thread : threads) {
thread.join();
}
assertEquals(threadNum * loop, cache.size());
}

/**
* get private loadClass method.
*/
Expand Down

0 comments on commit 586d571

Please sign in to comment.