Buggy code:
final AtomicInteger totTermCount = new AtomicInteger ( 100 ) ; while ( ( ( System . currentTimeMillis ( ) ) < stopTime ) && ( ! ( failed . get ( ) ) ) ) { if ( random . nextBoolean ( ) ) { if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>now<seq2seq4repair_space>reopen<seq2seq4repair_space>r=" + r ) ) ; } final IndexReader r2 = r . reopen ( ) ; if ( r != r2 ) { r . close ( ) ; r = r2 ; } } else { if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>now<seq2seq4repair_space>close<seq2seq4repair_space>reader=" + r ) ) ; } r . close ( ) ; writer . commit ( ) ; final Set < String > openDeletedFiles = dir . getOpenDeletedFiles ( ) ; if ( ( openDeletedFiles . size ( ) ) > 0 ) { System . out . println ( ( "OBD<seq2seq4repair_space>files:<seq2seq4repair_space>" + openDeletedFiles ) ) ; } any |= ( openDeletedFiles . size ( ) ) > 0 ; if ( VERBOSE ) { System . out . println ( "TEST:<seq2seq4repair_space>now<seq2seq4repair_space>open" ) ; } r = IndexReader . open ( writer ) ; } if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>got<seq2seq4repair_space>new<seq2seq4repair_space>reader=" + r ) ) ; } smokeTestReader ( r ) ; if ( ( r . numDocs ( ) ) > 0 ) { final IndexSearcher s = new IndexSearcher ( r ) ; final long searchStopTime = ( System . currentTimeMillis ( ) ) + 500 ; final Thread [ ] searchThreads = new Thread [ NUM_SEARCH_THREADS ] ; final AtomicInteger totHits = new AtomicInteger ( ) ; for ( int thread = 0 ; thread < NUM_SEARCH_THREADS ; thread ++ ) { searchThreads [ thread ] = new Thread ( ) { @ Override public void run ( ) { try { TermsEnum termsEnum = MultiFields . getTerms ( s . getIndexReader ( ) , "body" ) . iterator ( ) ; int seenTermCount = 0 ; int shift ; int trigger ; if ( ( totTermCount . get ( ) ) == 0 ) { shift = 0 ; trigger = 1 ; } else { shift = random . nextInt ( ( ( totTermCount . get ( ) ) // 10 ) ) ; trigger = ( totTermCount . get ( ) ) // 10 ; } while ( ( System . currentTimeMillis ( ) ) < searchStopTime ) { BytesRef term = termsEnum . next ( ) ; if ( term == null ) { if ( seenTermCount == 0 ) { break ; } totTermCount . set ( seenTermCount ) ; seenTermCount = 0 ; trigger = ( totTermCount . get ( ) ) // 10 ; shift = random . nextInt ( ( ( totTermCount . get ( ) ) // 10 ) ) ; termsEnum . seek ( new BytesRef ( "" ) ) ; continue ; } seenTermCount ++ ; if ( trigger == 0 ) { trigger = 1 ; } if ( ( ( seenTermCount + shift ) % trigger ) == 0 ) { totHits . addAndGet ( runQuery ( s , new TermQuery ( new Term ( "body" , term ) ) ) ) ; } } if ( VERBOSE ) { System . out . println ( ( ( Thread . currentThread ( ) . getName ( ) ) + ":<seq2seq4repair_space>search<seq2seq4repair_space>done" ) ) ; } } catch ( Throwable t ) { failed . set ( true ) ; t . printStackTrace ( System . out ) ; throw new RuntimeException ( t ) ; } } } ; searchThreads [ thread ] . setDaemon ( true ) ; searchThreads [ thread ] . start ( ) ; } for ( int thread = 0 ; thread < NUM_SEARCH_THREADS ; thread ++ ) { searchThreads [ thread ] . join ( ) ; } if ( VERBOSE ) { System . out . println ( ( "TEST:<seq2seq4repair_space>DONE<seq2seq4repair_space>search:<seq2seq4repair_space>totHits=" + totHits ) ) ; } } else { Thread . sleep ( 100 ) ; } } if ( VERBOSE ) { System . out . println ( ( ( "TEST:<seq2seq4repair_space>all<seq2seq4repair_space>searching<seq2seq4repair_space>done<seq2seq4repair_space>[" + ( ( System . currentTimeMillis ( ) ) - t0 ) ) + "<seq2seq4repair_space>ms]" ) ) ; } r . close ( ) ; final Set < String > openDeletedFiles = dir . getOpenDeletedFiles ( ) ; if ( ( openDeletedFiles . size ( ) ) > 0 ) { System . out . println ( ( "OBD<seq2seq4repair_space>files:<seq2seq4repair_space>" + openDeletedFiles ) ) ; } any |= ( openDeletedFiles . size ( ) ) > 0 ; assertFalse ( "saw<seq2seq4repair_space>non-zero<seq2seq4repair_space>open-but-deleted<seq2seq4repair_space>count" , any ) ; if ( VERBOSE ) { System . out . println ( "TEST:<seq2seq4repair_space>now<seq2seq4repair_space>join" ) ; } for ( int thread = 0 ; thread < NUM_INDEX_THREADS ; thread ++ ) { threads [ thread ] . join ( ) ; } if ( VERBOSE ) { System . out . println ( ( ( ( ( ( "TEST:<seq2seq4repair_space>done<seq2seq4repair_space>join<seq2seq4repair_space>[" + ( ( System . currentTimeMillis ( ) ) - t0 ) ) + "<seq2seq4repair_space>ms];<seq2seq4repair_space>addCount=" ) + addCount ) + "<seq2seq4repair_space>delCount=" ) + delCount ) ) ; } writer . commit ( ) ; <START_BUG> assertEquals ( ( ( addCount . get ( ) ) - ( delCount . get ( ) ) ) , writer . numDocs ( ) ) ; <END_BUG> writer . close ( false ) ; dir . close ( ) ; _TestUtil . rmDir ( tempDir ) ; docs . close ( ) ; if ( VERBOSE ) { System . out . println ( ( ( "TEST:<seq2seq4repair_space>done<seq2seq4repair_space>[" + ( ( System . currentTimeMillis ( ) ) - t0 ) ) + "<seq2seq4repair_space>ms]" ) ) ; } } private int runQuery ( IndexSearcher s , Query q ) throws Exception { } private void smokeTestReader ( IndexReader r ) throws Exception { } }

Fix:
assertEquals ( ( "index=" + ( writer . segString ( ) ) ) , ( ( addCount . get ( ) ) - ( delCount . get ( ) ) ) , writer . numDocs ( ) ) ;