def test_requires_sequence(self): # we leave these untouched (no req. added) a1 = J(sl(0.1), label="a1") a2 = J(sl(0.1), label="a2") a3 = J(sl(0.1), label="a3") a4 = J(sl(0.1), label="a4") a5 = J(sl(0.1), label="a5") # re-create these each time to have fresh data def bs(): b1 = J(sl(0.1), label="b1") b2 = J(sl(0.1), label="b2") b3 = J(sl(0.1), label="b3") return b1, b2, b3 b1, b2, b3, *_ = bs() s1 = Seq(b1, b2, b3, required=[a1, a2]) self.assertEqual(len(b1.required), 2) self.assertEqual(len(b2.required), 1) b1, b2, b3, *_ = bs() s1 = Seq(b1, b2, b3) s1.requires([a1, a2]) self.assertEqual(len(b1.required), 2) self.assertEqual(len(b2.required), 1)
def test_sequence6(self): "adding a sequence" sched = Scheduler() a1 = J(sl(0.1), label=1) a2 = J(sl(0.1), label=2) a3 = J(sl(0.1), label=3) sched.add(Seq(a1, a2, a3)) self.assertTrue(sched.orchestrate())
def test_creation_scheduler(self): sched = Scheduler() s = Seq(J(sl(1)), J(sl(2)), scheduler=sched) j = J(sl(3), required=s, scheduler=sched) # make sure that jobs appended in the sequence # even later on are also added to the scheduler s.append(J(sl(.5))) self.assertEqual(len(sched.jobs), 4) self.assertTrue(sched.rain_check()) self.assertTrue(sched.orchestrate())
def test_timeout(self): a1 = J(sl(1), label="a1") a2 = J(sl(2), label="a2") a3 = J(sl(10), label="a3") result = Scheduler(a1, a2, a3).orchestrate(timeout=3) self.assertEqual(result, False) self.assertEqual(a1.is_done(), True) self.assertEqual(a1.result(), 1) self.assertEqual(a2.is_done(), True) self.assertEqual(a2.result(), 2) self.assertEqual(a3.is_done(), False)
def test_cycle(self): """a simple loop with 3 jobs - cannot handle that""" a1, a2, a3 = J(sl(1.1)), J(sl(1.2)), J(sl(1.3)) a1.requires(a2) a2.requires(a3) a3.requires(a1) sched = Scheduler(a1, a2, a3) # these lines seem to trigger a nasty message about a coro not being # waited self.assertFalse(sched.rain_check())
def test_forever(self): async def tick(n): while True: print('tick {}'.format(n)) await asyncio.sleep(n) a1 = J(sl(0.5), label="finite") a2 = J(tick(0.1), forever=True, label="forever") sched = Scheduler(a1, a2) result = sched.orchestrate() self.assertEqual(result, True) self.assertEqual(a1.is_done(), True) self.assertEqual(a2.is_done(), False)
def test_sequence2(self): "a job and a sequence" a1 = J(sl(0.1), label=1) a2 = J(sl(0.1), label=2) a3 = J(sl(0.1), label=3) s = Seq(a2, a3, required=a1) sched = Scheduler(a1, s) list_sep(sched, sep + "sequence2") self.assertEqual(len(a1.required), 0) self.assertEqual(len(a2.required), 1) self.assertEqual(len(a3.required), 1) self.assertTrue(check_required_types(sched, "test_sequence2")) self.assertTrue(sched.orchestrate())
def test_sequence1(self): "a simple sequence" a1 = J(sl(0.1), label=1) a2 = J(sl(0.1), label=2) a3 = J(sl(0.1), label=3) s = Seq(a1, a2, a3) sched = PureScheduler(s) list_sep(sched, common_sep + "sequence1") self.assertEqual(len(a1.required), 0) self.assertEqual(len(a2.required), 1) self.assertEqual(len(a3.required), 1) self.assertTrue(check_required_types(sched, "test_sequence1")) self.assertTrue(sched.run())
def test_sequence3(self): "a sequence and a job" a1 = J(sl(0.1), label=1) a2 = J(sl(0.1), label=2) s = Seq(a1, a2) a3 = J(sl(0.1), label=3, required=s) sched = Scheduler() sched.update((s, a3)) list_sep(sched, sep + "sequence3") self.assertEqual(len(a1.required), 0) self.assertEqual(len(a2.required), 1) self.assertEqual(len(a3.required), 1) self.assertTrue(check_required_types(sched, "test_sequence3")) self.assertTrue(sched.orchestrate())
def test_sequence4(self): "a sequence of 2 sequences" a1 = J(sl(0.1), label=1) a2 = J(sl(0.1), label=2) a3 = J(sl(0.1), label=3) a4 = J(sl(0.1), label=4) s1 = Seq(a1, a2) s2 = Seq(a3, a4) sched = Scheduler(Seq(s1, s2)) list_sep(sched, sep + "sequence4") self.assertEqual(len(a1.required), 0) self.assertEqual(len(a2.required), 1) self.assertEqual(len(a3.required), 1) self.assertEqual(len(a4.required), 1) self.assertTrue(check_required_types(sched, "test_sequence4")) self.assertTrue(sched.orchestrate())
def _test_exc_non_critical(self, verbose): print("verbose = {}".format(verbose)) a1, a2 = SLJ(1), J(co_exception(0.5), label='non critical boom') sched = Scheduler(a1, a2, verbose=verbose) self.assertTrue(sched.orchestrate()) print(sep + 'debrief()') sched.debrief()
def _test_exc_critical(self, verbose): print("verbose = {}".format(verbose)) a1, a2 = SLJ(1), J(co_exception(0.5), label='critical boom', critical=True) sched = PureScheduler(a1, a2, verbose=verbose) self.assertFalse(sched.run()) print(common_sep + 'debrief()') sched.debrief()
def test_simple(self): """a simple topology, that should work""" jobs = SLJ(0.1), SLJ(0.2), SLJ(0.3), SLJ( 0.4), SLJ(0.5), J(sl(0.6)), J(sl(0.7)) a1, a2, a3, a4, a5, a6, a7 = jobs a4.requires(a1, a2, a3) a5.requires(a4) a6.requires(a4) a7.requires(a5) a7.requires(a6) sched = Scheduler(*jobs) list_sep(sched, sep + "LIST BEFORE") self.assertTrue(sched.rain_check()) self.assertTrue(sched.orchestrate(loop=asyncio.get_event_loop())) for j in jobs: self.assertFalse(j.raised_exception()) list_sep(sched, sep + "LIST AFTER") print(sep + "DEBRIEF") sched.debrief()
def test_display(self): class FakeTask: def __init__(self): self._result = 0 self._exception = None def annotate_job_with_fake_task(job, state, boom): task = FakeTask() if state == "done": task._state = asyncio.futures._FINISHED job._task = task job._running = True elif state == "running": task._state = "NONE" job._task = task job._running = True elif state == "scheduled": task._state = "NONE" job._task = task job._running = False else: pass # here we assume that a job that has raised an exception is # necessarily done if boom: if state in ("idle", "scheduled", "running"): print("incompatible combination boom x idle - ignored") return else: job._task._exception = True return job class J(AbstractJob): pass sched = Scheduler() previous = None for state in "idle", "scheduled", "running", "done": for boom in True, False: for critical in True, False: for forever in True, False: j = J(critical=critical, forever=forever, label="forever={} crit.={} status={} boom={}" .format(forever, critical, state, boom), required=previous ) if annotate_job_with_fake_task(j, state, boom): sched.add(j) previous = j sched.list()
def test_simple(self): """a simple topology, that should work""" jobs = SLJ(0.1), SLJ(0.2), SLJ(0.3), SLJ(0.4), SLJ(0.5), J(sl(0.6)), J( sl(0.7)) a1, a2, a3, a4, a5, a6, a7 = jobs a4.requires(a1, a2, a3) a5.requires(a4) a6.requires(a4) a7.requires(a5) a7.requires(a6) sched = PureScheduler(*jobs) list_sep(sched, common_sep + "LIST BEFORE") self.assertTrue(sched.check_cycles()) self.assertTrue(sched.run()) for j in jobs: self.assertFalse(j.raised_exception()) list_sep(sched, common_sep + "LIST AFTER") print(common_sep + "DEBRIEF") sched.debrief()
def test_sequence5(self): "sequences with required" a1 = J(sl(0.1), label=1) a2 = J(sl(0.1), label=2) a3 = J(sl(0.1), label=3) a4 = J(sl(0.1), label=4) a5 = J(sl(0.1), label=5) a6 = J(sl(0.1), label=6) s1 = Seq(a1, a2) s2 = Seq(a3, a4, required=s1) s3 = Seq(a5, a6, required=s2) sched = Scheduler(s1, s2, s3) list_sep(sched, sep + "sequence5") self.assertEqual(len(a1.required), 0) self.assertEqual(len(a2.required), 1) self.assertEqual(len(a3.required), 1) self.assertEqual(len(a4.required), 1) self.assertEqual(len(a5.required), 1) self.assertEqual(len(a6.required), 1) self.assertTrue(check_required_types(sched, "test_sequence5")) self.assertTrue(sched.orchestrate())
def test_loop(self): s = Scheduler() Seq(J(sl(.1)), J(sl(.2)), scheduler=s) loop = asyncio.get_event_loop() self.assertTrue(s.orchestrate(loop=loop))
def test_requires_job(self): a1 = J(sl(0.1), label="a1") a2 = J(sl(0.1), label="a2") a3 = J(sl(0.1), label="a3") a4 = J(sl(0.1), label="a4") a5 = J(sl(0.1), label="a5") # several forms to create b = J(sl(0.2), required=None) self.assertEqual(len(b.required), 0) b = J(sl(0.2), required=(None,)) self.assertEqual(len(b.required), 0) b = J(sl(0.2), required=[None]) self.assertEqual(len(b.required), 0) b = J(sl(0.2), required=a1) self.assertEqual(len(b.required), 1) b = J(sl(0.2), required=(a1,)) self.assertEqual(len(b.required), 1) b = J(sl(0.2), required=[a1]) self.assertEqual(len(b.required), 1) b = J(sl(0.2), label='BROKEN', required=(a1, a2)) self.assertEqual(len(b.required), 2) b = J(sl(0.2), required=[a1, a2]) self.assertEqual(len(b.required), 2) b = J(sl(0.2), required=[a1, (a2,), set([a3, a4]), [[[[[[a5]]]]]]]) self.assertEqual(len(b.required), 5)
def test_seq(self): s = PureScheduler() Seq(J(sl(.1)), J(sl(.2)), scheduler=s) self.assertTrue(s.run())
def bs(): b1 = J(sl(0.1), label="b1") b2 = J(sl(0.1), label="b2") b3 = J(sl(0.1), label="b3") return b1, b2, b3