Ϫf,dZddlmZmZmZmZddlmZmZGddeZ GddeZ Gdd eZ Gd d e Z Gd d e ZGdde ZGddeZy)z Tests for L{twisted.internet.defer.inlineCallbacks}. Some tests for inlineCallbacks are defined in L{twisted.test.test_defgen} as well. )CancelledErrorDeferredinlineCallbacks returnValue)SynchronousTestCaseTestCaseceZdZdZdZy)StopIterationReturnTestsz On Python 3.4 and newer generator functions may use the C{return} statement with a value, which is attached to the L{StopIteration} exception that is raised. L{inlineCallbacks} will use this value when it fires the C{callback}. cdti}td|t}|d|}|jd|j |j |dy)z If the C{return} statement has a value it is propagated back to the L{Deferred} that the C{inlineCallbacks} function returned. rz> @inlineCallbacks def f(d): yield d return 14 fN)rexecrcallback assertEqualsuccessResultOf)selfenvirond1d2s E/usr/lib/python3/dist-packages/twisted/internet/test/test_inlinecb.pytest_returnWithValuez-StopIterationReturnTests.test_returnWithValues` %o6   Z WS\"  D --b126N)__name__ __module__ __qualname____doc__rrrr r s 7rr c(eZdZdZdZdZdZdZy)NonLocalExitTestsa It's possible for L{returnValue} to be (accidentally) invoked at a stack level below the L{inlineCallbacks}-decorated function which it is exiting. If this happens, L{returnValue} should report useful errors. If L{returnValue} is invoked from a function not decorated by L{inlineCallbacks}, it will emit a warning if it causes an L{inlineCallbacks} function further up the stack to exit. ctdy)z This method mistakenly invokes L{returnValue}, despite the fact that it is not decorated with L{inlineCallbacks}. N)rrs rmistakenMethodz NonLocalExitTests.mistakenMethod?s Arc|j|dg|j|jg}|jt|d|j|ddt|j|dddy)ac Flush the current warnings and assert that we have been told that C{mistakenMethod} was invoked, and that the result from the Deferred that was fired (appended to the given list) is C{mistakenMethod}'s result. The warning should indicate that an inlineCallbacks function called 'inline' was made to exit. r!)offendingFunctionsrcategorymessagezreturnValue() in 'mistakenMethod' causing 'inline' to exit: returnValue should only be invoked by functions decorated with inlineCallbacksN)r flushWarningsr#lenDeprecationWarning)r resultListwarningss rassertMistakenMethodWarningz-NonLocalExitTests.assertMistakenMethodWarningFs aS)%%$:M:M9N%O X* !Z02DE  QK "  rctfd}|}g}|j|jj|y)z L{returnValue} will emit a non-local exit warning in the simplest case, where the offending function is invoked immediately. c3LKjtddyw)Nrr#rr"srinlinezANonLocalExitTests.test_returnValueNonLocalWarning..inline_s    ! NG!$N)r addCallbackappendr-)rr2dresultss` rtest_returnValueNonLocalWarningz1NonLocalExitTests.test_returnValueNonLocalWarningYsC     H gnn% ((1rcttfd}|}g}|j|jj |gj dj |y)z L{returnValue} will emit a non-local warning in the case where the L{inlineCallbacks}-decorated function has already yielded a Deferred and therefore moved its generator function along. c3LKjtdyw)Nr0r1)causersrr2zBNonLocalExitTests.test_returnValueNonLocalDeferred..inlinersK    ! Nr3r!N)rrr4r5rrr-)rr2effectr7r;s` @r test_returnValueNonLocalDeferredz2NonLocalExitTests.test_returnValueNonLocalDeferredjsh       7>>* "% q ((1rN)rrrrr#r-r8r=rrrrr4s &2"2rrceZdZdZdZy)ForwardTraceBackTestsctdtfd}|}|j|}|j}|jd||jd||jd|y)z Chained inlineCallbacks are forwarding the traceback information from generator to generator. A first simple test with a couple of inline callbacks. c3(KdtdwNzforcing generator Error Marker Exceptionrrrerroringz>ForwardTraceBackTests.test_forwardTracebacks..erroring% %N+ +c3 KywNrrFsrcallingz=ForwardTraceBackTests.test_forwardTracebacks..calling*   in erroring in callingrCN)rfailureResultOf getTracebackassertIn)rrLr6r tbrFs @rtest_forwardTracebacksz,ForwardTraceBackTests.test_forwardTracebackssz  ,  ,     I   # ^^  mR( lB' nb)rctdtfdtfdtfd}|}|j|}|j}|jd||jd||jd||jd||j d ||jd ||jd|jy ) a Several Chained inlineCallbacks gives information about all generators. A wider test with a 4 chained inline callbacks. Application stack-trace should be reported, and implementation details like "throwExceptionIntoGenerator" symbols are omitted from the stack. Note that the previous test is testing the simple case, and this one is testing the deep recursion case. That case needs specific code in failure.py to accomodate to stack breakage introduced by throwExceptionIntoGenerator. Hence we keep the two tests in order to sort out which code we might have regression in. c3(KdtdwrBrDrrrrFzDForwardTraceBackTests.test_forwardLotsOfTracebacks..erroringrGrHc3 KywrJrrKsrcalling3zDForwardTraceBackTests.test_forwardLotsOfTracebacks..calling3rMrNc3 KywrJr)rYsrcalling2zDForwardTraceBackTests.test_forwardLotsOfTracebacks..calling2rMrNc3 KywrJr)r[srrLzCForwardTraceBackTests.test_forwardLotsOfTracebacks..callingrMrNrOrPz in calling2z in calling3throwExceptionIntoGeneratorrCN)rrQrRrS assertNotIn)rrLr6r rTr[rYrFs @@@rtest_forwardLotsOfTracebacksz2ForwardTraceBackTests.test_forwardLotsOfTracebackss&  ,  ,             I   # ^^  mR( lB' mR( mR( 6; nb) mQ^^%56rN)rrrrUr_rrrr?r?s *0-7rr?ceZdZdZy)UntranslatedErrorzL Untranslated exception type when testing an exception translation. NrrrrrrrrararraceZdZdZy)TranslatedErrorzJ Translated exception type when testing an exception translation. NrbrrrrerercrreceZdZdZdZy)DontFailz Sample exception type. c<tj|||_yrJ)rE__init__ actualValue)ractuals rrizDontFail.__init__s4 !rN)rrrrrirrrrgrgs "rrgcZeZdZdZdZdZed dZdZd dZ dZ d Z d Z d Z d Zy)CancellationTestsa! Tests for cancellation of L{Deferred}s returned by L{inlineCallbacks}. For each of these tests, let: - C{G} be a generator decorated with C{inlineCallbacks} - C{D} be a L{Deferred} returned by C{G} - C{C} be a L{Deferred} awaited by C{G} with C{yield} cg|_y)z> Set up the list of outstanding L{Deferred}s. N)deferredsOutstandingr"s rsetUpzCancellationTests.setUps %'!rcX|jr|j|jryy)zG If any L{Deferred}s are still outstanding, fire them. N)rodeferredGottenr"s rtearDownzCancellationTests.tearDowns$''    !''rNc#K| |j} |}t |dzy#t$r tt$r}|jdz }Yd}~>d}~wwxYww)z Generator for testing cascade cancelling cases. @param getChildDeferred: Some callable returning L{Deferred} that we awaiting (with C{yield}) Nr0r!) getDeferredrarergrjr)rgetChildDeferredxdfs rsampleInlineCBz CancellationTests.sampleInlineCBsh  ##//  #&((A AE ! $!# # #"A #s+A! *A!AAA!AA!cf|jjt|jdS)z A sample function that returns a L{Deferred} that can be fired on demand, by L{CancellationTests.deferredGotten}. @return: L{Deferred} that can be fired on demand. )ror5rr"s rruzCancellationTests.getDeferreds+ !!((4((,,rcX|jjdj|y)z Fire the L{Deferred} returned from the least-recent call to L{CancellationTests.getDeferred}. @param result: result object to be used when firing the L{Deferred}. rN)ropopr)rresults rrrz CancellationTests.deferredGottens# !!%%a(11&9rcdgfd}|j|}|jd|j|jdtdy)zN When C{D} cancelled, C{C} will be immediately cancelled too. FAILUREcHt}fd}|j||S)Nc8|jtd<|S)Nr)checkr)r~childResultHolders r_ebzWCancellationTests.test_cascadeCancellingOnCancel..getChildDeferred.._eb#s'-||N'C!!$ rr addErrback)r6rrs rrvzJCancellationTests.test_cascadeCancellingOnCancel..getChildDeferred s" A  LL HrrvcyrJr)r~s rzBCancellationTests.test_cascadeCancellingOnCancel..+srrzno cascade cancelling occursN)ryrcancelrr)rrvr6rs @rtest_cascadeCancellingOnCancelz0CancellationTests.test_cascadeCancellingOnCancelsZ'K    1A  B ()    a  * rc|j}|j|jt|j |j y)zg When C{D} cancelled, CancelledError from C{C} will be errbacked through C{D}. N)ryr assertRaisesrrQraiseException)rr6s r"test_errbackCancelledErrorOnCancelz4CancellationTests.test_errbackCancelledErrorOnCancel3s@    !       # 2 2 rcd}t||jfd}|j|jt|j |j y)z When C{D} is cancelled, and C raises a particular type of error, C{G} may catch that error at the point of yielding and translate it into a different error which may be received by application code. c6|jtyrJ)errbackraits rrz>CancellationTests.test_errorToErrorTranslation..cancelFs JJ(* +rcSrJrasrrz@CancellationTests.test_errorToErrorTranslation..JrN)rryrrrerQr)rrr6rs @rtest_errorToErrorTranslationz.CancellationTests.test_errorToErrorTranslation?sR , V     *       # 2 2 rcd}t||jfd}g}|j|j|j |j |dgy)z When C{D} is cancelled, and C{C} raises a particular type of error, C{G} may catch that error at the point of yielding and translate it into a result value which may be received by application code. c8|jtdy)Ni)rrgrs rrz@CancellationTests.test_errorToSuccessTranslation..cancelXs JJx~ &rcSrJrrsrrzBCancellationTests.test_errorToSuccessTranslation..\rriN)rryr4r5r assertEquals)rrr6r7rs @rtest_errorToSuccessTranslationz0CancellationTests.test_errorToSuccessTranslationQsT ' V     * gnn%   'D6*rctfdfd}|j|}|j|j|j d|j |j |dy)a" When C{D} is cancelled, it won't reach the callbacks added to it by application code until C{C} reaches the point in its callback chain where C{G} awaits it. Otherwise, application code won't be able to track resource usage that C{D} may be using. c2|jtSrJ)trapr)r~ moreDeferreds r deferMeMorezDCancellationTests.test_asynchronousCancellation..deferMeMoreks KK ' rc>t}|j|SrJr)r6rs rdeferMez@CancellationTests.test_asynchronousCancellation..deferMeos A LL %HrriiN)rryrassertNoResultrrr)rrr6rrs @@rtest_asynchronousCancellationz/CancellationTests.test_asynchronousCancellationbsk z        9   Ad# --a0$7rrJ)rrrrrprsrryrurrrrrrrrrrrmrmsH' ""-: 2   $+"8rrmN)rtwisted.internet.deferrrrrtwisted.trial.unittestrrr rr?rErarergrmrrrrsx  A7x7