def test_tasks_with_dependencies(self): # Given s = self._make_scheduler() cmd = 'python -c "import time; print(time.time())"' ct1_dir = os.path.join(self.sim_dir, '1') ct2_dir = os.path.join(self.sim_dir, '2') ct3_dir = os.path.join(self.sim_dir, '3') ct1 = CommandTask(cmd, output_dir=ct1_dir) ct2 = CommandTask(cmd, output_dir=ct2_dir, depends=[ct1]) ct3 = CommandTask(cmd, output_dir=ct3_dir, depends=[ct1, ct2]) # When t = TaskRunner(tasks=[ct1, ct2, ct3], scheduler=s) # Then self.assertEqual(len(t.todo), 3) # When t.run(wait=0.1) wait_until(lambda: not ct3.complete()) # Then. # Ensure that the tasks are run in the right order. ct1_t, ct2_t, ct3_t = [ self._get_time(x) for x in (ct1_dir, ct2_dir, ct3_dir) ] self.assertTrue(ct2_t > ct1_t) self.assertTrue(ct3_t > ct2_t)
def test_command_tasks_handles_errors_correctly(self): # Given s = self._make_scheduler() cmd = 'python --junk' t = CommandTask(cmd, output_dir=self.sim_dir) self.assertFalse(t.complete()) # When t.run(s) try: wait_until(lambda: not t.complete()) except RuntimeError: pass # Then self.assertFalse(t.complete()) self.assertEqual(t.job_proxy.status(), 'error') # A new command task should still detect that the run failed, even # though the output directory exists. # Given t = CommandTask(cmd, output_dir=self.sim_dir) # When/Then self.assertFalse(t.complete())
def test_automates_tasks_and_problems(self, mock_run): # Given a = Automator('sim', 'output', [EllipticalDrop]) cmd = 'python -c "print(1)"' task = CommandTask(cmd, output_dir=self.sim_dir) a.add_task(task) dir2 = os.path.join(self.sim_dir, '2') task2 = CommandTask(cmd, output_dir=dir2) a.add_task(task2, post_proc=True) # When a.run([]) # Then self.assertEqual(len(a.runner.todo), 6) expect = [ 'RunAll', 'SolveProblem', 'PySPHTask', 'PySPHTask', 'CommandTask', 'CommandTask' ] names = [x.__class__.__name__ for x in a.runner.todo] self.assertEqual(names, expect) self.assertEqual(a.runner.todo[-2], task) self.assertEqual(a.runner.todo[-1], task2) self.assertEqual(task2.depends, [a.runner.todo[0]])
def test_task_runner_checks_for_error_in_running_tasks(self, m_t_cores): # Given s = self._make_scheduler() cmd = 'python -c "import sys, time; time.sleep(0.1); sys.exit(1)"' ct1_dir = os.path.join(self.sim_dir, '1') ct2_dir = os.path.join(self.sim_dir, '2') ct3_dir = os.path.join(self.sim_dir, '3') job_info = dict(n_core=2, n_thread=2) ct1 = CommandTask(cmd, output_dir=ct1_dir, job_info=job_info) ct2 = CommandTask(cmd, output_dir=ct2_dir, job_info=job_info) ct3 = CommandTask(cmd, output_dir=ct3_dir, job_info=job_info) # When t = TaskRunner(tasks=[ct1, ct2, ct3], scheduler=s) # Then self.assertEqual(len(t.todo), 3) # When n_errors = t.run(wait=0.1) # Then # In this case, two tasks should have run and one should not have run # as the other two had errors. self.assertEqual(n_errors, 2) self.assertEqual(len(t.todo), 1) self.assertTrue(os.path.exists(ct3_dir)) self.assertTrue(os.path.exists(ct2_dir)) self.assertFalse(os.path.exists(ct1_dir))
def test_task_runner_does_not_add_repeated_tasks(self): # Given s = self._make_scheduler() cmd = 'python -c "print(1)"' ct1 = CommandTask(cmd, output_dir=self.sim_dir) ct2 = CommandTask(cmd, output_dir=self.sim_dir) # When t = TaskRunner(tasks=[ct1, ct2, ct1], scheduler=s) # Then self.assertEqual(len(t.todo), 1)
def test_task_runner_waits_for_tasks_in_the_end(self, m_t_cores): # Given s = self._make_scheduler() cmd = 'python -c "import sys, time; time.sleep(0.1); sys.exit(1)"' ct1_dir = os.path.join(self.sim_dir, '1') ct2_dir = os.path.join(self.sim_dir, '2') ct3_dir = os.path.join(self.sim_dir, '3') ct1 = CommandTask(cmd, output_dir=ct1_dir) ct2 = CommandTask(cmd, output_dir=ct2_dir) ct3 = CommandTask(cmd, output_dir=ct3_dir) # When t = TaskRunner(tasks=[ct1, ct2, ct3], scheduler=s) n_errors = t.run(wait=0.1) # Then # All the tasks may have been run but those that ran will fail. self.assertEqual(n_errors + len(t.todo), 3) self.assertTrue(n_errors > 0)
def test_automates_only_tasks(self, mock_run): # Given a = Automator('sim', 'output', []) cmd = 'python -c "print(1)"' task = CommandTask(cmd, output_dir=self.sim_dir) a.add_task(task) # When a.run([]) # Then mock_run.assert_called_with() self.assertEqual(len(a.runner.todo), 1) self.assertEqual(a.runner.todo[-1], task) # Given a = Automator('sim', 'output', []) cmd = 'python -c "print(1)"' task = CommandTask(cmd, output_dir=self.sim_dir) a.add_task(task, name='task') # When a.run([]) # Then mock_run.assert_called_with() self.assertEqual(len(a.runner.todo), 0) # Given a = Automator('sim', 'output', []) cmd = 'python -c "print(1)"' task = CommandTask(cmd, output_dir=self.sim_dir) a.add_task(task, name='task') # When a.run(['task']) # Then mock_run.assert_called_with() self.assertEqual(len(a.runner.todo), 1) self.assertEqual(a.runner.todo[-1], task)
def test_command_tasks_converts_dollar_output_dir(self): # Given s = self._make_scheduler() cmd = '''python -c "print('$output_dir')"''' t = CommandTask(cmd, output_dir=self.sim_dir) self.assertFalse(t.complete()) # When t.run(s) wait_until(lambda: not t.complete()) # Then self.assertTrue(t.complete()) self.assertEqual(t.job_proxy.status(), 'done') self.assertEqual(t.job_proxy.get_stdout().strip(), self.sim_dir)
def test_remote_command_tasks_complete_method_works(self): # Given s = self._make_scheduler() cmd = 'python -c "print(1)"' t = CommandTask(cmd, output_dir=self.sim_dir) self.assertFalse(t.complete()) self.assertFalse( os.path.exists(os.path.join(self.sim_dir, 'stdout.txt'))) # When t.run(s) wait_until(lambda: not t.complete()) # Then self.assertTrue(t.complete()) self.assertTrue( os.path.exists(os.path.join(self.sim_dir, 'stdout.txt'))) # Test that if we call it repeatedly that it does indeed return True self.assertTrue(t.complete())
def get_requires(self): cmd = 'python -c "print(1)"' # Can return tasks ... ct = CommandTask(cmd, output_dir=self.sim_dir) return [('task1', ct)]
def get_requires(self): cmd = ('python -c "import sys, time; time.sleep(0.1); ' 'sys.exit(1)"') ct = CommandTask(cmd, output_dir=self.input_path()) return [('task1', ct)]