add startingGun to BaseTokenStreamTestCase to release all threads at once

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1580020 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-03-21 18:52:36 +00:00
parent bf2086c831
commit f02e15f9f2
1 changed files with 9 additions and 3 deletions

View File

@ -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;
}
@Override
public 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 problem
int 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();