Buggy code:
@ SuppressCodecs ( { "SimpleText" , "Memory" , "Direct" } ) public class Test2BTerms extends LuceneTestCase { private static final int TOKEN_LEN = 10 ; private static final BytesRef bytes = new BytesRef ( Test2BTerms . TOKEN_LEN ) ; private static final class MyTokenStream extends TokenStream { private final int tokensPerDoc ; private int tokenCount ; public final List < BytesRef > savedTerms = new ArrayList < > ( ) ; private int nextSave ; private final Random random ; public MyTokenStream ( Random random , int tokensPerDoc ) { } @ Override public boolean incrementToken ( ) { } @ Override public void reset ( ) { } private static final class MyTermAttributeImpl extends AttributeImpl implements TermToBytesRefAttribute { @ Override public int fillBytesRef ( ) { } @ Override public BytesRef getBytesRef ( ) { } @ Override public void clear ( ) { } @ Override public boolean equals ( Object other ) { } @ Override public int hashCode ( ) { } @ Override public void copyTo ( AttributeImpl target ) { } @ Override public Test2BTerms . MyTokenStream . MyTermAttributeImpl clone ( ) { } } private static final class MyAttributeFactory extends AttributeFactory { private final AttributeFactory delegate ; public MyAttributeFactory ( AttributeFactory delegate ) { } @ Override public AttributeImpl createAttributeInstance ( Class < ? extends Attribute > attClass ) { } } } @ Ignore ( "Very<seq2seq4repair_space>slow.<seq2seq4repair_space>Enable<seq2seq4repair_space>manually<seq2seq4repair_space>by<seq2seq4repair_space>removing<seq2seq4repair_space>@Ignore." ) public void test2BTerms ( ) throws IOException { System . out . println ( "Starting<seq2seq4repair_space>Test2B" ) ; final long TERM_COUNT = ( ( long ) ( Integer . MAX_VALUE ) ) + 100000000 ; final int TERMS_PER_DOC = TestUtil . nextInt ( random ( ) , 100000 , 1000000 ) ; List < BytesRef > savedTerms = null ; BaseDirectoryWrapper dir = newFSDirectory ( createTempDir ( "2BTerms" ) ) ; if ( dir instanceof MockDirectoryWrapper ) { ( ( MockDirectoryWrapper ) ( dir ) ) . setThrottling ( NEVER ) ; } dir . setCheckIndexOnClose ( false ) ; if ( true ) { IndexWriter w = new IndexWriter ( dir , new IndexWriterConfig ( TEST_VERSION_CURRENT , new MockAnalyzer ( random ( ) ) ) . setMaxBufferedDocs ( DISABLE_AUTO_FLUSH ) . setRAMBufferSizeMB ( 256.0 ) . setMergeScheduler ( new ConcurrentMergeScheduler ( ) ) . setMergePolicy ( newLogMergePolicy ( false , 10 ) ) . setOpenMode ( CREATE ) ) ; MergePolicy mp = w . getConfig ( ) . getMergePolicy ( ) ; if ( mp instanceof LogByteSizeMergePolicy ) { ( ( LogByteSizeMergePolicy ) ( mp ) ) . setMaxMergeMB ( ( ( 1024 * 1024 ) * 1024 ) ) ; } Document doc = new Document ( ) ; final Test2BTerms . MyTokenStream ts = new Test2BTerms . MyTokenStream ( random ( ) , TERMS_PER_DOC ) ; FieldType customType = new FieldType ( TextField . TYPE_NOT_STORED ) ; customType . setIndexOptions ( DOCS_ONLY ) ; customType . setOmitNorms ( true ) ; Field field = new Field ( "field" , ts , customType ) ; doc . add ( field ) ; final int numDocs = ( ( int ) ( TERM_COUNT // TERMS_PER_DOC ) ) ; System . out . println ( ( "TERMS_PER_DOC=" + TERMS_PER_DOC ) ) ; System . out . println ( ( "numDocs=" + numDocs ) ) ; for ( int i = 0 ; i < numDocs ; i ++ ) { final long t0 = System . currentTimeMillis ( ) ; w . addDocument ( doc ) ; System . out . println ( ( ( ( ( ( i + "<seq2seq4repair_space>of<seq2seq4repair_space>" ) + numDocs ) + "<seq2seq4repair_space>" ) + ( ( System . currentTimeMillis ( ) ) - t0 ) ) + "<seq2seq4repair_space>msec" ) ) ; } savedTerms = ts . savedTerms ; System . out . println ( "TEST:<seq2seq4repair_space>full<seq2seq4repair_space>merge" ) ; w . forceMerge ( 1 ) ; System . out . println ( "TEST:<seq2seq4repair_space>close<seq2seq4repair_space>writer" ) ; <START_BUG> w . close ( ) ; <END_BUG> } System . out . println ( "TEST:<seq2seq4repair_space>open<seq2seq4repair_space>reader" ) ; final IndexReader r = DirectoryReader . open ( dir ) ; if ( savedTerms == null ) { savedTerms = findTerms ( r ) ; } final int numSavedTerms = savedTerms . size ( ) ; final List < BytesRef > bigOrdTerms = new ArrayList ( savedTerms . subList ( ( numSavedTerms - 10 ) , numSavedTerms ) ) ; System . out . println ( "TEST:<seq2seq4repair_space>test<seq2seq4repair_space>big<seq2seq4repair_space>ord<seq2seq4repair_space>terms..." ) ; testSavedTerms ( r , bigOrdTerms ) ; System . out . println ( "TEST:<seq2seq4repair_space>test<seq2seq4repair_space>all<seq2seq4repair_space>saved<seq2seq4repair_space>terms..." ) ; testSavedTerms ( r , savedTerms ) ; r . close ( ) ; System . out . println ( "TEST:<seq2seq4repair_space>now<seq2seq4repair_space>CheckIndex..." ) ; CheckIndex . Status status = TestUtil . checkIndex ( dir ) ; final long tc = status . segmentInfos . get ( 0 ) . termIndexStatus . termCount ; assertTrue ( ( ( ( "count<seq2seq4repair_space>" + tc ) + "<seq2seq4repair_space>is<seq2seq4repair_space>not<seq2seq4repair_space>><seq2seq4repair_space>" ) + ( Integer . MAX_VALUE ) ) , ( tc > ( Integer . MAX_VALUE ) ) ) ; dir . close ( ) ; System . out . println ( "TEST:<seq2seq4repair_space>done!" ) ; } private List < BytesRef > findTerms ( IndexReader r ) throws IOException { } private void testSavedTerms ( IndexReader r , List < BytesRef > terms ) throws IOException { } }

Fix:
w . shutdown ( ) ;