def testRequestAndAccept(self): task = MockTask(lambda x: 10*x, (2,), {}, 100, None) worker = MockWorker() worker.hired = False self.af.request(worker, None) d = defer.DeferredList([self.af.new(task), task.d]) d.addCallback(lambda _: self.failUnlessEqual(worker.ran, [task])) return d
def testTerminateGracefully(self): self._count = 0 worker = MockWorker() workerID = self.mgr.hire(worker) task = MockTask(lambda x: x, ('foo',), {}, 100, None) d1 = self.mgr.assignment(task) d1.addCallback(self._callback, "Assignment accepted", 1) d2 = task.d d2.addCallback(self._callback, "Task done", 2) d3 = self.mgr.terminate(workerID) d3.addCallback(self._callback, "Worker terminated", 3) return defer.gatherResults([d1,d2,d3])
def testTerminateByCrashing(self): def checkTask(null): self.failIf(task.d.called) self._count = 0 worker = MockWorker(runDelay=1.0) workerID = self.mgr.hire(worker) task = MockTask(lambda x: x, ('foo',), {}, 100, None) d = self.mgr.assignment(task) d.addCallback(self._callback, "Assignment accepted", 1) d.addCallback(lambda _: self.mgr.terminate(workerID, crash=True)) d.addCallback(self._callback, "Worker terminated", 2, value=[task]) return d.addCallback(checkTask)
def testTerminateBeforeTimeout(self): def checkTask(null): self.failUnless(task.d.called) self._count = 0 worker = MockWorker(runDelay=1.0) workerID = self.mgr.hire(worker) task = MockTask(lambda x: x, ('foo',), {}, 100, None) d1 = self.mgr.assignment(task) d1.addCallback(self._callback, "Assignment accepted", 1) d2 = self.mgr.terminate(workerID, timeout=2.0) d2.addCallback(self._callback, "Worker terminated", 2, value=[]) return defer.gatherResults([d1,d2]).addCallback(checkTask)
def testOneWorker(self): worker = MockWorker(0.2) N = 10 def completed(null): self.failUnlessEqual( [type(x) for x in worker.ran], [MockTask]*N) self.mgr.hire(worker) dList = [] for null in xrange(N): task = MockTask(lambda x: x, ('foo',), {}, 100, None) # For this test, we don't care about when assignments are accepted self.mgr.assignment(task) # We only care about when they are done dList.append(task.d) d = defer.DeferredList(dList) d.addCallback(completed) return d
def testMultipleWorkers(self): N = 50 mutable = [] workerFast = MockWorker(0.1) workerSlow = MockWorker(0.2) def checkResults(null): self.failUnlessEqual(len(mutable), N) self.failUnlessApproximates( 2*len(workerSlow.ran), len(workerFast.ran), 2) self.mgr.hire(workerFast) self.mgr.hire(workerSlow) dList = [] for null in xrange(N): task = MockTask(lambda : mutable.append(None), (), {}, 100, None) # For this test, we don't care about when assignments are accepted self.mgr.assignment(task) # We only care about when they are done dList.append(task.d) d = defer.DeferredList(dList) d.addCallback(checkResults) return d
def testRequestBasic(self): OriginalAssignment = tasks.Assignment class ModifiedAssignment(tasks.Assignment): mutable = [] def accept(self, worker): self.mutable.append(worker) self.d.callback(None) def finishUp(null, worker): self.failUnlessEqual(ModifiedAssignment.mutable, [worker]) tasks.Assignment = OriginalAssignment tasks.Assignment = ModifiedAssignment task = MockTask(lambda x: 10*x, (2,), {}, 100, None) worker = MockWorker() worker.hired = False self.af.request(worker, None) for dList in worker.assignments.itervalues(): self.failUnlessEqual( [isinstance(x, defer.Deferred) for x in dList], [True]) d = self.af.new(task) d.addCallback(finishUp, worker) return d