FileDocCategorySizeDatePackage
TestThreadSafe.javaAPI DocApache Lucene 2.1.04807Wed Feb 14 10:46:36 GMT 2007org.apache.lucene.search

TestThreadSafe

public class TestThreadSafe extends TestCase
author
yonik
version
$Id: TestThreadSafe.java 472959 2006-11-09 16:21:50Z yonik $

Fields Summary
Random
r
Directory
dir1
Directory
dir2
IndexReader
ir1
IndexReader
ir2
String
failure
String[]
words
Constructors Summary
Methods Summary
voidbuildDir(org.apache.lucene.store.Directory dir, int nDocs, int maxFields, int maxFieldLen)


             
    IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
    iw.setMaxBufferedDocs(10);
    for (int j=0; j<nDocs; j++) {
      Document d = new Document();
      int nFields = r.nextInt(maxFields);
      for (int i=0; i<nFields; i++) {
        int flen = r.nextInt(maxFieldLen);
        StringBuffer sb = new StringBuffer("^ ");
        while (sb.length() < flen) sb.append(" " + words[r.nextInt(words.length)]);
        sb.append(" $");
        Field.Store store = Field.Store.YES;  // make random later
        Field.Index index = Field.Index.TOKENIZED;  // make random later
        d.add(new Field("f"+i, sb.toString(), store, index));
      }
      iw.addDocument(d);
    }
    iw.close();
  
voiddoTest(int iter, int nThreads)

    Thr[] tarr = new Thr[nThreads];
    for (int i=0; i<nThreads; i++) {
      tarr[i] = new Thr(iter, new Random(), 1);
      tarr[i].start();
    }
    for (int i=0; i<nThreads; i++) {
      tarr[i].join();
    }
    if (failure!=null) {
      TestCase.fail(failure);
    }
  
public voidtestLazyLoadThreadSafety()

    dir1 = new RAMDirectory();
    // test w/ field sizes bigger than the buffer of an index input
    buildDir(dir1, 15, 5, 2000);

    // do many small tests so the thread locals go away inbetween
    for (int i=0; i<100; i++) {
      ir1 = IndexReader.open(dir1);
      doTest(10,100);
    }
  
voidvalidateField(org.apache.lucene.document.Fieldable f)

    String val = f.stringValue();
    if (!val.startsWith("^") || !val.endsWith("$")) {
      throw new RuntimeException("Invalid field:" + f.toString() + " val=" +val);
    }