code
stringlengths 26
870k
| docstring
stringlengths 1
65.6k
| func_name
stringlengths 1
194
| language
stringclasses 1
value | repo
stringlengths 8
68
| path
stringlengths 5
194
| url
stringlengths 46
254
| license
stringclasses 4
values |
---|---|---|---|---|---|---|---|
def test_asynchronousImplicitErrorChain(self):
"""
Let C{a} and C{b} be two L{Deferred}s.
If C{a} has no result and is returned from a callback on C{b} then when
C{a} fails, C{b}'s result becomes the L{Failure} that was C{a}'s result,
the result of C{a} becomes L{None} so that no unhandled error is logged
when it is garbage collected.
"""
first = defer.Deferred()
second = defer.Deferred()
second.addCallback(lambda ign: first)
second.callback(None)
secondError = []
second.addErrback(secondError.append)
firstResult = []
first.addCallback(firstResult.append)
secondResult = []
second.addCallback(secondResult.append)
self.assertEqual(firstResult, [])
self.assertEqual(secondResult, [])
first.errback(RuntimeError("First Deferred's Failure"))
self.assertTrue(secondError[0].check(RuntimeError))
self.assertEqual(firstResult, [None])
self.assertEqual(len(secondResult), 1) | Let C{a} and C{b} be two L{Deferred}s.
If C{a} has no result and is returned from a callback on C{b} then when
C{a} fails, C{b}'s result becomes the L{Failure} that was C{a}'s result,
the result of C{a} becomes L{None} so that no unhandled error is logged
when it is garbage collected. | test_asynchronousImplicitErrorChain | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_doubleAsynchronousImplicitChaining(self):
"""
L{Deferred} chaining is transitive.
In other words, let A, B, and C be Deferreds. If C is returned from a
callback on B and B is returned from a callback on A then when C fires,
A fires.
"""
first = defer.Deferred()
second = defer.Deferred()
second.addCallback(lambda ign: first)
third = defer.Deferred()
third.addCallback(lambda ign: second)
thirdResult = []
third.addCallback(thirdResult.append)
result = object()
# After this, second is waiting for first to tell it to continue.
second.callback(None)
# And after this, third is waiting for second to tell it to continue.
third.callback(None)
# Still waiting
self.assertEqual(thirdResult, [])
# This will tell second to continue which will tell third to continue.
first.callback(result)
self.assertEqual(thirdResult, [result]) | L{Deferred} chaining is transitive.
In other words, let A, B, and C be Deferreds. If C is returned from a
callback on B and B is returned from a callback on A then when C fires,
A fires. | test_doubleAsynchronousImplicitChaining | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_nestedAsynchronousChainedDeferreds(self):
"""
L{Deferred}s can have callbacks that themselves return L{Deferred}s.
When these "inner" L{Deferred}s fire (even asynchronously), the
callback chain continues.
"""
results = []
failures = []
# A Deferred returned in the inner callback.
inner = defer.Deferred()
def cb(result):
results.append(('start-of-cb', result))
d = defer.succeed('inner')
def firstCallback(result):
results.append(('firstCallback', 'inner'))
# Return a Deferred that definitely has not fired yet, so we
# can fire the Deferreds out of order.
return inner
def secondCallback(result):
results.append(('secondCallback', result))
return result * 2
d.addCallback(firstCallback).addCallback(secondCallback)
d.addErrback(failures.append)
return d
# Create a synchronous Deferred that has a callback 'cb' that returns
# a Deferred 'd' that has fired but is now waiting on an unfired
# Deferred 'inner'.
outer = defer.succeed('outer')
outer.addCallback(cb)
outer.addCallback(results.append)
# At this point, the callback 'cb' has been entered, and the first
# callback of 'd' has been called.
self.assertEqual(
results, [('start-of-cb', 'outer'), ('firstCallback', 'inner')])
# Once the inner Deferred is fired, processing of the outer Deferred's
# callback chain continues.
inner.callback('orange')
# Make sure there are no errors.
inner.addErrback(failures.append)
outer.addErrback(failures.append)
self.assertEqual(
[], failures, "Got errbacks but wasn't expecting any.")
self.assertEqual(
results,
[('start-of-cb', 'outer'),
('firstCallback', 'inner'),
('secondCallback', 'orange'),
'orangeorange']) | L{Deferred}s can have callbacks that themselves return L{Deferred}s.
When these "inner" L{Deferred}s fire (even asynchronously), the
callback chain continues. | test_nestedAsynchronousChainedDeferreds | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_nestedAsynchronousChainedDeferredsWithExtraCallbacks(self):
"""
L{Deferred}s can have callbacks that themselves return L{Deferred}s.
These L{Deferred}s can have other callbacks added before they are
returned, which subtly changes the callback chain. When these "inner"
L{Deferred}s fire (even asynchronously), the outer callback chain
continues.
"""
results = []
failures = []
# A Deferred returned in the inner callback after a callback is
# added explicitly and directly to it.
inner = defer.Deferred()
def cb(result):
results.append(('start-of-cb', result))
d = defer.succeed('inner')
def firstCallback(ignored):
results.append(('firstCallback', ignored))
# Return a Deferred that definitely has not fired yet with a
# result-transforming callback so we can fire the Deferreds
# out of order and see how the callback affects the ultimate
# results.
return inner.addCallback(lambda x: [x])
def secondCallback(result):
results.append(('secondCallback', result))
return result * 2
d.addCallback(firstCallback)
d.addCallback(secondCallback)
d.addErrback(failures.append)
return d
# Create a synchronous Deferred that has a callback 'cb' that returns
# a Deferred 'd' that has fired but is now waiting on an unfired
# Deferred 'inner'.
outer = defer.succeed('outer')
outer.addCallback(cb)
outer.addCallback(results.append)
# At this point, the callback 'cb' has been entered, and the first
# callback of 'd' has been called.
self.assertEqual(
results, [('start-of-cb', 'outer'), ('firstCallback', 'inner')])
# Once the inner Deferred is fired, processing of the outer Deferred's
# callback chain continues.
inner.callback('withers')
# Make sure there are no errors.
outer.addErrback(failures.append)
inner.addErrback(failures.append)
self.assertEqual(
[], failures, "Got errbacks but wasn't expecting any.")
self.assertEqual(
results,
[('start-of-cb', 'outer'),
('firstCallback', 'inner'),
('secondCallback', ['withers']),
['withers', 'withers']]) | L{Deferred}s can have callbacks that themselves return L{Deferred}s.
These L{Deferred}s can have other callbacks added before they are
returned, which subtly changes the callback chain. When these "inner"
L{Deferred}s fire (even asynchronously), the outer callback chain
continues. | test_nestedAsynchronousChainedDeferredsWithExtraCallbacks | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_chainDeferredRecordsExplicitChain(self):
"""
When we chain a L{Deferred}, that chaining is recorded explicitly.
"""
a = defer.Deferred()
b = defer.Deferred()
b.chainDeferred(a)
self.assertIs(a._chainedTo, b) | When we chain a L{Deferred}, that chaining is recorded explicitly. | test_chainDeferredRecordsExplicitChain | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_explicitChainClearedWhenResolved(self):
"""
Any recorded chaining is cleared once the chaining is resolved, since
it no longer exists.
In other words, if one L{Deferred} is recorded as depending on the
result of another, and I{that} L{Deferred} has fired, then the
dependency is resolved and we no longer benefit from recording it.
"""
a = defer.Deferred()
b = defer.Deferred()
b.chainDeferred(a)
b.callback(None)
self.assertIsNone(a._chainedTo) | Any recorded chaining is cleared once the chaining is resolved, since
it no longer exists.
In other words, if one L{Deferred} is recorded as depending on the
result of another, and I{that} L{Deferred} has fired, then the
dependency is resolved and we no longer benefit from recording it. | test_explicitChainClearedWhenResolved | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_chainDeferredRecordsImplicitChain(self):
"""
We can chain L{Deferred}s implicitly by adding callbacks that return
L{Deferred}s. When this chaining happens, we record it explicitly as
soon as we can find out about it.
"""
a = defer.Deferred()
b = defer.Deferred()
a.addCallback(lambda ignored: b)
a.callback(None)
self.assertIs(a._chainedTo, b) | We can chain L{Deferred}s implicitly by adding callbacks that return
L{Deferred}s. When this chaining happens, we record it explicitly as
soon as we can find out about it. | test_chainDeferredRecordsImplicitChain | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_circularChainWarning(self):
"""
When a Deferred is returned from a callback directly attached to that
same Deferred, a warning is emitted.
"""
d = defer.Deferred()
def circularCallback(result):
return d
d.addCallback(circularCallback)
d.callback("foo")
circular_warnings = self.flushWarnings([circularCallback])
self.assertEqual(len(circular_warnings), 1)
warning = circular_warnings[0]
self.assertEqual(warning['category'], DeprecationWarning)
pattern = "Callback returned the Deferred it was attached to"
self.assertTrue(
re.search(pattern, warning['message']),
"\nExpected match: %r\nGot: %r" % (pattern, warning['message'])) | When a Deferred is returned from a callback directly attached to that
same Deferred, a warning is emitted. | test_circularChainWarning | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_circularChainException(self):
"""
If the deprecation warning for circular deferred callbacks is
configured to be an error, the exception will become the failure
result of the Deferred.
"""
self.addCleanup(setattr, warnings, "filters", warnings.filters)
warnings.filterwarnings("error", category=DeprecationWarning)
d = defer.Deferred()
def circularCallback(result):
return d
d.addCallback(circularCallback)
d.callback("foo")
failure = self.failureResultOf(d)
failure.trap(DeprecationWarning) | If the deprecation warning for circular deferred callbacks is
configured to be an error, the exception will become the failure
result of the Deferred. | test_circularChainException | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_repr(self):
"""
The C{repr()} of a L{Deferred} contains the class name and a
representation of the internal Python ID.
"""
d = defer.Deferred()
address = id(d)
self.assertEqual(
repr(d), '<Deferred at 0x%x>' % (address,)) | The C{repr()} of a L{Deferred} contains the class name and a
representation of the internal Python ID. | test_repr | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_reprWithResult(self):
"""
If a L{Deferred} has been fired, then its C{repr()} contains its
result.
"""
d = defer.Deferred()
d.callback('orange')
self.assertEqual(
repr(d), "<Deferred at 0x%x current result: 'orange'>" % (
id(d),)) | If a L{Deferred} has been fired, then its C{repr()} contains its
result. | test_reprWithResult | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_reprWithChaining(self):
"""
If a L{Deferred} C{a} has been fired, but is waiting on another
L{Deferred} C{b} that appears in its callback chain, then C{repr(a)}
says that it is waiting on C{b}.
"""
a = defer.Deferred()
b = defer.Deferred()
b.chainDeferred(a)
self.assertEqual(
repr(a), "<Deferred at 0x%x waiting on Deferred at 0x%x>" % (
id(a), id(b))) | If a L{Deferred} C{a} has been fired, but is waiting on another
L{Deferred} C{b} that appears in its callback chain, then C{repr(a)}
says that it is waiting on C{b}. | test_reprWithChaining | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_boundedStackDepth(self):
"""
The depth of the call stack does not grow as more L{Deferred} instances
are chained together.
"""
def chainDeferreds(howMany):
stack = []
def recordStackDepth(ignored):
stack.append(len(traceback.extract_stack()))
top = defer.Deferred()
innerDeferreds = [defer.Deferred() for ignored in range(howMany)]
originalInners = innerDeferreds[:]
last = defer.Deferred()
inner = innerDeferreds.pop()
top.addCallback(lambda ign, inner=inner: inner)
top.addCallback(recordStackDepth)
while innerDeferreds:
newInner = innerDeferreds.pop()
inner.addCallback(lambda ign, inner=newInner: inner)
inner = newInner
inner.addCallback(lambda ign: last)
top.callback(None)
for inner in originalInners:
inner.callback(None)
# Sanity check - the record callback is not intended to have
# fired yet.
self.assertEqual(stack, [])
# Now fire the last thing and return the stack depth at which the
# callback was invoked.
last.callback(None)
return stack[0]
# Callbacks should be invoked at the same stack depth regardless of
# how many Deferreds are chained.
self.assertEqual(chainDeferreds(1), chainDeferreds(2)) | The depth of the call stack does not grow as more L{Deferred} instances
are chained together. | test_boundedStackDepth | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled(self):
"""
Given three Deferreds, one chained to the next chained to the next,
callbacks on the middle Deferred which are added after the chain is
created are called once the last Deferred fires.
This is more of a regression-style test. It doesn't exercise any
particular code path through the current implementation of Deferred, but
it does exercise a broken codepath through one of the variations of the
implementation proposed as a resolution to ticket #411.
"""
first = defer.Deferred()
second = defer.Deferred()
third = defer.Deferred()
first.addCallback(lambda ignored: second)
second.addCallback(lambda ignored: third)
second.callback(None)
first.callback(None)
third.callback(None)
L = []
second.addCallback(L.append)
self.assertEqual(L, [None]) | Given three Deferreds, one chained to the next chained to the next,
callbacks on the middle Deferred which are added after the chain is
created are called once the last Deferred fires.
This is more of a regression-style test. It doesn't exercise any
particular code path through the current implementation of Deferred, but
it does exercise a broken codepath through one of the variations of the
implementation proposed as a resolution to ticket #411. | test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errbackWithNoArgsNoDebug(self):
"""
C{Deferred.errback()} creates a failure from the current Python
exception. When Deferred.debug is not set no globals or locals are
captured in that failure.
"""
defer.setDebugging(False)
d = defer.Deferred()
l = []
exc = GenericError("Bang")
try:
raise exc
except:
d.errback()
d.addErrback(l.append)
fail = l[0]
self.assertEqual(fail.value, exc)
localz, globalz = fail.frames[0][-2:]
self.assertEqual([], localz)
self.assertEqual([], globalz) | C{Deferred.errback()} creates a failure from the current Python
exception. When Deferred.debug is not set no globals or locals are
captured in that failure. | test_errbackWithNoArgsNoDebug | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errbackWithNoArgs(self):
"""
C{Deferred.errback()} creates a failure from the current Python
exception. When Deferred.debug is set globals and locals are captured
in that failure.
"""
defer.setDebugging(True)
d = defer.Deferred()
l = []
exc = GenericError("Bang")
try:
raise exc
except:
d.errback()
d.addErrback(l.append)
fail = l[0]
self.assertEqual(fail.value, exc)
localz, globalz = fail.frames[0][-2:]
self.assertNotEqual([], localz)
self.assertNotEqual([], globalz) | C{Deferred.errback()} creates a failure from the current Python
exception. When Deferred.debug is set globals and locals are captured
in that failure. | test_errbackWithNoArgs | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorInCallbackDoesNotCaptureVars(self):
"""
An error raised by a callback creates a Failure. The Failure captures
locals and globals if and only if C{Deferred.debug} is set.
"""
d = defer.Deferred()
d.callback(None)
defer.setDebugging(False)
def raiseError(ignored):
raise GenericError("Bang")
d.addCallback(raiseError)
l = []
d.addErrback(l.append)
fail = l[0]
localz, globalz = fail.frames[0][-2:]
self.assertEqual([], localz)
self.assertEqual([], globalz) | An error raised by a callback creates a Failure. The Failure captures
locals and globals if and only if C{Deferred.debug} is set. | test_errorInCallbackDoesNotCaptureVars | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorInCallbackCapturesVarsWhenDebugging(self):
"""
An error raised by a callback creates a Failure. The Failure captures
locals and globals if and only if C{Deferred.debug} is set.
"""
d = defer.Deferred()
d.callback(None)
defer.setDebugging(True)
def raiseError(ignored):
raise GenericError("Bang")
d.addCallback(raiseError)
l = []
d.addErrback(l.append)
fail = l[0]
localz, globalz = fail.frames[0][-2:]
self.assertNotEqual([], localz)
self.assertNotEqual([], globalz) | An error raised by a callback creates a Failure. The Failure captures
locals and globals if and only if C{Deferred.debug} is set. | test_errorInCallbackCapturesVarsWhenDebugging | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_inlineCallbacksTracebacks(self):
"""
L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
should not lose their tracebacks.
"""
f = getDivisionFailure()
d = defer.Deferred()
try:
f.raiseException()
except:
d.errback()
def ic(d):
yield d
ic = defer.inlineCallbacks(ic)
newFailure = self.failureResultOf(d)
tb = traceback.extract_tb(newFailure.getTracebackObject())
if _PY3:
self.assertEqual(len(tb), 3)
self.assertIn('test_defer', tb[2][0])
self.assertEqual('getDivisionFailure', tb[2][2])
self.assertEqual('1/0', tb[2][3])
else:
self.assertEqual(len(tb), 2)
self.assertIn('test_defer', tb[1][0])
self.assertEqual('getDivisionFailure', tb[1][2])
self.assertEqual('1/0', tb[1][3])
self.assertIn('test_defer', tb[0][0])
self.assertEqual('test_inlineCallbacksTracebacks', tb[0][2])
self.assertEqual('f.raiseException()', tb[0][3]) | L{defer.inlineCallbacks} that re-raise tracebacks into their deferred
should not lose their tracebacks. | test_inlineCallbacksTracebacks | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
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),)) | 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}. | test_repr | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
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),)) | The str of a L{FirstError} instance includes the str of the
sub-failure and the index which corresponds to the L{FirstError}. | test_str | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
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) | 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. | test_comparison | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_noCanceller(self):
"""
A L{defer.Deferred} without a canceller must errback with a
L{defer.CancelledError} and not callback.
"""
d = defer.Deferred()
d.addCallbacks(self._callback, self._errback)
d.cancel()
self.assertEqual(self.errbackResults.type, defer.CancelledError)
self.assertIsNone(self.callbackResults) | A L{defer.Deferred} without a canceller must errback with a
L{defer.CancelledError} and not callback. | test_noCanceller | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_raisesAfterCancelAndCallback(self):
"""
A L{defer.Deferred} without a canceller, when cancelled must allow
a single extra call to callback, and raise
L{defer.AlreadyCalledError} if callbacked or errbacked thereafter.
"""
d = defer.Deferred()
d.addCallbacks(self._callback, self._errback)
d.cancel()
# A single extra callback should be swallowed.
d.callback(None)
# But a second call to callback or errback is not.
self.assertRaises(defer.AlreadyCalledError, d.callback, None)
self.assertRaises(defer.AlreadyCalledError, d.errback, Exception()) | A L{defer.Deferred} without a canceller, when cancelled must allow
a single extra call to callback, and raise
L{defer.AlreadyCalledError} if callbacked or errbacked thereafter. | test_raisesAfterCancelAndCallback | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_raisesAfterCancelAndErrback(self):
"""
A L{defer.Deferred} without a canceller, when cancelled must allow
a single extra call to errback, and raise
L{defer.AlreadyCalledError} if callbacked or errbacked thereafter.
"""
d = defer.Deferred()
d.addCallbacks(self._callback, self._errback)
d.cancel()
# A single extra errback should be swallowed.
d.errback(Exception())
# But a second call to callback or errback is not.
self.assertRaises(defer.AlreadyCalledError, d.callback, None)
self.assertRaises(defer.AlreadyCalledError, d.errback, Exception()) | A L{defer.Deferred} without a canceller, when cancelled must allow
a single extra call to errback, and raise
L{defer.AlreadyCalledError} if callbacked or errbacked thereafter. | test_raisesAfterCancelAndErrback | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_noCancellerMultipleCancelsAfterCancelAndCallback(self):
"""
A L{Deferred} without a canceller, when cancelled and then
callbacked, ignores multiple cancels thereafter.
"""
d = defer.Deferred()
d.addCallbacks(self._callback, self._errback)
d.cancel()
currentFailure = self.errbackResults
# One callback will be ignored
d.callback(None)
# Cancel should have no effect.
d.cancel()
self.assertIs(currentFailure, self.errbackResults) | A L{Deferred} without a canceller, when cancelled and then
callbacked, ignores multiple cancels thereafter. | test_noCancellerMultipleCancelsAfterCancelAndCallback | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_noCancellerMultipleCancelsAfterCancelAndErrback(self):
"""
A L{defer.Deferred} without a canceller, when cancelled and then
errbacked, ignores multiple cancels thereafter.
"""
d = defer.Deferred()
d.addCallbacks(self._callback, self._errback)
d.cancel()
self.assertEqual(self.errbackResults.type, defer.CancelledError)
currentFailure = self.errbackResults
# One errback will be ignored
d.errback(GenericError())
# I.e., we should still have a CancelledError.
self.assertEqual(self.errbackResults.type, defer.CancelledError)
d.cancel()
self.assertIs(currentFailure, self.errbackResults) | A L{defer.Deferred} without a canceller, when cancelled and then
errbacked, ignores multiple cancels thereafter. | test_noCancellerMultipleCancelsAfterCancelAndErrback | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_noCancellerMultipleCancel(self):
"""
Calling cancel multiple times on a deferred with no canceller
results in a L{defer.CancelledError}. Subsequent calls to cancel
do not cause an error.
"""
d = defer.Deferred()
d.addCallbacks(self._callback, self._errback)
d.cancel()
self.assertEqual(self.errbackResults.type, defer.CancelledError)
currentFailure = self.errbackResults
d.cancel()
self.assertIs(currentFailure, self.errbackResults) | Calling cancel multiple times on a deferred with no canceller
results in a L{defer.CancelledError}. Subsequent calls to cancel
do not cause an error. | test_noCancellerMultipleCancel | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancellerMultipleCancel(self):
"""
Verify that calling cancel multiple times on a deferred with a
canceller that does not errback results in a
L{defer.CancelledError} and that subsequent calls to cancel do not
cause an error and that after all that, the canceller was only
called once.
"""
def cancel(d):
self.cancellerCallCount += 1
d = defer.Deferred(canceller=cancel)
d.addCallbacks(self._callback, self._errback)
d.cancel()
self.assertEqual(self.errbackResults.type, defer.CancelledError)
currentFailure = self.errbackResults
d.cancel()
self.assertIs(currentFailure, self.errbackResults)
self.assertEqual(self.cancellerCallCount, 1) | Verify that calling cancel multiple times on a deferred with a
canceller that does not errback results in a
L{defer.CancelledError} and that subsequent calls to cancel do not
cause an error and that after all that, the canceller was only
called once. | test_cancellerMultipleCancel | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_simpleCanceller(self):
"""
Verify that a L{defer.Deferred} calls its specified canceller when
it is cancelled, and that further call/errbacks raise
L{defer.AlreadyCalledError}.
"""
def cancel(d):
self.cancellerCallCount += 1
d = defer.Deferred(canceller=cancel)
d.addCallbacks(self._callback, self._errback)
d.cancel()
self.assertEqual(self.cancellerCallCount, 1)
self.assertEqual(self.errbackResults.type, defer.CancelledError)
# Test that further call/errbacks are *not* swallowed
self.assertRaises(defer.AlreadyCalledError, d.callback, None)
self.assertRaises(defer.AlreadyCalledError, d.errback, Exception()) | Verify that a L{defer.Deferred} calls its specified canceller when
it is cancelled, and that further call/errbacks raise
L{defer.AlreadyCalledError}. | test_simpleCanceller | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancellerArg(self):
"""
Verify that a canceller is given the correct deferred argument.
"""
def cancel(d1):
self.assertIs(d1, d)
d = defer.Deferred(canceller=cancel)
d.addCallbacks(self._callback, self._errback)
d.cancel() | Verify that a canceller is given the correct deferred argument. | test_cancellerArg | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelAfterCallback(self):
"""
Test that cancelling a deferred after it has been callbacked does
not cause an error.
"""
def cancel(d):
self.cancellerCallCount += 1
d.errback(GenericError())
d = defer.Deferred(canceller=cancel)
d.addCallbacks(self._callback, self._errback)
d.callback('biff!')
d.cancel()
self.assertEqual(self.cancellerCallCount, 0)
self.assertIsNone(self.errbackResults)
self.assertEqual(self.callbackResults, 'biff!') | Test that cancelling a deferred after it has been callbacked does
not cause an error. | test_cancelAfterCallback | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelAfterErrback(self):
"""
Test that cancelling a L{Deferred} after it has been errbacked does
not result in a L{defer.CancelledError}.
"""
def cancel(d):
self.cancellerCallCount += 1
d.errback(GenericError())
d = defer.Deferred(canceller=cancel)
d.addCallbacks(self._callback, self._errback)
d.errback(GenericError())
d.cancel()
self.assertEqual(self.cancellerCallCount, 0)
self.assertEqual(self.errbackResults.type, GenericError)
self.assertIsNone(self.callbackResults) | Test that cancelling a L{Deferred} after it has been errbacked does
not result in a L{defer.CancelledError}. | test_cancelAfterErrback | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancellerThatErrbacks(self):
"""
Test a canceller which errbacks its deferred.
"""
def cancel(d):
self.cancellerCallCount += 1
d.errback(GenericError())
d = defer.Deferred(canceller=cancel)
d.addCallbacks(self._callback, self._errback)
d.cancel()
self.assertEqual(self.cancellerCallCount, 1)
self.assertEqual(self.errbackResults.type, GenericError) | Test a canceller which errbacks its deferred. | test_cancellerThatErrbacks | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancellerThatCallbacks(self):
"""
Test a canceller which calls its deferred.
"""
def cancel(d):
self.cancellerCallCount += 1
d.callback('hello!')
d = defer.Deferred(canceller=cancel)
d.addCallbacks(self._callback, self._errback)
d.cancel()
self.assertEqual(self.cancellerCallCount, 1)
self.assertEqual(self.callbackResults, 'hello!')
self.assertIsNone(self.errbackResults) | Test a canceller which calls its deferred. | test_cancellerThatCallbacks | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelNestedDeferred(self):
"""
Verify that a Deferred, a, which is waiting on another Deferred, b,
returned from one of its callbacks, will propagate
L{defer.CancelledError} when a is cancelled.
"""
def innerCancel(d):
self.cancellerCallCount += 1
def cancel(d):
self.assertTrue(False)
b = defer.Deferred(canceller=innerCancel)
a = defer.Deferred(canceller=cancel)
a.callback(None)
a.addCallback(lambda data: b)
a.cancel()
a.addCallbacks(self._callback, self._errback)
# The cancel count should be one (the cancellation done by B)
self.assertEqual(self.cancellerCallCount, 1)
# B's canceller didn't errback, so defer.py will have called errback
# with a CancelledError.
self.assertEqual(self.errbackResults.type, defer.CancelledError) | Verify that a Deferred, a, which is waiting on another Deferred, b,
returned from one of its callbacks, will propagate
L{defer.CancelledError} when a is cancelled. | test_cancelNestedDeferred | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def setUp(self):
"""
Add a custom observer to observer logging.
"""
self.c = []
log.addObserver(self.c.append) | Add a custom observer to observer logging. | setUp | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def tearDown(self):
"""
Remove the observer.
"""
log.removeObserver(self.c.append) | Remove the observer. | tearDown | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def _check(self):
"""
Check the output of the log observer to see if the error is present.
"""
c2 = self._loggedErrors()
self.assertEqual(len(c2), 2)
c2[1]["failure"].trap(ZeroDivisionError)
self.flushLoggedErrors(ZeroDivisionError) | Check the output of the log observer to see if the error is present. | _check | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorLog(self):
"""
Verify that when a L{Deferred} with no references to it is fired,
and its final result (the one not handled by any callback) is an
exception, that exception will be logged immediately.
"""
defer.Deferred().addCallback(lambda x: 1 // 0).callback(1)
gc.collect()
self._check() | Verify that when a L{Deferred} with no references to it is fired,
and its final result (the one not handled by any callback) is an
exception, that exception will be logged immediately. | test_errorLog | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorLogWithInnerFrameRef(self):
"""
Same as L{test_errorLog}, but with an inner frame.
"""
def _subErrorLogWithInnerFrameRef():
d = defer.Deferred()
d.addCallback(lambda x: 1 // 0)
d.callback(1)
_subErrorLogWithInnerFrameRef()
gc.collect()
self._check() | Same as L{test_errorLog}, but with an inner frame. | test_errorLogWithInnerFrameRef | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorLogWithInnerFrameCycle(self):
"""
Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle.
"""
def _subErrorLogWithInnerFrameCycle():
d = defer.Deferred()
d.addCallback(lambda x, d=d: 1 // 0)
d._d = d
d.callback(1)
_subErrorLogWithInnerFrameCycle()
gc.collect()
self._check() | Same as L{test_errorLogWithInnerFrameRef}, plus create a cycle. | test_errorLogWithInnerFrameCycle | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorLogNoRepr(self):
"""
Verify that when a L{Deferred} with no references to it is fired,
the logged message does not contain a repr of the failure object.
"""
defer.Deferred().addCallback(lambda x: 1 // 0).callback(1)
gc.collect()
self._check()
self.assertEqual(2, len(self.c))
msg = log.textFromEventDict(self.c[-1])
expected = "Unhandled Error\nTraceback "
self.assertTrue(msg.startswith(expected),
"Expected message starting with: {0!r}".
format(expected)) | Verify that when a L{Deferred} with no references to it is fired,
the logged message does not contain a repr of the failure object. | test_errorLogNoRepr | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorLogDebugInfo(self):
"""
Verify that when a L{Deferred} with no references to it is fired,
the logged message includes debug info if debugging on the deferred
is enabled.
"""
def doit():
d = defer.Deferred()
d.debug = True
d.addCallback(lambda x: 1 // 0)
d.callback(1)
doit()
gc.collect()
self._check()
self.assertEqual(2, len(self.c))
msg = log.textFromEventDict(self.c[-1])
expected = "(debug: I"
self.assertTrue(msg.startswith(expected),
"Expected message starting with: {0!r}".
format(expected)) | Verify that when a L{Deferred} with no references to it is fired,
the logged message includes debug info if debugging on the deferred
is enabled. | test_errorLogDebugInfo | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_chainedErrorCleanup(self):
"""
If one Deferred with an error result is returned from a callback on
another Deferred, when the first Deferred is garbage collected it does
not log its error.
"""
d = defer.Deferred()
d.addCallback(lambda ign: defer.fail(RuntimeError("zoop")))
d.callback(None)
# Sanity check - this isn't too interesting, but we do want the original
# Deferred to have gotten the failure.
results = []
errors = []
d.addCallbacks(results.append, errors.append)
self.assertEqual(results, [])
self.assertEqual(len(errors), 1)
errors[0].trap(Exception)
# Get rid of any references we might have to the inner Deferred (none of
# these should really refer to it, but we're just being safe).
del results, errors, d
# Force a collection cycle so that there's a chance for an error to be
# logged, if it's going to be logged.
gc.collect()
# And make sure it is not.
self.assertEqual(self._loggedErrors(), []) | If one Deferred with an error result is returned from a callback on
another Deferred, when the first Deferred is garbage collected it does
not log its error. | test_chainedErrorCleanup | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errorClearedByChaining(self):
"""
If a Deferred with a failure result has an errback which chains it to
another Deferred, the initial failure is cleared by the errback so it is
not logged.
"""
# Start off with a Deferred with a failure for a result
bad = defer.fail(Exception("oh no"))
good = defer.Deferred()
# Give it a callback that chains it to another Deferred
bad.addErrback(lambda ignored: good)
# That's all, clean it up. No Deferred here still has a failure result,
# so nothing should be logged.
good = bad = None
gc.collect()
self.assertEqual(self._loggedErrors(), []) | If a Deferred with a failure result has an errback which chains it to
another Deferred, the initial failure is cleared by the errback so it is
not logged. | test_errorClearedByChaining | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def testDeferredListEmpty(self):
"""Testing empty DeferredList."""
dl = defer.DeferredList([])
dl.addCallback(self.cb_empty) | Testing empty DeferredList. | testDeferredListEmpty | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelLockAfterAcquired(self):
"""
When canceling a L{Deferred} from a L{DeferredLock} that already
has the lock, the cancel should have no effect.
"""
def _failOnErrback(_):
self.fail("Unexpected errback call!")
lock = defer.DeferredLock()
d = lock.acquire()
d.addErrback(_failOnErrback)
d.cancel() | When canceling a L{Deferred} from a L{DeferredLock} that already
has the lock, the cancel should have no effect. | test_cancelLockAfterAcquired | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelLockBeforeAcquired(self):
"""
When canceling a L{Deferred} from a L{DeferredLock} that does not
yet have the lock (i.e., the L{Deferred} has not fired), the cancel
should cause a L{defer.CancelledError} failure.
"""
lock = defer.DeferredLock()
lock.acquire()
d = lock.acquire()
d.cancel()
self.assertImmediateFailure(d, defer.CancelledError) | When canceling a L{Deferred} from a L{DeferredLock} that does not
yet have the lock (i.e., the L{Deferred} has not fired), the cancel
should cause a L{defer.CancelledError} failure. | test_cancelLockBeforeAcquired | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_semaphoreInvalidTokens(self):
"""
If the token count passed to L{DeferredSemaphore} is less than one
then L{ValueError} is raised.
"""
self.assertRaises(ValueError, defer.DeferredSemaphore, 0)
self.assertRaises(ValueError, defer.DeferredSemaphore, -1) | If the token count passed to L{DeferredSemaphore} is less than one
then L{ValueError} is raised. | test_semaphoreInvalidTokens | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelSemaphoreAfterAcquired(self):
"""
When canceling a L{Deferred} from a L{DeferredSemaphore} that
already has the semaphore, the cancel should have no effect.
"""
def _failOnErrback(_):
self.fail("Unexpected errback call!")
sem = defer.DeferredSemaphore(1)
d = sem.acquire()
d.addErrback(_failOnErrback)
d.cancel() | When canceling a L{Deferred} from a L{DeferredSemaphore} that
already has the semaphore, the cancel should have no effect. | test_cancelSemaphoreAfterAcquired | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelSemaphoreBeforeAcquired(self):
"""
When canceling a L{Deferred} from a L{DeferredSemaphore} that does
not yet have the semaphore (i.e., the L{Deferred} has not fired),
the cancel should cause a L{defer.CancelledError} failure.
"""
sem = defer.DeferredSemaphore(1)
sem.acquire()
d = sem.acquire()
d.cancel()
self.assertImmediateFailure(d, defer.CancelledError) | When canceling a L{Deferred} from a L{DeferredSemaphore} that does
not yet have the semaphore (i.e., the L{Deferred} has not fired),
the cancel should cause a L{defer.CancelledError} failure. | test_cancelSemaphoreBeforeAcquired | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelQueueAfterSynchronousGet(self):
"""
When canceling a L{Deferred} from a L{DeferredQueue} that already has
a result, the cancel should have no effect.
"""
def _failOnErrback(_):
self.fail("Unexpected errback call!")
queue = defer.DeferredQueue()
d = queue.get()
d.addErrback(_failOnErrback)
queue.put(None)
d.cancel() | When canceling a L{Deferred} from a L{DeferredQueue} that already has
a result, the cancel should have no effect. | test_cancelQueueAfterSynchronousGet | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelQueueAfterGet(self):
"""
When canceling a L{Deferred} from a L{DeferredQueue} that does not
have a result (i.e., the L{Deferred} has not fired), the cancel
causes a L{defer.CancelledError} failure. If the queue has a result
later on, it doesn't try to fire the deferred.
"""
queue = defer.DeferredQueue()
d = queue.get()
d.cancel()
self.assertImmediateFailure(d, defer.CancelledError)
def cb(ignore):
# If the deferred is still linked with the deferred queue, it will
# fail with an AlreadyCalledError
queue.put(None)
return queue.get().addCallback(self.assertIs, None)
d.addCallback(cb)
done = []
d.addCallback(done.append)
self.assertEqual(len(done), 1) | When canceling a L{Deferred} from a L{DeferredQueue} that does not
have a result (i.e., the L{Deferred} has not fired), the cancel
causes a L{defer.CancelledError} failure. If the queue has a result
later on, it doesn't try to fire the deferred. | test_cancelQueueAfterGet | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_waitUntilLockedWithNoLock(self):
"""
Test that the lock can be acquired when no lock is held
"""
d = self.lock.deferUntilLocked(timeout=1)
return d | Test that the lock can be acquired when no lock is held | test_waitUntilLockedWithNoLock | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_waitUntilLockedWithTimeoutLocked(self):
"""
Test that the lock can not be acquired when the lock is held
for longer than the timeout.
"""
self.assertTrue(self.lock.lock())
d = self.lock.deferUntilLocked(timeout=5.5)
self.assertFailure(d, defer.TimeoutError)
self.clock.pump([1] * 10)
return d | Test that the lock can not be acquired when the lock is held
for longer than the timeout. | test_waitUntilLockedWithTimeoutLocked | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_waitUntilLockedWithTimeoutUnlocked(self):
"""
Test that a lock can be acquired while a lock is held
but the lock is unlocked before our timeout.
"""
def onTimeout(f):
f.trap(defer.TimeoutError)
self.fail("Should not have timed out")
self.assertTrue(self.lock.lock())
self.clock.callLater(1, self.lock.unlock)
d = self.lock.deferUntilLocked(timeout=10)
d.addErrback(onTimeout)
self.clock.pump([1] * 10)
return d | Test that a lock can be acquired while a lock is held
but the lock is unlocked before our timeout. | test_waitUntilLockedWithTimeoutUnlocked | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_defaultScheduler(self):
"""
Test that the default scheduler is set up properly.
"""
lock = defer.DeferredFilesystemLock(self.mktemp())
self.assertEqual(lock._scheduler, reactor) | Test that the default scheduler is set up properly. | test_defaultScheduler | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_concurrentUsage(self):
"""
Test that an appropriate exception is raised when attempting
to use deferUntilLocked concurrently.
"""
self.lock.lock()
self.clock.callLater(1, self.lock.unlock)
d = self.lock.deferUntilLocked()
d2 = self.lock.deferUntilLocked()
self.assertFailure(d2, defer.AlreadyTryingToLockError)
self.clock.advance(1)
return d | Test that an appropriate exception is raised when attempting
to use deferUntilLocked concurrently. | test_concurrentUsage | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_multipleUsages(self):
"""
Test that a DeferredFilesystemLock can be used multiple times
"""
def lockAquired(ign):
self.lock.unlock()
d = self.lock.deferUntilLocked()
return d
self.lock.lock()
self.clock.callLater(1, self.lock.unlock)
d = self.lock.deferUntilLocked()
d.addCallback(lockAquired)
self.clock.advance(1)
return d | Test that a DeferredFilesystemLock can be used multiple times | test_multipleUsages | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelDeferUntilLocked(self):
"""
When cancelling a L{defer.Deferred} returned by
L{defer.DeferredFilesystemLock.deferUntilLocked}, the
L{defer.DeferredFilesystemLock._tryLockCall} is cancelled.
"""
self.lock.lock()
deferred = self.lock.deferUntilLocked()
tryLockCall = self.lock._tryLockCall
deferred.cancel()
self.assertFalse(tryLockCall.active())
self.assertIsNone(self.lock._tryLockCall)
self.failureResultOf(deferred, defer.CancelledError) | When cancelling a L{defer.Deferred} returned by
L{defer.DeferredFilesystemLock.deferUntilLocked}, the
L{defer.DeferredFilesystemLock._tryLockCall} is cancelled. | test_cancelDeferUntilLocked | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelDeferUntilLockedWithTimeout(self):
"""
When cancel a L{defer.Deferred} returned by
L{defer.DeferredFilesystemLock.deferUntilLocked}, if the timeout is
set, the timeout call will be cancelled.
"""
self.lock.lock()
deferred = self.lock.deferUntilLocked(timeout=1)
timeoutCall = self.lock._timeoutCall
deferred.cancel()
self.assertFalse(timeoutCall.active())
self.assertIsNone(self.lock._timeoutCall)
self.failureResultOf(deferred, defer.CancelledError) | When cancel a L{defer.Deferred} returned by
L{defer.DeferredFilesystemLock.deferUntilLocked}, if the timeout is
set, the timeout call will be cancelled. | test_cancelDeferUntilLockedWithTimeout | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def _overrideFunc(v, t):
"""
Private function to be used to pass as an alternate onTimeoutCancel value
to timeoutDeferred
"""
return "OVERRIDDEN" | Private function to be used to pass as an alternate onTimeoutCancel value
to timeoutDeferred | _overrideFunc | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_timeoutChainable(self):
"""
L{defer.Deferred.addTimeout} returns its own L{defer.Deferred} so it
can be called in a callback chain.
"""
d = defer.Deferred().addTimeout(5, Clock()).addCallback(lambda _: "done")
d.callback(None)
self.assertEqual("done", self.successResultOf(d)) | L{defer.Deferred.addTimeout} returns its own L{defer.Deferred} so it
can be called in a callback chain. | test_timeoutChainable | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_successResultBeforeTimeout(self):
"""
The L{defer.Deferred} callbacks with the result if it succeeds before
the timeout. No cancellation happens after the callback either,
which could also cancel inner deferreds.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock)
# addTimeout is added first so that if d is timed out, d would be
# canceled before innerDeferred gets returned from an callback on d
innerDeferred = defer.Deferred()
dCallbacked = [None]
def onCallback(results):
dCallbacked[0] = results
return innerDeferred
d.addCallback(onCallback)
d.callback("results")
# d is callbacked immediately, before innerDeferred is returned from
# the callback on d
self.assertIsNot(None, dCallbacked[0])
self.assertEqual(dCallbacked[0], "results")
# The timeout never happens - if it did, d would have been cancelled,
# which would cancel innerDeferred too.
clock.advance(15)
self.assertNoResult(innerDeferred) | The L{defer.Deferred} callbacks with the result if it succeeds before
the timeout. No cancellation happens after the callback either,
which could also cancel inner deferreds. | test_successResultBeforeTimeout | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_successResultBeforeTimeoutCustom(self):
"""
The L{defer.Deferred} callbacks with the result if it succeeds before
the timeout, even if a custom C{onTimeoutCancel} function is provided.
No cancellation happens after the callback either, which could also
cancel inner deferreds.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock, onTimeoutCancel=_overrideFunc)
# addTimeout is added first so that if d is timed out, d would be
# canceled before innerDeferred gets returned from an callback on d
innerDeferred = defer.Deferred()
dCallbacked = [None]
def onCallback(results):
dCallbacked[0] = results
return innerDeferred
d.addCallback(onCallback)
d.callback("results")
# d is callbacked immediately, before innerDeferred is returned from
# the callback on d
self.assertIsNot(None, dCallbacked[0])
self.assertEqual(dCallbacked[0], "results")
# The timeout never happens - if it did, d would have been cancelled,
# which would cancel innerDeferred too
clock.advance(15)
self.assertNoResult(innerDeferred) | The L{defer.Deferred} callbacks with the result if it succeeds before
the timeout, even if a custom C{onTimeoutCancel} function is provided.
No cancellation happens after the callback either, which could also
cancel inner deferreds. | test_successResultBeforeTimeoutCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_failureBeforeTimeout(self):
"""
The L{defer.Deferred} errbacks with the failure if it fails before the
timeout. No cancellation happens after the errback either, which
could also cancel inner deferreds.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock)
# addTimeout is added first so that if d is timed out, d would be
# canceled before innerDeferred gets returned from an errback on d
innerDeferred = defer.Deferred()
dErrbacked = [None]
error = ValueError("fail")
def onErrback(f):
dErrbacked[0] = f
return innerDeferred
d.addErrback(onErrback)
d.errback(error)
# d is errbacked immediately, before innerDeferred is returned from the
# errback on d
self.assertIsInstance(dErrbacked[0], failure.Failure)
self.assertIs(dErrbacked[0].value, error)
# The timeout never happens - if it did, d would have been cancelled,
# which would cancel innerDeferred too
clock.advance(15)
self.assertNoResult(innerDeferred) | The L{defer.Deferred} errbacks with the failure if it fails before the
timeout. No cancellation happens after the errback either, which
could also cancel inner deferreds. | test_failureBeforeTimeout | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_failureBeforeTimeoutCustom(self):
"""
The L{defer.Deferred} errbacks with the failure if it fails before the
timeout, even if using a custom C{onTimeoutCancel} function.
No cancellation happens after the errback either, which could also
cancel inner deferreds.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock, onTimeoutCancel=_overrideFunc)
# addTimeout is added first so that if d is timed out, d would be
# canceled before innerDeferred gets returned from an errback on d
innerDeferred = defer.Deferred()
dErrbacked = [None]
error = ValueError("fail")
def onErrback(f):
dErrbacked[0] = f
return innerDeferred
d.addErrback(onErrback)
d.errback(error)
# d is errbacked immediately, before innerDeferred is returned from the
# errback on d
self.assertIsInstance(dErrbacked[0], failure.Failure)
self.assertIs(dErrbacked[0].value, error)
# The timeout never happens - if it did, d would have been cancelled,
# which would cancel innerDeferred too
clock.advance(15)
self.assertNoResult(innerDeferred) | The L{defer.Deferred} errbacks with the failure if it fails before the
timeout, even if using a custom C{onTimeoutCancel} function.
No cancellation happens after the errback either, which could also
cancel inner deferreds. | test_failureBeforeTimeoutCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_timedOut(self):
"""
The L{defer.Deferred} by default errbacks with a L{defer.TimeoutError}
if it times out before callbacking or errbacking.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock)
self.assertNoResult(d)
clock.advance(15)
self.failureResultOf(d, defer.TimeoutError) | The L{defer.Deferred} by default errbacks with a L{defer.TimeoutError}
if it times out before callbacking or errbacking. | test_timedOut | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_timedOutCustom(self):
"""
If a custom C{onTimeoutCancel] function is provided, the
L{defer.Deferred} returns the custom function's return value if the
L{defer.Deferred} times out before callbacking or errbacking.
The custom C{onTimeoutCancel} function can return a result instead of
a failure.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock, onTimeoutCancel=_overrideFunc)
self.assertNoResult(d)
clock.advance(15)
self.assertEqual("OVERRIDDEN", self.successResultOf(d)) | If a custom C{onTimeoutCancel] function is provided, the
L{defer.Deferred} returns the custom function's return value if the
L{defer.Deferred} times out before callbacking or errbacking.
The custom C{onTimeoutCancel} function can return a result instead of
a failure. | test_timedOutCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_timedOutProvidedCancelSuccess(self):
"""
If a cancellation function is provided when the L{defer.Deferred} is
initialized, the L{defer.Deferred} returns the cancellation value's
non-failure return value when the L{defer.Deferred} times out.
"""
clock = Clock()
d = defer.Deferred(lambda c: c.callback('I was cancelled!'))
d.addTimeout(10, clock)
self.assertNoResult(d)
clock.advance(15)
self.assertEqual(self.successResultOf(d), 'I was cancelled!') | If a cancellation function is provided when the L{defer.Deferred} is
initialized, the L{defer.Deferred} returns the cancellation value's
non-failure return value when the L{defer.Deferred} times out. | test_timedOutProvidedCancelSuccess | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_timedOutProvidedCancelFailure(self):
"""
If a cancellation function is provided when the L{defer.Deferred} is
initialized, the L{defer.Deferred} returns the cancellation value's
non-L{CanceledError} failure when the L{defer.Deferred} times out.
"""
clock = Clock()
error = ValueError('what!')
d = defer.Deferred(lambda c: c.errback(error))
d.addTimeout(10, clock)
self.assertNoResult(d)
clock.advance(15)
f = self.failureResultOf(d, ValueError)
self.assertIs(f.value, error) | If a cancellation function is provided when the L{defer.Deferred} is
initialized, the L{defer.Deferred} returns the cancellation value's
non-L{CanceledError} failure when the L{defer.Deferred} times out. | test_timedOutProvidedCancelFailure | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelBeforeTimeout(self):
"""
If the L{defer.Deferred} is manually cancelled before the timeout, it
is not re-cancelled (no L{AlreadyCancelled} error, and also no
canceling of inner deferreds), and the default C{onTimeoutCancel}
function is not called, preserving the original L{CancelledError}.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock)
# addTimeout is added first so that if d is timed out, d would be
# canceled before innerDeferred gets returned from an errback on d
innerDeferred = defer.Deferred()
dCanceled = [None]
def onErrback(f):
dCanceled[0] = f
return innerDeferred
d.addErrback(onErrback)
d.cancel()
# d is cancelled immediately, before innerDeferred is returned from the
# errback on d
self.assertIsInstance(dCanceled[0], failure.Failure)
self.assertIs(dCanceled[0].type, defer.CancelledError)
# The timeout never happens - if it did, d would have been cancelled
# again, which would cancel innerDeferred too
clock.advance(15)
self.assertNoResult(innerDeferred) | If the L{defer.Deferred} is manually cancelled before the timeout, it
is not re-cancelled (no L{AlreadyCancelled} error, and also no
canceling of inner deferreds), and the default C{onTimeoutCancel}
function is not called, preserving the original L{CancelledError}. | test_cancelBeforeTimeout | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_cancelBeforeTimeoutCustom(self):
"""
If the L{defer.Deferred} is manually cancelled before the timeout, it
is not re-cancelled (no L{AlreadyCancelled} error, and also no
canceling of inner deferreds), and the custom C{onTimeoutCancel}
function is not called, preserving the original L{CancelledError}.
"""
clock = Clock()
d = defer.Deferred()
d.addTimeout(10, clock, onTimeoutCancel=_overrideFunc)
# addTimeout is added first so that if d is timed out, d would be
# canceled before innerDeferred gets returned from an errback on d
innerDeferred = defer.Deferred()
dCanceled = [None]
def onErrback(f):
dCanceled[0] = f
return innerDeferred
d.addErrback(onErrback)
d.cancel()
# d is cancelled immediately, before innerDeferred is returned from the
# errback on d
self.assertIsInstance(dCanceled[0], failure.Failure)
self.assertIs(dCanceled[0].type, defer.CancelledError)
# The timeout never happens - if it did, d would have been cancelled
# again, which would cancel innerDeferred too
clock.advance(15)
self.assertNoResult(innerDeferred) | If the L{defer.Deferred} is manually cancelled before the timeout, it
is not re-cancelled (no L{AlreadyCancelled} error, and also no
canceling of inner deferreds), and the custom C{onTimeoutCancel}
function is not called, preserving the original L{CancelledError}. | test_cancelBeforeTimeoutCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_providedCancelCalledBeforeTimeoutCustom(self):
"""
A custom translation function can handle a L{defer.Deferred} with a
custom cancellation function.
"""
clock = Clock()
d = defer.Deferred(lambda c: c.errback(ValueError('what!')))
d.addTimeout(10, clock, onTimeoutCancel=_overrideFunc)
self.assertNoResult(d)
clock.advance(15)
self.assertEqual("OVERRIDDEN", self.successResultOf(d)) | A custom translation function can handle a L{defer.Deferred} with a
custom cancellation function. | test_providedCancelCalledBeforeTimeoutCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errbackAddedBeforeTimeout(self):
"""
An errback added before a timeout is added errbacks with a
L{defer.CancelledError} when the timeout fires. If the
errback returns the L{defer.CancelledError}, it is translated
to a L{defer.TimeoutError} by the timeout implementation.
"""
clock = Clock()
d = defer.Deferred()
dErrbacked = [None]
def errback(f):
dErrbacked[0] = f
return f
d.addErrback(errback)
d.addTimeout(10, clock)
clock.advance(15)
self.assertIsInstance(dErrbacked[0], failure.Failure)
self.assertIsInstance(dErrbacked[0].value, defer.CancelledError)
self.failureResultOf(d, defer.TimeoutError) | An errback added before a timeout is added errbacks with a
L{defer.CancelledError} when the timeout fires. If the
errback returns the L{defer.CancelledError}, it is translated
to a L{defer.TimeoutError} by the timeout implementation. | test_errbackAddedBeforeTimeout | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errbackAddedBeforeTimeoutSuppressesCancellation(self):
"""
An errback added before a timeout is added errbacks with a
L{defer.CancelledError} when the timeout fires. If the
errback suppresses the L{defer.CancelledError}, the deferred
successfully completes.
"""
clock = Clock()
d = defer.Deferred()
dErrbacked = [None]
def errback(f):
dErrbacked[0] = f
f.trap(defer.CancelledError)
d.addErrback(errback)
d.addTimeout(10, clock)
clock.advance(15)
self.assertIsInstance(dErrbacked[0], failure.Failure)
self.assertIsInstance(dErrbacked[0].value, defer.CancelledError)
self.successResultOf(d) | An errback added before a timeout is added errbacks with a
L{defer.CancelledError} when the timeout fires. If the
errback suppresses the L{defer.CancelledError}, the deferred
successfully completes. | test_errbackAddedBeforeTimeoutSuppressesCancellation | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errbackAddedBeforeTimeoutCustom(self):
"""
An errback added before a timeout is added with a custom
timeout function errbacks with a L{defer.CancelledError} when
the timeout fires. The timeout function runs if the errback
returns the L{defer.CancelledError}.
"""
clock = Clock()
d = defer.Deferred()
dErrbacked = [None]
def errback(f):
dErrbacked[0] = f
return f
d.addErrback(errback)
d.addTimeout(10, clock, _overrideFunc)
clock.advance(15)
self.assertIsInstance(dErrbacked[0], failure.Failure)
self.assertIsInstance(dErrbacked[0].value, defer.CancelledError)
self.assertEqual("OVERRIDDEN", self.successResultOf(d)) | An errback added before a timeout is added with a custom
timeout function errbacks with a L{defer.CancelledError} when
the timeout fires. The timeout function runs if the errback
returns the L{defer.CancelledError}. | test_errbackAddedBeforeTimeoutCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_errbackAddedBeforeTimeoutSuppressesCancellationCustom(self):
"""
An errback added before a timeout is added with a custom
timeout function errbacks with a L{defer.CancelledError} when
the timeout fires. The timeout function runs if the errback
suppresses the L{defer.CancelledError}.
"""
clock = Clock()
d = defer.Deferred()
dErrbacked = [None]
def errback(f):
dErrbacked[0] = f
d.addErrback(errback)
d.addTimeout(10, clock, _overrideFunc)
clock.advance(15)
self.assertIsInstance(dErrbacked[0], failure.Failure)
self.assertIsInstance(dErrbacked[0].value, defer.CancelledError)
self.assertEqual("OVERRIDDEN", self.successResultOf(d)) | An errback added before a timeout is added with a custom
timeout function errbacks with a L{defer.CancelledError} when
the timeout fires. The timeout function runs if the errback
suppresses the L{defer.CancelledError}. | test_errbackAddedBeforeTimeoutSuppressesCancellationCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_callbackAddedToCancelerBeforeTimeout(self):
"""
Given a deferred with a cancellation function that resumes the
callback chain, a callback that is added to the deferred
before a timeout is added to runs when the timeout fires. The
deferred completes successfully, without a
L{defer.TimeoutError}.
"""
clock = Clock()
success = "success"
d = defer.Deferred(lambda d: d.callback(success))
dCallbacked = [None]
def callback(value):
dCallbacked[0] = value
return value
d.addCallback(callback)
d.addTimeout(10, clock)
clock.advance(15)
self.assertEqual(dCallbacked[0], success)
self.assertIs(success, self.successResultOf(d)) | Given a deferred with a cancellation function that resumes the
callback chain, a callback that is added to the deferred
before a timeout is added to runs when the timeout fires. The
deferred completes successfully, without a
L{defer.TimeoutError}. | test_callbackAddedToCancelerBeforeTimeout | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_callbackAddedToCancelerBeforeTimeoutCustom(self):
"""
Given a deferred with a cancellation function that resumes the
callback chain, a callback that is added to the deferred
before a timeout is added to runs when the timeout fires. The
deferred completes successfully, without a
L{defer.TimeoutError}. The timeout's custom timeout function
also runs.
"""
clock = Clock()
success = "success"
d = defer.Deferred(lambda d: d.callback(success))
dCallbacked = [None]
def callback(value):
dCallbacked[0] = value
return value
d.addCallback(callback)
d.addTimeout(10, clock, onTimeoutCancel=_overrideFunc)
clock.advance(15)
self.assertEqual(dCallbacked[0], success)
self.assertEqual("OVERRIDDEN", self.successResultOf(d)) | Given a deferred with a cancellation function that resumes the
callback chain, a callback that is added to the deferred
before a timeout is added to runs when the timeout fires. The
deferred completes successfully, without a
L{defer.TimeoutError}. The timeout's custom timeout function
also runs. | test_callbackAddedToCancelerBeforeTimeoutCustom | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_passesThroughDeferreds(self):
"""
L{defer.ensureDeferred} will pass through a Deferred unchanged.
"""
d = defer.Deferred()
d2 = defer.ensureDeferred(d)
self.assertIs(d, d2) | L{defer.ensureDeferred} will pass through a Deferred unchanged. | test_passesThroughDeferreds | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_willNotAllowNonDeferredOrCoroutine(self):
"""
Passing L{defer.ensureDeferred} a non-coroutine and a non-Deferred will
raise a L{ValueError}.
"""
with self.assertRaises(ValueError):
defer.ensureDeferred("something") | Passing L{defer.ensureDeferred} a non-coroutine and a non-Deferred will
raise a L{ValueError}. | test_willNotAllowNonDeferredOrCoroutine | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_deprecatedTimeout(self):
"""
L{twisted.internet.defer.timeout} is deprecated.
"""
deferred = defer.Deferred()
defer.timeout(deferred)
self.assertFailure(deferred, defer.TimeoutError)
warningsShown = self.flushWarnings([self.test_deprecatedTimeout])
self.assertEqual(len(warningsShown), 1)
self.assertIs(warningsShown[0]['category'], DeprecationWarning)
self.assertEqual(
warningsShown[0]['message'],
'twisted.internet.defer.timeout was deprecated in Twisted 17.1.0;'
' please use twisted.internet.defer.Deferred.addTimeout instead') | L{twisted.internet.defer.timeout} is deprecated. | test_deprecatedTimeout | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def callAllSoonCalls(loop):
"""
Tickle an asyncio event loop to call all of the things scheduled with
call_soon, inasmuch as this can be done via the public API.
@param loop: The asyncio event loop to flush the previously-called
C{call_soon} entries from.
"""
loop.call_soon(loop.stop)
loop.run_forever() | Tickle an asyncio event loop to call all of the things scheduled with
call_soon, inasmuch as this can be done via the public API.
@param loop: The asyncio event loop to flush the previously-called
C{call_soon} entries from. | callAllSoonCalls | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_asFuture(self):
"""
L{defer.Deferred.asFuture} returns a L{asyncio.Future} which fires when
the given L{defer.Deferred} does.
"""
d = defer.Deferred()
loop = new_event_loop()
aFuture = d.asFuture(loop)
self.assertEqual(aFuture.done(), False)
d.callback(13)
callAllSoonCalls(loop)
self.assertEqual(self.successResultOf(d), None)
self.assertEqual(aFuture.result(), 13) | L{defer.Deferred.asFuture} returns a L{asyncio.Future} which fires when
the given L{defer.Deferred} does. | test_asFuture | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_asFutureCancelFuture(self):
"""
L{defer.Deferred.asFuture} returns a L{asyncio.Future} which, when
cancelled, will cancel the original L{defer.Deferred}.
"""
def canceler(dprime):
canceler.called = True
canceler.called = False
d = defer.Deferred(canceler)
loop = new_event_loop()
aFuture = d.asFuture(loop)
aFuture.cancel()
callAllSoonCalls(loop)
self.assertEqual(canceler.called, True)
self.assertEqual(self.successResultOf(d), None)
self.assertRaises(CancelledError, aFuture.result) | L{defer.Deferred.asFuture} returns a L{asyncio.Future} which, when
cancelled, will cancel the original L{defer.Deferred}. | test_asFutureCancelFuture | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_asFutureSuccessCancel(self):
"""
While Futures don't support succeeding in response to cancellation,
Deferreds do; if a Deferred is coerced into a success by a Future
cancellation, that should just be ignored.
"""
def canceler(dprime):
dprime.callback(9)
d = defer.Deferred(canceler)
loop = new_event_loop()
aFuture = d.asFuture(loop)
aFuture.cancel()
callAllSoonCalls(loop)
self.assertEqual(self.successResultOf(d), None)
self.assertRaises(CancelledError, aFuture.result) | While Futures don't support succeeding in response to cancellation,
Deferreds do; if a Deferred is coerced into a success by a Future
cancellation, that should just be ignored. | test_asFutureSuccessCancel | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_asFutureFailure(self):
"""
L{defer.Deferred.asFuture} makes a L{asyncio.Future} fire with an
exception when the given L{defer.Deferred} does.
"""
d = defer.Deferred()
theFailure = failure.Failure(ZeroDivisionError())
loop = new_event_loop()
future = d.asFuture(loop)
callAllSoonCalls(loop)
d.errback(theFailure)
callAllSoonCalls(loop)
self.assertRaises(ZeroDivisionError, future.result) | L{defer.Deferred.asFuture} makes a L{asyncio.Future} fire with an
exception when the given L{defer.Deferred} does. | test_asFutureFailure | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_fromFuture(self):
"""
L{defer.Deferred.fromFuture} returns a L{defer.Deferred} that fires
when the given L{asyncio.Future} does.
"""
loop = new_event_loop()
aFuture = Future(loop=loop)
d = defer.Deferred.fromFuture(aFuture)
self.assertNoResult(d)
aFuture.set_result(7)
callAllSoonCalls(loop)
self.assertEqual(self.successResultOf(d), 7) | L{defer.Deferred.fromFuture} returns a L{defer.Deferred} that fires
when the given L{asyncio.Future} does. | test_fromFuture | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_fromFutureFutureCancelled(self):
"""
L{defer.Deferred.fromFuture} makes a L{defer.Deferred} fire with
an L{asyncio.CancelledError} when the given
L{asyncio.Future} is cancelled.
"""
loop = new_event_loop()
cancelled = Future(loop=loop)
d = defer.Deferred.fromFuture(cancelled)
cancelled.cancel()
callAllSoonCalls(loop)
self.assertRaises(CancelledError, cancelled.result)
self.failureResultOf(d).trap(CancelledError) | L{defer.Deferred.fromFuture} makes a L{defer.Deferred} fire with
an L{asyncio.CancelledError} when the given
L{asyncio.Future} is cancelled. | test_fromFutureFutureCancelled | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def test_fromFutureDeferredCancelled(self):
"""
L{defer.Deferred.fromFuture} makes a L{defer.Deferred} which, when
cancelled, cancels the L{asyncio.Future} it was created from.
"""
loop = new_event_loop()
cancelled = Future(loop=loop)
d = defer.Deferred.fromFuture(cancelled)
d.cancel()
callAllSoonCalls(loop)
self.assertEqual(cancelled.cancelled(), True)
self.assertRaises(CancelledError, cancelled.result)
self.failureResultOf(d).trap(CancelledError) | L{defer.Deferred.fromFuture} makes a L{defer.Deferred} which, when
cancelled, cancels the L{asyncio.Future} it was created from. | test_fromFutureDeferredCancelled | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defer.py | MIT |
def testBasics(self):
"""
Test that a normal deferredGenerator works. Tests yielding a
deferred which callbacks, as well as a deferred errbacks. Also
ensures returning a final value works.
"""
return self._genBasics().addCallback(self.assertEqual, 'WOOSH') | Test that a normal deferredGenerator works. Tests yielding a
deferred which callbacks, as well as a deferred errbacks. Also
ensures returning a final value works. | testBasics | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
def testBuggy(self):
"""
Ensure that a buggy generator properly signals a Failure
condition on result deferred.
"""
return self.assertFailure(self._genBuggy(), ZeroDivisionError) | Ensure that a buggy generator properly signals a Failure
condition on result deferred. | testBuggy | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
def testNothing(self):
"""Test that a generator which never yields results in None."""
return self._genNothing().addCallback(self.assertEqual, None) | Test that a generator which never yields results in None. | testNothing | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
def testHandledTerminalFailure(self):
"""
Create a Deferred Generator which yields a Deferred which fails and
handles the exception which results. Assert that the Deferred
Generator does not errback its Deferred.
"""
return self._genHandledTerminalFailure().addCallback(self.assertEqual, None) | Create a Deferred Generator which yields a Deferred which fails and
handles the exception which results. Assert that the Deferred
Generator does not errback its Deferred. | testHandledTerminalFailure | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
def testHandledTerminalAsyncFailure(self):
"""
Just like testHandledTerminalFailure, only with a Deferred which fires
asynchronously with an error.
"""
d = defer.Deferred()
deferredGeneratorResultDeferred = self._genHandledTerminalAsyncFailure(d)
d.errback(TerminalException("Handled Terminal Failure"))
return deferredGeneratorResultDeferred.addCallback(
self.assertEqual, None) | Just like testHandledTerminalFailure, only with a Deferred which fires
asynchronously with an error. | testHandledTerminalAsyncFailure | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
def testStackUsage(self):
"""
Make sure we don't blow the stack when yielding immediately
available deferreds.
"""
return self._genStackUsage().addCallback(self.assertEqual, 0) | Make sure we don't blow the stack when yielding immediately
available deferreds. | testStackUsage | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
def testStackUsage2(self):
"""
Make sure we don't blow the stack when yielding immediately
available values.
"""
return self._genStackUsage2().addCallback(self.assertEqual, 0) | Make sure we don't blow the stack when yielding immediately
available values. | testStackUsage2 | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
def deprecatedDeferredGenerator(f):
"""
Calls L{deferredGenerator} while suppressing the deprecation warning.
@param f: Function to call
@return: Return value of function.
"""
return runWithWarningsSuppressed(
[ SUPPRESS(message="twisted.internet.defer.deferredGenerator was "
"deprecated") ],
deferredGenerator, f) | Calls L{deferredGenerator} while suppressing the deprecation warning.
@param f: Function to call
@return: Return value of function. | deprecatedDeferredGenerator | python | wistbean/learn_python3_spider | stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | https://github.com/wistbean/learn_python3_spider/blob/master/stackoverflow/venv/lib/python3.6/site-packages/twisted/test/test_defgen.py | MIT |
Subsets and Splits