1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.util.Random;
21 import java.util.concurrent.atomic.AtomicInteger;
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LuceneTestCase;
28 public class TestNRTReaderWithThreads extends LuceneTestCase {
29 AtomicInteger seq = new AtomicInteger(1);
31 public void testIndexing() throws Exception {
32 Directory mainDir = newDirectory();
33 IndexWriter writer = new IndexWriter(
35 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
36 setMaxBufferedDocs(10).
37 setMergePolicy(newLogMergePolicy(false,2))
39 writer.setInfoStream(VERBOSE ? System.out : null);
40 IndexReader reader = writer.getReader(); // start pooling readers
42 RunThread[] indexThreads = new RunThread[4];
43 for (int x=0; x < indexThreads.length; x++) {
44 indexThreads[x] = new RunThread(x % 2, writer);
45 indexThreads[x].setName("Thread " + x);
46 indexThreads[x].start();
48 long startTime = System.currentTimeMillis();
50 while ((System.currentTimeMillis() - startTime) < duration) {
55 for (int x=0; x < indexThreads.length; x++) {
56 indexThreads[x].run = false;
57 assertNull("Exception thrown: "+indexThreads[x].ex, indexThreads[x].ex);
58 addCount += indexThreads[x].addCount;
59 delCount += indexThreads[x].delCount;
61 for (int x=0; x < indexThreads.length; x++) {
62 indexThreads[x].join();
64 for (int x=0; x < indexThreads.length; x++) {
65 assertNull("Exception thrown: "+indexThreads[x].ex, indexThreads[x].ex);
67 //System.out.println("addCount:"+addCount);
68 //System.out.println("delCount:"+delCount);
73 public class RunThread extends Thread {
75 volatile boolean run = true;
76 volatile Throwable ex;
80 final Random r = new Random(random.nextLong());
82 public RunThread(int type, IndexWriter writer) {
91 //int n = random.nextInt(2);
93 int i = seq.addAndGet(1);
94 Document doc = TestIndexWriterReader.createDocument(i, "index1", 10);
95 writer.addDocument(doc);
97 } else if (type == 1) {
98 // we may or may not delete because the term may not exist,
99 // however we're opening and closing the reader rapidly
100 IndexReader reader = writer.getReader();
101 int id = r.nextInt(seq.intValue());
102 Term term = new Term("id", Integer.toString(id));
103 int count = TestIndexWriterReader.count(term, reader);
104 writer.deleteDocuments(term);
109 } catch (Throwable ex) {
110 ex.printStackTrace(System.out);