def test_basic(self): num_tasks = 100000 tasks = [] task_types = [TaskType1, TaskType2, TaskType3] time0 = time.time() for i in range(num_tasks): p = random.randint(0, 10) r = p % 3 if i < 5: args = (i, i * p, p) if r == 0 else (i, i * 2) else: r0 = random.randint(0, i - 1) r1 = random.randint(0, i - 1) if r == 0: args = (i, TaskResult(r0), TaskResult(r1)) else: args = (TaskResult(r0), TaskResult(r1)) tasks.append(SimpleTask(name=i, action=task_types[r], args=args)) time1 = time.time() task_processor = FiniteThreadTaskProcessor(num_threads=5) task_runner = TaskRunner(tasks=tasks, target_tasks=tasks, task_processor=task_processor) task_runner.run() time2 = time.time() print("Successful tasks = ", len(task_runner.succeeded_tasks)) print("Time taken = ", (time2 - time1), " seconds")
def test_basic(self): mul2 = lambda x: (time.sleep(1), x * 2)[-1] sum_n = lambda *args: (time.sleep(0.1), sum(args))[-1] sum_n_plus_x = lambda *args, x=10: (time.sleep(1), sum(args) + x)[-1] t1 = SimpleTask("task1", action=mul2, args=(10, )) t2 = SimpleTask("task2", action=mul2, args=(20, )) t3 = SimpleTask("task3", action=mul2, args=(30, )) t4 = SimpleTask("task4", action=mul2, args=(40, )) t5 = SimpleTask("task5", action=mul2, args=(50, )) t6 = SimpleTask("task6", action=sum_n, args=(TaskResult("task1"), TaskResult("task2"), TaskResult("task3"), TaskResult("task5"))) t7 = SimpleTask("task7", action=sum_n_plus_x, args=(TaskResult("task1"), TaskResult("task5")), kwargs=dict(x=100)) t8 = SimpleTask("task8", action=sum_n_plus_x, args=(TaskResult("task3"), TaskResult("task6"), TaskResult("task5"))) task_processor = FiniteThreadTaskProcessor(num_threads=2, daemon_thread=False) task_runner = TaskRunner(tasks=[t1, t2, t3, t4, t5, t6, t7, t8], target_tasks=[t7, t8], task_processor=task_processor) task_runner.run() task_runner.printSuccessSummary() task_runner.printErrorSummary() self.assertEqual(220, task_runner.getTask("task7").getResult()) self.assertEqual(390, task_runner.getTask("task8").getResult()) task_processor.close()
def test_basic(self): output_file = "/tmp/taskon_sandwitch.txt" cheese_file = "/tmp/taskon_cheese.txt" t1 = BashCommandTask(name="make_sandwitch", command=lambda onion: "echo %s > %s" % (onion, output_file), args=(TaskResult("buy_onion"), )) t2 = SimpleTask(name="buy_onion", action=lambda: "Onion") t3 = SimpleTask(name="make_cheese_sandwitch", action=sample_tasks.makeCheeseSandwitch, args=("bread", "cheese", "onion", 18)) t4 = BashCommandTask(name="buy_cheese", command="echo Cheese > %s" % cheese_file) task_processor = FiniteThreadTaskProcessor(num_threads=5) task_runner = TaskRunner(tasks=[t1, t2, t3, t4], target_tasks=[t1, t3, t4], task_processor=task_processor) task_runner.run() task_runner.printSuccessSummary() task_runner.printErrorSummary() self.assertEqual(4, len(task_runner.succeeded_tasks)) self.assertEqual("Onion\n", readFile(output_file)) self.assertEqual("Cheese\n", readFile(cheese_file)) for task in task_runner.succeeded_tasks: self.assertEqual(TaskStatus.SUCCESS, task.getStatus())
def test_error_case(self, name, task_processor): t1 = SimpleTask(name="make_sandwitch", action=sample_tasks.makeSandwitch, args=(TaskResult("make_bread"), TaskResult("buy_onion"), 4)) t2 = SimpleTask(name="make_bread", action=sample_tasks.makeFaultyBread, args=("flour", )) t3 = SimpleTask(name="buy_onion", action=sample_tasks.buyOnion) task_runner = TaskRunner(tasks=[t1, t2, t3], target_tasks=[t1], task_processor=task_processor) task_runner.run(continue_on_failure=True) self.assertEqual(TaskStatus.FAILURE, task_runner.getTask("make_bread").getStatus()) e_error1 = 'ZeroDivisionError: division by zero' e_error2 = 'sample_tasks.py", line [0-9]+, in makeFaultyBread' o_error = task_runner.getTask("make_bread").getError() self.assertTrue(e_error1 in task_runner.getErrorSummaryString()) self.assertTrue(e_error1 in o_error) self.assertTrue(re.search(e_error2, o_error) is not None, o_error) self.assertEqual(TaskStatus.SUCCESS, task_runner.getTask("buy_onion").getStatus()) self.assertEqual("Onion", task_runner.getTask("buy_onion").getResult()) self.assertEqual(TaskStatus.SKIPPED, task_runner.getTask("make_sandwitch").getStatus()) name_list = lambda task_list: list(x.name for x in task_list) self.assertEqual(["make_bread"], name_list(task_runner.failed_tasks)) self.assertEqual(["buy_onion"], name_list(task_runner.succeeded_tasks)) self.assertEqual(["make_sandwitch"], name_list(task_runner.skipped_tasks))
def test_error_case(self): mul2 = lambda x: (time.sleep(1), x * 2)[-1] sum_n = lambda *args: (time.sleep(0.2), sum(args))[-1] sum_n_plus_x = lambda *args, x=10: (time.sleep(1), sum(args) + x)[-1] t0 = SimpleTask("task0", action=lambda x: x * 2, args=(5, )) t1 = SimpleTask("task1", action=mul2, args=(10, )) t2 = BashCommandTask("task2", command="sleep 20s", result=40) t3 = SimpleTask("task3", action=mul2, args=(30, )) t4 = SimpleTask(name="task4", action=lambda x: (time.sleep(0.5), 1 / 0), args=(TaskResult("task0"), )) t5 = SimpleTask("task5", action=mul2, args=(50, )) t6 = SimpleTask("task6", action=sum_n, args=(TaskResult("task1"), TaskResult("task2"), TaskResult("task5"))) t7 = SimpleTask("task7", action=sum_n_plus_x, args=(TaskResult("task1"), TaskResult("task5")), kwargs=dict(x=100)) t8 = SimpleTask("task8", action=sum_n_plus_x, args=(TaskResult("task2"), TaskResult("task6"), TaskResult("task4"), TaskResult("task5"))) task_processor = FiniteThreadTaskProcessor(num_threads=10) task_runner = TaskRunner(tasks=[t0, t1, t2, t3, t4, t5, t6, t7, t8], target_tasks=[t7, t8], task_processor=task_processor) task_runner.run() self.assertEqual(TaskStatus.SUCCESS, task_runner.getTask("task0").getStatus()) self.assertEqual(10, task_runner.getTask("task0").getResult()) self.assertEqual(TaskStatus.FAILURE, task_runner.getTask("task4").getStatus()) error = "ZeroDivisionError: division by zero" self.assertTrue(error in task_runner.getTask("task4").getError()) self.assertEqual(TaskStatus.SKIPPED, task_runner.getTask("task8").getStatus())
def test_CyclicDependencyError(self): t1 = SimpleTask(name="make_money", action=sample_tasks.makeMoney, args=(TaskResult("make_sandwitch"), )) t2 = SimpleTask(name="make_sandwitch", action=sample_tasks.makeSandwitch, args=(TaskResult("make_bread"), TaskResult("buy_onion"), 10)) t3 = SimpleTask(name="buy_onion", action=sample_tasks.buyGoodOnion, args=(TaskResult("make_money"), )) t4 = SimpleTask(name="make_bread", action=sample_tasks.makeBread, args=("flour", )) with self.assertRaises(TaskonFatalError) as context: task_runner = TaskRunner(tasks=[t1, t2, t3, t4], target_tasks=[t1], task_processor=self.task_processor) expected_error = "Cyclic dependency in tasks: make_money -> "\ "make_sandwitch -> buy_onion -> make_money" self.assertTrue(expected_error in str(context.exception))
def test_main(self, name, task_processor): t1 = SimpleTask(name="make_sandwitch", action=sample_tasks.makeSandwitch, args=(TaskResult("make_bread"), TaskResult("buy_onion"), 4)) t2 = SimpleTask(name="make_bread", action=sample_tasks.makeBread, args=("flour", )) t3 = SimpleTask(name="buy_onion", action=sample_tasks.buyOnion) task_runner = TaskRunner(tasks=[t1, t2, t3], target_tasks=[t1], task_processor=task_processor) task_runner.run() self.assertEqual(TaskStatus.SUCCESS, task_runner.getTask("make_sandwitch").getStatus()) self.assertEqual("Onion-Sandwitch", task_runner.getTask("make_sandwitch").getResult())
print("Cutting " + onion) print("Grilling " + bread + " for " + str(grill_duration) + " minutes") return onion + "-Sandwitch" def MakeBread(flour): print("Processing " + flour) return "Bread" def BuyOnion(): return "Onion" t1 = SimpleTask(name = "make_bread", action = MakeBread, args=("flour",)) t2 = SimpleTask(name = "make_sandwitch", action = MakeSandwitch, args = (TaskResult("make_bread"), TaskResult("buy_onion"), 4)) t3 = SimpleTask(name = "buy_onion", action = BuyOnion) task_processor = FiniteThreadTaskProcessor(num_threads=6) task_runner = TaskRunner(tasks = [t1, t2, t3], target_tasks = [t2], task_processor = task_processor) task_runner.run() assert task_runner.getTask("make_sandwitch").getStatus() == TaskStatus.SUCCESS assert task_runner.getTask("make_sandwitch").getResult() == "Onion-Sandwitch" print("Done")
def test_InvalidTasks(self): action = lambda: None with self.assertRaises(TaskonFatalError) as context: task_runner = TaskRunner(tasks=[ SimpleTask("task1", action=action), SimpleTask("task1", action=action) ], task_processor=self.task_processor) error = "Found multiple tasks with name 'task1'" self.assertTrue(error in str(context.exception)) with self.assertRaises(TaskonFatalError) as context: task_runner = TaskRunner(tasks=[ SimpleTask("task1", action=action, args=(TaskResult("task2"), )) ], task_processor=self.task_processor) error = "Invalid task name 'task2' used in the TaskResult of task "\ "'task1'." self.assertEqual(error, str(context.exception)) t1 = SimpleTask("task1", action=action) t2 = SimpleTask("task2", action=action) t3 = SimpleTask("task2", action=action) with self.assertRaises(TaskonFatalError) as context: task_runner = TaskRunner(tasks=[t1, t2], target_tasks=[t2, t3], task_processor=self.task_processor) error = "Invalid target task 'task2'. @target_tasks must also be "\ "present in @tasks" self.assertEqual(error, str(context.exception)) with self.assertRaises(TaskonFatalError) as context: task_runner = TaskRunner(tasks=[ SimpleTask(name="task1", action=action, args=10, kwargs=dict(a=5)) ], task_processor=self.task_processor) error = "Task 'task1' have invalid value for args field, it should "\ "be a tuple." self.assertEqual(error, str(context.exception)) with self.assertRaises(TaskonFatalError) as context: task_runner = TaskRunner(tasks=[ SimpleTask(name="task1", action=action, args=(10, 20), kwargs=10) ], task_processor=self.task_processor) error = "Task 'task1' have invalid value for kwargs field, it should "\ "be a dictionary." self.assertEqual(error, str(context.exception)) task_runner = TaskRunner(tasks=[ SimpleTask("task1", action=action), SimpleTask("task2", action=action) ], task_processor=self.task_processor) self.assertEqual("task2", task_runner.getTask("task2").name) with self.assertRaises(TaskonError) as context: task_runner.getTask("task3") self.assertEqual("Invalid task 'task3'", str(context.exception))