Skip to content

Apache Lucene open-source search software

LUCENE-6120: Fix MockDirectoryWrapper close() handling

Robert Muir7d4ba636b0bd
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c9eb8830516..7100e7fa2b1 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -419,6 +419,9 @@ Tests
* LUCENE-5968: Improve error message when 'ant beast' is run on top-level
modules. (Ramkumar Aiyengar, Uwe Schindler)
+* LUCENE-6120: Fix MockDirectoryWrapper's close() handling.
+ (Mike McCandless, Robert Muir)
+
Build
* LUCENE-5909: Smoke tester now has better command line parsing and
diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.java
index 64fbfdcc90b..82133e9f4ff 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexInputWrapper.java
@@ -1,5 +1,6 @@
package org.apache.lucene.store;
+import java.io.Closeable;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
@@ -43,21 +44,20 @@ public class MockIndexInputWrapper extends IndexInput {
@Override
public void close() throws IOException {
+ // TODO turn on the following to look for leaks closing inputs,
+ // after fixing TestTransactions
+ // dir.maybeThrowDeterministicException();
if (closed) {
delegate.close(); // don't mask double-close bugs
return;
}
closed = true;
- try {
- // turn on the following to look for leaks closing inputs,
- // after fixing TestTransactions
- // dir.maybeThrowDeterministicException();
- } finally {
- delegate.close();
+ try (Closeable delegate = this.delegate) {
// Pending resolution on LUCENE-686 we may want to
// remove the conditional check so we also track that
// all clones get closed:
+ assert delegate != null;
if (!isClone) {
dir.removeIndexInput(this, name);
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java
index 870c154777c..ad646bf46a4 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockIndexOutputWrapper.java
@@ -17,6 +17,7 @@ package org.apache.lucene.store;
* limitations under the License.
*/
+import java.io.Closeable;
import java.io.IOException;
import org.apache.lucene.util.LuceneTestCase;
@@ -98,10 +99,11 @@ public class MockIndexOutputWrapper extends IndexOutput {
}
closed = true;
- try {
+ try (Closeable delegate = this.delegate) {
+ assert delegate != null;
dir.maybeThrowDeterministicException();
} finally {
- delegate.close();
+ dir.removeIndexOutput(this, name);
if (dir.trackDiskUsage) {
// Now compute actual disk usage & track the maxUsedSize
// in the MockDirectoryWrapper:
@@ -110,7 +112,6 @@ public class MockIndexOutputWrapper extends IndexOutput {
dir.maxUsedSize = size;
}
}
- dir.removeIndexOutput(this, name);
}
}