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()); }
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()); }
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()); }