@Test
  public void testFixedRejected() throws Exception {
    EsThreadPoolExecutor executor =
        EsExecutors.newFixed(1, 1, EsExecutors.daemonThreadFactory("test"));
    final CountDownLatch wait = new CountDownLatch(1);

    final CountDownLatch exec1Wait = new CountDownLatch(1);
    final AtomicBoolean executed1 = new AtomicBoolean();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            try {
              wait.await();
            } catch (InterruptedException e) {
              throw new RuntimeException(e);
            }
            executed1.set(true);
            exec1Wait.countDown();
          }
        });

    final CountDownLatch exec2Wait = new CountDownLatch(1);
    final AtomicBoolean executed2 = new AtomicBoolean();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            executed2.set(true);
            exec2Wait.countDown();
          }
        });

    final AtomicBoolean executed3 = new AtomicBoolean();
    try {
      executor.execute(
          new Runnable() {
            @Override
            public void run() {
              executed3.set(true);
            }
          });
      fail("should be rejected...");
    } catch (EsRejectedExecutionException e) {
      // all is well
    }

    wait.countDown();

    exec1Wait.await();
    exec2Wait.await();

    assertThat(executed1.get(), equalTo(true));
    assertThat(executed2.get(), equalTo(true));
    assertThat(executed3.get(), equalTo(false));

    terminate(executor);
  }
  @Test
  public void testFixedForcedExecution() throws Exception {
    EsThreadPoolExecutor executor =
        EsExecutors.newFixed(1, 1, EsExecutors.daemonThreadFactory("test"));
    final CountDownLatch wait = new CountDownLatch(1);

    final CountDownLatch exec1Wait = new CountDownLatch(1);
    final AtomicBoolean executed1 = new AtomicBoolean();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            try {
              wait.await();
            } catch (InterruptedException e) {
              throw new RuntimeException(e);
            }
            executed1.set(true);
            exec1Wait.countDown();
          }
        });

    final CountDownLatch exec2Wait = new CountDownLatch(1);
    final AtomicBoolean executed2 = new AtomicBoolean();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            executed2.set(true);
            exec2Wait.countDown();
          }
        });

    final AtomicBoolean executed3 = new AtomicBoolean();
    final CountDownLatch exec3Wait = new CountDownLatch(1);
    executor.execute(
        new AbstractRunnable() {
          @Override
          protected void doRun() {
            executed3.set(true);
            exec3Wait.countDown();
          }

          @Override
          public boolean isForceExecution() {
            return true;
          }

          @Override
          public void onFailure(Throwable t) {
            throw new AssertionError(t);
          }
        });

    wait.countDown();

    exec1Wait.await();
    exec2Wait.await();
    exec3Wait.await();

    assertThat(executed1.get(), equalTo(true));
    assertThat(executed2.get(), equalTo(true));
    assertThat(executed3.get(), equalTo(true));

    executor.shutdownNow();
  }