3434import org .elasticsearch .common .bytes .BytesReference ;
3535import org .elasticsearch .common .network .InetAddresses ;
3636import org .elasticsearch .common .settings .Settings ;
37+ import org .elasticsearch .common .util .concurrent .EsExecutors ;
38+ import org .elasticsearch .common .util .concurrent .ThreadContext ;
3739import org .elasticsearch .mocksocket .MockHttpServer ;
3840import org .elasticsearch .repositories .RepositoriesService ;
3941import org .elasticsearch .repositories .Repository ;
4042import org .elasticsearch .repositories .RepositoryMissingException ;
4143import org .elasticsearch .repositories .RepositoryStats ;
4244import org .elasticsearch .test .BackgroundIndexer ;
45+ import org .elasticsearch .threadpool .ThreadPool ;
4346import org .junit .After ;
4447import org .junit .AfterClass ;
4548import org .junit .Before ;
5558import java .util .Map ;
5659import java .util .Objects ;
5760import java .util .concurrent .ConcurrentHashMap ;
61+ import java .util .concurrent .ExecutorService ;
62+ import java .util .concurrent .ThreadFactory ;
63+ import java .util .concurrent .TimeUnit ;
5864import java .util .concurrent .atomic .AtomicInteger ;
5965import java .util .stream .Collectors ;
6066import java .util .stream .StreamSupport ;
@@ -83,20 +89,27 @@ protected interface BlobStoreHttpHandler extends HttpHandler {
8389 private static final byte [] BUFFER = new byte [1024 ];
8490
8591 private static HttpServer httpServer ;
92+ private static ExecutorService executorService ;
8693 protected Map <String , HttpHandler > handlers ;
8794
8895 private static final Logger log = LogManager .getLogger ();
8996
9097 @ BeforeClass
9198 public static void startHttpServer () throws Exception {
9299 httpServer = MockHttpServer .createHttp (new InetSocketAddress (InetAddress .getLoopbackAddress (), 0 ), 0 );
100+ ThreadFactory threadFactory = EsExecutors .daemonThreadFactory ("[" + ESMockAPIBasedRepositoryIntegTestCase .class .getName () + "]" );
101+ // the EncryptedRepository can require more than one connection open at one time
102+ executorService = EsExecutors .newScaling (ESMockAPIBasedRepositoryIntegTestCase .class .getName (), 0 , 2 , 60 ,
103+ TimeUnit .SECONDS , threadFactory , new ThreadContext (Settings .EMPTY ));
93104 httpServer .setExecutor (r -> {
94- try {
95- r .run ();
96- } catch (Throwable t ) {
97- log .error ("Error in execution on mock http server IO thread" , t );
98- throw t ;
99- }
105+ executorService .execute (() -> {
106+ try {
107+ r .run ();
108+ } catch (Throwable t ) {
109+ log .error ("Error in execution on mock http server IO thread" , t );
110+ throw t ;
111+ }
112+ });
100113 });
101114 httpServer .start ();
102115 }
@@ -111,6 +124,7 @@ public void setUpHttpServer() {
111124 @ AfterClass
112125 public static void stopHttpServer () {
113126 httpServer .stop (0 );
127+ ThreadPool .terminate (executorService , 10 , TimeUnit .SECONDS );
114128 httpServer = null ;
115129 }
116130
0 commit comments