pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestNRTThreads.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.Set;
21 import java.util.concurrent.ExecutorService;
22
23 import org.apache.lucene.search.IndexSearcher;
24 import org.apache.lucene.store.MockDirectoryWrapper;
25
26 // TODO
27 //   - mix in forceMerge, addIndexes
28 //   - randomoly mix in non-congruent docs
29
30 public class TestNRTThreads extends ThreadedIndexingAndSearchingTestCase {
31   
32   @Override
33   protected void doSearching(ExecutorService es, long stopTime) throws Exception {
34
35     boolean anyOpenDelFiles = false;
36
37     IndexReader r = IndexReader.open(writer, true);
38
39     while (System.currentTimeMillis() < stopTime && !failed.get()) {
40       if (random.nextBoolean()) {
41         if (VERBOSE) {
42           System.out.println("TEST: now reopen r=" + r);
43         }
44         final IndexReader r2 = IndexReader.openIfChanged(r);
45         if (r2 != null) {
46           r.close();
47           r = r2;
48         }
49       } else {
50         if (VERBOSE) {
51           System.out.println("TEST: now close reader=" + r);
52         }
53         r.close();
54         writer.commit();
55         final Set<String> openDeletedFiles = ((MockDirectoryWrapper) dir).getOpenDeletedFiles();
56         if (openDeletedFiles.size() > 0) {
57           System.out.println("OBD files: " + openDeletedFiles);
58         }
59         anyOpenDelFiles |= openDeletedFiles.size() > 0;
60         //assertEquals("open but deleted: " + openDeletedFiles, 0, openDeletedFiles.size());
61         if (VERBOSE) {
62           System.out.println("TEST: now open");
63         }
64         r = IndexReader.open(writer, true);
65       }
66       if (VERBOSE) {
67         System.out.println("TEST: got new reader=" + r);
68       }
69       //System.out.println("numDocs=" + r.numDocs() + "
70       //openDelFileCount=" + dir.openDeleteFileCount());
71
72       if (r.numDocs() > 0) {
73         fixedSearcher = new IndexSearcher(r, es);
74         smokeTestSearcher(fixedSearcher);
75         runSearchThreads(System.currentTimeMillis() + 500);
76       }
77     }
78     r.close();
79
80     //System.out.println("numDocs=" + r.numDocs() + " openDelFileCount=" + dir.openDeleteFileCount());
81     final Set<String> openDeletedFiles = ((MockDirectoryWrapper) dir).getOpenDeletedFiles();
82     if (openDeletedFiles.size() > 0) {
83       System.out.println("OBD files: " + openDeletedFiles);
84     }
85     anyOpenDelFiles |= openDeletedFiles.size() > 0;
86
87     assertFalse("saw non-zero open-but-deleted count", anyOpenDelFiles);
88   }
89
90   private IndexSearcher fixedSearcher;
91
92   protected IndexSearcher getCurrentSearcher() throws Exception {
93     return fixedSearcher;
94   }
95
96   @Override
97   protected void releaseSearcher(IndexSearcher s) throws Exception {
98     if (s != fixedSearcher) {
99       // Final searcher:
100       s.getIndexReader().close();
101       s.close();
102     }
103   }
104
105   @Override
106   protected IndexSearcher getFinalSearcher() throws Exception {
107     final IndexReader r2;
108     if (random.nextBoolean()) {
109       r2 = writer.getReader();
110     } else {
111       writer.commit();
112       r2 = IndexReader.open(dir);
113     }
114     return newSearcher(r2);
115   }
116
117   public void testNRTThreads() throws Exception {
118     runTest("TestNRTThreads");
119   }
120 }