def test_popOrderFromPopulatedQueueFromConstructor(self): # Use custom values here to ensure correct pop order. deadline1 = 500 deadline2 = 200 processing = 100 items = [ TaskUnit( createdAt=self._createdAt, deadline=deadline1, profit=self._profit, processing=processing, taskID=self._taskID ), TaskUnit( createdAt=self._createdAt, deadline=deadline2, profit=self._profit, processing=processing, taskID='test2' ) ] pQueue = TaskUnitPriorityQueue(items) expectedFirstPop = items[1] expectedSecondPop = items[0] self.assertEqual(pQueue.pop(), expectedFirstPop) self.assertEqual(pQueue.pop(), expectedSecondPop)
def test_priority(self): taskUnit = TaskUnit(createdAt=self.createdAt, deadline=self.deadline, profit=self.profit, processing=self.processing, taskID=self.taskID) # As expected priority is just the release of the task, calculate it. expectedRelease = release.releaseAt(taskUnit.deadline, self.processing) expectedPriority = expectedRelease self.assertEqual(taskUnit.priority(), expectedPriority)
def test_assignedWorker(self): taskUnit = TaskUnit(createdAt=self.createdAt, deadline=self.deadline, profit=self.profit, processing=self.processing, taskID=self.taskID) worker = Worker(workerID="W1", begin=tHour(0, 00), end=tHour(23, 59), multitask=2) taskUnit.assignWorker(worker) self.assertEqual(taskUnit.assignedWorker, worker)
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_laterReleaseThanDeadline(self): with self.assertRaises(ValueError): TaskUnit(createdAt=tHour(0, 0), deadline=tHour(12, 30), profit=self.profit, processing=self.processing, release=tHour(13, 30), taskID=self.taskID)
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_constructWithNonListObject(self): item = TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ) with self.assertRaises(TypeError): _ = TaskUnitPriorityQueue(item)
def test_pushWithListObject(self): items = [ TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ), TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ) ] pQueue = TaskUnitPriorityQueue() pQueue.push(items) self.assertEqual(pQueue.count(), 2)
def test_pushWithNonListObject(self): item = TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ) pQueue = TaskUnitPriorityQueue() pQueue.push(item) expectedCount = 1 self.assertEqual(pQueue.count(), expectedCount)
def test_remove(self): item = TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ) pQueue = TaskUnitPriorityQueue() pQueue.push(item) pQueue.remove(item.taskID) self.assertEqual(pQueue.count(), 0)
def test_pushToPopulatedQueue(self): items = [ TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ) ] pQueue = TaskUnitPriorityQueue(items) pQueue.push( TaskUnit( createdAt=self._createdAt, deadline=100, profit=200, processing=20, taskID=self._taskID ) ) expectedCount = 2 self.assertEqual(pQueue.count(), expectedCount)
def test_count(self): items = [ TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ) ] pQueue = TaskUnitPriorityQueue(items) expectedCount = 1 self.assertEqual(pQueue.count(), expectedCount)
def setUp(self): self.task1 = TaskUnit( createdAt=datetime.now(), deadline=500, profit=2.56, processing=100, taskID="test1234" ) self.task2 = TaskUnit( createdAt=datetime.now(), deadline=500, profit=2.56, processing=100, taskID="test1234" ) self.task3 = TaskUnit( createdAt=datetime.now(), deadline=500, profit=2.56, processing=100, taskID="test1234" )
def setUp(self): self.createdAt = datetime.now() self.taskID = "test1234" self.deadline = 500 self.profit = 2.56 self.processing = 100 self.worker = Worker(workerID="W1", begin=tHour(0, 00), end=tHour(23, 59), multitask=2) self.globalTask = TaskUnit(createdAt=self.createdAt, deadline=self.deadline, profit=self.profit, processing=self.processing, taskID="test1")
def test_asDict(self): taskUnit = TaskUnit(createdAt=self.createdAt, deadline=self.deadline, profit=self.profit, processing=self.processing, taskID=self.taskID) taskUnit.assignWorker(self.worker) # Calculate the correct ISO for task. deadline = timeutil.addSeconds(self.createdAt, self.deadline) expectedReleaseISO = release.releaseAt(deadline, self.processing).isoformat() expectedDeadlineISO = timeutil.addSeconds(self.createdAt, self.deadline).isoformat() expectedCreatedAtISO = self.createdAt.isoformat() expectedResult = { "id": self.taskID, "releaseISO": expectedReleaseISO, "deadlineISO": expectedDeadlineISO, "createdAtISO": expectedCreatedAtISO, "profit": self.profit, "processing": self.processing, "assignedWorkerID": self.worker.workerID } self.assertEqual(taskUnit.asDict(), expectedResult)
def test_init(self): data = DataItem() taskUnit = TaskUnit(createdAt=self.createdAt, deadline=self.deadline, profit=self.profit, processing=self.processing, taskID=self.taskID, data=data) expectedDeadline = timeutil.addSeconds(self.createdAt, self.deadline) expectedRelease = release.releaseAt(expectedDeadline, self.processing) self.assertEqual(taskUnit.createdAt, self.createdAt) self.assertEqual(taskUnit.deadline, expectedDeadline) self.assertEqual(taskUnit.profit, self.profit) self.assertEqual(taskUnit.processing, self.processing) self.assertEqual(taskUnit.taskID, self.taskID) self.assertEqual(taskUnit.data, data) self.assertEqual(taskUnit.release, expectedRelease)
def test_contains(self): item = TaskUnit( createdAt=self._createdAt, deadline=self._deadline, profit=self._profit, processing=self._processing, taskID=self._taskID ) pQueue = TaskUnitPriorityQueue() # test contains with push pop pQueue.push(item) self.assertTrue(pQueue.contains(item.taskID)) pQueue.pop() self.assertFalse(pQueue.contains(item.taskID)) # test contains with push remove pQueue.push(item) self.assertTrue(pQueue.contains(item.taskID)) pQueue.remove(item.taskID) self.assertFalse(pQueue.contains(item.taskID))
from .testutil import tHour """ Visually the tasks would look something like this: t1 ---- t2 ----------------------- t3 ----- t7------------ t4 ---- t6 -------------- t5 --------------- t8------------ t9------------ 9.00-----9.30-----|10.00-----10.30-----|11.00-----11.30-----|12.00-----12.30-----|13.00-----13.30-----|14.00-----14.30-----|15.00-----15.30-----|16.00-----16.30 """ testtaskset = [ TaskUnit(createdAt=tHour(0, 0), deadline=tHour(10, 00), profit=0, processing=0, release=tHour(9, 30), taskID="t1"), TaskUnit(createdAt=tHour(0, 0), deadline=tHour(11, 00), profit=0, processing=0, release=tHour(9, 40), taskID="t2"), TaskUnit(createdAt=tHour(0, 0), deadline=tHour(12, 00), profit=0, processing=0, release=tHour(11, 30), taskID="t3"), TaskUnit(createdAt=tHour(0, 0),