मेरे पास कुछ ट्विस्ट कोड है जो डिफर्रेड की कई श्रृंखलाएं बनाता है। इनमें से कुछ बिना किसी त्रुटि के विफल हो सकते हैं जो उन्हें कॉलबैक श्रृंखला पर वापस रखता है। मैं इस कोड के लिए यूनिट टेस्ट लिखने में सक्षम नहीं हूं - असफल डिफरर्ड परीक्षण कोड पूरा होने के बाद परीक्षण विफल होने का कारण बनता है। मैं इस कोड के लिए पासिंग यूनिट टेस्ट कैसे लिख सकता हूं? क्या यह उम्मीद की जाती है कि सामान्य ऑपरेशन में असफल होने वाले प्रत्येक डिफरर्ड को श्रृंखला के अंत में एक गलती होनी चाहिए जो इसे वापस कॉलबैक श्रृंखला पर रखे?बिना किसी त्रुटि के ट्विस्ट डिफरर्ड त्रुटियों को परीक्षण के साथ परीक्षण किया जा सकता है?
वही बात तब होती है जब एक DeferredList में विफल विफल रहता है, जब तक कि मैं उपभोक्ता के साथ DeferredList नहीं बना देता। यह तब भी मामला है जब DeferredList को आग के साथ बनाया गया हैऑनऑनब्रेबैक और उसे एक त्रुटि दी गई है जो इसे वापस कॉलबैक श्रृंखला पर रखती है। परीक्षण विफलताओं और त्रुटि लॉगिंग दबाने के अलावा उपभोग करने वालों के लिए कोई प्रभाव है? क्या हर डिफर्ड जो किसी त्रुटि के बिना असफल हो सकता है उसे डिफरर्डलिस्ट डाला जाना चाहिए?
उदाहरण उदाहरण कोड का परीक्षण:
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)
शानदार उत्तर, लेकिन आप '--force-gc' का भी उल्लेख करना चाहेंगे। – Glyph
अच्छी कॉल, जोड़ा गया। –
यह विफलता उदाहरण के साथ log.err को कॉल करते समय भी होता है, सही? – Chris