92
92
import org .elasticsearch .test .IndexSettingsModule ;
93
93
import org .elasticsearch .threadpool .TestThreadPool ;
94
94
import org .elasticsearch .threadpool .ThreadPool ;
95
+ import org .elasticsearch .threadpool .ThreadPoolStats ;
95
96
import org .elasticsearch .xpack .searchablesnapshots .SearchableSnapshots ;
96
97
import org .elasticsearch .xpack .searchablesnapshots .cache .CacheService ;
97
98
import org .hamcrest .Matcher ;
119
120
import java .util .List ;
120
121
import java .util .Map ;
121
122
import java .util .Set ;
123
+ import java .util .concurrent .TimeUnit ;
122
124
import java .util .stream .Collectors ;
123
125
124
126
import static java .util .Arrays .asList ;
@@ -627,7 +629,7 @@ protected void assertSnapshotOrGenericThread() {
627
629
Releasables .close (releasables );
628
630
}
629
631
} finally {
630
- terminate (threadPool );
632
+ terminateSafely (threadPool );
631
633
}
632
634
}
633
635
@@ -744,7 +746,7 @@ public void testClearCache() throws Exception {
744
746
}
745
747
}
746
748
} finally {
747
- terminate (threadPool );
749
+ terminateSafely (threadPool );
748
750
}
749
751
}
750
752
}
@@ -940,6 +942,17 @@ private SearchableSnapshotRecoveryState createRecoveryState() {
940
942
return recoveryState ;
941
943
}
942
944
945
+ // Wait for all operations on the threadpool to complete to make sure we don't leak any reference count releasing and then shut it down
946
+ private static void terminateSafely (ThreadPool threadPool ) throws Exception {
947
+ assertBusy (() -> {
948
+ for (ThreadPoolStats .Stats stat : threadPool .stats ()) {
949
+ assertEquals (stat .getActive (), 0 );
950
+ assertEquals (stat .getQueue (), 0 );
951
+ }
952
+ });
953
+ assertTrue (ThreadPool .terminate (threadPool , 10 , TimeUnit .SECONDS ));
954
+ }
955
+
943
956
private static class FaultyReadsFileSystem extends FilterFileSystemProvider {
944
957
FaultyReadsFileSystem (FileSystem inner ) {
945
958
super ("faulty_fs://" , inner );
@@ -956,5 +969,4 @@ public int read(ByteBuffer dst) throws IOException {
956
969
};
957
970
}
958
971
}
959
-
960
972
}
0 commit comments