| 1 |
|
package org.apache.lucene.search; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 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 |
| 32 |
|
@version |
| 33 |
|
|
|
|
|
| 92.3% |
Uncovered Elements: 4 (52) |
Complexity: 13 |
Complexity Density: 0.41 |
|
| 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 |
|
|
|
|
|
| 86.7% |
Uncovered Elements: 4 (30) |
Complexity: 10 |
Complexity Density: 0.45 |
|
| 45 |
|
class Thr extends Thread { |
| 46 |
|
final int iter; |
| 47 |
|
final Random rand; |
| 48 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 49 |
10000
|
public Thr(int iter, Random rand, int level) {... |
| 50 |
10000
|
this.iter = iter; |
| 51 |
10000
|
this.rand = rand; |
| 52 |
|
} |
| 53 |
|
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 54 |
10000
|
public void run() {... |
| 55 |
10000
|
try { |
| 56 |
110000
|
for (int i=0; i<iter; i++) { |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 74 |
99999
|
void loadDoc(IndexReader ir) throws IOException {... |
| 75 |
|
|
| 76 |
99999
|
Document doc = ir.document(rand.nextInt(ir.maxDoc()), |
| 77 |
|
new FieldSelector() { |
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 78 |
259706
|
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 |
|
|
| 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 |
|
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
| 100 |
259696
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 4 |
Complexity Density: 0.25 |
|
| 109 |
1
|
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; |
| 121 |
39
|
Field.Index index = Field.Index.TOKENIZED; |
| 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 |
|
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 130 |
100
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
1
PASS
|
|
| 144 |
1
|
public void testLazyLoadThreadSafety() throws Exception{... |
| 145 |
1
|
dir1 = new RAMDirectory(); |
| 146 |
|
|
| 147 |
1
|
buildDir(dir1, 15, 5, 2000); |
| 148 |
|
|
| 149 |
|
|
| 150 |
101
|
for (int i=0; i<100; i++) { |
| 151 |
100
|
ir1 = IndexReader.open(dir1); |
| 152 |
100
|
doTest(10,100); |
| 153 |
|
} |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
} |