Exemplo n.º 1
0
 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()
Exemplo n.º 2
0
    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())
Exemplo n.º 3
0
 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())
Exemplo n.º 4
0
    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))
Exemplo n.º 5
0
  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")
Exemplo n.º 6
0
 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))