Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
54   156   23   6.75
20   104   0.43   4
8     2.88  
2    
 
  TestThreadSafe       Line # 34 32 13 92.3% 0.9230769
  TestThreadSafe.Thr       Line # 45 22 10 86.7% 0.8666667
 
  (1)
 
1    package org.apache.lucene.search;
2    /**
3    * Copyright 2006 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10    *
11    * Unless required by applicable law or agreed to in writing, software
12    * distributed under the License is distributed on an "AS IS" BASIS,
13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    * See the License for the specific language governing permissions and
15    * limitations under the License.
16    */
17   
18    import junit.framework.TestCase;
19    import org.apache.lucene.store.Directory;
20    import org.apache.lucene.store.RAMDirectory;
21    import org.apache.lucene.index.IndexReader;
22    import org.apache.lucene.index.IndexWriter;
23    import org.apache.lucene.analysis.WhitespaceAnalyzer;
24    import org.apache.lucene.document.*;
25   
26    import java.util.Random;
27    import java.util.List;
28    import java.io.IOException;
29   
30    /**
31    * @author yonik
32    * @version $Id: TestThreadSafe.java 465996 2006-10-20 05:13:32Z yonik $
33    */
 
34    public class TestThreadSafe extends TestCase {
35    Random r = new Random();
36    Directory dir1;
37    Directory dir2;
38   
39    IndexReader ir1;
40    IndexReader ir2;
41   
42    String failure=null;
43   
44   
 
45    class Thr extends Thread {
46    final int iter;
47    final Random rand;
48    // pass in random in case we want to make things reproducable
 
49  10000 toggle public Thr(int iter, Random rand, int level) {
50  10000 this.iter = iter;
51  10000 this.rand = rand;
52    }
53   
 
54  10000 toggle public void run() {
55  10000 try {
56  110000 for (int i=0; i<iter; i++) {
57    /*** future
58    // pick a random index reader... a shared one, or create your own
59    IndexReader ir;
60    ***/
61   
62  99999 switch(rand.nextInt(1)) {
63  99994 case 0: loadDoc(ir1); break;
64    }
65   
66    }
67    } catch (Throwable th) {
68  0 failure=th.toString();
69  0 TestCase.fail(failure);
70    }
71    }
72   
73   
 
74  99999 toggle void loadDoc(IndexReader ir) throws IOException {
75    // beware of deleted docs in the future
76  99999 Document doc = ir.document(rand.nextInt(ir.maxDoc()),
77    new FieldSelector() {
 
78  259706 toggle public FieldSelectorResult accept(String fieldName) {
79  259706 switch(rand.nextInt(2)) {
80  129976 case 0: return FieldSelectorResult.LAZY_LOAD;
81  129731 case 1: return FieldSelectorResult.LOAD;
82    // TODO: add other options
83  0 default: return FieldSelectorResult.LOAD;
84    }
85    }
86    }
87    );
88   
89  99996 List fields = doc.getFields();
90  359650 for (int i=0; i<fields.size(); i++) {
91  259688 Fieldable f = (Fieldable)fields.get(i);
92  259646 validateField(f);
93    }
94   
95    }
96   
97    }
98   
99   
 
100  259696 toggle void validateField(Fieldable f) {
101  259696 String val = f.stringValue();
102  259688 if (!val.startsWith("^") || !val.endsWith("$")) {
103  0 throw new RuntimeException("Invalid field:" + f.toString() + " val=" +val);
104    }
105    }
106   
107    String[] words = "now is the time for all good men to come to the aid of their country".split(" ");
108   
 
109  1 toggle void buildDir(Directory dir, int nDocs, int maxFields, int maxFieldLen) throws IOException {
110  1 IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
111  1 iw.setMaxBufferedDocs(10);
112  16 for (int j=0; j<nDocs; j++) {
113  15 Document d = new Document();
114  15 int nFields = r.nextInt(maxFields);
115  54 for (int i=0; i<nFields; i++) {
116  39 int flen = r.nextInt(maxFieldLen);
117  39 StringBuffer sb = new StringBuffer("^ ");
118  8957 while (sb.length() < flen) sb.append(" " + words[r.nextInt(words.length)]);
119  39 sb.append(" $");
120  39 Field.Store store = Field.Store.YES; // make random later
121  39 Field.Index index = Field.Index.TOKENIZED; // make random later
122  39 d.add(new Field("f"+i, sb.toString(), store, index));
123    }
124  15 iw.addDocument(d);
125    }
126  1 iw.close();
127    }
128   
129   
 
130  100 toggle void doTest(int iter, int nThreads) throws Exception {
131  100 Thr[] tarr = new Thr[nThreads];
132  10100 for (int i=0; i<nThreads; i++) {
133  10000 tarr[i] = new Thr(iter, new Random(), 1);
134  10000 tarr[i].start();
135    }
136  10100 for (int i=0; i<nThreads; i++) {
137  10000 tarr[i].join();
138    }
139  100 if (failure!=null) {
140  0 TestCase.fail(failure);
141    }
142    }
143   
 
144  1 toggle public void testLazyLoadThreadSafety() throws Exception{
145  1 dir1 = new RAMDirectory();
146    // test w/ field sizes bigger than the buffer of an index input
147  1 buildDir(dir1, 15, 5, 2000);
148   
149    // do many small tests so the thread locals go away inbetween
150  101 for (int i=0; i<100; i++) {
151  100 ir1 = IndexReader.open(dir1);
152  100 doTest(10,100);
153    }
154    }
155   
156    }