private static void execute(
     Settings settings,
     BiFunction<Runnable, TimeValue, ScheduledFuture<?>> scheduler,
     Consumer<Throwable> consumer,
     boolean constructionShouldFail,
     Runnable asserts)
     throws InterruptedException {
   assert constructionShouldFail == (consumer != null);
   assert constructionShouldFail == (asserts == null);
   ThreadPool threadPool = null;
   try {
     threadPool =
         new ThreadPool(JvmGcMonitorServiceSettingsTests.class.getCanonicalName()) {
           @Override
           public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, TimeValue interval) {
             return scheduler.apply(command, interval);
           }
         };
     try {
       JvmGcMonitorService service = new JvmGcMonitorService(settings, threadPool);
       if (constructionShouldFail) {
         fail("construction of jvm gc service should have failed");
       }
       service.doStart();
       asserts.run();
       service.doStop();
     } catch (Throwable t) {
       consumer.accept(t);
     }
   } finally {
     ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
   }
 }
    /** Builds a new instance of the transport client. */
    public TransportClient build() {
      Settings settings = InternalSettingsPreparer.prepareSettings(this.settings);
      settings =
          settingsBuilder()
              .put(
                  NettyTransport.PING_SCHEDULE,
                  "5s") // enable by default the transport schedule ping interval
              .put(settings)
              .put("network.server", false)
              .put("node.client", true)
              .put(CLIENT_TYPE_SETTING, CLIENT_TYPE)
              .build();

      PluginsService pluginsService = new PluginsService(settings, null, null, pluginClasses);
      this.settings = pluginsService.updatedSettings();

      Version version = Version.CURRENT;

      final ThreadPool threadPool = new ThreadPool(settings);
      final NetworkService networkService = new NetworkService(settings);
      final SettingsFilter settingsFilter = new SettingsFilter(settings);
      NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
      boolean success = false;
      try {
        ModulesBuilder modules = new ModulesBuilder();
        modules.add(new Version.Module(version));
        // plugin modules must be added here, before others or we can get crazy injection errors...
        for (Module pluginModule : pluginsService.nodeModules()) {
          modules.add(pluginModule);
        }
        modules.add(new PluginsModule(pluginsService));
        modules.add(new SettingsModule(this.settings, settingsFilter));
        modules.add(new NetworkModule(networkService, this.settings, true, namedWriteableRegistry));
        modules.add(new ClusterNameModule(this.settings));
        modules.add(new ThreadPoolModule(threadPool));
        modules.add(
            new SearchModule(settings, namedWriteableRegistry) {
              @Override
              protected void configure() {
                // noop
              }
            });
        modules.add(new ActionModule(true));
        modules.add(new CircuitBreakerModule(this.settings));

        pluginsService.processModules(modules);

        Injector injector = modules.createInjector();
        injector.getInstance(TransportService.class).start();
        TransportClient transportClient = new TransportClient(injector);
        success = true;
        return transportClient;
      } finally {
        if (!success) {
          ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
        }
      }
    }
    /** Builds a new instance of the transport client. */
    public TransportClient build() {
      final PluginsService pluginsService = newPluginService(providedSettings);
      final Settings settings = pluginsService.updatedSettings();

      Version version = Version.CURRENT;

      final ThreadPool threadPool = new ThreadPool(settings);
      final NetworkService networkService = new NetworkService(settings);
      NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry();
      boolean success = false;
      try {
        ModulesBuilder modules = new ModulesBuilder();
        modules.add(new Version.Module(version));
        // plugin modules must be added here, before others or we can get crazy injection errors...
        for (Module pluginModule : pluginsService.nodeModules()) {
          modules.add(pluginModule);
        }
        modules.add(new PluginsModule(pluginsService));
        modules.add(new SettingsModule(settings));
        modules.add(new NetworkModule(networkService, settings, true, namedWriteableRegistry));
        modules.add(new ClusterNameModule(settings));
        modules.add(new ThreadPoolModule(threadPool));
        modules.add(
            new SearchModule(settings, namedWriteableRegistry) {
              @Override
              protected void configure() {
                // noop
              }
            });
        modules.add(new ActionModule(false, true));
        modules.add(new CircuitBreakerModule(settings));

        pluginsService.processModules(modules);

        Injector injector = modules.createInjector();
        final TransportService transportService = injector.getInstance(TransportService.class);
        transportService.start();
        transportService.acceptIncomingRequests();

        TransportClient transportClient = new TransportClient(injector);
        success = true;
        return transportClient;
      } finally {
        if (!success) {
          ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS);
        }
      }
    }
 @Override
 protected synchronized void doStop() {
   for (NotifyTimeout onGoingTimeout : onGoingTimeouts) {
     onGoingTimeout.cancel();
     try {
       onGoingTimeout.cancel();
       onGoingTimeout.listener.onClose();
     } catch (Exception ex) {
       logger.debug("failed to notify listeners on shutdown", ex);
     }
   }
   ThreadPool.terminate(updateTasksExecutor, 10, TimeUnit.SECONDS);
   // close timeout listeners that did not have an ongoing timeout
   postAppliedListeners
       .stream()
       .filter(listener -> listener instanceof TimeoutClusterStateListener)
       .map(listener -> (TimeoutClusterStateListener) listener)
       .forEach(TimeoutClusterStateListener::onClose);
   remove(localNodeMasterListeners);
 }
  /** Closes the client. */
  @Override
  public void close() {
    injector.getInstance(TransportClientNodesService.class).close();
    injector.getInstance(TransportService.class).close();
    try {
      injector.getInstance(MonitorService.class).close();
    } catch (Exception e) {
      // ignore, might not be bounded
    }

    for (Class<? extends LifecycleComponent> plugin :
        injector.getInstance(PluginsService.class).nodeServices()) {
      injector.getInstance(plugin).close();
    }
    try {
      ThreadPool.terminate(injector.getInstance(ThreadPool.class), 10, TimeUnit.SECONDS);
    } catch (Exception e) {
      // ignore
    }

    injector.getInstance(PageCacheRecycler.class).close();
  }
 @AfterClass
 public static void stopThreadPool() {
   ThreadPool.terminate(THREAD_POOL, 30, TimeUnit.SECONDS);
   THREAD_POOL = null;
 }
 @Override
 public void tearDown() throws Exception {
   super.tearDown();
   ThreadPool.terminate(threadPool, 30, TimeUnit.SECONDS);
 }
 @Override
 protected void doClose() {
   ThreadPool.terminate(workers, 10, TimeUnit.SECONDS);
 }