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): 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): 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 getTestParameters(): return [["naive_task_processor", NaiveTaskProcessor()], [ "finite_thread_task_processor", FiniteThreadTaskProcessor(num_threads=4) ], ["infinite_thread_task_processor", InfiniteThreadTaskProcessor()]]
def test_error_case(self): t1 = SimpleTask(name="make_sandwitch", action=sample_tasks.makeCheeseSandwitch, args=(TaskResult("make_bread"), TaskResult("buy_cheese"), TaskResult("buy_onion"), 4)) t2 = SimpleTask(name="make_bread", action=sample_tasks.makeFaultyBreadSleep1, args=("flour", )) t3 = SimpleTask(name="buy_onion", action=sample_tasks.buyOnionSleep2) t4 = SimpleTask(name="buy_cheese", action=sample_tasks.buyCheese) task_processor = FiniteThreadTaskProcessor(num_threads=5) task_runner = TaskRunner(tasks=[t1, t2, t3, t4], target_tasks=[t1], task_processor=task_processor) task_runner.run() 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 o_error) self.assertTrue(re.search(e_error2, o_error) is not None, o_error) self.assertEqual(TaskStatus.SUCCESS, task_runner.getTask("buy_cheese").getStatus()) self.assertEqual("Cheese", task_runner.getTask("buy_cheese").getResult()) self.assertEqual(TaskStatus.SKIPPED, task_runner.getTask("buy_onion").getStatus()) 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_cheese"], name_list(task_runner.succeeded_tasks)) self.assertEqual(["make_sandwitch", "buy_onion"], name_list(task_runner.skipped_tasks)) task_runner.run(continue_on_failure=True) self.assertEqual(["make_bread"], name_list(task_runner.failed_tasks)) self.assertEqual(set(["buy_cheese", "buy_onion"]), set(name_list(task_runner.succeeded_tasks))) self.assertEqual(["make_sandwitch"], name_list(task_runner.skipped_tasks)) self.assertEqual("Onion", task_runner.getTask("buy_onion").getResult())
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())
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 setUp(self): self.task_processor = FiniteThreadTaskProcessor(num_threads=5)