From 6fb676d1a27327dd1051adeb292571b18c6042b0 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Tue, 3 Jan 2012 14:50:36 +0000 Subject: [PATCH] test analyzers with multiple threads in BaseTokenStreamTestCase.checkRandomData git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1226793 13f79535-47bb-0310-9956-ffa450edef68 --- .../analysis/BaseTokenStreamTestCase.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lucene/src/test-framework/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java b/lucene/src/test-framework/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java index c9b1e3c0722..91ce7848d5e 100644 --- a/lucene/src/test-framework/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java +++ b/lucene/src/test-framework/java/org/apache/lucene/analysis/BaseTokenStreamTestCase.java @@ -249,7 +249,46 @@ public abstract class BaseTokenStreamTestCase extends LuceneTestCase { // TODO: add a MockCharStream, and use it here too, to ensure that correctOffset etc is being done by tokenizers. public static void checkRandomData(Random random, Analyzer a, int iterations) throws IOException { checkRandomData(random, a, iterations, 20); + // now test with multiple threads + int numThreads = _TestUtil.nextInt(random, 4, 8); + Thread threads[] = new Thread[numThreads]; + for (int i = 0; i < threads.length; i++) { + threads[i] = new AnalysisThread(new Random(random.nextLong()), a, iterations); + } + for (int i = 0; i < threads.length; i++) { + threads[i].start(); + } + for (int i = 0; i < threads.length; i++) { + try { + threads[i].join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } } + + static class AnalysisThread extends Thread { + final int iterations; + final Random random; + final Analyzer a; + + AnalysisThread(Random random, Analyzer a, int iterations) { + this.random = random; + this.a = a; + this.iterations = iterations; + } + + @Override + public void run() { + try { + // see the part in checkRandomData where it replays the same text again + // to verify reproducability/reuse: hopefully this would catch thread hazards. + checkRandomData(random, a, iterations, 20); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }; public static void checkRandomData(Random random, Analyzer a, int iterations, int maxWordLength) throws IOException { for (int i = 0; i < iterations; i++) {