Search in sources :

Example 11 with FakeIOException

use of org.apache.lucene.store.MockDirectoryWrapper.FakeIOException in project lucene-solr by apache.

the class BaseFieldInfoFormatTestCase method testExceptionOnCreateOutput.

/** 
   * Test field infos write that hits exception immediately on open.
   * make sure we get our exception back, no file handle leaks, etc. 
   */
public void testExceptionOnCreateOutput() throws Exception {
    Failure fail = new Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
                if (doFail && "createOutput".equals(e.getMethodName())) {
                    throw new FakeIOException();
                }
            }
        }
    };
    MockDirectoryWrapper dir = newMockDirectory();
    dir.failOn(fail);
    Codec codec = getCodec();
    SegmentInfo segmentInfo = newSegmentInfo(dir, "_123");
    FieldInfos.Builder builder = new FieldInfos.Builder();
    FieldInfo fi = builder.getOrAdd("field");
    fi.setIndexOptions(TextField.TYPE_STORED.indexOptions());
    addAttributes(fi);
    FieldInfos infos = builder.finish();
    fail.setDoFail();
    expectThrows(FakeIOException.class, () -> {
        codec.fieldInfosFormat().write(dir, segmentInfo, "", infos, IOContext.DEFAULT);
    });
    fail.clearDoFail();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) Codec(org.apache.lucene.codecs.Codec) Failure(org.apache.lucene.store.MockDirectoryWrapper.Failure)

Example 12 with FakeIOException

use of org.apache.lucene.store.MockDirectoryWrapper.FakeIOException in project lucene-solr by apache.

the class BaseFieldInfoFormatTestCase method testExceptionOnOpenInput.

/** 
   * Test field infos read that hits exception immediately on open.
   * make sure we get our exception back, no file handle leaks, etc. 
   */
public void testExceptionOnOpenInput() throws Exception {
    Failure fail = new Failure() {

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
                if (doFail && "openInput".equals(e.getMethodName())) {
                    throw new FakeIOException();
                }
            }
        }
    };
    MockDirectoryWrapper dir = newMockDirectory();
    dir.failOn(fail);
    Codec codec = getCodec();
    SegmentInfo segmentInfo = newSegmentInfo(dir, "_123");
    FieldInfos.Builder builder = new FieldInfos.Builder();
    FieldInfo fi = builder.getOrAdd("field");
    fi.setIndexOptions(TextField.TYPE_STORED.indexOptions());
    addAttributes(fi);
    FieldInfos infos = builder.finish();
    codec.fieldInfosFormat().write(dir, segmentInfo, "", infos, IOContext.DEFAULT);
    fail.setDoFail();
    expectThrows(FakeIOException.class, () -> {
        codec.fieldInfosFormat().read(dir, segmentInfo, "", IOContext.DEFAULT);
    });
    fail.clearDoFail();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) Codec(org.apache.lucene.codecs.Codec) Failure(org.apache.lucene.store.MockDirectoryWrapper.Failure)

Example 13 with FakeIOException

use of org.apache.lucene.store.MockDirectoryWrapper.FakeIOException in project lucene-solr by apache.

the class TestDirectoryReaderReopen method testOverDecRefDuringReopen.

public void testOverDecRefDuringReopen() throws Exception {
    MockDirectoryWrapper dir = newMockDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    iwc.setCodec(TestUtil.getDefaultCodec());
    IndexWriter w = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(newStringField("id", "id", Field.Store.NO));
    w.addDocument(doc);
    doc = new Document();
    doc.add(newStringField("id", "id2", Field.Store.NO));
    w.addDocument(doc);
    w.commit();
    // Open reader w/ one segment w/ 2 docs:
    DirectoryReader r = DirectoryReader.open(dir);
    // Delete 1 doc from the segment:
    //System.out.println("TEST: now delete");
    w.deleteDocuments(new Term("id", "id"));
    //System.out.println("TEST: now commit");
    w.commit();
    // Fail when reopen tries to open the live docs file:
    dir.failOn(new MockDirectoryWrapper.Failure() {

        boolean failed;

        @Override
        public void eval(MockDirectoryWrapper dir) throws IOException {
            if (failed) {
                return;
            }
            //System.out.println("failOn: ");
            //new Throwable().printStackTrace(System.out);
            StackTraceElement[] trace = new Exception().getStackTrace();
            for (int i = 0; i < trace.length; i++) {
                if ("readLiveDocs".equals(trace[i].getMethodName())) {
                    if (VERBOSE) {
                        System.out.println("TEST: now fail; exc:");
                        new Throwable().printStackTrace(System.out);
                    }
                    failed = true;
                    throw new FakeIOException();
                }
            }
        }
    });
    // Now reopen:
    //System.out.println("TEST: now reopen");
    expectThrows(FakeIOException.class, () -> {
        DirectoryReader.openIfChanged(r);
    });
    IndexSearcher s = newSearcher(r);
    assertEquals(1, s.search(new TermQuery(new Term("id", "id")), 1).totalHits);
    r.close();
    w.close();
    dir.close();
}
Also used : MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) IOException(java.io.IOException) Document(org.apache.lucene.document.Document) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) IOException(java.io.IOException) FakeIOException(org.apache.lucene.store.MockDirectoryWrapper.FakeIOException) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer)

Aggregations

MockDirectoryWrapper (org.apache.lucene.store.MockDirectoryWrapper)13 FakeIOException (org.apache.lucene.store.MockDirectoryWrapper.FakeIOException)13 Codec (org.apache.lucene.codecs.Codec)8 Failure (org.apache.lucene.store.MockDirectoryWrapper.Failure)8 IOException (java.io.IOException)5 Document (org.apache.lucene.document.Document)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)3 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)3 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 StringField (org.apache.lucene.document.StringField)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 TermQuery (org.apache.lucene.search.TermQuery)1 Bits (org.apache.lucene.util.Bits)1 BytesRef (org.apache.lucene.util.BytesRef)1 ThreadInterruptedException (org.apache.lucene.util.ThreadInterruptedException)1 Test (org.junit.Test)1