def test_releaseAtWithProcessingLargerThanDeadline(self): deadline = 300 # 5 mins processing = 360 # 6 min createdAt = datetime.now() expectedReleaseAtTime = (createdAt + timedelta(seconds=deadline) - timedelta(seconds=processing)).isoformat() deadline = addSeconds(createdAt, deadline) releaseAtTime = release.releaseAt(deadline, processing).isoformat() # Compare them as string, with no milliseconds. expectedReleaseAtTime = expectedReleaseAtTime[:-7] releaseAtTime = releaseAtTime[:-7] self.assertEqual(expectedReleaseAtTime, releaseAtTime)
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 __init__(self, createdAt, deadline, profit, processing, taskID, release=None, data=None): """ A task unit is a schedulable piece of work that needs to be completed before a deadline. A TaskUnit is composed of the following properties: t_unit = (c, r, d, p, w) c - createdAt r - release time d - deadline p - processing time w - weight profit. @param createdAt:(datetime) The time the task arrived into the system or was made. @param deadline: The deadline. In seconds from now or datetime format. @param profit:(double) The potential profit from getting this task finished on on time. @param processing:(int) The number of seconds the task will take to process. @param release Release time in datetime format. @param taskID:(str) The id of the task. @param data:(object) Optionally an TaskUnit can encapsulate another object that has a relationship with the unit of work. For example, the most common scenario for this application would be customer orders. An order itself can be viewed as a unit of work, however it makes more sense to encapsulate it into a generic form i.e this class. """ self._createdAt = createdAt self._createdAtISO = createdAt.isoformat() self._processing = processing self._profit = profit self._taskID = taskID self._data = None if isinstance(data, DataItem): self._data = data elif not data: pass else: raise TypeError( "data must be of type %s" % DataItem ) self._assignedWorker = None if isinstance(deadline, datetime): #check deadline type self._deadline = deadline self._deadlineISO = self._deadline.isoformat() elif isinstance(deadline, int): self._deadline = timeutil.addSeconds(createdAt, deadline) self._deadlineISO = self._deadline.isoformat() else: raise TypeError( "Deadline cannot be type %s" % type(deadline) ) if isinstance(release, datetime): #check release type self._release = release self._releaseISO = self._release.isoformat() elif not release: from . import release self._release = release.releaseAt(self._deadline, self._processing) self._releaseISO = self._release.isoformat() else: raise TypeError( "Release cannot be %s" % type(release) ) if self._release > self._deadline: raise ValueError( "Release time is later than deadline, this is not allowed." )