Index: lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java =================================================================== --- lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java (revision 988600) +++ lucene/src/test/org/apache/lucene/store/MockDirectoryWrapper.java (working copy) @@ -17,9 +17,12 @@ * limitations under the License. */ +import java.io.Closeable; import java.io.IOException; import java.io.FileNotFoundException; import java.util.Collection; +import java.util.Collections; +import java.util.IdentityHashMap; import java.util.Iterator; import java.util.Random; import java.util.Map; @@ -49,6 +52,11 @@ private Set createdFiles; volatile boolean crashed; + // use this for tracking files for crash. + // additionally: provides debugging information in case you leave one open + Map files + = Collections.synchronizedMap(new IdentityHashMap()); + // NOTE: we cannot initialize the Map here due to the // order in which our constructor actually does this // member initialization vs when it calls super. It seems @@ -123,53 +131,36 @@ openFiles = new HashMap(); Iterator it = unSyncedFiles.iterator(); unSyncedFiles = new HashSet(); + // first force-close all files, so we can corrupt on windows etc. + try { + for (Closeable f : files.keySet()) + f.close(); + } catch (Exception ignored) {} + int count = 0; - if (!(delegate instanceof RAMDirectory)) { - while(it.hasNext()) { - String name = it.next(); - if (count % 3 == 0) { - deleteFile(name, true); - } else if (count % 3 == 1) { - // Zero out file entirely - long length = fileLength(name); - byte[] zeroes = new byte[256]; - long upto = 0; - IndexOutput out = delegate.createOutput(name); - while(upto < length) { - final int limit = (int) Math.min(length-upto, zeroes.length); - out.writeBytes(zeroes, 0, limit); - upto += limit; - } - out.close(); - } else if (count % 3 == 2) { - // Truncate the file: - IndexOutput out = delegate.createOutput(name); - out.setLength(fileLength(name)/2); - out.close(); + while(it.hasNext()) { + String name = it.next(); + if (count % 3 == 0) { + deleteFile(name, true); + } else if (count % 3 == 1) { + // Zero out file entirely + long length = fileLength(name); + byte[] zeroes = new byte[256]; + long upto = 0; + IndexOutput out = delegate.createOutput(name); + while(upto < length) { + final int limit = (int) Math.min(length-upto, zeroes.length); + out.writeBytes(zeroes, 0, limit); + upto += limit; } - count++; + out.close(); + } else if (count % 3 == 2) { + // Truncate the file: + IndexOutput out = delegate.createOutput(name); + out.setLength(fileLength(name)/2); + out.close(); } - } else { - - RAMDirectory ramDir = (RAMDirectory) delegate; - while(it.hasNext()) { - String name = it.next(); - RAMFile file = ramDir.fileMap.get(name); - if (count % 3 == 0) { - deleteFile(name, true); - } else if (count % 3 == 1) { - // Zero out file entirely - final int numBuffers = file.numBuffers(); - for(int i=0;i(); } if (noDeleteOpenFile && openFiles.size() > 0) { + // print the first one as its very verbose otherwise + Exception cause = null; + Iterator stacktraces = files.values().iterator(); + if (stacktraces.hasNext()) + cause = stacktraces.next(); // RuntimeException instead of IOException because // super() does not throw IOException currently: - throw new RuntimeException("MockRAMDirectory: cannot close: there are still open files: " + openFiles); + throw new RuntimeException("MockRAMDirectory: cannot close: there are still open files: " + openFiles, cause); } open = false; delegate.close(); Index: lucene/src/test/org/apache/lucene/store/MockIndexInputWrapper.java =================================================================== --- lucene/src/test/org/apache/lucene/store/MockIndexInputWrapper.java (revision 988600) +++ lucene/src/test/org/apache/lucene/store/MockIndexInputWrapper.java (working copy) @@ -57,6 +57,7 @@ dir.openFiles.put(name, v); } } + dir.files.remove(this); } } } Index: lucene/src/test/org/apache/lucene/store/MockIndexOutputWrapper.java =================================================================== --- lucene/src/test/org/apache/lucene/store/MockIndexOutputWrapper.java (revision 988600) +++ lucene/src/test/org/apache/lucene/store/MockIndexOutputWrapper.java (working copy) @@ -53,6 +53,7 @@ dir.maxUsedSize = size; } } + dir.files.remove(this); } @Override