コード例 #1
0
 public static void submitScheduledCallableFail(SubmitterSchedulerFactory factory) {
   try {
     SubmitterSchedulerInterface scheduler = factory.makeSubmitterScheduler(1, false);
     try {
       scheduler.submitScheduled((Callable<Object>) null, 1000);
       fail("Exception should have been thrown");
     } catch (IllegalArgumentException e) {
       // expected
     }
     try {
       scheduler.submitScheduled(new TestCallable(0), -1);
       fail("Exception should have been thrown");
     } catch (IllegalArgumentException e) {
       // expected
     }
   } finally {
     factory.shutdown();
   }
 }
コード例 #2
0
  public static void submitScheduledRunnableWithResultTest(SubmitterSchedulerFactory factory)
      throws InterruptedException, ExecutionException {
    try {
      int runnableCount = 10;
      int scheduleDelay = 50;

      SubmitterSchedulerInterface scheduler = factory.makeSubmitterScheduler(runnableCount, true);

      List<TestRunnable> runnables = new ArrayList<TestRunnable>(runnableCount);
      List<Future<TestRunnable>> futures = new ArrayList<Future<TestRunnable>>(runnableCount);
      for (int i = 0; i < runnableCount; i++) {
        TestRunnable tr = new TestRunnable();
        Future<TestRunnable> future = scheduler.submitScheduled(tr, tr, scheduleDelay);
        assertNotNull(future);
        runnables.add(tr);
        futures.add(future);
      }

      // verify execution and execution times
      Iterator<TestRunnable> it = runnables.iterator();
      while (it.hasNext()) {
        TestRunnable tr = it.next();
        long executionDelay = tr.getDelayTillFirstRun();
        assertTrue(executionDelay >= scheduleDelay);
        // should be very timely with a core pool size that matches runnable count
        assertTrue(executionDelay <= (scheduleDelay + 2000));
        assertEquals(1, tr.getRunCount());
      }

      it = runnables.iterator();
      Iterator<Future<TestRunnable>> futureIt = futures.iterator();
      while (futureIt.hasNext()) {
        Future<?> future = futureIt.next();
        assertTrue(future.isDone());
        assertTrue(future.get() == it.next());
      }
    } finally {
      factory.shutdown();
    }
  }