
[XMc           @   s  d  Z  d d l m Z d d l m Z m Z m Z d d l m Z m	 Z	 d d l
 m Z d e f d     YZ d e f d	     YZ d
 e j f d     YZ d e j f d     YZ d e j e j e j f d     YZ d e j e j f d     YZ e j e e  e j e e  e j j e  d e j f d     YZ d e j f d     YZ d e j f d     YZ d e f d     YZ d e f d     YZ  d e! f d     YZ" d e j f d     YZ# d  S(!   s!   
Tests for error handling in PB.
i(   t   unittest(   t   pbt   flavorst   jelly(   t   reactort   defer(   t   logt   AsynchronousExceptionc           B   s   e  Z d  Z RS(   s   
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are not L{pb.Error} subclasses.
    (   t   __name__t
   __module__t   __doc__(    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR      s   t   SynchronousExceptionc           B   s   e  Z d  Z RS(   sm   
    Helper used to test remote methods which raise exceptions which are not
    L{pb.Error} subclasses.
    (   R   R	   R
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR      s   t   AsynchronousErrorc           B   s   e  Z d  Z RS(   s   
    Helper used to test remote methods which return Deferreds which fail with
    exceptions which are L{pb.Error} subclasses.
    (   R   R	   R
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR      s   t   SynchronousErrorc           B   s   e  Z d  Z RS(   si   
    Helper used to test remote methods which raise exceptions which are
    L{pb.Error} subclasses.
    (   R   R	   R
   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   &   s   t
   JellyErrorc           B   s   e  Z RS(    (   R   R	   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   .   s   t   SecurityErrorc           B   s   e  Z RS(    (   R   R	   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   2   s   t
   SimpleRootc           B   sq   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d d	  Z d d
  Z RS(   c         C   s   t  j t d   S(   sD   
        Fail asynchronously with a non-pb.Error exception.
        s   remote asynchronous exception(   R   t   failR   (   t   self(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_asynchronousException>   s    c         C   s   t  d   d S(   sC   
        Fail synchronously with a non-pb.Error exception.
        s   remote synchronous exceptionN(   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_synchronousExceptionD   s    c         C   s   t  j t d   S(   s@   
        Fail asynchronously with a pb.Error exception.
        s   remote asynchronous error(   R   R   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_asynchronousErrorJ   s    c         C   s   t  d   d S(   s?   
        Fail synchronously with a pb.Error exception.
        s   remote synchronous errorN(   R   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_synchronousErrorP   s    c         C   s)   d t  j f d     Y} | d   d S(   s>   
        Fail with error that is not known to client.
        t   UnknownErrorc           B   s   e  Z RS(    (   R   R	   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   Z   s   s   I'm not known to client!N(   R   t   Error(   R   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_unknownErrorV   s    c         C   s   |  j    d  S(   N(   t
   raiseJelly(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_jelly^   s    c         C   s   |  j    d  S(   N(   t   raiseSecurity(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_securitya   s    c         C   s-   t  j   } | j |  j  | j d   | S(   N(   R   t   Deferredt   addCallbackR   t   callbackt   None(   R   t   d(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_deferredJellyd   s    c         C   s-   t  j   } | j |  j  | j d   | S(   N(   R   R   R   R   R    R!   (   R   R"   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   remote_deferredSecurityj   s    c         C   s   t  d   d  S(   Ns   I'm jellyable!(   R   (   R   t   results(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   p   s    c         C   s   t  d   d  S(   Ns   I'm secure!(   R   (   R   R%   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   s   s    N(   R   R	   R   R   R   R   R   R   R   R#   R$   R!   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   =   s   									t   SaveProtocolServerFactoryc           B   s   e  Z d  Z d Z d   Z RS(   sd   
    A L{pb.PBServerFactory} that saves the latest connected client in
    C{protocolInstance}.
    c         C   s   | |  _  d S(   s3   
        Keep track of the given protocol.
        N(   t   protocolInstance(   R   t   protocol(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   clientConnectionMade   s    N(   R   R	   R
   R!   R'   R)   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR&   x   s   t   PBConnTestCasec           B   sD   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   i    c         C   s   |  j    |  j   d  S(   N(   t   _setUpServert   _setUpClient(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   setUp   s    
c         C   sC   t  t    |  _ |  j |  j _ t j d |  j d d |  _ d  S(   Ni    t	   interfaces	   127.0.0.1(   R&   R   t   serverFactoryt   unsafeTracebacksR   t	   listenTCPt
   serverPort(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR+      s    c         C   s@   |  j  j   j } t j   |  _ t j d | |  j  |  _ d  S(   Ns	   127.0.0.1(	   R2   t   getHostt   portR   t   PBClientFactoryt   clientFactoryR   t
   connectTCPt   clientConnector(   R   t   portNo(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR,      s    c         C   sG   |  j  j d  k	 r( |  j  j j j   n  t j |  j   |  j   g  S(   N(	   R/   R'   R!   t	   transportt   loseConnectionR   t   gatherResultst   _tearDownServert   _tearDownClient(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   tearDown   s
    	c         C   s   t  j |  j j  S(   N(   R   t   maybeDeferredR2   t   stopListening(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR=      s    c         C   s   |  j  j   t j d   S(   N(   R8   t
   disconnectR   t   succeedR!   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR>      s    (	   R   R	   R0   R-   R+   R,   R?   R=   R>   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR*      s   					t   PBFailureTestc           B   s   e  Z e j j Z d    Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d d  Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z RS(   c            sG       f d      j  j   }   f d   } | j |  | S(   Nc            sd   |  j      j |  j d   rN   j   }   j t |  d  n  |  j |  j |  j f S(   Ns   Traceback unavailable
i   (   t   trapt   comparet	   tracebackt   flushLoggedErrorst   assertEqualt   lent   typet   value(   t   errt   errs(   R   t   exceptionTypet   flush(    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   eb   s    c            s    |  j    } | j    | S(   N(   t
   callRemotet
   addErrback(   t   rootR"   (   RQ   t   method(    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   gotRootObject   s    (   R6   t   getRootObjectR   (   R   RU   RO   RP   R"   RV   (    (   R   RO   RQ   RP   RU   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   _exceptionTest   s
    c         C   s   |  j  d t t  S(   s   
        Test that a Deferred returned by a remote method which already has a
        Failure correctly has that error passed back to the calling side.
        t   asynchronousException(   RX   R   t   True(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_asynchronousException   s    c         C   s   |  j  d t t  S(   sw   
        Like L{test_asynchronousException}, but for a method which raises an
        exception synchronously.
        t   synchronousException(   RX   R   RZ   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_synchronousException   s    c         C   s   |  j  d t t  S(   s   
        Like L{test_asynchronousException}, but for a method which returns a
        Deferred failing with an L{pb.Error} subclass.
        t   asynchronousError(   RX   R   t   False(   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_asynchronousError   s    c         C   s   |  j  d t t  S(   s}   
        Like L{test_asynchronousError}, but for a method which synchronously
        raises a L{pb.Error} subclass.
        t   synchronousError(   RX   R   R_   (   R   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_synchronousError   s    c         C   s   |  j  | |  | S(   N(   t   assertEquals(   R   t   resultt   expectedResult(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   _success   s    c         C   s    | j  |  j | d | f | S(   Nt   callbackArgs(   t   addCallbacksRf   (   R   t
   remoteCallRe   RQ   (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   _addFailingCallbacks   s    
c            s;    j  j   }       f d   } | j |  | S(   s   
        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            sP    j  |  j      }   d  k	 rL    f d   } | j |  n  | S(   Nc            s#     j  t   j    d  |  S(   Ni   (   Rc   RJ   RH   (   RM   (   R   t   exc(    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt
   gotFailure   s    (   Rj   RR   R!   t   addBoth(   t   objt   failureDeferredRl   (   Rk   R   RQ   t   expectedRU   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt
   gotRootObj   s
    (   R6   RW   R   (   R   RU   Rp   RQ   Rk   t   rootDeferredRq   (    (   Rk   R   RU   Rp   RQ   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt	   _testImpl   s    c            s"     f d   }   j  d d |  S(   s   
        Test that an exception which is a subclass of L{pb.Error} has more
        information passed across the network to the calling side.
        c            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni+   (   RE   R   t   failIft
   isinstanceRK   t   strt
   failUnlessRL   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureJelly  s    R   i+   (   Rs   (   R   Rx   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_jellyFailure   s    c            s"     f d   }   j  d d |  S(   s   
        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            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni  (   RE   R   Rt   Ru   RK   Rv   Rw   RL   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureDeferredJelly  s    t   deferredJellyi  (   Rs   (   R   Rz   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_deferredJellyFailure  s    c            s"     f d   }   j  d d |  S(   s   
        An 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            s     j  |  j d  d S(   Ns,   twisted.test.test_pbfailure.SynchronousErrori  (   RI   RK   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureUnjellyable  s    Ra   i  (   Rs   (   R   R}   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_unjellyableFailure  s    c            s"     f d   }   j  d d |  S(   s   
        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            s     j  |  j d  d S(   Ns(   twisted.test.test_pbfailure.UnknownErrori  (   RI   RK   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureUnknown+  s    t   unknownErrori  (   Rs   (   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_unknownFailure&  s    c            s"     f d   }   j  d d |  S(   s   
        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            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni  (   RE   R   Rt   Ru   RK   Rv   Rw   RL   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureSecurity8  s    t   securityi  (   Rs   (   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_securityFailure2  s    c            s"     f d   }   j  d d |  S(   s   
        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            sF   |  j  t    j t |  j t     j t |  j |  j   d S(   Ni  (   RE   R   Rt   Ru   RK   Rv   Rw   RL   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureDeferredSecurityF  s    t   deferredSecurityi  (   Rs   (   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_deferredSecurity@  s    c            s%     f d   }   j  d d | t  S(   s   
        Test that attempting to call a method which is not defined correctly
        results in an AttributeError on the calling side.
        c            s'   |  j  t j    j |  j d  d S(   Ns   Traceback unavailable
i  (   RE   R   t   NoSuchMethodRF   RG   (   R   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   failureNoSuchS  s    t   nosuchi  (   Rs   t   AttributeError(   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_noSuchMethodFailureN  s    c            sE     j  j   } d   } | j |    f d   } | j |  | S(   s   
        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   |  j  d  S(   NR\   (   RR   (   t   rootObj(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt	   connectedd  s    c            s6   t  j |     j t  }   j t |  d  d  S(   Ni   (   R   RM   RH   R   Rc   RJ   (   t   failureRN   (   R   (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt	   exceptionh  s    (   R6   RW   R   RS   (   R   R"   R   R   (    (   R   s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_copiedFailureLoggingZ  s    	N(   R   R	   R    t   TestCaseRc   RF   RX   R[   R]   R`   Rb   Rf   Rj   R!   Rs   Ry   R|   R~   R   R   R   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyRD      s"   																		t   PBFailureTestUnsafec           B   s   e  Z e j j Z d  Z RS(   i   (   R   R	   R    R   t   failIfEqualsRF   R0   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   r  s   t   DummyInvokerc           B   s   e  Z d  Z d Z RS(   sZ   
    A behaviorless object to be used as the invoker parameter to
    L{jelly.jelly}.
    N(   R   R	   R
   R!   t   serializingPerspective(    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR   x  s   t   FailureJellyingTestsc           B   s    e  Z d  Z d   Z d   Z RS(   s:   
    Tests for the interaction of jelly and failures.
    c         C   s   t  j t    } |  j | j t  t  |  j | j t  t  t j t j | d t    } |  j | j t  t  |  j | j t  t  d S(   s   
        An unjellied L{CopyableFailure} has a check method which behaves the
        same way as the original L{CopyableFailure}'s check method.
        t   invokerN(	   R   t   CopyableFailuret   ZeroDivisionErrort   assertIdenticalt   checkt   ArithmeticErrorR   t   unjellyR   (   R   t   originalt   copied(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_unjelliedFailureCheck  s    !c         C   s   t  j t    } |  j | j t  t  |  j | j t  t  t j t j | d t    } t  j |  } t j t j | d t    } |  j | j t  t  |  j | j t  t  d S(   sX  
        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   N(	   R   R   R   R   R   R   R   R   R   (   R   R   t
   copiedOncet
   derivativet   copiedTwice(    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   test_twiceUnjelliedFailureCheck  s    (   R   R	   R
   R   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyR     s   	N($   R
   t   twisted.trialR    t   twisted.spreadR   R   R   t   twisted.internetR   R   t   twisted.pythonR   t	   ExceptionR   R   R   R   R   t	   Jellyablet
   RemoteCopyR   R   t   setUnjellyableForClasst   globalSecurityt   allowInstancesOft   RootR   t   PBServerFactoryR&   R   R*   RD   R   t   objectR   R   (    (    (    s?   /usr/lib/python2.7/dist-packages/twisted/test/test_pbfailure.pyt   <module>   s(   %;!	