def testEmptyDeferredList(self): result = [] def cb(resultList, result=result): result.append(resultList) dl = deferred.DeferredList([]) dl.addCallbacks(cb) self.failUnlessEqual(result, [[]]) result[:] = [] dl = deferred.DeferredList([], fireOnOneCallback=1) dl.addCallbacks(cb) self.failUnlessEqual(result, [])
def testDeferredList(self): defr1 = deferred.Deferred() defr2 = deferred.Deferred() defr3 = deferred.Deferred() dl = deferred.DeferredList([defr1, defr2, defr3]) result = [] def cb(resultList, result=result): result.extend(resultList) def catch(err): return None dl.addCallbacks(cb, cb) defr1.callback("1") defr2.addErrback(catch) # "catch" is added to eat the GenericError that will be passed on by # the DeferredList's callback on defr2. If left unhandled, the # Failure object would cause a log.err() warning about "Unhandled # error in Deferred". Twisted's pyunit watches for log.err calls and # treats them as failures. So "catch" must eat the error to prevent # it from flunking the test. defr2.errback(GenericError("2")) defr3.callback("3") self.failUnlessEqual( [ result[0], #result[1][1] is now a Failure instead of an Exception (result[1][0], str(result[1][1].value)), result[2] ], [(deferred.SUCCESS, "1"), (deferred.FAILURE, "2"), (deferred.SUCCESS, "3")])
def testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds(self): # Create some deferreds, and errback one d1 = deferred.Deferred() d2 = deferred.Deferred() d1.errback(GenericError('Bang')) # *Then* build the DeferredList, with fireOnOneErrback=True dl = deferred.DeferredList([d1, d2], fireOnOneErrback=True) result = [] dl.addErrback(result.append) self.failUnlessEqual(1, len(result)) d1.addErrback(lambda e: None) # Swallow error
def testDeferredListConsumeErrors(self): d1 = deferred.Deferred() dl = deferred.DeferredList([d1], consumeErrors=True) errorTrap = [] d1.addErrback(errorTrap.append) result = [] dl.addCallback(result.append) d1.errback(GenericError('Bang')) self.failUnlessEqual([], errorTrap) self.failUnlessEqual(1, len(result)) self.failUnlessEqual('Bang', result[0][0][1].value.args[0])
def testDeferredListWithAlreadyFiredDeferreds(self): # Create some deferreds, and err one, call the other d1 = deferred.Deferred() d2 = deferred.Deferred() d1.errback(GenericError('Bang')) d2.callback(2) # *Then* build the DeferredList dl = deferred.DeferredList([d1, d2]) result = [] dl.addCallback(result.append) self.failUnlessEqual(1, len(result)) d1.addErrback(lambda e: None) # Swallow error
def testDeferredListFireOnOneError(self): defr1 = deferred.Deferred() defr2 = deferred.Deferred() defr3 = deferred.Deferred() dl = deferred.DeferredList([defr1, defr2, defr3], fireOnOneErrback=1) result = [] dl.addErrback(result.append) # consume errors after they pass through the DeferredList (to avoid # 'Unhandled error in Deferred'. def catch(err): return None defr2.addErrback(catch) # fire one Deferred's callback, no result yet defr1.callback("1") self.failUnlessEqual(result, []) # fire one Deferred's errback -- now we have a result defr2.errback(GenericError("from def2")) self.failUnlessEqual(len(result), 1) # extract the result from the list failure = result[0] # the type of the failure is a FirstError self.failUnless( issubclass(failure.type, deferred.FirstError), 'issubclass(failure.type, deferred.FirstError) failed: ' 'failure.type is %r' % (failure.type, )) firstError = failure.value # check that the GenericError("2") from the deferred at index 1 # (defr2) is intact inside failure.value self.failUnlessEqual(firstError.subFailure.type, GenericError) self.failUnlessEqual(firstError.subFailure.value.args, ("from def2", )) self.failUnlessEqual(firstError.index, 1)
def testDeferredListEmpty(self): """Testing empty DeferredList.""" dl = deferred.DeferredList([]) dl.addCallback(self.cb_empty)