add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / index / TestNRTReaderWithThreads.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.util.Random;
21 import java.util.concurrent.atomic.AtomicInteger;
22
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;
27
28 public class TestNRTReaderWithThreads extends LuceneTestCase {
29   AtomicInteger seq = new AtomicInteger(1);
30
31   public void testIndexing() throws Exception {
32     Directory mainDir = newDirectory();
33     IndexWriter writer = new IndexWriter(
34         mainDir,
35         newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
36             setMaxBufferedDocs(10).
37             setMergePolicy(newLogMergePolicy(false,2))
38     );
39     writer.setInfoStream(VERBOSE ? System.out : null);
40     IndexReader reader = writer.getReader(); // start pooling readers
41     reader.close();
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();
47     }    
48     long startTime = System.currentTimeMillis();
49     long duration = 1000;
50     while ((System.currentTimeMillis() - startTime) < duration) {
51       Thread.sleep(100);
52     }
53     int delCount = 0;
54     int addCount = 0;
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;
60     }
61     for (int x=0; x < indexThreads.length; x++) {
62       indexThreads[x].join();
63     }
64     for (int x=0; x < indexThreads.length; x++) {
65       assertNull("Exception thrown: "+indexThreads[x].ex, indexThreads[x].ex);
66     }
67     //System.out.println("addCount:"+addCount);
68     //System.out.println("delCount:"+delCount);
69     writer.close();
70     mainDir.close();
71   }
72
73   public class RunThread extends Thread {
74     IndexWriter writer;
75     volatile boolean run = true;
76     volatile Throwable ex;
77     int delCount = 0;
78     int addCount = 0;
79     int type;
80     final Random r = new Random(random.nextLong());
81     
82     public RunThread(int type, IndexWriter writer) {
83       this.type = type;
84       this.writer = writer;
85     }
86
87     @Override
88     public void run() {
89       try {
90         while (run) {
91           //int n = random.nextInt(2);
92           if (type == 0) {
93             int i = seq.addAndGet(1);
94             Document doc = DocHelper.createDocument(i, "index1", 10);
95             writer.addDocument(doc);
96             addCount++;
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);
105             reader.close();
106             delCount += count;
107           }
108         }
109       } catch (Throwable ex) {
110         ex.printStackTrace(System.out);
111         this.ex = ex;
112         run = false;
113       }
114     }
115   }
116 }