Apache Lucene open-source search software
add startingGun to BaseTokenStreamTestCase to release all threads at once
Robert Muirf02e15f9f291
diff --git a/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.javaindex d62a425f6df..668b7e7eb27 100644--- a/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java@@ -26,6 +26,7 @@ import java.io.StringReader;import java.io.StringWriter;import java.io.Writer;import java.util.*;+import java.util.concurrent.CountDownLatch;import org.apache.lucene.analysis.tokenattributes.*;import org.apache.lucene.document.Document;@@ -450,13 +451,14 @@ public abstract class BaseTokenStreamTestCase extends LuceneTestCase {final boolean simple;final boolean offsetsAreCorrect;final RandomIndexWriter iw;+ final CountDownLatch latch;// NOTE: not volatile because we don't want the tests to// add memory barriers (ie alter how threads// interact)... so this is just "best effort":public boolean failed;- AnalysisThread(long seed, Analyzer a, int iterations, int maxWordLength, boolean useCharFilter, boolean simple, boolean offsetsAreCorrect, RandomIndexWriter iw) {+ AnalysisThread(long seed, CountDownLatch latch, Analyzer a, int iterations, int maxWordLength, boolean useCharFilter, boolean simple, boolean offsetsAreCorrect, RandomIndexWriter iw) {this.seed = seed;this.a = a;this.iterations = iterations;@@ -465,17 +467,19 @@ public abstract class BaseTokenStreamTestCase extends LuceneTestCase {this.simple = simple;this.offsetsAreCorrect = offsetsAreCorrect;this.iw = iw;+ this.latch = latch;}@Overridepublic void run() {boolean success = false;try {+ latch.await();// see the part in checkRandomData where it replays the same text again// to verify reproducability/reuse: hopefully this would catch thread hazards.checkRandomData(new Random(seed), a, iterations, maxWordLength, useCharFilter, simple, offsetsAreCorrect, iw);success = true;- } catch (IOException e) {+ } catch (Exception e) {Rethrow.rethrow(e);} finally {failed = !success;@@ -507,13 +511,15 @@ public abstract class BaseTokenStreamTestCase extends LuceneTestCase {// now test with multiple threads: note we do the EXACT same thing we did before in each thread,// so this should only really fail from another thread if its an actual thread problemint numThreads = TestUtil.nextInt(random, 2, 4);+ final CountDownLatch startingGun = new CountDownLatch(1);AnalysisThread threads[] = new AnalysisThread[numThreads];for (int i = 0; i < threads.length; i++) {- threads[i] = new AnalysisThread(seed, a, iterations, maxWordLength, useCharFilter, simple, offsetsAreCorrect, iw);+ threads[i] = new AnalysisThread(seed, startingGun, a, iterations, maxWordLength, useCharFilter, simple, offsetsAreCorrect, iw);}for (int i = 0; i < threads.length; i++) {threads[i].start();}+ startingGun.countDown();for (int i = 0; i < threads.length; i++) {try {threads[i].join();