Buggy code:
public class TestPerSegmentDeletes extends LuceneTestCase { public void testDeletes1 ( ) throws Exception { Directory dir = new org . apache . lucene . store . MockDirectoryWrapper ( new Random ( random ( ) . nextLong ( ) ) , new RAMDirectory ( ) ) ; IndexWriterConfig iwc = new IndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ( ) ) ) ; iwc . setMergeScheduler ( new SerialMergeScheduler ( ) ) ; iwc . setMaxBufferedDocs ( 5000 ) ; iwc . setRAMBufferSizeMB ( 100 ) ; TestPerSegmentDeletes . RangeMergePolicy fsmp = new TestPerSegmentDeletes . RangeMergePolicy ( false ) ; iwc . setMergePolicy ( fsmp ) ; IndexWriter writer = new IndexWriter ( dir , iwc ) ; for ( int x = 0 ; x < 5 ; x ++ ) { writer . addDocument ( DocHelper . createDocument ( x , "1" , 2 ) ) ; } writer . commit ( ) ; assertEquals ( 1 , writer . segmentInfos . size ( ) ) ; for ( int x = 5 ; x < 10 ; x ++ ) { writer . addDocument ( DocHelper . createDocument ( x , "2" , 2 ) ) ; } writer . commit ( ) ; assertEquals ( 2 , writer . segmentInfos . size ( ) ) ; for ( int x = 10 ; x < 15 ; x ++ ) { writer . addDocument ( DocHelper . createDocument ( x , "3" , 2 ) ) ; } writer . deleteDocuments ( new Term ( "id" , "1" ) ) ; writer . deleteDocuments ( new Term ( "id" , "11" ) ) ; writer . flush ( false , false ) ; assertTrue ( writer . bufferedUpdatesStream . any ( ) ) ; IndexReader r1 = writer . getReader ( ) ; assertFalse ( writer . bufferedUpdatesStream . any ( ) ) ; r1 . close ( ) ; writer . deleteDocuments ( new Term ( "id" , "2" ) ) ; writer . flush ( false , false ) ; fsmp = ( ( TestPerSegmentDeletes . RangeMergePolicy ) ( writer . getConfig ( ) . getMergePolicy ( ) ) ) ; fsmp . doMerge = true ; fsmp . start = 0 ; fsmp . length = 2 ; writer . maybeMerge ( ) ; assertEquals ( 2 , writer . segmentInfos . size ( ) ) ; IndexReader r2 = writer . getReader ( ) ; int [ ] id2docs = toDocsArray ( new Term ( "id" , "2" ) , null , r2 ) ; assertTrue ( ( id2docs == null ) ) ; r2 . close ( ) ; <START_BUG> writer . close ( ) ; <END_BUG> dir . close ( ) ; } void part2 ( IndexWriter writer , TestPerSegmentDeletes . RangeMergePolicy fsmp ) throws Exception { } boolean segThere ( SegmentCommitInfo info , SegmentInfos infos ) { } public static void printDelDocs ( Bits bits ) { } public int [ ] toDocsArray ( Term term , Bits bits , IndexReader reader ) throws IOException { } public static int [ ] toArray ( DocsEnum docsEnum ) throws IOException { } public class RangeMergePolicy extends MergePolicy { boolean doMerge = false ; int start ; int length ; private final boolean useCompoundFile ; private RangeMergePolicy ( boolean useCompoundFile ) { } @ Override public void close ( ) { } @ Override public MergeSpecification findMerges ( MergeTrigger mergeTrigger , SegmentInfos segmentInfos ) throws IOException { } @ Override public MergeSpecification findForcedMerges ( SegmentInfos segmentInfos , int maxSegmentCount , Map < SegmentCommitInfo , Boolean > segmentsToMerge ) throws IOException { } @ Override public MergeSpecification findForcedDeletesMerges ( SegmentInfos segmentInfos ) throws IOException { } @ Override public boolean useCompoundFile ( SegmentInfos segments , SegmentCommitInfo newSegment ) { } } }

Fix:
writer . shutdown ( ) ;