Пример #1
0
  @Test
  public void testCancellationAwareTask2() {
    Callable task1 = new CancellationAwareTask(5000);
    ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask", 1);
    executor.submit(task1);

    Callable task2 = new BasicTestTask();
    Future future = executor.submit(task2);
    assertFalse(future.isDone());
    assertTrue(future.cancel(true));
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());

    try {
      future.get();
      fail("Should not complete the task successfully");
    } catch (CancellationException expected) {
    } catch (Exception e) {
      fail("Unexpected exception " + e);
    }
  }
Пример #2
0
  @Test
  public void testCancellationAwareTask() throws ExecutionException, InterruptedException {
    CancellationAwareTask task = new CancellationAwareTask(5000);
    ExecutorService executor = createSingleNodeExecutorService("testCancellationAwareTask");
    Future future = executor.submit(task);
    try {
      future.get(2, TimeUnit.SECONDS);
      fail("Should throw TimeoutException!");
    } catch (TimeoutException expected) {
    }
    assertFalse(future.isDone());
    assertTrue(future.cancel(true));
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());

    try {
      future.get();
      fail("Should not complete the task successfully");
    } catch (CancellationException expected) {
    } catch (Exception e) {
      fail("Unexpected exception " + e);
    }
  }
Пример #3
0
  @Test
  public void testExecutorServiceStats() throws InterruptedException, ExecutionException {
    final IExecutorService executorService =
        createSingleNodeExecutorService("testExecutorServiceStats");
    final int k = 10;
    final CountDownLatch latch = new CountDownLatch(k);
    final int executionTime = 200;
    for (int i = 0; i < k; i++) {
      executorService.execute(
          new Runnable() {
            public void run() {
              try {
                Thread.sleep(executionTime);
              } catch (InterruptedException e) {
                e.printStackTrace();
              }
              latch.countDown();
            }
          });
    }
    latch.await(2, TimeUnit.MINUTES);

    final Future<Boolean> f = executorService.submit(new CancellationAwareTask(10000));
    Thread.sleep(1000);
    f.cancel(true);
    try {
      f.get();
    } catch (CancellationException e) {
    }

    final LocalExecutorStats stats = executorService.getLocalExecutorStats();
    assertEquals(k + 1, stats.getStartedTaskCount());
    assertEquals(k, stats.getCompletedTaskCount());
    assertEquals(0, stats.getPendingTaskCount());
    assertEquals(1, stats.getCancelledTaskCount());
  }