o
    Óó·iƒ9  ã                   @   sr  d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ dejƒZG dd„ dejƒZG dd„ de
jejejƒZG dd„ dejejƒZe ee¡ e ee¡ ej e¡ G dd„ dejƒZG dd„ dej ƒZ!G dd„ dej"ƒZ#G dd„ de#ƒZ$G dd„ de$ƒZ%G dd „ d ƒZ&G d!d"„ d"ej"ƒZ'd#S )$z!
Tests for error handling in PB.
é    )ÚStringIO)Údefer)Úlog)Úqual)ÚflavorsÚjellyÚpb)ÚconnectedServerAndClient)Úunittestc                   @   ó   e Zd ZdZdS )ÚAsynchronousExceptionzˆ
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are not L{pb.Error} subclasses.
    N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úhC:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/spread/test/test_pbfailure.pyr      ó    r   c                   @   r   )ÚSynchronousExceptionzm
    Helper used to test remote methods which raise exceptions which are not
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r      r   r   c                   @   r   )ÚAsynchronousErrorz„
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are L{pb.Error} subclasses.
    Nr   r   r   r   r   r   "   r   r   c                   @   r   )ÚSynchronousErrorzi
    Helper used to test remote methods which raise exceptions which are
    L{pb.Error} subclasses.
    Nr   r   r   r   r   r   )   r   r   c                   @   ó   e Zd ZdS )Ú
JellyErrorN©r   r   r   r   r   r   r   r   0   ó    r   c                   @   r   )ÚSecurityErrorNr   r   r   r   r   r   4   r   r   c                   @   sh   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zddd„Zddd„ZdS )Ú
SimpleRootc                 C   ó   t  tdƒ¡S )zD
        Fail asynchronously with a non-pb.Error exception.
        zremote asynchronous exceptionN)r   Úfailr   ©Úselfr   r   r   Úremote_asynchronousException?   ó   z'SimpleRoot.remote_asynchronousExceptionc                 C   ó   t dƒ‚)zC
        Fail synchronously with a non-pb.Error exception.
        zremote synchronous exceptionN)r   r    r   r   r   Úremote_synchronousExceptionE   ó   z&SimpleRoot.remote_synchronousExceptionc                 C   r   )z@
        Fail asynchronously with a pb.Error exception.
        zremote asynchronous errorN)r   r   r   r    r   r   r   Úremote_asynchronousErrorK   r#   z#SimpleRoot.remote_asynchronousErrorc                 C   r$   )z?
        Fail synchronously with a pb.Error exception.
        zremote synchronous errorN)r   r    r   r   r   Úremote_synchronousErrorQ   r&   z"SimpleRoot.remote_synchronousErrorc                 C   s   G dd„ dt jƒ}|dƒ‚)z>
        Fail with error that is not known to client.
        c                   @   r   )z4SimpleRoot.remote_unknownError.<locals>.UnknownErrorNr   r   r   r   r   ÚUnknownError\   r   r)   zI'm not known to client!N)r   ÚError)r!   r)   r   r   r   Úremote_unknownErrorW   s   zSimpleRoot.remote_unknownErrorc                 C   ó   |   ¡  d S ©N)Ú
raiseJellyr    r   r   r   Úremote_jellya   ó   zSimpleRoot.remote_jellyc                 C   r,   r-   )ÚraiseSecurityr    r   r   r   Úremote_securityd   r0   zSimpleRoot.remote_securityc                 C   ó"   t  ¡ }| | j¡ | d ¡ |S r-   )r   ÚDeferredÚaddCallbackr.   Úcallback©r!   Údr   r   r   Úremote_deferredJellyg   ó   
zSimpleRoot.remote_deferredJellyc                 C   r3   r-   )r   r4   r5   r1   r6   r7   r   r   r   Úremote_deferredSecuritym   r:   z"SimpleRoot.remote_deferredSecurityNc                 C   r$   )NzI'm jellyable!)r   ©r!   Úresultsr   r   r   r.   s   ó   zSimpleRoot.raiseJellyc                 C   r$   )NzI'm secure!)r   r<   r   r   r   r1   v   r>   zSimpleRoot.raiseSecurityr-   )r   r   r   r"   r%   r'   r(   r+   r/   r2   r9   r;   r.   r1   r   r   r   r   r   >   s    

