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();
}
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();
}
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();
}
Aggregations