Beispiel #1
0
  public void testWaitPolicyWithShutdownExecutor() throws Exception {
    assertEquals(0, SleepyTask.activeTasks.get());

    // wants to wait forever, but will fail immediately
    executor.setRejectedExecutionHandler(new LastRejectedWaitPolicy());
    executor.shutdown();

    // create a task
    List tasks = new ArrayList();
    tasks.add(new SleepyTask("rejected", 1000));

    // should fail and return immediately
    LinkedList submitters = this.execute(tasks);
    assertFalse(submitters.isEmpty());

    // let submitted tasks run
    Thread.sleep(1000);

    LinkedList exceptions = threadGroup.collectedExceptions();
    assertEquals(1, exceptions.size());

    Map.Entry threadFailure =
        (Map.Entry) ((Map) (exceptions.getFirst())).entrySet().iterator().next();
    assertEquals(submitters.getFirst(), threadFailure.getKey());
    assertEquals(RejectedExecutionException.class, threadFailure.getValue().getClass());
    assertEquals(0, SleepyTask.activeTasks.get());
  }
Beispiel #2
0
  public void testWaitPolicyWithTimeoutFailure() throws Exception {
    assertEquals(0, SleepyTask.activeTasks.get());

    // set a really short wait interval
    long failureInterval = 100L;
    LastRejectedWaitPolicy policy =
        new LastRejectedWaitPolicy(failureInterval, TimeUnit.MILLISECONDS);
    executor.setRejectedExecutionHandler(policy);

    // create tasks
    List tasks = new ArrayList();
    // task 1 runs immediately
    tasks.add(new SleepyTask("run", 1000));
    // task 2 is queued
    tasks.add(new SleepyTask("queued", 1000));
    // task 3 is initially rejected & will retry but should fail quickly
    Runnable failedTask = new SleepyTask("waitAndFail", 1000);
    tasks.add(failedTask);

    // submit tasks
    LinkedList submitters = this.execute(tasks);
    assertFalse(submitters.isEmpty());

    // give failure a chance
    Thread.sleep(failureInterval * 10);

    // make sure there was one failure
    LinkedList exceptions = threadGroup.collectedExceptions();
    assertEquals(1, exceptions.size());

    // make sure the failed task was the right one
    Map.Entry threadFailure =
        (Map.Entry) ((Map) (exceptions.getFirst())).entrySet().iterator().next();
    assertEquals(submitters.getLast(), threadFailure.getKey());
    assertEquals(RejectedExecutionException.class, threadFailure.getValue().getClass());

    executor.shutdown();
    assertTrue(executor.awaitTermination(2500, TimeUnit.MILLISECONDS));
    assertSame(failedTask, policy.lastRejectedRunnable());
    assertEquals(0, SleepyTask.activeTasks.get());
  }
Beispiel #3
0
 // @Override
 protected void doTearDown() throws Exception {
   executor.shutdown();
   threadGroup.destroy();
   super.doTearDown();
 }