Apache Solr open-source search software
Use latches instead of sleeping for one second in SplitShardTest. (#3954)
Pierre Salagnacee0081e2e8c5
diff --git a/solr/core/src/test/org/apache/solr/cloud/SplitShardTest.java b/solr/core/src/test/org/apache/solr/cloud/SplitShardTest.javaindex 9018a32e294..0b539bd39d0 100644--- a/solr/core/src/test/org/apache/solr/cloud/SplitShardTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/SplitShardTest.java@@ -23,11 +23,13 @@ import java.io.IOException;import java.lang.invoke.MethodHandles;import java.util.ArrayList;import java.util.Collection;+import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicBoolean;import java.util.concurrent.atomic.AtomicInteger;@@ -261,10 +263,13 @@ public class SplitShardTest extends SolrCloudTestCase {new ConcurrentHashMap<>(); // what the index should containfinal AtomicBoolean doIndex = new AtomicBoolean(true);final AtomicInteger docsIndexed = new AtomicInteger();+ List<CountDownLatch> threadLatches = Collections.synchronizedList(new ArrayList<>());Thread[] indexThreads = new Thread[nThreads];try {for (int i = 0; i < nThreads; i++) {+ int threadIndex = i;+ threadLatches.add(new CountDownLatch(10));indexThreads[i] =new Thread(() -> {@@ -290,6 +295,7 @@ public class SplitShardTest extends SolrCloudTestCase {fail(e.getMessage());break;}+ threadLatches.get(threadIndex).countDown();}});}@@ -298,8 +304,12 @@ public class SplitShardTest extends SolrCloudTestCase {thread.start();}- Thread.sleep(100); // wait for a few docs to be indexed before invoking split+ // wait for a few docs (10 requests per threads) to be indexed before invoking split+ for (int i = 0; i < nThreads; i++) {+ assertTrue(threadLatches.get(i).await(30, TimeUnit.SECONDS));+ }int docCount = model.size();+ assertTrue("no docs indexed", docCount > 0);CollectionAdminRequest.SplitShard splitShard =CollectionAdminRequest.splitShard(collectionName).setShardName("shard1");@@ -313,8 +323,14 @@ public class SplitShardTest extends SolrCloudTestCase {// make sure that docs were indexed during the splitassertTrue(model.size() > docCount);- Thread.sleep(100); // wait for a few more docs to be indexed after split-+ // wait for a few more docs (10 more requests per thread) to be indexed after split+ // Reset the latch of each thread, and them wait for all of them+ for (int i = 0; i < nThreads; i++) {+ threadLatches.set(i, new CountDownLatch(10));+ }+ for (int i = 0; i < nThreads; i++) {+ assertTrue(threadLatches.get(i).await(30, TimeUnit.SECONDS));+ }} finally {// shut down the indexersdoIndex.set(false);