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_removeWorkerAfterAssigningTasks(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(self.tPeriod(), multitask=1)
     taskManager.addTasks([self.tasks[0], self.tasks[1]])
     taskManager.addWorkers(workers)
     taskManager.assignTasksToWorkers()
     # make sure the exception is thrown as the tasks are not finished yet.
     with self.assertRaises(UnfinishedTasksHeldByWorkerException):
         taskManager.removeWorker(workers[0].workerID)
     # finish the tasks and remove workers
     taskManager.finishTask(self.tasks[0].taskID)
     taskManager.removeWorker(workers[0].workerID)
     self.assertEqual(len(taskManager.workers), 1)
     taskManager.finishTask(self.tasks[1].taskID)
     taskManager.removeWorker(workers[1].workerID)
     self.assertEqual(len(taskManager.workers), 0)
 def test_addWorkers(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(self.tPeriod(), multitask=1)
     taskManager.addWorkers(workers)
     self.assertEqual(len(taskManager.workers), 2)
    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_assignTasksToWorkers(self):
     initialWorkers = [
         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)
     ]
     extraWorkers = [
         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(self.tPeriod(), multitask=1)
     taskManager.addTasks(self.tasks)
     taskManager.addWorkers(initialWorkers)
     taskManager.assignTasksToWorkers()
     self.assertEqual(self.tasks[0],
                      initialWorkers[0].assignedTasks[0])  #check w1
     self.assertEqual(self.tasks[1],
                      initialWorkers[1].assignedTasks[0])  #check w2
     self.assertEqual(self.tasks[2],
                      initialWorkers[2].assignedTasks[0])  #check w3
     self.assertEqual(taskManager.unassignedTasks[0], self.tasks[3])
     self.assertEqual(taskManager.unassignedTasks[1], self.tasks[4])
     self.assertEqual(taskManager.unassignedTasks[2], self.tasks[5])
     #add more tasks and make sure they were assigned to the new workers.
     taskManager.addWorkers(extraWorkers)
     taskManager.assignTasksToWorkers()
     self.assertEqual(self.tasks[3],
                      extraWorkers[0].assignedTasks[0])  #check w4
     self.assertEqual(self.tasks[4],
                      extraWorkers[1].assignedTasks[0])  #check w5
     self.assertEqual(taskManager.unassignedTasks[0], self.tasks[5])
 def test_addWorkersAfterInitialAssignment(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(self.tPeriod(), multitask=1)
     taskManager.addTasks(self.tasks)
     taskManager.addWorkers(workers)
     taskManager.assignTasksToWorkers()
     self.assertEqual(self.tasks[0], workers[0].assignedTasks[0])
     self.assertEqual(self.tasks[1], workers[1].assignedTasks[0])
 def test_analyseWorkersNeededTaskSet(self):
     workers = [
         Worker(workerID="W1",
                begin=tHour(0, 00),
                end=tHour(23, 59),
                multitask=2),
         Worker(workerID="W2",
                begin=tHour(0, 00),
                end=tHour(23, 59),
                multitask=2),
     ]
     taskManager = TaskManager(period=self.tPeriod(), multitask=2)
     taskManager.addWorkers(workers)
     taskManager.addTasks(self.tasks)
     taskManager.assignTasksToWorkers()
     conflicts, nonConflicts = taskManager.analyseWorkersForNeededTaskSet()
     self.assertEqual(len(conflicts), 2)
     self.assertEqual(len(nonConflicts), 1)
 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_workerGetsAssignedAnotherTaskAfterFinishing(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.addTasks(self.tasks)
     taskManager.assignTasksToWorkers()
     self.assertEqual(len(workers[0].assignedTasks), 1)
     self.assertEqual(len(workers[1].assignedTasks), 1)
     # check that task was unnasigned from worker.
     assignedTask = workers[0].assignedTasks[0]
     taskManager.finishTask(assignedTask.taskID)
     self.assertEqual(len(workers[0].assignedTasks), 0)
     # now assign tasks if theres any more.
     taskManager.assignTasksToWorkers()
     self.assertEqual(len(workers[0].assignedTasks), 1)