Index: lucene/backwards/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java =================================================================== --- lucene/backwards/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (revision 1127471) +++ lucene/backwards/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (working copy) @@ -43,30 +43,53 @@ this.delegate = delegate; } + private void safeMaybeThrowDeterministicException() throws IOException { + try { + dir.maybeThrowDeterministicException(); + } catch (IOException e) { + closeInternal(); + throw e; + } + } + + private void safeMaybeThrowIOException() throws IOException { + try { + dir.maybeThrowIOException(); + } catch (IOException e) { + closeInternal(); + throw e; + } + } + + // Closes w/o checking dir.maybeThrow... + private void closeInternal() throws IOException { + delegate.close(); + if (dir.trackDiskUsage) { + // Now compute actual disk usage & track the maxUsedSize + // in the MockDirectoryWrapper: + long size = dir.getRecomputedActualSizeInBytes(); + if (size > dir.maxUsedSize) { + dir.maxUsedSize = size; + } + } + synchronized(dir) { + dir.openFileHandles.remove(this); + dir.openFilesForWrite.remove(name); + } + } + @Override public void close() throws IOException { try { dir.maybeThrowDeterministicException(); } finally { - delegate.close(); - if (dir.trackDiskUsage) { - // Now compute actual disk usage & track the maxUsedSize - // in the MockDirectoryWrapper: - long size = dir.getRecomputedActualSizeInBytes(); - if (size > dir.maxUsedSize) { - dir.maxUsedSize = size; - } - } - synchronized(dir) { - dir.openFileHandles.remove(this); - dir.openFilesForWrite.remove(name); - } + closeInternal(); } } @Override public void flush() throws IOException { - dir.maybeThrowDeterministicException(); + safeMaybeThrowDeterministicException(); delegate.flush(); } @@ -123,13 +146,13 @@ } } - dir.maybeThrowDeterministicException(); + safeMaybeThrowDeterministicException(); if (first) { // Maybe throw random exception; only do this on first // write to a new file: first = false; - dir.maybeThrowIOException(); + safeMaybeThrowIOException(); } } @@ -157,6 +180,6 @@ public void copyBytes(IndexInput input, long numBytes) throws IOException { delegate.copyBytes(input, numBytes); // TODO: we may need to check disk full here as well - dir.maybeThrowDeterministicException(); + safeMaybeThrowDeterministicException(); } } Index: lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java =================================================================== --- lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (revision 1127471) +++ lucene/src/test-framework/org/apache/lucene/store/MockIndexOutputWrapper.java (working copy) @@ -43,30 +43,53 @@ this.delegate = delegate; } + private void safeMaybeThrowDeterministicException() throws IOException { + try { + dir.maybeThrowDeterministicException(); + } catch (IOException e) { + closeInternal(); + throw e; + } + } + + private void safeMaybeThrowIOException() throws IOException { + try { + dir.maybeThrowIOException(); + } catch (IOException e) { + closeInternal(); + throw e; + } + } + + // Closes w/o checking dir.maybeThrow... + private void closeInternal() throws IOException { + delegate.close(); + if (dir.trackDiskUsage) { + // Now compute actual disk usage & track the maxUsedSize + // in the MockDirectoryWrapper: + long size = dir.getRecomputedActualSizeInBytes(); + if (size > dir.maxUsedSize) { + dir.maxUsedSize = size; + } + } + synchronized(dir) { + dir.openFileHandles.remove(this); + dir.openFilesForWrite.remove(name); + } + } + @Override public void close() throws IOException { try { dir.maybeThrowDeterministicException(); } finally { - delegate.close(); - if (dir.trackDiskUsage) { - // Now compute actual disk usage & track the maxUsedSize - // in the MockDirectoryWrapper: - long size = dir.getRecomputedActualSizeInBytes(); - if (size > dir.maxUsedSize) { - dir.maxUsedSize = size; - } - } - synchronized(dir) { - dir.openFileHandles.remove(this); - dir.openFilesForWrite.remove(name); - } + closeInternal(); } } @Override public void flush() throws IOException { - dir.maybeThrowDeterministicException(); + safeMaybeThrowDeterministicException(); delegate.flush(); } @@ -123,13 +146,13 @@ } } - dir.maybeThrowDeterministicException(); + safeMaybeThrowDeterministicException(); if (first) { // Maybe throw random exception; only do this on first // write to a new file: first = false; - dir.maybeThrowIOException(); + safeMaybeThrowIOException(); } } @@ -157,6 +180,6 @@ public void copyBytes(IndexInput input, long numBytes) throws IOException { delegate.copyBytes(input, numBytes); // TODO: we may need to check disk full here as well - dir.maybeThrowDeterministicException(); + safeMaybeThrowDeterministicException(); } }