Apache Solr open-source search software
LUCENE-5916: Static scope test components should be consistent between tests (and test iterations)
Erick Erickson89658f13ccb1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex c07cf8c97c1..37dd9b8855c 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -127,6 +127,10 @@ Bug FixesMockDirectoryWrapper to have it simulate a virus checker holding afile 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* LUCENE-5909: Smoke tester now has better command line parsing anddiff --git a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.javaindex 9ac0e863e4c..59c1835762b 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java@@ -20,6 +20,7 @@ package org.apache.lucene.index;import java.io.File;import java.io.IOException;import java.util.*;+import java.util.concurrent.atomic.AtomicBoolean;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.Document;@@ -58,9 +59,8 @@ public class TestFieldsReader extends LuceneTestCase {IndexWriter writer = new IndexWriter(dir, conf);writer.addDocument(testDoc);writer.close();- FaultyIndexInput.doFail = false;}-+@AfterClasspublic static void afterClass() throws Exception {dir.close();@@ -105,77 +105,97 @@ public class TestFieldsReader extends LuceneTestCase {}- public static class FaultyFSDirectory extends BaseDirectory {-+ public class FaultyFSDirectory extends BaseDirectory {Directory fsDir;-+ AtomicBoolean doFail = new AtomicBoolean();+public FaultyFSDirectory(File dir) {fsDir = newFSDirectory(dir);lockFactory = fsDir.getLockFactory();}+@Overridepublic IndexInput openInput(String name, IOContext context) throws IOException {- return new FaultyIndexInput(fsDir.openInput(name, context));+ return new FaultyIndexInput(doFail, fsDir.openInput(name, context));}+@Overridepublic String[] listAll() throws IOException {return fsDir.listAll();}+@Overridepublic void deleteFile(String name) throws IOException {fsDir.deleteFile(name);}+@Overridepublic long fileLength(String name) throws IOException {return fsDir.fileLength(name);}+@Overridepublic IndexOutput createOutput(String name, IOContext context) throws IOException {return fsDir.createOutput(name, context);}+@Overridepublic void sync(Collection<String> names) throws IOException {fsDir.sync(names);}+@Overridepublic void close() throws IOException {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;- static boolean doFail;int count;- private FaultyIndexInput(IndexInput delegate) {++ private FaultyIndexInput(AtomicBoolean doFail, IndexInput delegate) {super("FaultyIndexInput(" + delegate + ")", BufferedIndexInput.BUFFER_SIZE);this.delegate = delegate;+ this.doFail = doFail;}+private void simOutage() throws IOException {- if (doFail && count++ % 2 == 1) {+ if (doFail.get() && count++ % 2 == 1) {throw new IOException("Simulated network outage");}}+@Overridepublic void readInternal(byte[] b, int offset, int length) throws IOException {simOutage();delegate.seek(getFilePointer());delegate.readBytes(b, offset, length);}+@Overridepublic void seekInternal(long pos) throws IOException {}+@Overridepublic long length() {return delegate.length();}+@Overridepublic void close() throws IOException {delegate.close();}+@Overridepublic FaultyIndexInput clone() {- FaultyIndexInput i = new FaultyIndexInput(delegate.clone());+ FaultyIndexInput i = new FaultyIndexInput(doFail, delegate.clone());// seek the clone to our current positiontry {i.seek(getFilePointer());@@ -188,7 +208,7 @@ public class TestFieldsReader extends LuceneTestCase {@Overridepublic IndexInput slice(String sliceDescription, long offset, long length) throws IOException {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");try {- Directory dir = new FaultyFSDirectory(indexDir);+ FaultyFSDirectory dir = new FaultyFSDirectory(indexDir);IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE);IndexWriter writer = new IndexWriter(dir, iwc);@@ -207,8 +227,7 @@ public class TestFieldsReader extends LuceneTestCase {writer.close();IndexReader reader = DirectoryReader.open(dir);-- FaultyIndexInput.doFail = true;+ dir.startFailing();boolean exc = false;