<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> import warnings <EOL> import gc , traceback <EOL> import re <EOL> from twisted . python import failure , log <EOL> from twisted . python . compat import _PY3 <EOL> from twisted . internet import defer , reactor <EOL> from twisted . internet . task import Clock <EOL> from twisted . trial import unittest <EOL> class GenericError ( Exception ) : <EOL> pass <EOL> def getDivisionFailure ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> <NUM_LIT:1> / <NUM_LIT:0> <EOL> except : <EOL> f = failure . Failure ( * args , ** kwargs ) <EOL> return f <EOL> def fakeCallbackCanceller ( deferred ) : <EOL> """<STR_LIT>""" <EOL> deferred . callback ( "<STR_LIT>" ) <EOL> class ImmediateFailureMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def assertImmediateFailure ( self , deferred , exception ) : <EOL> """<STR_LIT>""" <EOL> failures = [ ] <EOL> deferred . addErrback ( failures . append ) <EOL> self . assertEqual ( len ( failures ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( failures [ <NUM_LIT:0> ] . check ( exception ) ) <EOL> return failures [ <NUM_LIT:0> ] . value <EOL> class UtilTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_logErrorReturnsError ( self ) : <EOL> """<STR_LIT>""" <EOL> error = failure . Failure ( RuntimeError ( ) ) <EOL> result = defer . logError ( error ) <EOL> self . flushLoggedErrors ( RuntimeError ) <EOL> self . assertIs ( error , result ) <EOL> def test_logErrorLogsError ( self ) : <EOL> """<STR_LIT>""" <EOL> error = failure . Failure ( RuntimeError ( ) ) <EOL> defer . logError ( error ) <EOL> errors = self . flushLoggedErrors ( RuntimeError ) <EOL> self . assertEqual ( errors , [ error ] ) <EOL> def test_logErrorLogsErrorNoRepr ( self ) : <EOL> """<STR_LIT>""" <EOL> output = [ ] <EOL> def emit ( eventDict ) : <EOL> output . append ( log . textFromEventDict ( eventDict ) ) <EOL> log . addObserver ( emit ) <EOL> error = failure . Failure ( RuntimeError ( ) ) <EOL> defer . logError ( error ) <EOL> self . flushLoggedErrors ( RuntimeError ) <EOL> self . assertTrue ( output [ <NUM_LIT:0> ] . startswith ( "<STR_LIT>" ) ) <EOL> class DeferredTests ( unittest . SynchronousTestCase , ImmediateFailureMixin ) : <EOL> def setUp ( self ) : <EOL> self . callbackResults = None <EOL> self . errbackResults = None <EOL> self . callback2Results = None <EOL> self . addCleanup ( defer . setDebugging , defer . getDebugging ( ) ) <EOL> def _callback ( self , * args , ** kw ) : <EOL> self . callbackResults = args , kw <EOL> return args [ <NUM_LIT:0> ] <EOL> def _callback2 ( self , * args , ** kw ) : <EOL> self . callback2Results = args , kw <EOL> def _errback ( self , * args , ** kw ) : <EOL> self . errbackResults = args , kw <EOL> def testCallbackWithoutArgs ( self ) : <EOL> deferred = defer . Deferred ( ) <EOL> deferred . addCallback ( self . _callback ) <EOL> deferred . callback ( "<STR_LIT:hello>" ) <EOL> self . assertEqual ( self . errbackResults , None ) <EOL> self . assertEqual ( self . callbackResults , ( ( '<STR_LIT:hello>' , ) , { } ) ) <EOL> def testCallbackWithArgs ( self ) : <EOL> deferred = defer . Deferred ( ) <EOL> deferred . addCallback ( self . _callback , "<STR_LIT>" ) <EOL> deferred . callback ( "<STR_LIT:hello>" ) <EOL> self . assertEqual ( self . errbackResults , None ) <EOL> self . assertEqual ( self . callbackResults , ( ( '<STR_LIT:hello>' , '<STR_LIT>' ) , { } ) ) <EOL> def testCallbackWithKwArgs ( self ) : <EOL> deferred = defer . Deferred ( ) <EOL> deferred . addCallback ( self . _callback , world = "<STR_LIT>" ) <EOL> deferred . callback ( "<STR_LIT:hello>" ) <EOL> self . assertEqual ( self . errbackResults , None ) <EOL> self . assertEqual ( self . callbackResults , <EOL> ( ( '<STR_LIT:hello>' , ) , { '<STR_LIT>' : '<STR_LIT>' } ) ) <EOL> def testTwoCallbacks ( self ) : <EOL> deferred = defer . Deferred ( ) <EOL> deferred . addCallback ( self . _callback ) <EOL> deferred . addCallback ( self . _callback2 ) <EOL> deferred . callback ( "<STR_LIT:hello>" ) <EOL> self . assertEqual ( self . errbackResults , None ) <EOL> self . assertEqual ( self . callbackResults , <EOL> ( ( '<STR_LIT:hello>' , ) , { } ) ) <EOL> self . assertEqual ( self . callback2Results , <EOL> ( ( '<STR_LIT:hello>' , ) , { } ) ) <EOL> def testDeferredList ( self ) : <EOL> defr1 = defer . Deferred ( ) <EOL> defr2 = defer . Deferred ( ) <EOL> defr3 = defer . Deferred ( ) <EOL> dl = defer . DeferredList ( [ defr1 , defr2 , defr3 ] ) <EOL> result = [ ] <EOL> def cb ( resultList , result = result ) : <EOL> result . extend ( resultList ) <EOL> def catch ( err ) : <EOL> return None <EOL> dl . addCallbacks ( cb , cb ) <EOL> defr1 . callback ( "<STR_LIT:1>" ) <EOL> defr2 . addErrback ( catch ) <EOL> defr2 . errback ( GenericError ( "<STR_LIT:2>" ) ) <EOL> defr3 . callback ( "<STR_LIT:3>" ) <EOL> self . assertEqual ( [ result [ <NUM_LIT:0> ] , <EOL> ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , str ( result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . value ) ) , <EOL> result [ <NUM_LIT:2> ] ] , <EOL> [ ( defer . SUCCESS , "<STR_LIT:1>" ) , <EOL> ( defer . FAILURE , "<STR_LIT:2>" ) , <EOL> ( defer . SUCCESS , "<STR_LIT:3>" ) ] ) <EOL> def testEmptyDeferredList ( self ) : <EOL> result = [ ] <EOL> def cb ( resultList , result = result ) : <EOL> result . append ( resultList ) <EOL> dl = defer . DeferredList ( [ ] ) <EOL> dl . addCallbacks ( cb ) <EOL> self . assertEqual ( result , [ [ ] ] ) <EOL> result [ : ] = [ ] <EOL> dl = defer . DeferredList ( [ ] , fireOnOneCallback = <NUM_LIT:1> ) <EOL> dl . addCallbacks ( cb ) <EOL> self . assertEqual ( result , [ ] ) <EOL> def testDeferredListFireOnOneError ( self ) : <EOL> defr1 = defer . Deferred ( ) <EOL> defr2 = defer . Deferred ( ) <EOL> defr3 = defer . Deferred ( ) <EOL> dl = defer . DeferredList ( [ defr1 , defr2 , defr3 ] , fireOnOneErrback = <NUM_LIT:1> ) <EOL> result = [ ] <EOL> dl . addErrback ( result . append ) <EOL> def catch ( err ) : <EOL> return None <EOL> defr2 . addErrback ( catch ) <EOL> defr1 . callback ( "<STR_LIT:1>" ) <EOL> self . assertEqual ( result , [ ] ) <EOL> defr2 . errback ( GenericError ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:1> ) <EOL> aFailure = result [ <NUM_LIT:0> ] <EOL> self . assertTrue ( issubclass ( aFailure . type , defer . FirstError ) , <EOL> '<STR_LIT>' <EOL> "<STR_LIT>" % ( aFailure . type , ) <EOL> ) <EOL> firstError = aFailure . value <EOL> self . assertEqual ( firstError . subFailure . type , GenericError ) <EOL> self . assertEqual ( firstError . subFailure . value . args , ( "<STR_LIT>" , ) ) <EOL> self . assertEqual ( firstError . index , <NUM_LIT:1> ) <EOL> def testDeferredListDontConsumeErrors ( self ) : <EOL> d1 = defer . Deferred ( ) <EOL> dl = defer . DeferredList ( [ d1 ] ) <EOL> errorTrap = [ ] <EOL> d1 . addErrback ( errorTrap . append ) <EOL> result = [ ] <EOL> dl . addCallback ( result . append ) <EOL> d1 . errback ( GenericError ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( '<STR_LIT>' , errorTrap [ <NUM_LIT:0> ] . value . args [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result ) ) <EOL> self . assertEqual ( '<STR_LIT>' , result [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] . value . args [ <NUM_LIT:0> ] ) <EOL> def testDeferredListConsumeErrors ( self ) : <EOL> d1 = defer . Deferred ( ) <EOL> dl = defer . DeferredList ( [ d1 ] , consumeErrors = True ) <EOL> errorTrap = [ ] <EOL> d1 . addErrback ( errorTrap . append ) <EOL> result = [ ] <EOL> dl . addCallback ( result . append ) <EOL> d1 . errback ( GenericError ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( [ ] , errorTrap ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result ) ) <EOL> self . assertEqual ( '<STR_LIT>' , result [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] . value . args [ <NUM_LIT:0> ] ) <EOL> def testDeferredListFireOnOneErrorWithAlreadyFiredDeferreds ( self ) : <EOL> d1 = defer . Deferred ( ) <EOL> d2 = defer . Deferred ( ) <EOL> d1 . errback ( GenericError ( '<STR_LIT>' ) ) <EOL> dl = defer . DeferredList ( [ d1 , d2 ] , fireOnOneErrback = True ) <EOL> result = [ ] <EOL> dl . addErrback ( result . append ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result ) ) <EOL> d1 . addErrback ( lambda e : None ) <EOL> def testDeferredListWithAlreadyFiredDeferreds ( self ) : <EOL> d1 = defer . Deferred ( ) <EOL> d2 = defer . Deferred ( ) <EOL> d1 . errback ( GenericError ( '<STR_LIT>' ) ) <EOL> d2 . callback ( <NUM_LIT:2> ) <EOL> dl = defer . DeferredList ( [ d1 , d2 ] ) <EOL> result = [ ] <EOL> dl . addCallback ( result . append ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( result ) ) <EOL> d1 . addErrback ( lambda e : None ) <EOL> def test_cancelDeferredList ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] ) <EOL> deferredList . cancel ( ) <EOL> self . failureResultOf ( deferredOne , defer . CancelledError ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> def test_cancelDeferredListCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( fakeCallbackCanceller ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] ) <EOL> deferredList . cancel ( ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> result = self . successResultOf ( deferredList ) <EOL> self . assertTrue ( result [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> self . assertFalse ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] . check ( defer . CancelledError ) ) <EOL> def test_cancelDeferredListWithFireOnOneCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] , <EOL> fireOnOneCallback = True ) <EOL> deferredList . cancel ( ) <EOL> self . failureResultOf ( deferredOne , defer . CancelledError ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> def test_cancelDeferredListWithFireOnOneCallbackAndDeferredCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( fakeCallbackCanceller ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] , <EOL> fireOnOneCallback = True ) <EOL> deferredList . cancel ( ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> result = self . successResultOf ( deferredList ) <EOL> self . assertEqual ( result , ( "<STR_LIT>" , <NUM_LIT:0> ) ) <EOL> def test_cancelDeferredListWithFireOnOneErrback ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] , <EOL> fireOnOneErrback = True ) <EOL> deferredList . cancel ( ) <EOL> self . failureResultOf ( deferredOne , defer . CancelledError ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> deferredListFailure = self . failureResultOf ( deferredList , <EOL> defer . FirstError ) <EOL> firstError = deferredListFailure . value <EOL> self . assertTrue ( firstError . subFailure . check ( defer . CancelledError ) ) <EOL> def test_cancelDeferredListWithFireOnOneErrbackAllDeferredsCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( fakeCallbackCanceller ) <EOL> deferredTwo = defer . Deferred ( fakeCallbackCanceller ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] , <EOL> fireOnOneErrback = True ) <EOL> deferredList . cancel ( ) <EOL> result = self . successResultOf ( deferredList ) <EOL> self . assertTrue ( result [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> self . assertTrue ( result [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def test_cancelDeferredListWithOriginalDeferreds ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> argumentList = [ deferredOne , deferredTwo ] <EOL> deferredList = defer . DeferredList ( argumentList ) <EOL> deferredThree = defer . Deferred ( ) <EOL> argumentList . append ( deferredThree ) <EOL> deferredList . cancel ( ) <EOL> self . failureResultOf ( deferredOne , defer . CancelledError ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> self . assertNoResult ( deferredThree ) <EOL> def test_cancelDeferredListWithException ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancellerRaisesException ( deferred ) : <EOL> """<STR_LIT>""" <EOL> raise RuntimeError ( "<STR_LIT:test>" ) <EOL> deferredOne = defer . Deferred ( cancellerRaisesException ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] ) <EOL> deferredList . cancel ( ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> errors = self . flushLoggedErrors ( RuntimeError ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> def test_cancelFiredOnOneCallbackDeferredList ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] , <EOL> fireOnOneCallback = True ) <EOL> deferredOne . callback ( None ) <EOL> deferredList . cancel ( ) <EOL> self . assertNoResult ( deferredTwo ) <EOL> def test_cancelFiredOnOneErrbackDeferredList ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> deferredList = defer . DeferredList ( [ deferredOne , deferredTwo ] , <EOL> fireOnOneErrback = True ) <EOL> deferredOne . errback ( GenericError ( "<STR_LIT:test>" ) ) <EOL> deferredList . cancel ( ) <EOL> self . assertNoResult ( deferredTwo ) <EOL> self . failureResultOf ( deferredOne , GenericError ) <EOL> self . failureResultOf ( deferredList , defer . FirstError ) <EOL> def testImmediateSuccess ( self ) : <EOL> l = [ ] <EOL> d = defer . succeed ( "<STR_LIT:success>" ) <EOL> d . addCallback ( l . append ) <EOL> self . assertEqual ( l , [ "<STR_LIT:success>" ] ) <EOL> def testImmediateFailure ( self ) : <EOL> l = [ ] <EOL> d = defer . fail ( GenericError ( "<STR_LIT>" ) ) <EOL> d . addErrback ( l . append ) <EOL> self . assertEqual ( str ( l [ <NUM_LIT:0> ] . value ) , "<STR_LIT>" ) <EOL> def testPausedFailure ( self ) : <EOL> l = [ ] <EOL> d = defer . fail ( GenericError ( "<STR_LIT>" ) ) <EOL> d . pause ( ) <EOL> d . addErrback ( l . append ) <EOL> self . assertEqual ( l , [ ] ) <EOL> d . unpause ( ) <EOL> self . assertEqual ( str ( l [ <NUM_LIT:0> ] . value ) , "<STR_LIT>" ) <EOL> def testCallbackErrors ( self ) : <EOL> l = [ ] <EOL> d = defer . Deferred ( ) . addCallback ( lambda _ : <NUM_LIT:1> // <NUM_LIT:0> ) . addErrback ( l . append ) <EOL> d . callback ( <NUM_LIT:1> ) <EOL> self . assert_ ( isinstance ( l [ <NUM_LIT:0> ] . value , ZeroDivisionError ) ) <EOL> l = [ ] <EOL> d = defer . Deferred ( ) . addCallback ( <EOL> lambda _ : failure . Failure ( ZeroDivisionError ( ) ) ) . addErrback ( l . append ) <EOL> d . callback ( <NUM_LIT:1> ) <EOL> self . assert_ ( isinstance ( l [ <NUM_LIT:0> ] . value , ZeroDivisionError ) ) <EOL> def testUnpauseBeforeCallback ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . pause ( ) <EOL> d . addCallback ( self . _callback ) <EOL> d . unpause ( ) <EOL> def testReturnDeferred ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d2 = defer . Deferred ( ) <EOL> d2 . pause ( ) <EOL> d . addCallback ( lambda r , d2 = d2 : d2 ) <EOL> d . addCallback ( self . _callback ) <EOL> d . callback ( <NUM_LIT:1> ) <EOL> assert self . callbackResults is None , "<STR_LIT>" <EOL> d2 . callback ( <NUM_LIT:2> ) <EOL> assert self . callbackResults is None , "<STR_LIT>" <EOL> d2 . unpause ( ) <EOL> assert self . callbackResults [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == <NUM_LIT:2> , "<STR_LIT>" % ( self . callbackResults , ) <EOL> def test_chainedPausedDeferredWithResult ( self ) : <EOL> """<STR_LIT>""" <EOL> expected = object ( ) <EOL> paused = defer . Deferred ( ) <EOL> paused . callback ( expected ) <EOL> paused . pause ( ) <EOL> chained = defer . Deferred ( ) <EOL> chained . addCallback ( lambda ignored : paused ) <EOL> chained . callback ( None ) <EOL> result = [ ] <EOL> chained . addCallback ( result . append ) <EOL> self . assertEqual ( result , [ ] ) <EOL> paused . unpause ( ) <EOL> self . assertEqual ( result , [ expected ] ) <EOL> def test_pausedDeferredChained ( self ) : <EOL> """<STR_LIT>""" <EOL> first = defer . Deferred ( ) <EOL> second = defer . Deferred ( ) <EOL> first . addCallback ( lambda ignored : second ) <EOL> first . callback ( None ) <EOL> first . pause ( ) <EOL> second . callback ( None ) <EOL> result = [ ] <EOL> second . addCallback ( result . append ) <EOL> self . assertEqual ( result , [ None ] ) <EOL> def test_gatherResults ( self ) : <EOL> l = [ ] <EOL> defer . gatherResults ( [ defer . succeed ( <NUM_LIT:1> ) , defer . succeed ( <NUM_LIT:2> ) ] ) . addCallback ( l . append ) <EOL> self . assertEqual ( l , [ [ <NUM_LIT:1> , <NUM_LIT:2> ] ] ) <EOL> l = [ ] <EOL> dl = [ defer . succeed ( <NUM_LIT:1> ) , defer . fail ( ValueError ( ) ) ] <EOL> defer . gatherResults ( dl ) . addErrback ( l . append ) <EOL> self . assertEqual ( len ( l ) , <NUM_LIT:1> ) <EOL> self . assert_ ( isinstance ( l [ <NUM_LIT:0> ] , failure . Failure ) ) <EOL> dl [ <NUM_LIT:1> ] . addErrback ( lambda e : <NUM_LIT:1> ) <EOL> def test_gatherResultsWithConsumeErrors ( self ) : <EOL> """<STR_LIT>""" <EOL> dgood = defer . succeed ( <NUM_LIT:1> ) <EOL> dbad = defer . fail ( RuntimeError ( "<STR_LIT>" ) ) <EOL> d = defer . gatherResults ( [ dgood , dbad ] , consumeErrors = True ) <EOL> unconsumedErrors = [ ] <EOL> dbad . addErrback ( unconsumedErrors . append ) <EOL> gatheredErrors = [ ] <EOL> d . addErrback ( gatheredErrors . append ) <EOL> self . assertEqual ( ( len ( unconsumedErrors ) , len ( gatheredErrors ) ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> self . assertIsInstance ( gatheredErrors [ <NUM_LIT:0> ] . value , defer . FirstError ) <EOL> firstError = gatheredErrors [ <NUM_LIT:0> ] . value . subFailure <EOL> self . assertIsInstance ( firstError . value , RuntimeError ) <EOL> def test_cancelGatherResults ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( ) <EOL> deferredTwo = defer . Deferred ( ) <EOL> result = defer . gatherResults ( [ deferredOne , deferredTwo ] ) <EOL> result . cancel ( ) <EOL> self . failureResultOf ( deferredOne , defer . CancelledError ) <EOL> self . failureResultOf ( deferredTwo , defer . CancelledError ) <EOL> gatherResultsFailure = self . failureResultOf ( result , defer . FirstError ) <EOL> firstError = gatherResultsFailure . value <EOL> self . assertTrue ( firstError . subFailure . check ( defer . CancelledError ) ) <EOL> def test_cancelGatherResultsWithAllDeferredsCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> deferredOne = defer . Deferred ( fakeCallbackCanceller ) <EOL> deferredTwo = defer . Deferred ( fakeCallbackCanceller ) <EOL> result = defer . gatherResults ( [ deferredOne , deferredTwo ] ) <EOL> result . cancel ( ) <EOL> callbackResult = self . successResultOf ( result ) <EOL> self . assertEqual ( callbackResult [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . assertEqual ( callbackResult [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def test_maybeDeferredSync ( self ) : <EOL> """<STR_LIT>""" <EOL> S , E = [ ] , [ ] <EOL> d = defer . maybeDeferred ( ( lambda x : x + <NUM_LIT:5> ) , <NUM_LIT:10> ) <EOL> d . addCallbacks ( S . append , E . append ) <EOL> self . assertEqual ( E , [ ] ) <EOL> self . assertEqual ( S , [ <NUM_LIT:15> ] ) <EOL> def test_maybeDeferredSyncError ( self ) : <EOL> """<STR_LIT>""" <EOL> S , E = [ ] , [ ] <EOL> try : <EOL> '<STR_LIT>' + <NUM_LIT:5> <EOL> except TypeError as e : <EOL> expected = str ( e ) <EOL> d = defer . maybeDeferred ( ( lambda x : x + <NUM_LIT:5> ) , '<STR_LIT>' ) <EOL> d . addCallbacks ( S . append , E . append ) <EOL> self . assertEqual ( S , [ ] ) <EOL> self . assertEqual ( len ( E ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( str ( E [ <NUM_LIT:0> ] . value ) , expected ) <EOL> def test_maybeDeferredAsync ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d2 = defer . maybeDeferred ( lambda : d ) <EOL> d . callback ( '<STR_LIT>' ) <EOL> result = [ ] <EOL> d2 . addCallback ( result . append ) <EOL> self . assertEqual ( result , [ '<STR_LIT>' ] ) <EOL> def test_maybeDeferredAsyncError ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d2 = defer . maybeDeferred ( lambda : d ) <EOL> d . errback ( failure . Failure ( RuntimeError ( ) ) ) <EOL> self . assertImmediateFailure ( d2 , RuntimeError ) <EOL> def test_innerCallbacksPreserved ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> failures = [ ] <EOL> inner = defer . Deferred ( ) <EOL> def cb ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> d = defer . succeed ( '<STR_LIT>' ) <EOL> def firstCallback ( result ) : <EOL> results . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return inner <EOL> def secondCallback ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> return result * <NUM_LIT:2> <EOL> d . addCallback ( firstCallback ) . addCallback ( secondCallback ) <EOL> d . addErrback ( failures . append ) <EOL> return d <EOL> outer = defer . succeed ( '<STR_LIT>' ) <EOL> outer . addCallback ( cb ) <EOL> inner . callback ( '<STR_LIT>' ) <EOL> outer . addCallback ( results . append ) <EOL> inner . addErrback ( failures . append ) <EOL> outer . addErrback ( failures . append ) <EOL> self . assertEqual ( [ ] , failures ) <EOL> self . assertEqual ( <EOL> results , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ] ) <EOL> def test_continueCallbackNotFirst ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> failures = [ ] <EOL> a = defer . Deferred ( ) <EOL> def cb ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> d = defer . Deferred ( ) <EOL> def firstCallback ( ignored ) : <EOL> results . append ( ( '<STR_LIT>' , ignored ) ) <EOL> return defer . gatherResults ( [ a ] ) <EOL> def secondCallback ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> d . addCallback ( firstCallback ) <EOL> d . addCallback ( secondCallback ) <EOL> d . addErrback ( failures . append ) <EOL> d . callback ( None ) <EOL> return d <EOL> outer = defer . succeed ( '<STR_LIT>' ) <EOL> outer . addCallback ( cb ) <EOL> outer . addErrback ( failures . append ) <EOL> self . assertEqual ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , None ) ] , results ) <EOL> a . callback ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ ] , failures ) <EOL> self . assertEqual ( <EOL> results , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , [ '<STR_LIT>' ] ) ] ) <EOL> def test_callbackOrderPreserved ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> failures = [ ] <EOL> a = defer . Deferred ( ) <EOL> def cb ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> d = defer . Deferred ( ) <EOL> def firstCallback ( ignored ) : <EOL> results . append ( ( '<STR_LIT>' , ignored ) ) <EOL> return defer . gatherResults ( [ a ] ) <EOL> def secondCallback ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> d . addCallback ( firstCallback ) <EOL> d . addCallback ( secondCallback ) <EOL> d . addErrback ( failures . append ) <EOL> d . callback ( None ) <EOL> return d <EOL> outer = defer . Deferred ( ) <EOL> outer . addCallback ( cb ) <EOL> outer . addCallback ( lambda x : results . append ( '<STR_LIT>' ) ) <EOL> outer . addErrback ( failures . append ) <EOL> outer . callback ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , None ) ] , results ) <EOL> a . callback ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ ] , failures ) <EOL> self . assertEqual ( <EOL> results , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , [ '<STR_LIT>' ] ) , '<STR_LIT>' ] ) <EOL> def test_reentrantRunCallbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> deferred = defer . Deferred ( ) <EOL> called = [ ] <EOL> def callback3 ( result ) : <EOL> called . append ( <NUM_LIT:3> ) <EOL> def callback2 ( result ) : <EOL> called . append ( <NUM_LIT:2> ) <EOL> def callback1 ( result ) : <EOL> called . append ( <NUM_LIT:1> ) <EOL> deferred . addCallback ( callback3 ) <EOL> deferred . addCallback ( callback1 ) <EOL> deferred . addCallback ( callback2 ) <EOL> deferred . callback ( None ) <EOL> self . assertEqual ( called , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_nonReentrantCallbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> deferred = defer . Deferred ( ) <EOL> called = [ ] <EOL> def callback2 ( result ) : <EOL> called . append ( <NUM_LIT:2> ) <EOL> def callback1 ( result ) : <EOL> called . append ( <NUM_LIT:1> ) <EOL> deferred . addCallback ( callback2 ) <EOL> self . assertEqual ( called , [ <NUM_LIT:1> ] ) <EOL> deferred . addCallback ( callback1 ) <EOL> deferred . callback ( None ) <EOL> self . assertEqual ( called , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> def test_reentrantRunCallbacksWithFailure ( self ) : <EOL> """<STR_LIT>""" <EOL> exceptionMessage = "<STR_LIT>" <EOL> deferred = defer . Deferred ( ) <EOL> def callback2 ( result ) : <EOL> raise Exception ( exceptionMessage ) <EOL> def callback1 ( result ) : <EOL> deferred . addCallback ( callback2 ) <EOL> deferred . addCallback ( callback1 ) <EOL> deferred . callback ( None ) <EOL> exception = self . assertImmediateFailure ( deferred , Exception ) <EOL> self . assertEqual ( exception . args , ( exceptionMessage , ) ) <EOL> def test_synchronousImplicitChain ( self ) : <EOL> """<STR_LIT>""" <EOL> result = object ( ) <EOL> first = defer . succeed ( result ) <EOL> second = defer . Deferred ( ) <EOL> second . addCallback ( lambda ign : first ) <EOL> second . callback ( None ) <EOL> results = [ ] <EOL> first . addCallback ( results . append ) <EOL> self . assertIdentical ( results [ <NUM_LIT:0> ] , None ) <EOL> second . addCallback ( results . append ) <EOL> self . assertIdentical ( results [ <NUM_LIT:1> ] , result ) <EOL> def test_asynchronousImplicitChain ( self ) : <EOL> """<STR_LIT>""" <EOL> first = defer . Deferred ( ) <EOL> second = defer . Deferred ( ) <EOL> second . addCallback ( lambda ign : first ) <EOL> second . callback ( None ) <EOL> firstResult = [ ] <EOL> first . addCallback ( firstResult . append ) <EOL> secondResult = [ ] <EOL> second . addCallback ( secondResult . append ) <EOL> self . assertEqual ( firstResult , [ ] ) <EOL> self . assertEqual ( secondResult , [ ] ) <EOL> result = object ( ) <EOL> first . callback ( result ) <EOL> self . assertEqual ( firstResult , [ None ] ) <EOL> self . assertEqual ( secondResult , [ result ] ) <EOL> def test_synchronousImplicitErrorChain ( self ) : <EOL> """<STR_LIT>""" <EOL> first = defer . fail ( RuntimeError ( "<STR_LIT>" ) ) <EOL> second = defer . Deferred ( ) <EOL> second . addCallback ( lambda ign , first = first : first ) <EOL> second . callback ( None ) <EOL> firstResult = [ ] <EOL> first . addCallback ( firstResult . append ) <EOL> self . assertIdentical ( firstResult [ <NUM_LIT:0> ] , None ) <EOL> self . assertImmediateFailure ( second , RuntimeError ) <EOL> def test_asynchronousImplicitErrorChain ( self ) : <EOL> """<STR_LIT>""" <EOL> first = defer . Deferred ( ) <EOL> second = defer . Deferred ( ) <EOL> second . addCallback ( lambda ign : first ) <EOL> second . callback ( None ) <EOL> secondError = [ ] <EOL> second . addErrback ( secondError . append ) <EOL> firstResult = [ ] <EOL> first . addCallback ( firstResult . append ) <EOL> secondResult = [ ] <EOL> second . addCallback ( secondResult . append ) <EOL> self . assertEqual ( firstResult , [ ] ) <EOL> self . assertEqual ( secondResult , [ ] ) <EOL> first . errback ( RuntimeError ( "<STR_LIT>" ) ) <EOL> self . assertTrue ( secondError [ <NUM_LIT:0> ] . check ( RuntimeError ) ) <EOL> self . assertEqual ( firstResult , [ None ] ) <EOL> self . assertEqual ( len ( secondResult ) , <NUM_LIT:1> ) <EOL> def test_doubleAsynchronousImplicitChaining ( self ) : <EOL> """<STR_LIT>""" <EOL> first = defer . Deferred ( ) <EOL> second = defer . Deferred ( ) <EOL> second . addCallback ( lambda ign : first ) <EOL> third = defer . Deferred ( ) <EOL> third . addCallback ( lambda ign : second ) <EOL> thirdResult = [ ] <EOL> third . addCallback ( thirdResult . append ) <EOL> result = object ( ) <EOL> second . callback ( None ) <EOL> third . callback ( None ) <EOL> self . assertEqual ( thirdResult , [ ] ) <EOL> first . callback ( result ) <EOL> self . assertEqual ( thirdResult , [ result ] ) <EOL> def test_nestedAsynchronousChainedDeferreds ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> failures = [ ] <EOL> inner = defer . Deferred ( ) <EOL> def cb ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> d = defer . succeed ( '<STR_LIT>' ) <EOL> def firstCallback ( result ) : <EOL> results . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return inner <EOL> def secondCallback ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> return result * <NUM_LIT:2> <EOL> d . addCallback ( firstCallback ) . addCallback ( secondCallback ) <EOL> d . addErrback ( failures . append ) <EOL> return d <EOL> outer = defer . succeed ( '<STR_LIT>' ) <EOL> outer . addCallback ( cb ) <EOL> outer . addCallback ( results . append ) <EOL> self . assertEqual ( <EOL> results , [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> inner . callback ( '<STR_LIT>' ) <EOL> inner . addErrback ( failures . append ) <EOL> outer . addErrback ( failures . append ) <EOL> self . assertEqual ( <EOL> [ ] , failures , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> results , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' ] ) <EOL> def test_nestedAsynchronousChainedDeferredsWithExtraCallbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> failures = [ ] <EOL> inner = defer . Deferred ( ) <EOL> def cb ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> d = defer . succeed ( '<STR_LIT>' ) <EOL> def firstCallback ( ignored ) : <EOL> results . append ( ( '<STR_LIT>' , ignored ) ) <EOL> return inner . addCallback ( lambda x : [ x ] ) <EOL> def secondCallback ( result ) : <EOL> results . append ( ( '<STR_LIT>' , result ) ) <EOL> return result * <NUM_LIT:2> <EOL> d . addCallback ( firstCallback ) <EOL> d . addCallback ( secondCallback ) <EOL> d . addErrback ( failures . append ) <EOL> return d <EOL> outer = defer . succeed ( '<STR_LIT>' ) <EOL> outer . addCallback ( cb ) <EOL> outer . addCallback ( results . append ) <EOL> self . assertEqual ( <EOL> results , [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> inner . callback ( '<STR_LIT>' ) <EOL> outer . addErrback ( failures . append ) <EOL> inner . addErrback ( failures . append ) <EOL> self . assertEqual ( <EOL> [ ] , failures , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> results , <EOL> [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , [ '<STR_LIT>' ] ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ] ) <EOL> def test_chainDeferredRecordsExplicitChain ( self ) : <EOL> """<STR_LIT>""" <EOL> a = defer . Deferred ( ) <EOL> b = defer . Deferred ( ) <EOL> b . chainDeferred ( a ) <EOL> self . assertIdentical ( a . _chainedTo , b ) <EOL> def test_explicitChainClearedWhenResolved ( self ) : <EOL> """<STR_LIT>""" <EOL> a = defer . Deferred ( ) <EOL> b = defer . Deferred ( ) <EOL> b . chainDeferred ( a ) <EOL> b . callback ( None ) <EOL> self . assertIdentical ( a . _chainedTo , None ) <EOL> def test_chainDeferredRecordsImplicitChain ( self ) : <EOL> """<STR_LIT>""" <EOL> a = defer . Deferred ( ) <EOL> b = defer . Deferred ( ) <EOL> a . addCallback ( lambda ignored : b ) <EOL> a . callback ( None ) <EOL> self . assertIdentical ( a . _chainedTo , b ) <EOL> def test_circularChainWarning ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> def circularCallback ( result ) : <EOL> return d <EOL> d . addCallback ( circularCallback ) <EOL> d . callback ( "<STR_LIT:foo>" ) <EOL> circular_warnings = self . flushWarnings ( [ circularCallback ] ) <EOL> self . assertEqual ( len ( circular_warnings ) , <NUM_LIT:1> ) <EOL> warning = circular_warnings [ <NUM_LIT:0> ] <EOL> self . assertEqual ( warning [ '<STR_LIT>' ] , DeprecationWarning ) <EOL> pattern = "<STR_LIT>" <EOL> self . assertTrue ( <EOL> re . search ( pattern , warning [ '<STR_LIT:message>' ] ) , <EOL> "<STR_LIT>" % ( pattern , warning [ '<STR_LIT:message>' ] ) ) <EOL> def test_circularChainException ( self ) : <EOL> """<STR_LIT>""" <EOL> self . addCleanup ( setattr , warnings , "<STR_LIT>" , warnings . filters ) <EOL> warnings . filterwarnings ( "<STR_LIT:error>" , category = DeprecationWarning ) <EOL> d = defer . Deferred ( ) <EOL> def circularCallback ( result ) : <EOL> return d <EOL> d . addCallback ( circularCallback ) <EOL> d . callback ( "<STR_LIT:foo>" ) <EOL> failure = self . failureResultOf ( d ) <EOL> failure . trap ( DeprecationWarning ) <EOL> def test_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> address = id ( d ) <EOL> self . assertEqual ( <EOL> repr ( d ) , '<STR_LIT>' % ( address , ) ) <EOL> def test_reprWithResult ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . callback ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> repr ( d ) , "<STR_LIT>" % ( <EOL> id ( d ) , ) ) <EOL> def test_reprWithChaining ( self ) : <EOL> """<STR_LIT>""" <EOL> a = defer . Deferred ( ) <EOL> b = defer . Deferred ( ) <EOL> b . chainDeferred ( a ) <EOL> self . assertEqual ( <EOL> repr ( a ) , "<STR_LIT>" % ( <EOL> id ( a ) , id ( b ) ) ) <EOL> def test_boundedStackDepth ( self ) : <EOL> """<STR_LIT>""" <EOL> def chainDeferreds ( howMany ) : <EOL> stack = [ ] <EOL> def recordStackDepth ( ignored ) : <EOL> stack . append ( len ( traceback . extract_stack ( ) ) ) <EOL> top = defer . Deferred ( ) <EOL> innerDeferreds = [ defer . Deferred ( ) for ignored in range ( howMany ) ] <EOL> originalInners = innerDeferreds [ : ] <EOL> last = defer . Deferred ( ) <EOL> inner = innerDeferreds . pop ( ) <EOL> top . addCallback ( lambda ign , inner = inner : inner ) <EOL> top . addCallback ( recordStackDepth ) <EOL> while innerDeferreds : <EOL> newInner = innerDeferreds . pop ( ) <EOL> inner . addCallback ( lambda ign , inner = newInner : inner ) <EOL> inner = newInner <EOL> inner . addCallback ( lambda ign : last ) <EOL> top . callback ( None ) <EOL> for inner in originalInners : <EOL> inner . callback ( None ) <EOL> self . assertEqual ( stack , [ ] ) <EOL> last . callback ( None ) <EOL> return stack [ <NUM_LIT:0> ] <EOL> self . assertEqual ( chainDeferreds ( <NUM_LIT:1> ) , chainDeferreds ( <NUM_LIT:2> ) ) <EOL> def test_resultOfDeferredResultOfDeferredOfFiredDeferredCalled ( self ) : <EOL> """<STR_LIT>""" <EOL> first = defer . Deferred ( ) <EOL> second = defer . Deferred ( ) <EOL> third = defer . Deferred ( ) <EOL> first . addCallback ( lambda ignored : second ) <EOL> second . addCallback ( lambda ignored : third ) <EOL> second . callback ( None ) <EOL> first . callback ( None ) <EOL> third . callback ( None ) <EOL> L = [ ] <EOL> second . addCallback ( L . append ) <EOL> self . assertEqual ( L , [ None ] ) <EOL> def test_errbackWithNoArgsNoDebug ( self ) : <EOL> """<STR_LIT>""" <EOL> defer . setDebugging ( False ) <EOL> d = defer . Deferred ( ) <EOL> l = [ ] <EOL> exc = GenericError ( "<STR_LIT>" ) <EOL> try : <EOL> raise exc <EOL> except : <EOL> d . errback ( ) <EOL> d . addErrback ( l . append ) <EOL> fail = l [ <NUM_LIT:0> ] <EOL> self . assertEqual ( fail . value , exc ) <EOL> localz , globalz = fail . frames [ <NUM_LIT:0> ] [ - <NUM_LIT:2> : ] <EOL> self . assertEqual ( [ ] , localz ) <EOL> self . assertEqual ( [ ] , globalz ) <EOL> def test_errbackWithNoArgs ( self ) : <EOL> """<STR_LIT>""" <EOL> defer . setDebugging ( True ) <EOL> d = defer . Deferred ( ) <EOL> l = [ ] <EOL> exc = GenericError ( "<STR_LIT>" ) <EOL> try : <EOL> raise exc <EOL> except : <EOL> d . errback ( ) <EOL> d . addErrback ( l . append ) <EOL> fail = l [ <NUM_LIT:0> ] <EOL> self . assertEqual ( fail . value , exc ) <EOL> localz , globalz = fail . frames [ <NUM_LIT:0> ] [ - <NUM_LIT:2> : ] <EOL> self . assertNotEqual ( [ ] , localz ) <EOL> self . assertNotEqual ( [ ] , globalz ) <EOL> def test_errorInCallbackDoesNotCaptureVars ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . callback ( None ) <EOL> defer . setDebugging ( False ) <EOL> def raiseError ( ignored ) : <EOL> raise GenericError ( "<STR_LIT>" ) <EOL> d . addCallback ( raiseError ) <EOL> l = [ ] <EOL> d . addErrback ( l . append ) <EOL> fail = l [ <NUM_LIT:0> ] <EOL> localz , globalz = fail . frames [ <NUM_LIT:0> ] [ - <NUM_LIT:2> : ] <EOL> self . assertEqual ( [ ] , localz ) <EOL> self . assertEqual ( [ ] , globalz ) <EOL> def test_errorInCallbackCapturesVarsWhenDebugging ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . callback ( None ) <EOL> defer . setDebugging ( True ) <EOL> def raiseError ( ignored ) : <EOL> raise GenericError ( "<STR_LIT>" ) <EOL> d . addCallback ( raiseError ) <EOL> l = [ ] <EOL> d . addErrback ( l . append ) <EOL> fail = l [ <NUM_LIT:0> ] <EOL> localz , globalz = fail . frames [ <NUM_LIT:0> ] [ - <NUM_LIT:2> : ] <EOL> self . assertNotEqual ( [ ] , localz ) <EOL> self . assertNotEqual ( [ ] , globalz ) <EOL> def test_inlineCallbacksTracebacks ( self ) : <EOL> """<STR_LIT>""" <EOL> f = getDivisionFailure ( ) <EOL> d = defer . Deferred ( ) <EOL> try : <EOL> f . raiseException ( ) <EOL> except : <EOL> d . errback ( ) <EOL> def ic ( d ) : <EOL> yield d <EOL> ic = defer . inlineCallbacks ( ic ) <EOL> newFailure = self . failureResultOf ( d ) <EOL> tb = traceback . extract_tb ( newFailure . getTracebackObject ( ) ) <EOL> self . assertEqual ( len ( tb ) , <NUM_LIT:2> ) <EOL> self . assertIn ( '<STR_LIT>' , tb [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( '<STR_LIT>' , tb [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( '<STR_LIT>' , tb [ <NUM_LIT:0> ] [ <NUM_LIT:3> ] ) <EOL> self . assertIn ( '<STR_LIT>' , tb [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( '<STR_LIT>' , tb [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( '<STR_LIT>' , tb [ <NUM_LIT:1> ] [ <NUM_LIT:3> ] ) <EOL> if _PY3 : <EOL> test_inlineCallbacksTracebacks . skip = ( <EOL> "<STR_LIT>" ) <EOL> class FirstErrorTests ( unittest . SynchronousTestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> exc = ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> raise exc <EOL> except : <EOL> f = failure . Failure ( ) <EOL> error = defer . FirstError ( f , <NUM_LIT:3> ) <EOL> self . assertEqual ( <EOL> repr ( error ) , <EOL> "<STR_LIT>" % ( repr ( exc ) , ) ) <EOL> def test_str ( self ) : <EOL> """<STR_LIT>""" <EOL> exc = ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> raise exc <EOL> except : <EOL> f = failure . Failure ( ) <EOL> error = defer . FirstError ( f , <NUM_LIT:5> ) <EOL> self . assertEqual ( <EOL> str ( error ) , <EOL> "<STR_LIT>" % ( str ( f ) , ) ) <EOL> def test_comparison ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> <NUM_LIT:1> // <NUM_LIT:0> <EOL> except : <EOL> firstFailure = failure . Failure ( ) <EOL> one = defer . FirstError ( firstFailure , <NUM_LIT> ) <EOL> anotherOne = defer . FirstError ( firstFailure , <NUM_LIT> ) <EOL> try : <EOL> raise ValueError ( "<STR_LIT:bar>" ) <EOL> except : <EOL> secondFailure = failure . Failure ( ) <EOL> another = defer . FirstError ( secondFailure , <NUM_LIT:9> ) <EOL> self . assertTrue ( one == anotherOne ) <EOL> self . assertFalse ( one == another ) <EOL> self . assertTrue ( one != another ) <EOL> self . assertFalse ( one != anotherOne ) <EOL> self . assertFalse ( one == <NUM_LIT:10> ) <EOL> class AlreadyCalledTests ( unittest . SynchronousTestCase ) : <EOL> def setUp ( self ) : <EOL> self . _deferredWasDebugging = defer . getDebugging ( ) <EOL> defer . setDebugging ( True ) <EOL> def tearDown ( self ) : <EOL> defer . setDebugging ( self . _deferredWasDebugging ) <EOL> def _callback ( self , * args , ** kw ) : <EOL> pass <EOL> def _errback ( self , * args , ** kw ) : <EOL> pass <EOL> def _call_1 ( self , d ) : <EOL> d . callback ( "<STR_LIT:hello>" ) <EOL> def _call_2 ( self , d ) : <EOL> d . callback ( "<STR_LIT>" ) <EOL> def _err_1 ( self , d ) : <EOL> d . errback ( failure . Failure ( RuntimeError ( ) ) ) <EOL> def _err_2 ( self , d ) : <EOL> d . errback ( failure . Failure ( RuntimeError ( ) ) ) <EOL> def testAlreadyCalled_CC ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _call_1 ( d ) <EOL> self . failUnlessRaises ( defer . AlreadyCalledError , self . _call_2 , d ) <EOL> def testAlreadyCalled_CE ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _call_1 ( d ) <EOL> self . failUnlessRaises ( defer . AlreadyCalledError , self . _err_2 , d ) <EOL> def testAlreadyCalled_EE ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _err_1 ( d ) <EOL> self . failUnlessRaises ( defer . AlreadyCalledError , self . _err_2 , d ) <EOL> def testAlreadyCalled_EC ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _err_1 ( d ) <EOL> self . failUnlessRaises ( defer . AlreadyCalledError , self . _call_2 , d ) <EOL> def _count ( self , linetype , func , lines , expected ) : <EOL> count = <NUM_LIT:0> <EOL> for line in lines : <EOL> if ( line . startswith ( '<STR_LIT>' % linetype ) and <EOL> line . endswith ( '<STR_LIT>' % func ) ) : <EOL> count += <NUM_LIT:1> <EOL> self . assertTrue ( count == expected ) <EOL> def _check ( self , e , caller , invoker1 , invoker2 ) : <EOL> lines = e . args [ <NUM_LIT:0> ] . split ( "<STR_LIT:\n>" ) <EOL> self . _count ( '<STR_LIT:C>' , caller , lines , <NUM_LIT:1> ) <EOL> self . _count ( '<STR_LIT:C>' , '<STR_LIT>' , lines , <NUM_LIT:0> ) <EOL> self . _count ( '<STR_LIT:C>' , '<STR_LIT>' , lines , <NUM_LIT:0> ) <EOL> self . _count ( '<STR_LIT:C>' , '<STR_LIT>' , lines , <NUM_LIT:0> ) <EOL> self . _count ( '<STR_LIT:C>' , '<STR_LIT>' , lines , <NUM_LIT:0> ) <EOL> self . _count ( '<STR_LIT:I>' , invoker1 , lines , <NUM_LIT:1> ) <EOL> self . _count ( '<STR_LIT:I>' , invoker2 , lines , <NUM_LIT:0> ) <EOL> def testAlreadyCalledDebug_CC ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _call_1 ( d ) <EOL> try : <EOL> self . _call_2 ( d ) <EOL> except defer . AlreadyCalledError as e : <EOL> self . _check ( e , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testAlreadyCalledDebug_CE ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _call_1 ( d ) <EOL> try : <EOL> self . _err_2 ( d ) <EOL> except defer . AlreadyCalledError as e : <EOL> self . _check ( e , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testAlreadyCalledDebug_EC ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _err_1 ( d ) <EOL> try : <EOL> self . _call_2 ( d ) <EOL> except defer . AlreadyCalledError as e : <EOL> self . _check ( e , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testAlreadyCalledDebug_EE ( self ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _err_1 ( d ) <EOL> try : <EOL> self . _err_2 ( d ) <EOL> except defer . AlreadyCalledError as e : <EOL> self . _check ( e , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testNoDebugging ( self ) : <EOL> defer . setDebugging ( False ) <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> self . _call_1 ( d ) <EOL> try : <EOL> self . _call_2 ( d ) <EOL> except defer . AlreadyCalledError as e : <EOL> self . assertFalse ( e . args ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def testSwitchDebugging ( self ) : <EOL> defer . setDebugging ( False ) <EOL> d = defer . Deferred ( ) <EOL> d . addBoth ( lambda ign : None ) <EOL> defer . setDebugging ( True ) <EOL> d . callback ( None ) <EOL> defer . setDebugging ( False ) <EOL> d = defer . Deferred ( ) <EOL> d . callback ( None ) <EOL> defer . setDebugging ( True ) <EOL> d . addBoth ( lambda ign : None ) <EOL> class DeferredCancellerTests ( unittest . SynchronousTestCase ) : <EOL> def setUp ( self ) : <EOL> self . callbackResults = None <EOL> self . errbackResults = None <EOL> self . callback2Results = None <EOL> self . cancellerCallCount = <NUM_LIT:0> <EOL> def tearDown ( self ) : <EOL> self . assertTrue ( self . cancellerCallCount in ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> def _callback ( self , data ) : <EOL> self . callbackResults = data <EOL> return data <EOL> def _callback2 ( self , data ) : <EOL> self . callback2Results = data <EOL> def _errback ( self , data ) : <EOL> self . errbackResults = data <EOL> def test_noCanceller ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . errbackResults . type , defer . CancelledError ) <EOL> self . assertEqual ( self . callbackResults , None ) <EOL> def test_raisesAfterCancelAndCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> d . callback ( None ) <EOL> self . assertRaises ( defer . AlreadyCalledError , d . callback , None ) <EOL> self . assertRaises ( defer . AlreadyCalledError , d . errback , Exception ( ) ) <EOL> def test_raisesAfterCancelAndErrback ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> d . errback ( Exception ( ) ) <EOL> self . assertRaises ( defer . AlreadyCalledError , d . callback , None ) <EOL> self . assertRaises ( defer . AlreadyCalledError , d . errback , Exception ( ) ) <EOL> def test_noCancellerMultipleCancelsAfterCancelAndCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> currentFailure = self . errbackResults <EOL> d . callback ( None ) <EOL> d . cancel ( ) <EOL> self . assertIdentical ( currentFailure , self . errbackResults ) <EOL> def test_noCancellerMultipleCancelsAfterCancelAndErrback ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . errbackResults . type , defer . CancelledError ) <EOL> currentFailure = self . errbackResults <EOL> d . errback ( GenericError ( ) ) <EOL> self . assertEqual ( self . errbackResults . type , defer . CancelledError ) <EOL> d . cancel ( ) <EOL> self . assertIdentical ( currentFailure , self . errbackResults ) <EOL> def test_noCancellerMultipleCancel ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . errbackResults . type , defer . CancelledError ) <EOL> currentFailure = self . errbackResults <EOL> d . cancel ( ) <EOL> self . assertIdentical ( currentFailure , self . errbackResults ) <EOL> def test_cancellerMultipleCancel ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d ) : <EOL> self . cancellerCallCount += <NUM_LIT:1> <EOL> d = defer . Deferred ( canceller = cancel ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . errbackResults . type , defer . CancelledError ) <EOL> currentFailure = self . errbackResults <EOL> d . cancel ( ) <EOL> self . assertIdentical ( currentFailure , self . errbackResults ) <EOL> self . assertEqual ( self . cancellerCallCount , <NUM_LIT:1> ) <EOL> def test_simpleCanceller ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d ) : <EOL> self . cancellerCallCount += <NUM_LIT:1> <EOL> d = defer . Deferred ( canceller = cancel ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . cancellerCallCount , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . errbackResults . type , defer . CancelledError ) <EOL> self . assertRaises ( defer . AlreadyCalledError , d . callback , None ) <EOL> self . assertRaises ( defer . AlreadyCalledError , d . errback , Exception ( ) ) <EOL> def test_cancellerArg ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d1 ) : <EOL> self . assertIdentical ( d1 , d ) <EOL> d = defer . Deferred ( canceller = cancel ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> def test_cancelAfterCallback ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d ) : <EOL> self . cancellerCallCount += <NUM_LIT:1> <EOL> d . errback ( GenericError ( ) ) <EOL> d = defer . Deferred ( canceller = cancel ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . callback ( '<STR_LIT>' ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . cancellerCallCount , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . errbackResults , None ) <EOL> self . assertEqual ( self . callbackResults , '<STR_LIT>' ) <EOL> def test_cancelAfterErrback ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d ) : <EOL> self . cancellerCallCount += <NUM_LIT:1> <EOL> d . errback ( GenericError ( ) ) <EOL> d = defer . Deferred ( canceller = cancel ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . errback ( GenericError ( ) ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . cancellerCallCount , <NUM_LIT:0> ) <EOL> self . assertEqual ( self . errbackResults . type , GenericError ) <EOL> self . assertEqual ( self . callbackResults , None ) <EOL> def test_cancellerThatErrbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d ) : <EOL> self . cancellerCallCount += <NUM_LIT:1> <EOL> d . errback ( GenericError ( ) ) <EOL> d = defer . Deferred ( canceller = cancel ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . cancellerCallCount , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . errbackResults . type , GenericError ) <EOL> def test_cancellerThatCallbacks ( self ) : <EOL> """<STR_LIT>""" <EOL> def cancel ( d ) : <EOL> self . cancellerCallCount += <NUM_LIT:1> <EOL> d . callback ( '<STR_LIT>' ) <EOL> d = defer . Deferred ( canceller = cancel ) <EOL> d . addCallbacks ( self . _callback , self . _errback ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . cancellerCallCount , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . callbackResults , '<STR_LIT>' ) <EOL> self . assertEqual ( self . errbackResults , None ) <EOL> def test_cancelNestedDeferred ( self ) : <EOL> """<STR_LIT>""" <EOL> def innerCancel ( d ) : <EOL> self . cancellerCallCount += <NUM_LIT:1> <EOL> def cancel ( d ) : <EOL> self . assert_ ( False ) <EOL> b = defer . Deferred ( canceller = innerCancel ) <EOL> a = defer . Deferred ( canceller = cancel ) <EOL> a . callback ( None ) <EOL> a . addCallback ( lambda data : b ) <EOL> a . cancel ( ) <EOL> a . addCallbacks ( self . _callback , self . _errback ) <EOL> self . assertEqual ( self . cancellerCallCount , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . errbackResults . type , defer . CancelledError ) <EOL> class LogTests ( unittest . SynchronousTestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> self . c = [ ] <EOL> log . addObserver ( self . c . append ) <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> log . removeObserver ( self . c . append ) <EOL> def _loggedErrors ( self ) : <EOL> return [ e for e in self . c if e [ "<STR_LIT>" ] ] <EOL> def _check ( self ) : <EOL> """<STR_LIT>""" <EOL> c2 = self . _loggedErrors ( ) <EOL> self . assertEqual ( len ( c2 ) , <NUM_LIT:2> ) <EOL> c2 [ <NUM_LIT:1> ] [ "<STR_LIT>" ] . trap ( ZeroDivisionError ) <EOL> self . flushLoggedErrors ( ZeroDivisionError ) <EOL> def test_errorLog ( self ) : <EOL> """<STR_LIT>""" <EOL> defer . Deferred ( ) . addCallback ( lambda x : <NUM_LIT:1> // <NUM_LIT:0> ) . callback ( <NUM_LIT:1> ) <EOL> gc . collect ( ) <EOL> self . _check ( ) <EOL> def test_errorLogWithInnerFrameRef ( self ) : <EOL> """<STR_LIT>""" <EOL> def _subErrorLogWithInnerFrameRef ( ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( lambda x : <NUM_LIT:1> // <NUM_LIT:0> ) <EOL> d . callback ( <NUM_LIT:1> ) <EOL> _subErrorLogWithInnerFrameRef ( ) <EOL> gc . collect ( ) <EOL> self . _check ( ) <EOL> def test_errorLogWithInnerFrameCycle ( self ) : <EOL> """<STR_LIT>""" <EOL> def _subErrorLogWithInnerFrameCycle ( ) : <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( lambda x , d = d : <NUM_LIT:1> // <NUM_LIT:0> ) <EOL> d . _d = d <EOL> d . callback ( <NUM_LIT:1> ) <EOL> _subErrorLogWithInnerFrameCycle ( ) <EOL> gc . collect ( ) <EOL> self . _check ( ) <EOL> def test_errorLogNoRepr ( self ) : <EOL> """<STR_LIT>""" <EOL> defer . Deferred ( ) . addCallback ( lambda x : <NUM_LIT:1> // <NUM_LIT:0> ) . callback ( <NUM_LIT:1> ) <EOL> gc . collect ( ) <EOL> self . _check ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . c ) ) <EOL> msg = log . textFromEventDict ( self . c [ - <NUM_LIT:1> ] ) <EOL> expected = "<STR_LIT>" <EOL> self . assertTrue ( msg . startswith ( expected ) , <EOL> "<STR_LIT>" . <EOL> format ( expected ) ) <EOL> def test_errorLogDebugInfo ( self ) : <EOL> """<STR_LIT>""" <EOL> def doit ( ) : <EOL> d = defer . Deferred ( ) <EOL> d . debug = True <EOL> d . addCallback ( lambda x : <NUM_LIT:1> // <NUM_LIT:0> ) <EOL> d . callback ( <NUM_LIT:1> ) <EOL> doit ( ) <EOL> gc . collect ( ) <EOL> self . _check ( ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . c ) ) <EOL> msg = log . textFromEventDict ( self . c [ - <NUM_LIT:1> ] ) <EOL> expected = "<STR_LIT>" <EOL> self . assertTrue ( msg . startswith ( expected ) , <EOL> "<STR_LIT>" . <EOL> format ( expected ) ) <EOL> def test_chainedErrorCleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> d = defer . Deferred ( ) <EOL> d . addCallback ( lambda ign : defer . fail ( RuntimeError ( "<STR_LIT>" ) ) ) <EOL> d . callback ( None ) <EOL> results = [ ] <EOL> errors = [ ] <EOL> d . addCallbacks ( results . append , errors . append ) <EOL> self . assertEqual ( results , [ ] ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> errors [ <NUM_LIT:0> ] . trap ( Exception ) <EOL> del results , errors , d <EOL> gc . collect ( ) <EOL> self . assertEqual ( self . _loggedErrors ( ) , [ ] ) <EOL> def test_errorClearedByChaining ( self ) : <EOL> """<STR_LIT>""" <EOL> bad = defer . fail ( Exception ( "<STR_LIT>" ) ) <EOL> good = defer . Deferred ( ) <EOL> bad . addErrback ( lambda ignored : good ) <EOL> good = bad = None <EOL> gc . collect ( ) <EOL> self . assertEqual ( self . _loggedErrors ( ) , [ ] ) <EOL> class DeferredListEmptyTests ( unittest . SynchronousTestCase ) : <EOL> def setUp ( self ) : <EOL> self . callbackRan = <NUM_LIT:0> <EOL> def testDeferredListEmpty ( self ) : <EOL> """<STR_LIT>""" <EOL> dl = defer . DeferredList ( [ ] ) <EOL> dl . addCallback ( self . cb_empty ) <EOL> def cb_empty ( self , res ) : <EOL> self . callbackRan = <NUM_LIT:1> <EOL> self . assertEqual ( [ ] , res ) <EOL> def tearDown ( self ) : <EOL> self . assertTrue ( self . callbackRan , "<STR_LIT>" ) <EOL> class OtherPrimitivesTests ( unittest . SynchronousTestCase , ImmediateFailureMixin ) : <EOL> def _incr ( self , result ) : <EOL> self . counter += <NUM_LIT:1> <EOL> def setUp ( self ) : <EOL> self . counter = <NUM_LIT:0> <EOL> def testLock ( self ) : <EOL> lock = defer . DeferredLock ( ) <EOL> lock . acquire ( ) . addCallback ( self . _incr ) <EOL> self . assertTrue ( lock . locked ) <EOL> self . assertEqual ( self . counter , <NUM_LIT:1> ) <EOL> lock . acquire ( ) . addCallback ( self . _incr ) <EOL> self . assertTrue ( lock . locked ) <EOL> self . assertEqual ( self . counter , <NUM_LIT:1> ) <EOL> lock . release ( ) <EOL> self . assertTrue ( lock . locked ) <EOL> self . assertEqual ( self . counter , <NUM_LIT:2> ) <EOL> lock . release ( ) <EOL> self . assertFalse ( lock . locked ) <EOL> self . assertEqual ( self . counter , <NUM_LIT:2> ) <EOL> self . assertRaises ( TypeError , lock . run ) <EOL> firstUnique = object ( ) <EOL> secondUnique = object ( ) <EOL> controlDeferred = defer . Deferred ( ) <EOL> def helper ( self , b ) : <EOL> self . b = b <EOL> return controlDeferred <EOL> resultDeferred = lock . run ( helper , self = self , b = firstUnique ) <EOL> self . assertTrue ( lock . locked ) <EOL> self . assertEqual ( self . b , firstUnique ) <EOL> resultDeferred . addCallback ( lambda x : setattr ( self , '<STR_LIT:result>' , x ) ) <EOL> lock . acquire ( ) . addCallback ( self . _incr ) <EOL> self . assertTrue ( lock . locked ) <EOL> self . assertEqual ( self . counter , <NUM_LIT:2> ) <EOL> controlDeferred . callback ( secondUnique ) <EOL> self . assertEqual ( self . result , secondUnique ) <EOL> self . assertTrue ( lock . locked ) <EOL> self . assertEqual ( self . counter , <NUM_LIT:3> ) <EOL> d = lock . acquire ( ) . addBoth ( lambda x : setattr ( self , '<STR_LIT:result>' , x ) ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( self . result . type , defer . CancelledError ) <EOL> lock . release ( ) <EOL> self . assertFalse ( lock . locked ) <EOL> def test_cancelLockAfterAcquired ( self ) : <EOL> """<STR_LIT>""" <EOL> def _failOnErrback ( _ ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> lock = defer . DeferredLock ( ) <EOL> d = lock . acquire ( ) <EOL> d . addErrback ( _failOnErrback ) <EOL> d . cancel ( ) <EOL> def test_cancelLockBeforeAcquired ( self ) : <EOL> """<STR_LIT>""" <EOL> lock = defer . DeferredLock ( ) <EOL> lock . acquire ( ) <EOL> d = lock . acquire ( ) <EOL> d . cancel ( ) <EOL> self . assertImmediateFailure ( d , defer . CancelledError ) <EOL> def testSemaphore ( self ) : <EOL> N = <NUM_LIT> <EOL> sem = defer . DeferredSemaphore ( N ) <EOL> controlDeferred = defer . Deferred ( ) <EOL> def helper ( self , arg ) : <EOL> self . arg = arg <EOL> return controlDeferred <EOL> results = [ ] <EOL> uniqueObject = object ( ) <EOL> resultDeferred = sem . run ( helper , self = self , arg = uniqueObject ) <EOL> resultDeferred . addCallback ( results . append ) <EOL> resultDeferred . addCallback ( self . _incr ) <EOL> self . assertEqual ( results , [ ] ) <EOL> self . assertEqual ( self . arg , uniqueObject ) <EOL> controlDeferred . callback ( None ) <EOL> self . assertEqual ( results . pop ( ) , None ) <EOL> self . assertEqual ( self . counter , <NUM_LIT:1> ) <EOL> self . counter = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:1> + N ) : <EOL> sem . acquire ( ) . addCallback ( self . _incr ) <EOL> self . assertEqual ( self . counter , i ) <EOL> success = [ ] <EOL> def fail ( r ) : <EOL> success . append ( False ) <EOL> def succeed ( r ) : <EOL> success . append ( True ) <EOL> d = sem . acquire ( ) . addCallbacks ( fail , succeed ) <EOL> d . cancel ( ) <EOL> self . assertEqual ( success , [ True ] ) <EOL> sem . acquire ( ) . addCallback ( self . _incr ) <EOL> self . assertEqual ( self . counter , N ) <EOL> sem . release ( ) <EOL> self . assertEqual ( self . counter , N + <NUM_LIT:1> ) <EOL> for i in range ( <NUM_LIT:1> , <NUM_LIT:1> + N ) : <EOL> sem . release ( ) <EOL> self . assertEqual ( self . counter , N + <NUM_LIT:1> ) <EOL> def test_semaphoreInvalidTokens ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( ValueError , defer . DeferredSemaphore , <NUM_LIT:0> ) <EOL> self . assertRaises ( ValueError , defer . DeferredSemaphore , - <NUM_LIT:1> ) <EOL> def test_cancelSemaphoreAfterAcquired ( self ) : <EOL> """<STR_LIT>""" <EOL> def _failOnErrback ( _ ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> sem = defer . DeferredSemaphore ( <NUM_LIT:1> ) <EOL> d = sem . acquire ( ) <EOL> d . addErrback ( _failOnErrback ) <EOL> d . cancel ( ) <EOL> def test_cancelSemaphoreBeforeAcquired ( self ) : <EOL> """<STR_LIT>""" <EOL> sem = defer . DeferredSemaphore ( <NUM_LIT:1> ) <EOL> sem . acquire ( ) <EOL> d = sem . acquire ( ) <EOL> d . cancel ( ) <EOL> self . assertImmediateFailure ( d , defer . CancelledError ) <EOL> def testQueue ( self ) : <EOL> N , M = <NUM_LIT:2> , <NUM_LIT:2> <EOL> queue = defer . DeferredQueue ( N , M ) <EOL> gotten = [ ] <EOL> for i in range ( M ) : <EOL> queue . get ( ) . addCallback ( gotten . append ) <EOL> self . assertRaises ( defer . QueueUnderflow , queue . get ) <EOL> for i in range ( M ) : <EOL> queue . put ( i ) <EOL> self . assertEqual ( gotten , list ( range ( i + <NUM_LIT:1> ) ) ) <EOL> for i in range ( N ) : <EOL> queue . put ( N + i ) <EOL> self . assertEqual ( gotten , list ( range ( M ) ) ) <EOL> self . assertRaises ( defer . QueueOverflow , queue . put , None ) <EOL> gotten = [ ] <EOL> for i in range ( N ) : <EOL> queue . get ( ) . addCallback ( gotten . append ) <EOL> self . assertEqual ( gotten , list ( range ( N , N + i + <NUM_LIT:1> ) ) ) <EOL> queue = defer . DeferredQueue ( ) <EOL> gotten = [ ] <EOL> for i in range ( N ) : <EOL> queue . get ( ) . addCallback ( gotten . append ) <EOL> for i in range ( N ) : <EOL> queue . put ( i ) <EOL> self . assertEqual ( gotten , list ( range ( N ) ) ) <EOL> queue = defer . DeferredQueue ( size = <NUM_LIT:0> ) <EOL> self . assertRaises ( defer . QueueOverflow , queue . put , None ) <EOL> queue = defer . DeferredQueue ( backlog = <NUM_LIT:0> ) <EOL> self . assertRaises ( defer . QueueUnderflow , queue . get ) <EOL> def test_cancelQueueAfterSynchronousGet ( self ) : <EOL> """<STR_LIT>""" <EOL> def _failOnErrback ( _ ) : <EOL> self . fail ( "<STR_LIT>" ) <EOL> queue = defer . DeferredQueue ( ) <EOL> d = queue . get ( ) <EOL> d . addErrback ( _failOnErrback ) <EOL> queue . put ( None ) <EOL> d . cancel ( ) <EOL> def test_cancelQueueAfterGet ( self ) : <EOL> """<STR_LIT>""" <EOL> queue = defer . DeferredQueue ( ) <EOL> d = queue . get ( ) <EOL> d . cancel ( ) <EOL> self . assertImmediateFailure ( d , defer . CancelledError ) <EOL> def cb ( ignore ) : <EOL> queue . put ( None ) <EOL> return queue . get ( ) . addCallback ( self . assertIdentical , None ) <EOL> d . addCallback ( cb ) <EOL> done = [ ] <EOL> d . addCallback ( done . append ) <EOL> self . assertEqual ( len ( done ) , <NUM_LIT:1> ) <EOL> class DeferredFilesystemLockTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . clock = Clock ( ) <EOL> self . lock = defer . DeferredFilesystemLock ( self . mktemp ( ) , <EOL> scheduler = self . clock ) <EOL> def test_waitUntilLockedWithNoLock ( self ) : <EOL> """<STR_LIT>""" <EOL> d = self . lock . deferUntilLocked ( timeout = <NUM_LIT:1> ) <EOL> return d <EOL> def test_waitUntilLockedWithTimeoutLocked ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertTrue ( self . lock . lock ( ) ) <EOL> d = self . lock . deferUntilLocked ( timeout = <NUM_LIT> ) <EOL> self . assertFailure ( d , defer . TimeoutError ) <EOL> self . clock . pump ( [ <NUM_LIT:1> ] * <NUM_LIT:10> ) <EOL> return d <EOL> def test_waitUntilLockedWithTimeoutUnlocked ( self ) : <EOL> """<STR_LIT>""" <EOL> def onTimeout ( f ) : <EOL> f . trap ( defer . TimeoutError ) <EOL> self . fail ( "<STR_LIT>" ) <EOL> self . assertTrue ( self . lock . lock ( ) ) <EOL> self . clock . callLater ( <NUM_LIT:1> , self . lock . unlock ) <EOL> d = self . lock . deferUntilLocked ( timeout = <NUM_LIT:10> ) <EOL> d . addErrback ( onTimeout ) <EOL> self . clock . pump ( [ <NUM_LIT:1> ] * <NUM_LIT:10> ) <EOL> return d <EOL> def test_defaultScheduler ( self ) : <EOL> """<STR_LIT>""" <EOL> lock = defer . DeferredFilesystemLock ( self . mktemp ( ) ) <EOL> self . assertEqual ( lock . _scheduler , reactor ) <EOL> def test_concurrentUsage ( self ) : <EOL> """<STR_LIT>""" <EOL> self . lock . lock ( ) <EOL> self . clock . callLater ( <NUM_LIT:1> , self . lock . unlock ) <EOL> d = self . lock . deferUntilLocked ( ) <EOL> d2 = self . lock . deferUntilLocked ( ) <EOL> self . assertFailure ( d2 , defer . AlreadyTryingToLockError ) <EOL> self . clock . advance ( <NUM_LIT:1> ) <EOL> return d <EOL> def test_multipleUsages ( self ) : <EOL> """<STR_LIT>""" <EOL> def lockAquired ( ign ) : <EOL> self . lock . unlock ( ) <EOL> d = self . lock . deferUntilLocked ( ) <EOL> return d <EOL> self . lock . lock ( ) <EOL> self . clock . callLater ( <NUM_LIT:1> , self . lock . unlock ) <EOL> d = self . lock . deferUntilLocked ( ) <EOL> d . addCallback ( lockAquired ) <EOL> self . clock . advance ( <NUM_LIT:1> ) <EOL> return d <EOL> def test_cancelDeferUntilLocked ( self ) : <EOL> """<STR_LIT>""" <EOL> self . lock . lock ( ) <EOL> deferred = self . lock . deferUntilLocked ( ) <EOL> tryLockCall = self . lock . _tryLockCall <EOL> deferred . cancel ( ) <EOL> self . assertFalse ( tryLockCall . active ( ) ) <EOL> self . assertEqual ( self . lock . _tryLockCall , None ) <EOL> self . failureResultOf ( deferred , defer . CancelledError ) <EOL> def test_cancelDeferUntilLockedWithTimeout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . lock . lock ( ) <EOL> deferred = self . lock . deferUntilLocked ( timeout = <NUM_LIT:1> ) <EOL> timeoutCall = self . lock . _timeoutCall <EOL> deferred . cancel ( ) <EOL> self . assertFalse ( timeoutCall . active ( ) ) <EOL> self . assertEqual ( self . lock . _timeoutCall , None ) <EOL> self . failureResultOf ( deferred , defer . CancelledError ) </s>
