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

    // tasks wait forever
    LastRejectedWaitPolicy policy = new LastRejectedWaitPolicy(-1, TimeUnit.SECONDS);
    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 but waits forever
    Runnable waiting = new SleepyTask("waitingForever", 1000);
    tasks.add(waiting);

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

    // the last task should have been queued
    assertFalse(executor.awaitTermination(4000, TimeUnit.MILLISECONDS));
    assertSame(waiting, policy.lastRejectedRunnable());
    assertEquals(0, SleepyTask.activeTasks.get());
  }
Example #2
0
  public void testWaitPolicyWithTimeout() throws Exception {
    assertEquals(0, SleepyTask.activeTasks.get());

    // set a reasonable retry interval
    LastRejectedWaitPolicy policy = new LastRejectedWaitPolicy(2500, 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 but will eventually succeed
    Runnable waiting = new SleepyTask("waiting", 1000);
    tasks.add(waiting);

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

    assertFalse(executor.awaitTermination(5000, TimeUnit.MILLISECONDS));
    assertSame(waiting, policy.lastRejectedRunnable());
    assertEquals(0, SleepyTask.activeTasks.get());
  }
Example #3
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());
  }
Example #4
0
  public void testCustomRejectedExecutionHandler() {
    RejectedExecutionHandler handler =
        new RejectedExecutionHandler() {
          public void rejectedExecution(Runnable arg0, ThreadPoolExecutor arg1) {
            // nothing to do here
          }
        };

    ThreadingProfile profile = new ThreadingProfile();
    profile.setRejectedExecutionHandler(handler);
    ThreadPoolExecutor pool = profile.createPool("testPool");
    assertSame(handler, pool.getRejectedExecutionHandler());
  }
Example #5
0
  public void testCustomThreadFactory() {
    ThreadingProfile profile = new ThreadingProfile();

    ThreadFactory configuredFactory =
        new ThreadFactory() {
          public Thread newThread(Runnable r) {
            return null;
          }
        };

    profile.setThreadFactory(configuredFactory);
    ThreadPoolExecutor pool = profile.createPool();
    ThreadFactory returnedFactory = pool.getThreadFactory();
    assertSame(configuredFactory, returnedFactory);
  }
Example #6
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());
  }
Example #7
0
  // @Override
  protected void doSetUp() throws Exception {
    super.doSetUp();

    // allow 1 active & 1 queued Thread
    executor =
        new ThreadPoolExecutor(1, 1, 10000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(1));
    executor.prestartAllCoreThreads();

    // the lock must be fair to guarantee FIFO access to the executor;
    // 'synchronized' on a monitor is not good enough.
    executorLock = new ReentrantLock(true);

    // this is a Threadgroup that collects uncaught exceptions. Necessary for JDK
    // 1.4.x only.
    threadGroup = new ExceptionCollectingThreadGroup();

    // reset counter of active SleepyTasks
    SleepyTask.activeTasks.set(0);
  }
Example #8
0
 // @Override
 protected void doTearDown() throws Exception {
   executor.shutdown();
   threadGroup.destroy();
   super.doTearDown();
 }
Example #9
0
 public void testDefaultNamedThreadFactory() {
   ThreadingProfile profile = new ThreadingProfile();
   ThreadPoolExecutor pool = profile.createPool("myThreadPool");
   ThreadFactory returnedFactory = pool.getThreadFactory();
   assertTrue(returnedFactory instanceof NamedThreadFactory);
 }
Example #10
0
 public void testDefaultThreadFactory() {
   ThreadingProfile profile = new ThreadingProfile();
   ThreadPoolExecutor pool = profile.createPool();
   ThreadFactory returnedFactory = pool.getThreadFactory();
   assertTrue(returnedFactory.getClass().isInstance(Executors.defaultThreadFactory()));
 }