private ScheduledTask<Integer> createScheduledTask(Schedule schedule) { TestCallable callable = new TestCallable(); return new DefaultScheduledTask<Integer>( "1", TASK_NAME, callable.getClass().getSimpleName(), // TODO this is only use for testing, but we are expecting that the TaskHint matches the // Classname. defaultScheduler, callable, schedule); }
public static void submitScheduledCallableTest(SubmitterSchedulerFactory factory) throws InterruptedException, ExecutionException { try { int runnableCount = 10; int scheduleDelay = 50; SubmitterSchedulerInterface scheduler = factory.makeSubmitterScheduler(runnableCount, true); List<TestCallable> callables = new ArrayList<TestCallable>(runnableCount); List<Future<Object>> futures = new ArrayList<Future<Object>>(runnableCount); for (int i = 0; i < runnableCount; i++) { TestCallable tc = new TestCallable(0); Future<Object> future = scheduler.submitScheduled(tc, scheduleDelay); assertNotNull(future); callables.add(tc); futures.add(future); } // verify execution and execution times Iterator<TestCallable> it = callables.iterator(); Iterator<Future<Object>> futureIt = futures.iterator(); while (futureIt.hasNext()) { Future<Object> future = futureIt.next(); TestCallable tc = it.next(); assertTrue(tc.getReturnedResult() == future.get()); assertTrue(future.isDone()); long executionDelay = tc.getDelayTillFirstRun(); assertTrue(executionDelay >= scheduleDelay); // should be very timely with a core pool size that matches runnable count assertTrue(executionDelay <= (scheduleDelay + 2000)); } } finally { factory.shutdown(); } }
@Test public void testRunNowCallable() throws Exception { TestCallable tr = new TestCallable(); ScheduledTask<Integer> st = defaultScheduler.submit("default", tr); assertEquals(1, defaultScheduler.getActiveTasks().size()); while (!st.getTaskState().isEndingState()) { Thread.sleep(300); } assertEquals(1, tr.getRunCount()); assertEquals(1, st.getResults().size()); assertEquals(Integer.valueOf(0), st.getResults().get(0)); assertEquals(TaskState.FINISHED, st.getTaskState()); assertNull(st.getNextRun()); assertEquals(0, defaultScheduler.getActiveTasks().size()); }