예제 #1
0
    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())
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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())
예제 #5
0
    def test_file_command_tasks_works(self):
        # Given
        s = self._make_scheduler()
        pth = os.path.join(self.sim_dir, 'output.txt')
        cmd = 'python -c "from pathlib import Path; Path(%r).touch()"' % pth
        t = FileCommandTask(cmd, files=[pth])

        self.assertFalse(t.complete())

        # When
        t.run(s)
        wait_until(lambda: not t.complete())

        # Then
        self.assertTrue(t.complete())
        output_dir = pth + '.job_info'
        self.assertEqual(t.output_dir, output_dir)
        self.assertTrue(os.path.exists(t.output_dir))
        self.assertEqual(t.job_proxy.status(), 'done')
        self.assertTrue(os.path.exists(pth))
예제 #6
0
    def test_simulation_with_dependencies(self):
        # Given
        class A(Problem):
            def setup(self):
                cmd = 'python -c "import time; print(time.time())"'
                s1 = Simulation(self.input_path('1'), cmd)
                s2 = Simulation(self.input_path('2'), cmd, depends=[s1])
                s3 = Simulation(self.input_path('3'), cmd, depends=[s1, s2])
                self.cases = [s1, s2, s3]

            def run(self):
                self.make_output_dir()

        s = self._make_scheduler()

        # When
        problem = A(self.sim_dir, self.output_dir)
        task = SolveProblem(problem)
        t = TaskRunner(tasks=[task], scheduler=s)

        # Then
        self.assertEqual(len(t.todo), 4)
        # Basically only one instance of CommandTask should be created.
        names = [x.__class__.__name__ for x in t.todo]
        self.assertEqual(names.count('CommandTask'), 3)
        self.assertEqual(names.count('SolveProblem'), 1)

        # When
        t.run(wait=0.1)
        wait_until(lambda: not task.complete())

        # Then
        ct1_t, ct2_t, ct3_t = [
            self._get_time(problem.input_path(x)) for x in ('1', '2', '3')
        ]
        self.assertTrue(ct2_t > ct1_t)
        self.assertTrue(ct3_t > ct2_t)