Ho qualche codice Twisted che crea più catene di differite. Alcuni di questi potrebbero fallire senza avere una battuta d'arresto che li riporta alla catena di callback. Non sono stato in grado di scrivere un test unitario per questo codice: il mancato rinvio fa sì che il test non riesca dopo che il codice del test è stato completato. Come posso scrivere un test di unità passeggero per questo codice? È previsto che ogni Deferred che potrebbe fallire nel normale funzionamento dovrebbe avere una battuta d'arresto alla fine della catena che la reinserisce nella catena di callback?In che modo è possibile testare gli errori deviati e deviati senza errori?
La stessa cosa accade quando c'è un Deferred fallito in una DeferredList, a meno che non crei la DeferredList con ConsumeErrors. Questo è il caso anche quando DeferredList viene creato con fireOnOneErrback e viene restituito un errore che lo rimette nella catena di callback. Ci sono implicazioni per ConsumeErrors oltre a sopprimere i fallimenti dei test e la registrazione degli errori? Ogni Deferred che può fallire senza un errback deve essere inserito in DeferredList?
test esempio di codice di esempio:
from twisted.trial import unittest
from twisted.internet import defer
def get_dl(**kwargs):
"Return a DeferredList with a failure and any kwargs given."
return defer.DeferredList(
[defer.succeed(True), defer.fail(ValueError()), defer.succeed(True)],
**kwargs)
def two_deferreds():
"Create a failing Deferred, and create and return a succeeding Deferred."
d = defer.fail(ValueError())
return defer.succeed(True)
class DeferredChainTest(unittest.TestCase):
def check_success(self, result):
"If we're called, we're on the callback chain."
self.fail()
def check_error(self, failure):
"""
If we're called, we're on the errback chain.
Return to put us back on the callback chain.
"""
return True
def check_error_fail(self, failure):
"""
If we're called, we're on the errback chain.
"""
self.fail()
# This fails after all callbacks and errbacks have been run, with the
# ValueError from the failed defer, even though we're
# not on the errback chain.
def test_plain(self):
"""
Test that a DeferredList without arguments is on the callback chain.
"""
# check_error_fail asserts that we are on the callback chain.
return get_dl().addErrback(self.check_error_fail)
# This fails after all callbacks and errbacks have been run, with the
# ValueError from the failed defer, even though we're
# not on the errback chain.
def test_fire(self):
"""
Test that a DeferredList with fireOnOneErrback errbacks on failure,
and that an errback puts it back on the callback chain.
"""
# check_success asserts that we don't callback.
# check_error_fail asserts that we are on the callback chain.
return get_dl(fireOnOneErrback=True).addCallbacks(
self.check_success, self.check_error).addErrback(
self.check_error_fail)
# This succeeds.
def test_consume(self):
"""
Test that a DeferredList with consumeErrors errbacks on failure,
and that an errback puts it back on the callback chain.
"""
# check_error_fail asserts that we are on the callback chain.
return get_dl(consumeErrors=True).addErrback(self.check_error_fail)
# This succeeds.
def test_fire_consume(self):
"""
Test that a DeferredList with fireOnOneCallback and consumeErrors
errbacks on failure, and that an errback puts it back on the
callback chain.
"""
# check_success asserts that we don't callback.
# check_error_fail asserts that we are on the callback chain.
return get_dl(fireOnOneErrback=True, consumeErrors=True).addCallbacks(
self.check_success, self.check_error).addErrback(
self.check_error_fail)
# This fails after all callbacks and errbacks have been run, with the
# ValueError from the failed defer, even though we're
# not on the errback chain.
def test_two_deferreds(self):
# check_error_fail asserts that we are on the callback chain.
return two_deferreds().addErrback(self.check_error_fail)
Ottima risposta, ma si potrebbe anche voler menzionare '--force-gc'. – Glyph
Buona chiamata, aggiunta. –
Ciò accade anche quando si chiama log.err con un'istanza di errore, corretta? – Chris