Skip to content

Commit

Permalink
Make ClusterNodeTest.testGetOrCreateOriginNodeMultiThread stable
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjoo2010 authored and sczyh30 committed Apr 12, 2019
1 parent 16082a8 commit 73f166e
Showing 1 changed file with 2 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -81,8 +80,6 @@ public void testGetOrCreateOriginNodeMultiThread() {
// Here we need a thread-safe concurrent set (created from ConcurrentHashMap).
final Set<Node> createdNodes = Collections.newSetFromMap(new ConcurrentHashMap<Node, Boolean>());

final Random random = new Random();

// 10 threads, 3 origins, 20 tasks (calling 20 times of ClusterNode#getOrCreateOriginNode concurrently)
final ExecutorService es = Executors.newFixedThreadPool(10);
final List<String> origins = Arrays.asList("origin1", "origin2", "origin3");
Expand All @@ -91,11 +88,12 @@ public void testGetOrCreateOriginNodeMultiThread() {

List<Callable<Object>> tasks = new ArrayList<Callable<Object>>(taskCount);
for (int i = 0; i < taskCount; i++) {
final int index = i % origins.size();
tasks.add(new Callable<Object>() {
@Override
public Object call() throws Exception {
// one task call one times of ClusterNode#getOrCreateOriginNode
Node node = clusterNode.getOrCreateOriginNode(origins.get(random.nextInt(origins.size())));
Node node = clusterNode.getOrCreateOriginNode(origins.get(index));
// add the result node to the createdNodes set
createdNodes.add(node);
latch.countDown();
Expand Down

0 comments on commit 73f166e

Please sign in to comment.