diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java index 5d3ab90dd0b..fa229d8ed17 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriter.java @@ -2504,7 +2504,10 @@ public class TestIndexWriter extends LuceneTestCase { public void testCorruptFirstCommit() throws Exception { for(int i=0;i<6;i++) { BaseDirectoryWrapper dir = newDirectory(); + + // Create a corrupt first commit: dir.createOutput("segments_0", IOContext.DEFAULT).close(); + IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())); int mode = i/2; if (mode == 0) { @@ -2544,6 +2547,16 @@ public class TestIndexWriter extends LuceneTestCase { if (mode != 0) { dir.setCheckIndexOnClose(false); } + + if (dir instanceof MockDirectoryWrapper) { + MockDirectoryWrapper mdw = (MockDirectoryWrapper) dir; + if (Arrays.equals(new String[] {"segments_0"}, dir.listAll()) && + mdw.didTryToDelete("segments_0")) { + // This means virus checker blocked IW deleting the corrupt first commit + dir.setCheckIndexOnClose(false); + } + } + dir.close(); } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java index fb208b35c61..a4bd94dd0f4 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java +++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java @@ -494,6 +494,12 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper { in.deleteFile(name); } + /** Returns true if {@link #deleteFile} was called with this + * fileName, but the virus checker prevented the deletion. */ + public boolean didTryToDelete(String fileName) { + return triedToDelete.contains(fileName); + } + public synchronized Set getOpenDeletedFiles() { return new HashSet<>(openFilesDeleted); }