예제 #1
0
 def setUp(self):
     self.taskpile = Taskpile()
예제 #2
0
class TestTaskpile(object):
    def setUp(self):
        self.taskpile = Taskpile()

    @staticmethod
    def _create_mocktask_in_state(state):

        task = MagicMock(spec=Task)
        task.state = state

        def start():
            task.state = State.RUNNING

        task.start.side_effect = start
        return task

    def test_can_add_task(self):
        task = Task(noop)
        self.taskpile.enqueue(task)
        assert_that(self.taskpile.pending, contains(task))

    def test_start_up_to_max_parallel_tasks_on(self):
        num_more = 2
        tasks = [self._create_mocktask_in_state(State.PENDING)
                 for i in range(self.taskpile.max_parallel + num_more)]
        for task in tasks:
            self.taskpile.enqueue(task)
        self.taskpile.update()
        for task in tasks[:-num_more]:
            task.start.assert_called_once_with()
        for task in tasks[-num_more:]:
            assert_that(task.start.called, is_(False))

    def test_starts_all_tasks_if_less_then_max(self):
        tasks = [self._create_mocktask_in_state(State.PENDING)
                 for i in range(max(1, self.taskpile.max_parallel - 1))]
        for task in tasks:
            self.taskpile.enqueue(task)
        self.taskpile.update()
        for task in tasks:
            task.start.assert_called_once_with()

    def test_starts_tasks_later_added_but_at_most_max_parallel(self):
        num_more = 2
        tasks = [self._create_mocktask_in_state(State.PENDING)
                 for i in range(self.taskpile.max_parallel + num_more)]
        self.taskpile.enqueue(tasks[0])
        self.taskpile.update()
        for task in tasks[1:]:
            self.taskpile.enqueue(task)
        self.taskpile.update()
        for task in tasks[:-num_more]:
            task.start.assert_called_once_with()
        for task in tasks[-num_more:]:
            assert_that(task.start.called, is_(False))

    def test_stop_newest_process_on_reducing_max_parallel(self):
        self.taskpile.max_parallel = 2
        tasks = [self._create_mocktask_in_state(State.PENDING)
                 for i in range(2)]
        for task in tasks:
            self.taskpile.enqueue(task)
        self.taskpile.update()
        self.taskpile.max_parallel = 1
        self.taskpile.update()
        tasks[1].stop.assert_called_once_with()
        assert_that(tasks[0].stop.called, is_(False))

    def test_continues_an_already_started_process(self):
        task = self._create_mocktask_in_state(State.STOPPED)
        self.taskpile.enqueue(task)
        self.taskpile.update()
        assert_that(task.start.called, is_(False))
        task.cont.assert_called_once_with()

    def test_update_sorts_queues(self):
        pending_tasks = [self._create_mocktask_in_state(State.PENDING)
                         for i in range(2)]
        running_tasks = [self._create_mocktask_in_state(State.RUNNING)
                         for i in range(2)]
        stopped_tasks = [self._create_mocktask_in_state(State.STOPPED)
                         for i in range(2)]
        finished_tasks = [self._create_mocktask_in_state(State.FINISHED)
                          for i in range(2)]
        self.taskpile.pending = [pending_tasks[0], running_tasks[0],
                                 stopped_tasks[0], finished_tasks[0]]
        self.taskpile.running = [pending_tasks[1], running_tasks[1],
                                 stopped_tasks[1], finished_tasks[1]]
        self.taskpile.max_parallel = len(running_tasks)
        self.taskpile.update()

        assert_that(
            self.taskpile.pending, contains(*stopped_tasks + pending_tasks))
        assert_that(self.taskpile.running, contains(*running_tasks))
        assert_that(self.taskpile.finished, contains(*finished_tasks))

    def test_joins_finished_tasks(self):
        task = self._create_mocktask_in_state(State.FINISHED)
        self.taskpile.running = [task]
        self.taskpile.update()
        task.join.assert_called_once_with()