def test_finishTasksAfterAssigningTasksToWorkers(self):
        workers = [
            Worker(workerID="W1",
                   begin=tHour(0, 00),
                   end=tHour(23, 59),
                   multitask=1),
            Worker(workerID="W2",
                   begin=tHour(0, 00),
                   end=tHour(23, 59),
                   multitask=1),
            Worker(workerID="W3",
                   begin=tHour(0, 00),
                   end=tHour(23, 59),
                   multitask=1)
        ]
        taskManager = TaskManager(period=self.tPeriod(), multitask=1)
        taskManager.addTask(self.tasks[0])
        taskManager.addTask(self.tasks[1])
        taskManager.addWorkers(workers)
        taskManager.assignTasksToWorkers()
        self.assertEqual(len(taskManager.workers), 3)
        taskManager.finishTask(self.tasks[0].taskID)
        self.assertEqual(len(workers[0].assignedTasks), 0)
        self.assertEqual(len(taskManager.workers), 3)

        taskManager.finishTask(self.tasks[1].taskID)  # test with id too.
        self.assertEqual(len(workers[1].assignedTasks), 0)
        self.assertEqual(len(taskManager.workers), 3)
 def test_finishTasks(self):
     taskManager = TaskManager(period=self.tPeriod(), multitask=1)
     taskManager.addTask(self.tasks[0])
     taskManager.addTask(self.tasks[1])
     self.assertEqual(len(taskManager.taskSet.tasks), 2)
     taskManager.finishTask(self.tasks[0].taskID)
     self.assertEqual(len(taskManager.taskSet.tasks), 1)
     taskManager.finishTask(self.tasks[1].taskID)
     self.assertEqual(len(taskManager.taskSet.tasks), 0)
 def test_taskAfterDeadline(self):
     task = TaskUnit(createdAt=tHour(0, 0),
                     deadline=tHour(21, 1),
                     profit=0,
                     processing=0,
                     release=tHour(16, 25),
                     taskID="t1")
     taskManager = TaskManager(period=self.tPeriod(), multitask=1)
     with self.assertRaises(LateDeadlineException):
         taskManager.addTask(task)
    def test_assignTaskToWorkersThatNeedsToBeSwapped(self, isProcessing):
        # mock task unit so its isProcessing method always returns true
        # as it is time dependent, and will fail if this test if run at certain times
        isProcessing.return_value = False

        task1 = TaskUnit(createdAt=tHour(0, 0),
                         deadline=tHour(10, 00),
                         profit=0,
                         processing=0,
                         release=tHour(9, 30),
                         taskID="t1")
        task2 = TaskUnit(createdAt=tHour(0, 0),
                         deadline=tHour(11, 00),
                         profit=0,
                         processing=0,
                         release=tHour(9, 40),
                         taskID="t2")
        task3 = TaskUnit(createdAt=tHour(0, 0),
                         deadline=tHour(12, 00),
                         profit=0,
                         processing=0,
                         release=tHour(11, 30),
                         taskID="t3")
        workers = [
            Worker(workerID="W4",
                   begin=tHour(0, 00),
                   end=tHour(23, 59),
                   multitask=1),
            Worker(workerID="W1",
                   begin=tHour(0, 00),
                   end=tHour(23, 59),
                   multitask=1)
        ]
        taskManager = TaskManager(period=self.tPeriod(), multitask=1)
        taskManager.addTask(task2)
        taskManager.addTask(task3)
        taskManager.addWorkers(workers)
        taskManager.assignTasksToWorkers()
        self.assertEqual(task2, workers[0].assignedTasks[0])
        self.assertEqual(task3, workers[1].assignedTasks[0])
        taskManager.addTask(
            task1)  # earlier release, therfore should be swapped.
        taskManager.assignTasksToWorkers()
        self.assertEqual(
            task1, workers[0].assignedTasks[0])  # should be assigned to worker
        self.assertIsNone(
            task3.assignedWorker)  # workers for task3 should be None
 def test_assignedTasksCountAndUnassignedTasksCount(self):
     workers = [
         Worker(workerID="W1",
                begin=tHour(0, 00),
                end=tHour(23, 59),
                multitask=1),
         Worker(workerID="W2",
                begin=tHour(0, 00),
                end=tHour(23, 59),
                multitask=1)
     ]
     taskManager = TaskManager(period=self.tPeriod(), multitask=1)
     taskManager.addTask(self.tasks[0])
     taskManager.addTask(self.tasks[1])
     taskManager.addTask(self.tasks[2])
     taskManager.addWorkers(workers)
     taskManager.assignTasksToWorkers()
     self.assertEqual(len(taskManager.assignedTasks), 2)
     self.assertEqual(len(taskManager.unassignedTasks), 1)
 def test_noConflictsAfterAllTasksRemoved(self):
     workers = [
         Worker(workerID="W1",
                begin=tHour(0, 00),
                end=tHour(23, 59),
                multitask=1),
         Worker(workerID="W2",
                begin=tHour(0, 00),
                end=tHour(23, 59),
                multitask=1),
     ]
     taskManager = TaskManager(period=self.tPeriod(), multitask=1)
     taskManager.addWorkers(workers)
     taskManager.addTask(self.tasks[0])
     taskManager.addTask(self.tasks[1])
     taskManager.assignTasksToWorkers()
     conflicts = taskManager.analyseWorkersForNeededTaskSet()[0]
     self.assertEqual(conflicts[0].workersNeeded, 2)
     taskManager.finishTask(self.tasks[0].taskID)
     taskManager.finishTask(self.tasks[1].taskID)
     conflicts = taskManager.analyseWorkersForNeededTaskSet()[0]
     self.assertEqual(len(conflicts), 0)  # there should be not conflicts
 def test_addTask(self):
     taskManager = TaskManager(period=self.tPeriod(), multitask=1)
     taskManager.addTask(self.tasks[0])
     self.assertEqual(taskManager.taskSet.tasks[0], self.tasks[0])