LUCENE-5916: Static scope test components should be consistent between tests (and test iterations)

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1621889 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erick Erickson 2014-09-01 21:22:47 +00:00
parent a8ffbd429f
commit 89658f13cc
2 changed files with 38 additions and 15 deletions

View File

@ -127,6 +127,10 @@ Bug Fixes
MockDirectoryWrapper to have it simulate a virus checker holding a MockDirectoryWrapper to have it simulate a virus checker holding a
file open and preventing deletion (Robert Muir, Mike McCandless) file open and preventing deletion (Robert Muir, Mike McCandless)
* LUCENE-5916: Static scope test components should be consistent between
tests (and test iterations). Fix for FaultyIndexInput in particular.
(Dawid Weiss)
Build Build
* LUCENE-5909: Smoke tester now has better command line parsing and * LUCENE-5909: Smoke tester now has better command line parsing and

View File

@ -20,6 +20,7 @@ package org.apache.lucene.index;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.lucene.analysis.MockAnalyzer; import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document; import org.apache.lucene.document.Document;
@ -58,9 +59,8 @@ public class TestFieldsReader extends LuceneTestCase {
IndexWriter writer = new IndexWriter(dir, conf); IndexWriter writer = new IndexWriter(dir, conf);
writer.addDocument(testDoc); writer.addDocument(testDoc);
writer.close(); writer.close();
FaultyIndexInput.doFail = false;
} }
@AfterClass @AfterClass
public static void afterClass() throws Exception { public static void afterClass() throws Exception {
dir.close(); dir.close();
@ -105,77 +105,97 @@ public class TestFieldsReader extends LuceneTestCase {
} }
public static class FaultyFSDirectory extends BaseDirectory { public class FaultyFSDirectory extends BaseDirectory {
Directory fsDir; Directory fsDir;
AtomicBoolean doFail = new AtomicBoolean();
public FaultyFSDirectory(File dir) { public FaultyFSDirectory(File dir) {
fsDir = newFSDirectory(dir); fsDir = newFSDirectory(dir);
lockFactory = fsDir.getLockFactory(); lockFactory = fsDir.getLockFactory();
} }
@Override @Override
public IndexInput openInput(String name, IOContext context) throws IOException { public IndexInput openInput(String name, IOContext context) throws IOException {
return new FaultyIndexInput(fsDir.openInput(name, context)); return new FaultyIndexInput(doFail, fsDir.openInput(name, context));
} }
@Override @Override
public String[] listAll() throws IOException { public String[] listAll() throws IOException {
return fsDir.listAll(); return fsDir.listAll();
} }
@Override @Override
public void deleteFile(String name) throws IOException { public void deleteFile(String name) throws IOException {
fsDir.deleteFile(name); fsDir.deleteFile(name);
} }
@Override @Override
public long fileLength(String name) throws IOException { public long fileLength(String name) throws IOException {
return fsDir.fileLength(name); return fsDir.fileLength(name);
} }
@Override @Override
public IndexOutput createOutput(String name, IOContext context) throws IOException { public IndexOutput createOutput(String name, IOContext context) throws IOException {
return fsDir.createOutput(name, context); return fsDir.createOutput(name, context);
} }
@Override @Override
public void sync(Collection<String> names) throws IOException { public void sync(Collection<String> names) throws IOException {
fsDir.sync(names); fsDir.sync(names);
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
fsDir.close(); fsDir.close();
} }
public void startFailing() {
doFail.set(true);
}
} }
private static class FaultyIndexInput extends BufferedIndexInput { private class FaultyIndexInput extends BufferedIndexInput {
private final AtomicBoolean doFail;
IndexInput delegate; IndexInput delegate;
static boolean doFail;
int count; int count;
private FaultyIndexInput(IndexInput delegate) {
private FaultyIndexInput(AtomicBoolean doFail, IndexInput delegate) {
super("FaultyIndexInput(" + delegate + ")", BufferedIndexInput.BUFFER_SIZE); super("FaultyIndexInput(" + delegate + ")", BufferedIndexInput.BUFFER_SIZE);
this.delegate = delegate; this.delegate = delegate;
this.doFail = doFail;
} }
private void simOutage() throws IOException { private void simOutage() throws IOException {
if (doFail && count++ % 2 == 1) { if (doFail.get() && count++ % 2 == 1) {
throw new IOException("Simulated network outage"); throw new IOException("Simulated network outage");
} }
} }
@Override @Override
public void readInternal(byte[] b, int offset, int length) throws IOException { public void readInternal(byte[] b, int offset, int length) throws IOException {
simOutage(); simOutage();
delegate.seek(getFilePointer()); delegate.seek(getFilePointer());
delegate.readBytes(b, offset, length); delegate.readBytes(b, offset, length);
} }
@Override @Override
public void seekInternal(long pos) throws IOException { public void seekInternal(long pos) throws IOException {
} }
@Override @Override
public long length() { public long length() {
return delegate.length(); return delegate.length();
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
delegate.close(); delegate.close();
} }
@Override @Override
public FaultyIndexInput clone() { public FaultyIndexInput clone() {
FaultyIndexInput i = new FaultyIndexInput(delegate.clone()); FaultyIndexInput i = new FaultyIndexInput(doFail, delegate.clone());
// seek the clone to our current position // seek the clone to our current position
try { try {
i.seek(getFilePointer()); i.seek(getFilePointer());
@ -188,7 +208,7 @@ public class TestFieldsReader extends LuceneTestCase {
@Override @Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException { public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
IndexInput slice = delegate.slice(sliceDescription, offset, length); IndexInput slice = delegate.slice(sliceDescription, offset, length);
return new FaultyIndexInput(slice); return new FaultyIndexInput(doFail, slice);
} }
} }
@ -197,7 +217,7 @@ public class TestFieldsReader extends LuceneTestCase {
File indexDir = createTempDir("testfieldswriterexceptions"); File indexDir = createTempDir("testfieldswriterexceptions");
try { try {
Directory dir = new FaultyFSDirectory(indexDir); FaultyFSDirectory dir = new FaultyFSDirectory(indexDir);
IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())) IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random()))
.setOpenMode(OpenMode.CREATE); .setOpenMode(OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, iwc); IndexWriter writer = new IndexWriter(dir, iwc);
@ -207,8 +227,7 @@ public class TestFieldsReader extends LuceneTestCase {
writer.close(); writer.close();
IndexReader reader = DirectoryReader.open(dir); IndexReader reader = DirectoryReader.open(dir);
dir.startFailing();
FaultyIndexInput.doFail = true;
boolean exc = false; boolean exc = false;