Buggy code:
public class TestSort extends SolrTestCaseJ4 { @ BeforeClass public static void beforeClass ( ) throws Exception { } Random r ; int ndocs = 77 ; int iter = 50 ; int qiter = 1000 ; int commitCount = ( ( ndocs ) // 5 ) + 1 ; int maxval = ( ndocs ) * 2 ; @ Override public void setUp ( ) throws Exception { } static class MyDoc { int doc ; String val ; String val2 ; @ Override public String toString ( ) { } } public void testRandomFieldNameSorts ( ) throws Exception { } public void testSort ( ) throws Exception { Directory dir = new RAMDirectory ( ) ; Field f = new org . apache . lucene . document . StringField ( "f" , "0" , Store . NO ) ; Field f2 = new org . apache . lucene . document . StringField ( "f2" , "0" , Store . NO ) ; for ( int iterCnt = 0 ; iterCnt < ( iter ) ; iterCnt ++ ) { IndexWriter iw = new IndexWriter ( dir , new org . apache . lucene . index . IndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . core . SimpleAnalyzer ( TEST_VERSION_CURRENT ) ) . setOpenMode ( CREATE ) ) ; final TestSort . MyDoc [ ] mydocs = new TestSort . MyDoc [ ndocs ] ; int v1EmptyPercent = 50 ; int v2EmptyPercent = 50 ; int commitCountdown = commitCount ; for ( int i = 0 ; i < ( ndocs ) ; i ++ ) { TestSort . MyDoc mydoc = new TestSort . MyDoc ( ) ; mydoc . doc = i ; mydocs [ i ] = mydoc ; Document document = new Document ( ) ; if ( ( r . nextInt ( 100 ) ) < v1EmptyPercent ) { mydoc . val = Integer . toString ( r . nextInt ( maxval ) ) ; f . setStringValue ( mydoc . val ) ; document . add ( f ) ; } if ( ( r . nextInt ( 100 ) ) < v2EmptyPercent ) { mydoc . val2 = Integer . toString ( r . nextInt ( maxval ) ) ; f2 . setStringValue ( mydoc . val2 ) ; document . add ( f2 ) ; } iw . addDocument ( document ) ; if ( ( -- commitCountdown ) <= 0 ) { commitCountdown = commitCount ; iw . commit ( ) ; } } <START_BUG> iw . close ( ) ; <END_BUG> DirectoryReader reader = DirectoryReader . open ( dir ) ; IndexSearcher searcher = new IndexSearcher ( reader ) ; assertTrue ( ( ( reader . leaves ( ) . size ( ) ) > 1 ) ) ; for ( int i = 0 ; i < ( qiter ) ; i ++ ) { Filter filt = new Filter ( ) { @ Override public DocIdSet getDocIdSet ( AtomicReaderContext context , Bits acceptDocs ) { return BitsFilteredDocIdSet . wrap ( randSet ( context . reader ( ) . maxDoc ( ) ) , acceptDocs ) ; } } ; int top = ( r . nextInt ( ( ( ( ndocs ) > > 3 ) + 1 ) ) ) + 1 ; final boolean luceneSort = r . nextBoolean ( ) ; final boolean sortMissingLast = ( ! luceneSort ) && ( r . nextBoolean ( ) ) ; final boolean sortMissingFirst = ( ! luceneSort ) && ( ! sortMissingLast ) ; final boolean reverse = r . nextBoolean ( ) ; List < SortField > sfields = new ArrayList < > ( ) ; final boolean secondary = r . nextBoolean ( ) ; final boolean luceneSort2 = r . nextBoolean ( ) ; final boolean sortMissingLast2 = ( ! luceneSort2 ) && ( r . nextBoolean ( ) ) ; final boolean sortMissingFirst2 = ( ! luceneSort2 ) && ( ! sortMissingLast2 ) ; final boolean reverse2 = r . nextBoolean ( ) ; if ( r . nextBoolean ( ) ) sfields . add ( new SortField ( null , Type . SCORE ) ) ; sfields . add ( Sorting . getStringSortField ( "f" , reverse , sortMissingLast , sortMissingFirst ) ) ; if ( secondary ) { sfields . add ( Sorting . getStringSortField ( "f2" , reverse2 , sortMissingLast2 , sortMissingFirst2 ) ) ; } if ( r . nextBoolean ( ) ) sfields . add ( new SortField ( null , Type . SCORE ) ) ; Sort sort = new Sort ( sfields . toArray ( new SortField [ sfields . size ( ) ] ) ) ; final String nullRep = ( ( luceneSort || ( sortMissingFirst && ( ! reverse ) ) ) || ( sortMissingLast && reverse ) ) ? "" : "zzz" ; final String nullRep2 = ( ( luceneSort2 || ( sortMissingFirst2 && ( ! reverse2 ) ) ) || ( sortMissingLast2 && reverse2 ) ) ? "" : "zzz" ; boolean trackScores = r . nextBoolean ( ) ; boolean trackMaxScores = r . nextBoolean ( ) ; boolean scoreInOrder = r . nextBoolean ( ) ; final TopFieldCollector topCollector = TopFieldCollector . create ( sort , top , true , trackScores , trackMaxScores , scoreInOrder ) ; final List < TestSort . MyDoc > collectedDocs = new ArrayList < > ( ) ; Collector myCollector = new FilterCollector ( topCollector ) { @ Override public LeafCollector getLeafCollector ( AtomicReaderContext context ) throws IOException { final int docBase = context . docBase ; return new FilterLeafCollector ( super . getLeafCollector ( context ) ) { @ Override public void collect ( int doc ) throws IOException { super . collect ( doc ) ; collectedDocs . add ( mydocs [ ( docBase + doc ) ] ) ; } } ; } } ;

Fix:
iw . shutdown ( ) ;