r   c                   @   s   e Zd ZdZdZdd„ ZdS )ÚSaveProtocolServerFactoryzd
    A L{pb.PBServerFactory} that saves the latest connected client in
    C{protocolInstance}.
    Nc                 C   s
   || _ dS )z3
        Keep track of the given protocol.
        N)ÚprotocolInstance)r!   Úprotocolr   r   r   ÚclientConnectionMade‚   s   
z.SaveProtocolServerFactory.clientConnectionMade)r   r   r   r   r@   rB   r   r   r   r   r?   z   s    r?   c                   @   s   e Zd ZdZdd„ ZdS )ÚPBConnTestCaser   c                    sJ   t tƒ ƒˆ _ˆ jˆ j_t ¡ ˆ _t‡ fdd„‡ fdd„ƒ\ˆ _ˆ _	ˆ _
d S )Nc                      ó   ˆ j  d ¡S r-   )ÚserverFactoryÚbuildProtocolr   r    r   r   Ú<lambda>•   ó    z&PBConnTestCase.setUp.<locals>.<lambda>c                      rD   r-   )ÚclientFactoryrF   r   r    r   r   rG   –   rH   )r?   r   rE   ÚunsafeTracebacksr   ZPBClientFactoryrI   r	   ZconnectedServerZconnectedClientÚpumpr    r   r    r   ÚsetUpŒ   s   



þüzPBConnTestCase.setUpN)r   r   r   rJ   rL   r   r   r   r   rC   ‰   s    rC   c                   @   sž   e Zd ZejjZdd„ Zdd„ Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zd$dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ ZdS )%ÚPBFailureTestsc                    s@   ‡‡‡fdd„‰ ˆj  ¡ }‡ ‡fdd„}| |¡ ˆj ¡  d S )Nc                    sF   |   ˆ ¡ ˆ | jd¡ ˆrˆ ˆ ¡}ˆ t|ƒd¡ | j| j| jfS )NúTraceback unavailable
é   )ÚtrapÚcompareÚ	tracebackÚflushLoggedErrorsÚassertEqualÚlenÚtypeÚvalue)ÚerrÚerrs)ÚexceptionTypeÚflushr!   r   r   Úebž   s   

z)PBFailureTests._exceptionTest.<locals>.ebc                    s   |   ˆ¡}| ˆ ¡ |S r-   )Ú
callRemoteÚ
addErrback)Úrootr8   )r\   Úmethodr   r   ÚgotRootObject¨   s   

z4PBFailureTests._exceptionTest.<locals>.gotRootObject©rI   ÚgetRootObjectr5   rK   r[   )r!   r`   rZ   r[   r8   ra   r   )r\   rZ   r[   r`   r!   r   Ú_exceptionTest�   s
   

zPBFailureTests._exceptionTestc                 C   ó   |   dtd¡S )z 
        Test that a Deferred returned by a remote method which already has a
        Failure correctly has that error passed back to the calling side.
        ZasynchronousExceptionTN)rd   r   r    r   r   r   Útest_asynchronousException°   ó   z)PBFailureTests.test_asynchronousExceptionc                 C   re   )zw
        Like L{test_asynchronousException}, but for a method which raises an
        exception synchronously.
        ÚsynchronousExceptionTN)rd   r   r    r   r   r   Útest_synchronousException·   rg   z(PBFailureTests.test_synchronousExceptionc                 C   re   )z�
        Like L{test_asynchronousException}, but for a method which returns a
        Deferred failing with an L{pb.Error} subclass.
        ZasynchronousErrorFN)rd   r   r    r   r   r   Útest_asynchronousError¾   rg   z%PBFailureTests.test_asynchronousErrorc                 C   re   )z}
        Like L{test_asynchronousError}, but for a method which synchronously
        raises a L{pb.Error} subclass.
        ÚsynchronousErrorFN)rd   r   r    r   r   r   Útest_synchronousErrorÅ   rg   z$PBFailureTests.test_synchronousErrorc                 C   s   |   ||¡ |S r-   )rT   )r!   ÚresultÚexpectedResultr   r   r   Ú_successÌ   s   zPBFailureTests._successc                 C   s   |j | j||fd� |S )N)ZcallbackArgs)ZaddCallbacksro   )r!   Z
