Buggy code:
public class TestThreadedOptimize extends LuceneTestCase { private static final Analyzer ANALYZER = new SimpleAnalyzer ( ) ; private static final int NUM_THREADS = 3 ; private static final int NUM_ITER = 1 ; private static final int NUM_ITER2 = 1 ; private boolean failed ; private void setFailed ( ) { } public void runTest ( Directory directory , boolean autoCommit , MergeScheduler merger ) throws Exception { IndexWriter writer = new IndexWriter ( directory , autoCommit , TestThreadedOptimize . ANALYZER , true ) ; writer . setMaxBufferedDocs ( 2 ) ; if ( merger != null ) writer . setMergeScheduler ( merger ) ; for ( int iter = 0 ; iter < ( TestThreadedOptimize . NUM_ITER ) ; iter ++ ) { final int iterFinal = iter ; writer . setMergeFactor ( 1000 ) ; for ( int i = 0 ; i < 200 ; i ++ ) { Document d = new Document ( ) ; d . add ( new org . apache . lucene . document . Field ( "id" , Integer . toString ( i ) , Store . YES , Index . NOT_ANALYZED ) ) ; d . add ( new org . apache . lucene . document . Field ( "contents" , English . intToEnglish ( i ) , Store . NO , Index . ANALYZED ) ) ; writer . addDocument ( d ) ; } writer . setMergeFactor ( 4 ) ; final int docCount = writer . docCount ( ) ; Thread [ ] threads = new Thread [ TestThreadedOptimize . NUM_THREADS ] ; for ( int i = 0 ; i < ( TestThreadedOptimize . NUM_THREADS ) ; i ++ ) { final int iFinal = i ; final IndexWriter writerFinal = writer ; threads [ i ] = new Thread ( ) { public void run ( ) { try { for ( int j = 0 ; j < ( TestThreadedOptimize . NUM_ITER2 ) ; j ++ ) { writerFinal . optimize ( false ) ; for ( int k = 0 ; k < ( 17 * ( 1 + iFinal ) ) ; k ++ ) { Document d = new Document ( ) ; d . add ( new org . apache . lucene . document . Field ( "id" , ( ( ( ( ( ( iterFinal + "_" ) + iFinal ) + "_" ) + j ) + "_" ) + k ) , Store . YES , Index . NOT_ANALYZED ) ) ; d . add ( new org . apache . lucene . document . Field ( "contents" , English . intToEnglish ( ( iFinal + k ) ) , Store . NO , Index . ANALYZED ) ) ; writerFinal . addDocument ( d ) ; } for ( int k = 0 ; k < ( 9 * ( 1 + iFinal ) ) ; k ++ ) writerFinal . deleteDocuments ( new Term ( "id" , ( ( ( ( ( ( iterFinal + "_" ) + iFinal ) + "_" ) + j ) + "_" ) + k ) ) ) ; writerFinal . optimize ( ) ; } } catch ( Throwable t ) { setFailed ( ) ; System . out . println ( ( ( Thread . currentThread ( ) . getName ( ) ) + ":<seq2seq4repair_space>hit<seq2seq4repair_space>exception" ) ) ; t . printStackTrace ( System . out ) ; } } } ; } for ( int i = 0 ; i < ( TestThreadedOptimize . NUM_THREADS ) ; i ++ ) threads [ i ] . start ( ) ; for ( int i = 0 ; i < ( TestThreadedOptimize . NUM_THREADS ) ; i ++ ) threads [ i ] . join ( ) ; assertTrue ( ( ! ( failed ) ) ) ; final int expectedDocCount = ( ( int ) ( ( 1 + iter ) * ( 200 + ( ( ( 8 * ( TestThreadedOptimize . NUM_ITER2 ) ) * ( ( TestThreadedOptimize . NUM_THREADS ) // 2.0 ) ) * ( 1 + ( TestThreadedOptimize . NUM_THREADS ) ) ) ) ) ) ; assertEquals ( expectedDocCount , writer . docCount ( ) ) ; if ( ! autoCommit ) { writer . close ( ) ; writer = new IndexWriter ( directory , autoCommit , TestThreadedOptimize . ANALYZER , false ) ; writer . setMaxBufferedDocs ( 2 ) ; } <START_BUG> IndexReader reader = IndexReader . open ( directory ) ; <END_BUG> assertTrue ( reader . isOptimized ( ) ) ; assertEquals ( expectedDocCount , reader . numDocs ( ) ) ; reader . close ( ) ; } writer . close ( ) ; } public void testThreadedOptimize ( ) throws Exception { } }

Fix:
IndexReader reader = IndexReader . open ( directory , true ) ;