本文整理汇总了Python中twisted.internet.defer.FirstError方法的典型用法代码示例。如果您正苦于以下问题:Python defer.FirstError方法的具体用法?Python defer.FirstError怎么用?Python defer.FirstError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.defer
的用法示例。
在下文中一共展示了defer.FirstError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cancelDeferredListWithFireOnOneErrback
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_cancelDeferredListWithFireOnOneErrback(self):
"""
When cancelling an unfired L{defer.DeferredList} with the flag
C{fireOnOneErrback} set, cancel every L{defer.Deferred} in the list.
"""
deferredOne = defer.Deferred()
deferredTwo = defer.Deferred()
deferredList = defer.DeferredList([deferredOne, deferredTwo],
fireOnOneErrback=True)
deferredList.cancel()
self.failureResultOf(deferredOne, defer.CancelledError)
self.failureResultOf(deferredTwo, defer.CancelledError)
deferredListFailure = self.failureResultOf(deferredList,
defer.FirstError)
firstError = deferredListFailure.value
self.assertTrue(firstError.subFailure.check(defer.CancelledError))
示例2: test_gatherResultsWithConsumeErrors
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_gatherResultsWithConsumeErrors(self):
"""
If a L{Deferred} in the list passed to L{gatherResults} fires with a
failure and C{consumerErrors} is C{True}, the failure is converted to a
L{None} result on that L{Deferred}.
"""
# test successful list of deferreds
dgood = defer.succeed(1)
dbad = defer.fail(RuntimeError("oh noes"))
d = defer.gatherResults([dgood, dbad], consumeErrors=True)
unconsumedErrors = []
dbad.addErrback(unconsumedErrors.append)
gatheredErrors = []
d.addErrback(gatheredErrors.append)
self.assertEqual((len(unconsumedErrors), len(gatheredErrors)),
(0, 1))
self.assertIsInstance(gatheredErrors[0].value, defer.FirstError)
firstError = gatheredErrors[0].value.subFailure
self.assertIsInstance(firstError.value, RuntimeError)
示例3: test_changes_run_after_exception
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_changes_run_after_exception(self):
"""
If one of the ``IStateChange`` implementations passed to
``in_parallel`` raises an exception, ``run_state_changes`` nevertheless
runs the other ``IStateChange`` implementations passed along with it.
"""
some_change = ControllableAction(result=None)
other_change = ControllableAction(result=None)
subchanges = [
smallest(IStateChange, some_change),
BrokenAction(exception=CustomException()),
largest(IStateChange, other_change),
]
change = in_parallel(changes=subchanges)
result = run_state_change(change, DEPLOYER, InMemoryStatePersister())
self.failureResultOf(result, FirstError)
self.assertEqual(
(some_change.called, other_change.called),
(True, True),
"Other changes passed to in_parallel with a broken IStateChange "
"were not run."
)
示例4: timeout
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def timeout(secs):
"""
Decorator to add timeout to Deferred calls
"""
def wrap(func):
@defer.inlineCallbacks
def _timeout(*args, **kwargs):
rawD = func(*args, **kwargs)
if not isinstance(rawD, defer.Deferred):
defer.returnValue(rawD)
timeoutD = defer.Deferred()
timesUp = reactor.callLater(secs, timeoutD.callback, None)
try:
rawResult, timeoutResult = yield defer.DeferredList([rawD, timeoutD], fireOnOneCallback=True, fireOnOneErrback=True, consumeErrors=True)
except defer.FirstError, e:
#Only rawD should raise an exception
assert e.index == 0
timesUp.cancel()
e.subFailure.raiseException()
else:
示例5: _unwrapFirstError
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def _unwrapFirstError(failure):
failure.trap(defer.FirstError)
return failure.value.subFailure
示例6: testDeferredListFireOnOneError
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def testDeferredListFireOnOneError(self):
defr1 = defer.Deferred()
defr2 = defer.Deferred()
defr3 = defer.Deferred()
dl = defer.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.assertEqual(result, [])
# fire one Deferred's errback -- now we have a result
defr2.errback(GenericError("from def2"))
self.assertEqual(len(result), 1)
# extract the result from the list
aFailure = result[0]
# the type of the failure is a FirstError
self.assertTrue(issubclass(aFailure.type, defer.FirstError),
'issubclass(aFailure.type, defer.FirstError) failed: '
"failure's type is %r" % (aFailure.type,)
)
firstError = aFailure.value
# check that the GenericError("2") from the deferred at index 1
# (defr2) is intact inside failure.value
self.assertEqual(firstError.subFailure.type, GenericError)
self.assertEqual(firstError.subFailure.value.args, ("from def2",))
self.assertEqual(firstError.index, 1)
示例7: test_cancelFiredOnOneErrbackDeferredList
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_cancelFiredOnOneErrbackDeferredList(self):
"""
When a L{defer.DeferredList} has fired because one L{defer.Deferred} in
the list fired with a failure result, the cancellation will do
nothing instead of cancelling the rest of the L{defer.Deferred}s.
"""
deferredOne = defer.Deferred()
deferredTwo = defer.Deferred()
deferredList = defer.DeferredList([deferredOne, deferredTwo],
fireOnOneErrback=True)
deferredOne.errback(GenericError("test"))
deferredList.cancel()
self.assertNoResult(deferredTwo)
self.failureResultOf(deferredOne, GenericError)
self.failureResultOf(deferredList, defer.FirstError)
示例8: test_repr
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_repr(self):
"""
The repr of a L{FirstError} instance includes the repr of the value of
the sub-failure and the index which corresponds to the L{FirstError}.
"""
exc = ValueError("some text")
try:
raise exc
except:
f = failure.Failure()
error = defer.FirstError(f, 3)
self.assertEqual(
repr(error),
"FirstError[#3, %s]" % (repr(exc),))
示例9: test_str
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_str(self):
"""
The str of a L{FirstError} instance includes the str of the
sub-failure and the index which corresponds to the L{FirstError}.
"""
exc = ValueError("some text")
try:
raise exc
except:
f = failure.Failure()
error = defer.FirstError(f, 5)
self.assertEqual(
str(error),
"FirstError[#5, %s]" % (str(f),))
示例10: test_comparison
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_comparison(self):
"""
L{FirstError} instances compare equal to each other if and only if
their failure and index compare equal. L{FirstError} instances do not
compare equal to instances of other types.
"""
try:
1 // 0
except:
firstFailure = failure.Failure()
one = defer.FirstError(firstFailure, 13)
anotherOne = defer.FirstError(firstFailure, 13)
try:
raise ValueError("bar")
except:
secondFailure = failure.Failure()
another = defer.FirstError(secondFailure, 9)
self.assertTrue(one == anotherOne)
self.assertFalse(one == another)
self.assertTrue(one != another)
self.assertFalse(one != anotherOne)
self.assertFalse(one == 10)
示例11: test_first_error
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_first_error(self, logger):
"""
If any of the supplied ``deferreds`` fail, ``gather_deferreds`` will
errback with a ``FirstError``.
"""
d = gather_deferreds(
[succeed('SUCCESS1'),
fail(ZeroDivisionError('failure1')),
succeed('SUCCESS2')])
self.failureResultOf(d, FirstError)
示例12: test_first_error_value
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_first_error_value(self, logger):
"""
The ``FirstError`` has a reference to the ``Failure`` produced by the
first of the supplied ``deferreds`` that failed.
"""
failure1 = Failure(ZeroDivisionError('failure1'))
failure2 = Failure(ZeroDivisionError('failure1'))
d = gather_deferreds([fail(failure1), succeed(None), fail(failure2)])
first_error = self.failureResultOf(d, FirstError)
self.assertIs(first_error.value.subFailure, failure1)
示例13: test_exception_result
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_exception_result(self):
"""
When called with the result of ``in_parallel``, ``run_state_changes``
returns a ``Deferred`` that fires with the first exception raised
(synchronously, not returned as a ``Failure``).
"""
subchanges = [BrokenAction(exception=CustomException())]
change = in_parallel(changes=subchanges)
result = run_state_change(change, DEPLOYER, InMemoryStatePersister())
failure = self.failureResultOf(result, FirstError)
self.assertEqual(failure.value.subFailure.type, CustomException)
示例14: test_failure_result
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_failure_result(self):
"""
When called with the result of ``in_parallel``, ``run_state_changes``
returns a ``Deferred`` that fires with the failure which occurs first.
"""
subchanges = [ControllableAction(result=fail(RuntimeError()))]
change = in_parallel(changes=subchanges)
result = run_state_change(change, DEPLOYER, InMemoryStatePersister())
failure = self.failureResultOf(result, FirstError)
self.assertEqual(failure.value.subFailure.type, RuntimeError)
示例15: test_fetch_many_async_with_error
# 需要导入模块: from twisted.internet import defer [as 别名]
# 或者: from twisted.internet.defer import FirstError [as 别名]
def test_fetch_many_async_with_error(self):
"""
L{fetch_many_async} aborts as soon as one URL fails.
"""
url_results = {"http://right/": b"right",
"http://wrong/": (b"wrong", 501),
"http://impossible/": b"impossible"}
failed_urls = []
def errback(failure, url):
failed_urls.append(url)
self.assertEqual(failure.value.body, b"wrong")
self.assertEqual(failure.value.http_code, 501)
return failure
curl = CurlManyStub(url_results)
urls = ["http://right/", "http://wrong/", "http://impossible/"]
result = fetch_many_async(urls, callback=None,
errback=errback, curl=curl)
def check_failure(failure):
self.assertTrue(isinstance(failure.subFailure.value,
HTTPCodeError))
self.assertEqual(failed_urls, ["http://wrong/"])
self.assertFailure(result, FirstError)
return result.addCallback(check_failure)