remoteCallrn   r\   r   r   r   Ú_addFailingCallbacksÐ   s   z#PBFailureTests._addFailingCallbacksNc                    s6   ˆj  ¡ }‡ ‡‡‡‡fdd„}| |¡ ˆj ¡  dS )zÆ
        Call the given remote method and attach the given errback to the
        resulting Deferred.  If C{exc} is not None, also assert that one
        exception of that type was logged.
        c                    s8   ˆ  |  ˆ¡ˆˆ ¡}ˆd ur‡‡fdd„}| |¡ |S )Nc                    s   ˆ  tˆ ˆ ¡ƒd¡ | S )NrO   )rT   rU   rS   )rX   )Úexcr!   r   r   Ú
gotFailureâ   s   z@PBFailureTests._testImpl.<locals>.gotRootObj.<locals>.gotFailure)rp   r]   ZaddBoth)ÚobjZfailureDeferredrr   ©r\   rq   Úexpectedr`   r!   r   r   Ú
gotRootObjÜ   s   ÿ
z,PBFailureTests._testImpl.<locals>.gotRootObjNrb   )r!   r`   ru   r\   rq   ZrootDeferredrv   r   rt   r   Ú	_testImplÔ   s   

zPBFailureTests._testImplc                    ó   ‡ fdd„}ˆ   dd|¡S )z—
        Test that an exception which is a subclass of L{pb.Error} has more
        information passed across the network to the calling side.
        c                    ó,   |   t¡ ˆ  | jt¡ ˆ  | j| j¡ dS )Né+   ©rP   r   ÚassertNotIsInstancerV   ÚstrÚassertIsInstancerW   ©r   r    r   r   ÚfailureJellyò   ó   
