Buggy code:
public class AllGroupsCollectorTest extends LuceneTestCase { public void testTotalGroupCount ( ) throws Exception { final String groupField = "author" ; FieldType customType = new FieldType ( ) ; customType . setStored ( true ) ; Directory dir = newDirectory ( ) ; RandomIndexWriter w = new RandomIndexWriter ( random ( ) , dir , newIndexWriterConfig ( TEST_VERSION_CURRENT , new org . apache . lucene . analysis . MockAnalyzer ( random ( ) ) ) . setMergePolicy ( newLogMergePolicy ( ) ) ) ; Document doc = new Document ( ) ; addGroupField ( doc , groupField , "author1" ) ; doc . add ( new org . apache . lucene . document . TextField ( "content" , "random<seq2seq4repair_space>text" , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . Field ( "id" , "1" , customType ) ) ; w . addDocument ( doc ) ; doc = new Document ( ) ; addGroupField ( doc , groupField , "author1" ) ; doc . add ( new org . apache . lucene . document . TextField ( "content" , "some<seq2seq4repair_space>more<seq2seq4repair_space>random<seq2seq4repair_space>text<seq2seq4repair_space>blob" , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . Field ( "id" , "2" , customType ) ) ; w . addDocument ( doc ) ; doc = new Document ( ) ; addGroupField ( doc , groupField , "author1" ) ; doc . add ( new org . apache . lucene . document . TextField ( "content" , "some<seq2seq4repair_space>more<seq2seq4repair_space>random<seq2seq4repair_space>textual<seq2seq4repair_space>data" , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . Field ( "id" , "3" , customType ) ) ; w . addDocument ( doc ) ; w . commit ( ) ; doc = new Document ( ) ; addGroupField ( doc , groupField , "author2" ) ; doc . add ( new org . apache . lucene . document . TextField ( "content" , "some<seq2seq4repair_space>random<seq2seq4repair_space>text" , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . Field ( "id" , "4" , customType ) ) ; w . addDocument ( doc ) ; doc = new Document ( ) ; addGroupField ( doc , groupField , "author3" ) ; doc . add ( new org . apache . lucene . document . TextField ( "content" , "some<seq2seq4repair_space>more<seq2seq4repair_space>random<seq2seq4repair_space>text" , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . Field ( "id" , "5" , customType ) ) ; w . addDocument ( doc ) ; doc = new Document ( ) ; addGroupField ( doc , groupField , "author3" ) ; doc . add ( new org . apache . lucene . document . TextField ( "content" , "random<seq2seq4repair_space>blob" , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . Field ( "id" , "6" , customType ) ) ; w . addDocument ( doc ) ; doc = new Document ( ) ; doc . add ( new org . apache . lucene . document . TextField ( "content" , "random<seq2seq4repair_space>word<seq2seq4repair_space>stuck<seq2seq4repair_space>in<seq2seq4repair_space>alot<seq2seq4repair_space>of<seq2seq4repair_space>other<seq2seq4repair_space>text" , Store . YES ) ) ; doc . add ( new org . apache . lucene . document . Field ( "id" , "6" , customType ) ) ; w . addDocument ( doc ) ; IndexSearcher indexSearcher = newSearcher ( w . getReader ( ) ) ; <START_BUG> w . close ( ) ; <END_BUG> AbstractAllGroupsCollector < ? > allGroupsCollector = createRandomCollector ( groupField ) ; indexSearcher . search ( new org . apache . lucene . search . TermQuery ( new Term ( "content" , "random" ) ) , allGroupsCollector ) ; assertEquals ( 4 , allGroupsCollector . getGroupCount ( ) ) ; allGroupsCollector = createRandomCollector ( groupField ) ; indexSearcher . search ( new org . apache . lucene . search . TermQuery ( new Term ( "content" , "some" ) ) , allGroupsCollector ) ; assertEquals ( 3 , allGroupsCollector . getGroupCount ( ) ) ; allGroupsCollector = createRandomCollector ( groupField ) ; indexSearcher . search ( new org . apache . lucene . search . TermQuery ( new Term ( "content" , "blob" ) ) , allGroupsCollector ) ; assertEquals ( 2 , allGroupsCollector . getGroupCount ( ) ) ; indexSearcher . getIndexReader ( ) . close ( ) ; dir . close ( ) ; } private void addGroupField ( Document doc , String groupField , String value ) { } private AbstractAllGroupsCollector < ? > createRandomCollector ( String groupField ) { } }

Fix:
w . shutdown ( ) ;