public void testIsTaskSchedulable() { fact.setResult(null); StepTask t1 = task(100, 200, 50); assertFalse(tr.isTaskSchedulable(t1)); fact.setResult(sched(ListUtil.list(t1))); assertTrue(tr.isTaskSchedulable(t1)); }
// remove task-ending chunk, past task deadline, s.b. Timeout error. public void testRemoveChunkTaskEndTimeout() { final List finished = new ArrayList(); StepTask t1 = task( 100, 200, 100, new TaskCallback() { public void taskEvent(SchedulableTask task, Schedule.EventType event) { if (log.isDebug2()) { log.debug2("testRemoveChunkTaskEndTimeout callback"); } if (event == Schedule.EventType.FINISH) { finished.add(task); } } }); Schedule s = sched(ListUtil.list(t1)); fact.setResult(s); assertTrue(tr.addToSchedule(t1)); Schedule.Chunk chunk = (Schedule.Chunk) s.getEvents().get(0); assertTrue(tr.getCurrentSchedule().getEvents().contains(chunk)); chunk.setTaskEnd(); TimeBase.setSimulated(201); tr.removeChunk(chunk); assertFalse(tr.getCurrentSchedule().getEvents().contains(chunk)); assertSame(t1, finished.get(0)); assertNotNull(t1.e); assertTrue(t1.e.toString(), t1.e instanceof SchedService.Timeout); assertEmpty(tr.getAcceptedTasks()); }
// remove overrunnable task-ending chunk, before deadline, public void testRemoveChunkTaskEndOver() { final List finished = new ArrayList(); StepTask t1 = task( 100, 200, 100, new TaskCallback() { public void taskEvent(SchedulableTask task, Schedule.EventType event) { if (log.isDebug2()) { log.debug2("testRemoveChunkTaskEndOver callback"); } if (event == Schedule.EventType.FINISH) { finished.add(task); } } }); t1.setOverrunAllowed(true); Schedule s = sched(ListUtil.list(t1)); fact.setResult(s); assertTrue(tr.addToSchedule(t1)); Schedule.Chunk chunk = (Schedule.Chunk) s.getEvents().get(0); assertTrue(tr.getCurrentSchedule().getEvents().contains(chunk)); chunk.setTaskEnd(); tr.removeChunk(chunk); assertFalse(tr.getCurrentSchedule().getEvents().contains(chunk)); assertEmpty(finished); assertIsomorphic(ListUtil.list(t1), tr.getAcceptedTasks()); assertIsomorphic(SetUtil.set(t1), tr.getOverrunTasks()); }
// one task to drop, two failed schedule tries public void testAddToScheduleFailNoCleanup() { ConfigurationUtil.addFromArgs( TaskRunner.PARAM_DROP_TASK_MAX, "10", TaskRunner.PARAM_MIN_CLEANUP_INTERVAL, "0"); StepTask t1 = task(100, 200, 50); StepTask t2 = task(100, 200, 100); Schedule sched = sched(ListUtil.list(t1)); fact.setResult(sched); assertTrue(tr.addToSchedule(t1)); assertIsomorphic(ListUtil.list(t1), fact.scheduler.tasks); assertEquals(SetUtil.set(t1), SetUtil.theSet(tr.getAcceptedTasks())); assertForegroundStat(1, TaskRunner.STAT_ACCEPTED); assertForegroundStat(0, TaskRunner.STAT_REFUSED); assertForegroundStat(1, TaskRunner.STAT_WAITING); assertForegroundStat(0, TaskRunner.STAT_DROPPED); assertFalse(tr.addToSchedule(t2)); assertEquals( ListUtil.list(ListUtil.list(t1), ListUtil.list(t1, t2), ListUtil.list(t1)), fact.createArgs); assertEquals(SetUtil.set(t1), SetUtil.theSet(fact.scheduler.tasks)); assertEquals(sched, tr.getCurrentSchedule()); assertEquals(SetUtil.set(t1), SetUtil.theSet(tr.getAcceptedTasks())); assertForegroundStat(1, TaskRunner.STAT_ACCEPTED); assertForegroundStat(1, TaskRunner.STAT_REFUSED); assertForegroundStat(1, TaskRunner.STAT_WAITING); assertForegroundStat(0, TaskRunner.STAT_DROPPED); }
public void testRunStepsWithOverrunDisallowed() { StepTask t1 = task(100, 300, 100, null, new MyMockStepper(15, -10)); // t1.setOverrunAllowed(true); StepTask t2 = task(150, 250, 100, null, new MyMockStepper(10, -10)); Schedule s = sched(ListUtil.list(t1, t2)); fact.setResults(s, s); assertTrue(tr.addToSchedule(t1)); assertTrue(tr.addToSchedule(t2)); TimeBase.setSimulated(101); Interrupter intr = null; try { intr = interruptMeIn(TIMEOUT_SHOULDNT, true); while (tr.findTaskToRun()) { tr.runSteps(new MutableBoolean(true), null); } intr.cancel(); } catch (Exception e) { log.error("runSteps threw:", e); } finally { if (intr.did()) { fail("runSteps looped"); } } assertEquals(SetUtil.set(t1, t2), SetUtil.theSet(removedTasks)); assertTrue(t1.e.toString(), t1.e instanceof SchedService.Overrun); }
public void testStepperThrows() { final List finished = new ArrayList(); TaskCallback cb = new TaskCallback() { public void taskEvent(SchedulableTask task, Schedule.EventType event) { if (event == Schedule.EventType.FINISH) { finished.add(task); } } }; MyMockStepper stepper = new MyMockStepper(10, -10); stepper.setWhenToThrow(5); StepTask t1 = task(100, 200, 100, cb, stepper); Schedule s = sched(ListUtil.list(t1)); fact.setResult(s); assertTrue(tr.addToSchedule(t1)); TimeBase.setSimulated(101); assertTrue(tr.findTaskToRun()); Interrupter intr = null; try { intr = interruptMeIn(TIMEOUT_SHOULDNT, true); tr.runSteps(new MutableBoolean(true), null); intr.cancel(); } catch (Exception e) { log.error("runSteps threw:", e); } finally { if (intr.did()) { fail("runSteps looped"); } } assertSame(t1, finished.get(0)); assertTrue(t1.e instanceof ExpectedRuntimeException); assertEquals(5, stepper.nSteps); }
// ensure addToSchedule returns false if (Mock)Scheduler returns false public void testAddToScheduleFail() { fact.setResult(null); StepTask t1 = task(100, 200, 50); assertFalse(tr.addToSchedule(t1)); assertEmpty(tr.getAcceptedTasks()); assertForegroundStat(0, TaskRunner.STAT_ACCEPTED); assertForegroundStat(1, TaskRunner.STAT_REFUSED); }
// ensure addToSchedule updates structures if (Mock)Scheduler returns true public void testAddToScheduleOk() { StepTask t1 = task(100, 200, 50); StepTask t2 = task(100, 200, 100); Schedule sched = sched(ListUtil.list(t1, t2)); fact.setResult(sched); assertTrue(tr.addToSchedule(t1)); assertIsomorphic(ListUtil.list(t1), fact.scheduler.tasks); assertForegroundStat(1, TaskRunner.STAT_ACCEPTED); assertForegroundStat(0, TaskRunner.STAT_REFUSED); fact.setResult(sched); assertTrue(tr.addToSchedule(t2)); assertEquals(SetUtil.set(t1, t2), SetUtil.theSet(fact.scheduler.tasks)); assertEquals(sched, tr.getCurrentSchedule()); assertEquals(SetUtil.set(t1, t2), SetUtil.theSet(tr.getAcceptedTasks())); assertForegroundStat(2, TaskRunner.STAT_ACCEPTED); assertForegroundStat(2, TaskRunner.STAT_WAITING); assertForegroundStat(0, TaskRunner.STAT_REFUSED); }
// only one try, because no tasks to drop public void testAddToScheduleFailNothingToDrop() { ConfigurationUtil.addFromArgs(TaskRunner.PARAM_DROP_TASK_MAX, "2"); fact.setResult(null); StepTask t1 = task(100, 200, 50); assertFalse(tr.addToSchedule(t1)); assertEmpty(tr.getAcceptedTasks()); assertEquals(1, fact.createArgs.size()); assertForegroundStat(1, TaskRunner.STAT_REFUSED); assertForegroundStat(0, TaskRunner.STAT_WAITING); assertForegroundStat(0, TaskRunner.STAT_DROPPED); }
public void testRemoveChunk() { StepTask t1 = task(100, 200, 100); Schedule s = sched(ListUtil.list(t1)); fact.setResult(s); assertTrue(tr.addToSchedule(t1)); assertIsomorphic(ListUtil.list(t1), tr.getAcceptedTasks()); Schedule.Chunk chunk = (Schedule.Chunk) s.getEvents().get(0); assertTrue(tr.getCurrentSchedule().getEvents().contains(chunk)); tr.removeChunk(chunk); assertFalse(tr.getCurrentSchedule().getEvents().contains(chunk)); }
public void testFindOverrunTaskToRun() { assertFalse(tr.findTaskToRun()); StepTask t1 = task(100, 200, 100); Schedule s = sched(ListUtil.list(t1)); fact.setResult(s); assertTrue(tr.addToSchedule(t1)); assertFalse(tr.findTaskToRun()); assertEquals(Deadline.at(100), tr.runningDeadline); StepTask t2 = task(0, 300, 50); tr.addOverrunner(t2); assertTrue(tr.findTaskToRun()); assertEquals(t2, tr.runningTask); assertEquals(Deadline.at(100), tr.runningDeadline); assertNull(tr.runningChunk); }
public void testFindChunkTaskToRun() { assertFalse(tr.findTaskToRun()); StepTask t1 = task(100, 200, 100); StepTask t2 = task(100, 300, 50); Schedule s = sched(ListUtil.list(t1, t2)); fact.setResults(s, s); assertTrue(tr.addToSchedule(t1)); assertTrue(tr.addToSchedule(t2)); assertFalse(tr.findTaskToRun()); assertEquals(Deadline.at(100), tr.runningDeadline); TimeBase.setSimulated(101); assertTrue(tr.findTaskToRun()); assertEquals(t1, tr.runningTask); assertEquals(t1.getLatestFinish(), tr.runningDeadline); assertEquals(s.getEvents().get(0), tr.runningChunk); }
// one task to drop, succeeds after dropping it public void testAddToScheduleOkAfterDrops() { log.debug("testAddToScheduleOkAfterDrops()"); ConfigurationUtil.addFromArgs( TaskRunner.PARAM_DROP_TASK_MAX, "10", TaskRunner.PARAM_MIN_CLEANUP_INTERVAL, "0"); StepTask t1 = task(100, 200, 50); StepTask t2 = task(100, 200, 100); Schedule sched1 = sched(ListUtil.list(t1)); Schedule sched2 = sched(ListUtil.list(t2)); fact.setResults(ListUtil.list(sched1, null, null, sched2, sched2)); TimeBase.step(101); assertTrue(tr.addToSchedule(t1)); assertIsomorphic(ListUtil.list(t1), fact.scheduler.tasks); assertEquals(SetUtil.set(t1), SetUtil.theSet(tr.getAcceptedTasks())); assertForegroundStat(1, TaskRunner.STAT_ACCEPTED); assertForegroundStat(0, TaskRunner.STAT_REFUSED); assertForegroundStat(1, TaskRunner.STAT_WAITING); assertForegroundStat(0, TaskRunner.STAT_DROPPED); assertTrue(tr.addToSchedule(t2)); assertEquals( ListUtil.list( ListUtil.list(t1), ListUtil.list(t1, t2), ListUtil.list(t1), Collections.EMPTY_SET, ListUtil.list(t1, t2)), fact.createArgs); assertEquals(SetUtil.set(t1, t2), SetUtil.theSet(fact.scheduler.tasks)); assertTrue(t1.isDropped()); assertEquals(sched2, tr.getCurrentSchedule()); assertEquals(SetUtil.set(t1, t2), SetUtil.theSet(tr.getAcceptedTasks())); assertEquals(SetUtil.set(t1), SetUtil.theSet(tr.getOverrunTasks())); assertForegroundStat(2, TaskRunner.STAT_ACCEPTED); assertForegroundStat(0, TaskRunner.STAT_REFUSED); assertForegroundStat(1, TaskRunner.STAT_WAITING); assertForegroundStat(1, TaskRunner.STAT_DROPPED); t2.cancel(); assertForegroundStat(2, TaskRunner.STAT_ACCEPTED); assertForegroundStat(0, TaskRunner.STAT_REFUSED); assertForegroundStat(0, TaskRunner.STAT_WAITING); assertForegroundStat(1, TaskRunner.STAT_DROPPED); assertForegroundStat(1, TaskRunner.STAT_CANCELLED); }
public void testFindTaskToRunRemovesExpiredChunks() { assertFalse(tr.findTaskToRun()); StepTask t1 = task(100, 200, 100); StepTask t2 = task(100, 300, 50); StepTask texp1 = task(0, 0, 50); StepTask texp2 = task(0, 0, 50); Schedule s = sched(ListUtil.list(texp1, texp2, t1, t2)); fact.setResults(s, s); assertTrue(tr.addToSchedule(t1)); assertTrue(tr.addToSchedule(t2)); assertFalse(tr.findTaskToRun()); assertEquals(2, removedChunks.size()); assertEquals( SetUtil.set(texp1, texp2), SetUtil.set( ((Schedule.Chunk) removedChunks.get(0)).getTask(), ((Schedule.Chunk) removedChunks.get(1)).getTask())); }
public void testFindRunnableChunk() { assertFalse(tr.findTaskToRun()); StepTask t1 = task(100, 200, 100); StepTask t2 = task(10, 300, 50); Schedule.Chunk c1 = new Schedule.Chunk(t1, Deadline.at(100), Deadline.at(200), 100); Schedule.Chunk c2 = new Schedule.Chunk(t2, Deadline.at(200), Deadline.at(300), 100); Schedule s = new Schedule(ListUtil.list(c1, c2)); fact.setResults(s, s); assertTrue(tr.addToSchedule(t1)); assertTrue(tr.addToSchedule(t2)); assertFalse(tr.findTaskToRun()); assertEquals(Deadline.at(100), tr.runningDeadline); TimeBase.setSimulated(11); assertTrue(tr.findTaskToRun()); assertEquals(t2, tr.runningTask); assertEquals(c2, tr.runningChunk); assertEquals(Deadline.at(100), tr.runningDeadline); assertEquals(s.getEvents().get(1), tr.runningChunk); }
// one task not ready to start yet, so not droppable public void testAddToScheduleFailNoDroppable() { log.debug("testAddToScheduleOkAfterDrops()"); ConfigurationUtil.addFromArgs( TaskRunner.PARAM_DROP_TASK_MAX, "10", TaskRunner.PARAM_MIN_CLEANUP_INTERVAL, "0"); StepTask t1 = task(100, 200, 50); StepTask t2 = task(100, 200, 100); Schedule sched1 = sched(ListUtil.list(t1)); Schedule sched2 = sched(ListUtil.list(t2)); fact.setResults(ListUtil.list(sched1, null, null, sched2, sched2)); assertTrue(tr.addToSchedule(t1)); assertIsomorphic(ListUtil.list(t1), fact.scheduler.tasks); assertEquals(SetUtil.set(t1), SetUtil.theSet(tr.getAcceptedTasks())); assertFalse(tr.addToSchedule(t2)); assertEquals( ListUtil.list(ListUtil.list(t1), ListUtil.list(t1, t2), ListUtil.list(t1)), fact.createArgs); assertEquals(SetUtil.set(t1), SetUtil.theSet(fact.scheduler.tasks)); assertFalse(t1.isDropped()); assertEquals(sched1, tr.getCurrentSchedule()); assertEquals(SetUtil.set(t1), SetUtil.theSet(tr.getAcceptedTasks())); assertEmpty(SetUtil.theSet(tr.getOverrunTasks())); }
public void testFindTaskToRunRemovesExpiredOverrunners() { assertFalse(tr.findTaskToRun()); StepTask t1 = task(100, 200, 100); StepTask t2 = task(100, 300, 50); StepTask texp1 = task(0, 0, 50); StepTask texp2 = task(0, 0, 49); Schedule s = sched(ListUtil.list(t1, t2)); fact.setResults(s, s); assertTrue(tr.addToSchedule(t1)); assertTrue(tr.addToSchedule(t2)); tr.addOverrunner(texp1); tr.addOverrunner(texp2); // if this fails, it might be because the sorted list/set is treating // sort-order equivalence as object equality, which we don't want assertEquals(2, tr.getOverrunTasks().size()); assertFalse(tr.findTaskToRun()); assertEquals(0, removedChunks.size()); assertEquals(2, removedTasks.size()); assertEquals( SetUtil.set(texp1, texp2), SetUtil.set((StepTask) removedTasks.get(0), (StepTask) removedTasks.get(1))); }
public void testBackground() { final List rec = new ArrayList(); TaskCallback cb = new TaskCallback() { public void taskEvent(SchedulableTask task, Schedule.EventType event) { rec.add(new BERec(Deadline.in(0), (BackgroundTask) task, event)); } }; assertFalse(tr.findTaskToRun()); BackgroundTask t1 = btask(100, 200, .1, cb); BackgroundTask t2 = btask(100, 300, .2, cb); BackgroundTask t3 = btask(150, 200, .4, cb); Schedule s = sched( ListUtil.list( bEvent(t1, Schedule.EventType.START), bEvent(t2, Schedule.EventType.START), bEvent(t3, Schedule.EventType.START), bEvent(t1, Schedule.EventType.FINISH), bEvent(t3, Schedule.EventType.FINISH), bEvent(t2, Schedule.EventType.FINISH))); fact.setResults(ListUtil.list(s, s, s)); assertTrue(tr.addToSchedule(t1)); assertTrue(tr.addToSchedule(t2)); assertTrue(tr.addToSchedule(t3)); assertEquals(3, tr.getAcceptedTasks().size()); assertIsomorphic(ListUtil.list(t1, t2, t3), tr.getAcceptedTasks()); assertFalse(tr.findTaskToRun()); assertEquals(0, rec.size()); assertEquals(0, tr.getBackgroundLoadFactor(), .005); assertEquals(Deadline.at(100), tr.runningDeadline); TimeBase.setSimulated(101); assertFalse(tr.findTaskToRun()); assertEquals(2, rec.size()); assertEquals(.3, tr.getBackgroundLoadFactor(), .005); TimeBase.setSimulated(151); assertFalse(tr.findTaskToRun()); assertEquals(3, rec.size()); assertEquals(.7, tr.getBackgroundLoadFactor(), .005); assertEquals(3, tr.getAcceptedTasks().size()); TimeBase.setSimulated(201); assertFalse(tr.findTaskToRun()); assertEquals(5, rec.size()); assertEquals(.2, tr.getBackgroundLoadFactor(), .005); assertEquals(1, tr.getAcceptedTasks().size()); t2.taskIsFinished(); TimeBase.setSimulated(202); assertFalse(tr.findTaskToRun()); assertEquals(6, rec.size()); assertEquals(0, tr.getBackgroundLoadFactor(), .005); assertEquals(0, tr.getAcceptedTasks().size()); TimeBase.setSimulated(301); assertFalse(tr.findTaskToRun()); assertEquals(6, rec.size()); assertEquals(0, tr.getBackgroundLoadFactor(), .005); List exp = ListUtil.list( new BERec(101, t1, Schedule.EventType.START), new BERec(101, t2, Schedule.EventType.START), new BERec(151, t3, Schedule.EventType.START), new BERec(201, t1, Schedule.EventType.FINISH), new BERec(201, t3, Schedule.EventType.FINISH), new BERec(201, t2, Schedule.EventType.FINISH)); assertEquals(exp, rec); }