z6PBFailureTests.test_jellyFailure.<locals>.failureJellyr   rz   N©rw   )r!   r€   r   r    r   Útest_jellyFailureì   ó   z PBFailureTests.test_jellyFailurec                    rx   )z�
        Test that a Deferred which fails with a L{pb.Error} is treated in
        the same way as a synchronously raised L{pb.Error}.
        c                    ry   )Né®  r{   r   r    r   r   ÚfailureDeferredJelly   r�   zFPBFailureTests.test_deferredJellyFailure.<locals>.failureDeferredJellyZdeferredJellyr…   Nr‚   )r!   r†   r   r    r   Útest_deferredJellyFailureú   r„   z(PBFailureTests.test_deferredJellyFailurec                    rx   )z¬
        A non-jellyable L{pb.Error} subclass raised by a remote method is
        turned into a Failure with a type set to the FQPN of the exception
        type.
        c                    ó   ˆ   | jd¡ dS )Ns3   twisted.spread.test.test_pbfailure.SynchronousErroré¯  ©rT   rV   r   r    r   r   ÚfailureUnjellyable  ó   ÿzBPBFailureTests.test_unjellyableFailure.<locals>.failureUnjellyablerk   r‰   Nr‚   )r!   r‹   r   r    r   Útest_unjellyableFailure  ó   z&PBFailureTests.test_unjellyableFailurec                    rx   )z�
        Test that an exception which is a subclass of L{pb.Error} but not
        known on the client side has its type set properly.
        c                    rˆ   )Ns/   twisted.spread.test.test_pbfailure.UnknownErroréÖ  rŠ   r   r    r   r   ÚfailureUnknown  rŒ   z:PBFailureTests.test_unknownFailure.<locals>.failureUnknownZunknownErrorr�   Nr‚   )r!   r�   r   r    r   Útest_unknownFailure  r„   z"PBFailureTests.test_unknownFailurec                    rx   )zÔ
        Test that even if an exception is not explicitly jellyable (by being
        a L{pb.Jellyable} subclass), as long as it is an L{pb.Error}
        subclass it receives the same special treatment.
        c                    ry   )NéÌ  ©rP   r   r|   rV   r}   r~   rW   r   r    r   r   ÚfailureSecurity,  r�   z<PBFailureTests.test_securityFailure.<locals>.failureSecurityÚsecurityr’   Nr‚   )r!   r”   r   r    r   Útest_securityFailure%  rŽ   z#PBFailureTests.test_securityFailurec                    rx   )zÊ
        Test that a Deferred which fails with a L{pb.Error} which is not
        also a L{pb.Jellyable} is treated in the same way as a synchronously
        raised exception of the same type.
        c                    ry   )Néø§  r“   r   r    r   r   ÚfailureDeferredSecurity;  r�   zEPBFailureTests.test_deferredSecurity.<locals>.failureDeferredSecurityZdeferredSecurityr—   Nr‚   )r!   r˜   r   r    r   Útest_deferredSecurity4  rŽ   z$PBFailureTests.test_deferredSecurityc                    s   ‡ fdd„}ˆ   dd|t¡S )z�
        Test that attempting to call a method which is not defined correctly
        results in an AttributeError on the calling side.
        c                    s   |   tj¡ ˆ  | jd¡ dS )NrN   é¤  )rP   r   ZNoSuchMethodrQ   rR   r   r    r   r   ÚfailureNoSuchI  s   z>PBFailureTests.test_noSuchMethodFailure.<locals>.failureNoSuchZnosuchrš   N)rw   ÚAttributeError)r!   r›   r   r    r   Útest_noSuchMethodFailureC  s   z'PBFailureTests.test_noSuchMethodFailurec                    s@   ˆ j  ¡ }dd„ }| |¡ ‡ fdd„}| |¡ ˆ j ¡  dS )zì
        Test that a copied failure received from a PB call can be logged
        locally.

        Note: this test needs some serious help: all it really tests is that
        log.err(copiedFailure) doesn't raise an exception.
        c                 S   s
   |   d¡S )Nrh   )r]   )ZrootObjr   r   r   Ú	connectedZ  s   
z;PBFailureTests.test_copiedFailureLogging.<locals>.connectedc                    s(   t  | ¡ ˆ  t¡}ˆ  t|ƒd¡ d S )Né   )r   rX   rS   r   rT   rU   )ZfailurerY   r    r   r   Ú	exception_  s   

z;PBFailureTests.test_copiedFailureLogging.<locals>.exceptionN)rI   rc   r5   r^   rK   r[   )r!   r8   rž   r    r   r    r   Útest_copiedFailureLoggingP  s   


z(PBFailureTests.test_copiedFailureLoggingc                    s¦   t  tdƒ¡}t tj|tƒ d�¡}g ‰ ‡ ‡fdd„}|ƒ }| d¡ ˆ t|j	|¡ ˆ 
tˆ ƒd¡ ˆ d }ˆ 
|jttƒ d¡¡ ˆ 
|jd	¡ ˆ 
|jd
¡ dS )z±
        L{pb.CopiedFailure.throwExceptionIntoGenerator} will throw a
        L{RemoteError} into the given paused generator at the point where it
        last yielded.
        Úfoo©Zinvokerc               
   3   sN   � zd V  W n t jy }  zˆ  | ¡ W Y d } ~ d S d } ~ ww ˆ d¡ d S )NzRemoteError not raised)r   ZRemoteErrorÚappendr   )rq   ©r    r!   r   r   ÚgeneratorFuncq  s   €
€ÿzFPBFailureTests.test_throwExceptionIntoGenerator.<locals>.generatorFuncNrO   r   Úascii)r¢   rN   )r   ÚCopyableFailurerœ   r   ÚunjellyÚDummyInvokerÚsendÚassertRaisesÚStopIterationZthrowExceptionIntoGeneratorrT   rU   Z
remoteTyper   ÚencodeÚargsZremoteTraceback)r!   ÚoriginalÚcopyr¦   Úgenrq   r   r¥   r   Ú test_throwExceptionIntoGeneratorg  s   
z/PBFailureTests.test_throwExceptionIntoGeneratorr-   )r   r   r   r
   ÚTestCaserT   rQ   rd   rf   ri   rj   rl   ro   rp   rw   rƒ   r‡   r�   r‘   r–   r™   r�   r¡   r³   r   r   r   r   rM   š   s&    
rM   c                   @   s   e Zd ZejjZdZdS )ÚPBFailureUnsafeTestsrO   N)r   r   r   r
   r´   ZfailIfEqualsrQ   rJ   r   r   r   r   rµ   ƒ  s    rµ   c                   @   s   e Zd ZdZdZdS )rª   zZ
    A behaviorless object to be used as the invoker parameter to
    L{jelly.jelly}.
    N)r   r   r   r   ZserializingPerspectiver   r   r   r   rª   ˆ  s    rª   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚFailureJellyingTestsz:
    Tests for the interaction of jelly and failures.
    c                 C   sn   t  tƒ ¡}|  | t¡t¡ |  | t¡t¡ t tj|tƒ d�¡}|  | t¡t¡ |  | t¡t¡ dS )zš
        An unjellied L{CopyableFailure} has a check method which behaves the
        same way as the original L{CopyableFailure}'s check method.
        r£   N©	r   r¨   ÚZeroDivisionErrorZassertIsÚcheckÚArithmeticErrorr   r©   rª   )r!   r°   Úcopiedr   r   r   Útest_unjelliedFailureCheck–  s   z/FailureJellyingTests.test_unjelliedFailureCheckc                 C   sŽ   t  tƒ ¡}|  | t¡t¡ |  | t¡t¡ t tj|tƒ d�¡}t  |¡}t tj|tƒ d�¡}|  | t¡t¡ |  | t¡t¡ dS )aX  
        The object which results from jellying a L{CopyableFailure}, unjellying
        the result, creating a new L{CopyableFailure} from the result of that,
        jellying it, and finally unjellying the result of that has a check
        method which behaves the same way as the original L{CopyableFailure}'s
        check method.
        r£   Nr·   )r!   r°   Z
copiedOnceZ
derivativeZcopiedTwicer   r   r   Útest_twiceUnjelliedFailureCheck¢  s   
z4FailureJellyingTests.test_twiceUnjelliedFailureCheckc                 C   sZ   t  tdƒ¡}t tj|tƒ d�¡}tƒ }| |¡ ttƒ}d 	|¡}|  
|| ¡ ¡ dS )a  
        When L{CopiedFailure.printTraceback} is used to print a copied failure
        which was unjellied from a L{CopyableFailure} with C{unsafeTracebacks}
        set to C{False}, the string representation of the exception value is
        included in the output.
        zsome reasonr£   z.Traceback from remote host -- {}: some reason
N)r   r¨   Ú	Exceptionr   r©   rª   r   ZprintTracebackr   ÚformatrT   Úgetvalue)r!   r°   r»   Úoutputr    ZexpectedOutputr   r   r   Ú test_printTracebackIncludesValue³  s   
ÿz5FailureJellyingTests.test_printTracebackIncludesValueN)r   r   r   r   r¼   r½   rÂ   r   r   r   r   r¶   ‘  s
    r¶   N)(r   Úior   Ztwisted.internetr   Ztwisted.pythonr   Ztwisted.python.reflectr   Ztwisted.spreadr   r   r   Ztwisted.test.iosimr	   Ztwisted.trialr
   r¾   r   r   r*   r   r   Z	JellyableZ
RemoteCopyr   r   ZsetUnjellyableForClassZglobalSecurityZallowInstancesOfZRootr   ZPBServerFactoryr?   r´   rC   rM   rµ   rª   r¶   r   r   r   r   Ú<module>   s2   < j	