ó
[³XMc           @   sô   d  Z  d d l 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 d d l m Z d d	 l m Z m Z d d
 l m Z d d l m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s%   
Tests for L{twisted.internet.base}.
iÿÿÿÿN(   t   Queue(   t
   implements(   t
   ThreadPool(   t   setIDFunction(   t   IReactorTimet   IReactorThreads(   t   DNSLookupError(   t   ThreadedResolvert   DelayedCall(   t   Clock(   t   TestCaset   FakeReactorc           B   s?   e  Z d  Z e e e ƒ d „  Z d „  Z d „  Z d „  Z	 RS(   sl   
    A fake reactor implementation which just supports enough reactor APIs for
    L{ThreadedResolver}.
    c            sV   t  ƒ  ˆ  _ ˆ  j j ˆ  _ t ƒ  ˆ  _ ˆ  j j ƒ  ‡  f d †  ˆ  _ t ƒ  ˆ  _ d  S(   Nc              s   ˆ  j  S(   N(   t   _threadpool(    (   t   self(    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   <lambda>#   s    (	   R	   t   _clockt	   callLaterR   R   t   startt   getThreadPoolR    t   _threadCalls(   R   (    (   R   sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   __init__   s    c         O   s   |  j  j | | | f ƒ d  S(   N(   R   t   put(   R   t   ft   argst   kwargs(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   callFromThread(   s    c         C   s)   |  j  j ƒ  \ } } } | | | Ž  d  S(   N(   R   t   get(   R   R   R   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   _runThreadCalls,   s    c         C   s   |  j  j ƒ  d  S(   N(   R   t   stop(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   _stop1   s    (
   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyR      s   			t   ThreadedResolverTestsc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s(   
    Tests for L{ThreadedResolver}.
    c            së   d ‰  d } d } t  ƒ  } |  j | j ƒ g  ‰ g  } ‡  ‡ f d †  } |  j t d | ƒ t | ƒ } | j | | f ƒ } | j | j ƒ | j	 ƒ  |  j
 ˆ | g ƒ |  j
 | ˆ  g ƒ | j j | d ƒ |  j
 | j j g  ƒ d S(   sí   
        L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires
        with the value returned by the call to L{socket.gethostbyname} in the
        threadpool of the reactor passed to L{ThreadedResolver.__init__}.
        s	   10.0.0.17s   foo.bar.example.comi   c            s   ˆ j  |  ƒ ˆ  S(   N(   t   append(   t   name(   t   ipt   lookedUp(    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   fakeGetHostByNameI   s    t   gethostbynamei   N(   R   t
   addCleanupR   t   patcht   socketR   t   getHostByNamet   addCallbackR"   R   t   assertEqualR   t   advancet   calls(   R   R#   t   timeoutt   reactort
   resolvedToR&   t   resolvert   d(    (   R$   R%   sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_success:   s"    	
c         C   sÐ   d } t  ƒ  } |  j | j ƒ d „  } |  j t d | ƒ g  } t | ƒ } | j d | f ƒ } |  j | t ƒ | j	 | j
 ƒ | j ƒ  |  j t | ƒ d ƒ | j j | d ƒ |  j | j j g  ƒ d S(   s¦   
        L{ThreadedResolver.getHostByName} returns a L{Deferred} which fires a
        L{Failure} if the call to L{socket.gethostbyname} raises an exception.
        i   c         S   s   t  d ƒ ‚ d  S(   Ns   ENOBUFS (this is a funny joke)(   t   IOError(   R#   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyR&   g   s    R'   s	   some.namei   N(   R   R(   R   R)   R*   R   R+   t   assertFailureR   R,   R"   R   R-   t   lenR   R.   R/   (   R   R0   R1   R&   t
   failedWithR3   R4   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_failure]   s    		
c            sò   d } t  ƒ  } |  j | j ƒ t ƒ  ‰  ‡  f d †  } |  j t d | ƒ g  } t | ƒ } | j d | f ƒ } |  j | t	 ƒ | j
 | j ƒ | j j | d ƒ |  j | g  ƒ | j j d ƒ |  j t | ƒ d ƒ ˆ  j t d ƒ ƒ d S(   sÐ   
        If L{socket.gethostbyname} does not complete before the specified
        timeout elapsed, the L{Deferred} returned by
        L{ThreadedResolver.getHostByBame} fails with L{DNSLookupError}.
        i
   c            s   ˆ  j  ƒ  ‚ d  S(   N(   R   (   R#   (   t   result(    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyR&   ‡   s    R'   s	   some.namei   s   The I/O was errorfulN(   R   R(   R   R    R)   R*   R   R+   R7   R   R,   R"   R   R.   R-   R8   R   R6   (   R   R0   R1   R&   R9   R3   R4   (    (   R;   sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_timeout{   s     		(   R   R   R    R5   R:   R<   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyR!   6   s   	#	t   DelayedCallTestsc           B   s_   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   s#   
    Tests for L{DelayedCall}.
    c         C   s(   d „  } t  | d „  d i  | | d ƒ S(   s·   
        Get a L{DelayedCall} instance at a given C{time}.
        
        @param time: The absolute time at which the returned L{DelayedCall}
            will be scheduled.
        c         S   s   d  S(   N(    (   t   call(    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   noop¨   s    c           S   s   d  S(   N(   t   None(    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyR   ª   s    (    N(   R   R@   (   R   t   timeR?   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   _getDelayedCallAt¡   s    	c         C   s(   |  j  d ƒ |  _ |  j  d ƒ |  _ d S(   sb   
        Create two L{DelayedCall} instanced scheduled to run at different
        times.
        i    i   N(   RB   t   zerot   one(   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   setUp­   s    c            s}   d „  } t  d | d i d d 6d
 d
 d „  ƒ } i d | 6‰  ‡  f d †  } |  j t t | ƒ ƒ |  j t | ƒ d	 ƒ d
 S(   sç   
        The string representation of a L{DelayedCall} instance, as returned by
        C{str}, includes the unsigned id of the instance, as well as its state,
        the function to be called, and the function arguments.
        c           S   s   d  S(   N(    (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   nothing¼   s    i   i   i   t   Ac           S   s   d S(   Ng      ø?(    (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyR   ¾   s    iÈ   c            s1   y ˆ  |  SWn t  t f k
 r, t |  ƒ SXd  S(   N(   t	   TypeErrort   KeyErrort   id(   t   obj(   t   ids(    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   fakeIDÀ   s    s?   <DelayedCall 0xc8 [10.5s] called=0 cancelled=0 nothing(3, A=5)>N(   i   (   R   R@   R(   R   t   assertEqualst   str(   R   RF   t   dcRM   (    (   RL   sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_str¶   s    	(	c         C   sc   |  j  |  j } } |  j | | k  ƒ |  j | | k  ƒ |  j | | k  ƒ |  j | | k  ƒ d S(   s’   
        For two instances of L{DelayedCall} C{a} and C{b}, C{a < b} is true
        if and only if C{a} is scheduled to run before C{b}.
        N(   RC   RD   t
   assertTruet   assertFalse(   R   RC   RD   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_ltË   s
    c         C   sc   |  j  |  j } } |  j | | k ƒ |  j | | k ƒ |  j | | k ƒ |  j | | k ƒ d S(   s·   
        For two instances of L{DelayedCall} C{a} and C{b}, C{a <= b} is true
        if and only if C{a} is scheduled to run before C{b} or at the same
        time as C{b}.
        N(   RC   RD   RR   RS   (   R   RC   RD   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_le×   s
    c         C   sc   |  j  |  j } } |  j | | k ƒ |  j | | k ƒ |  j | | k ƒ |  j | | k ƒ d S(   s‘   
        For two instances of L{DelayedCall} C{a} and C{b}, C{a > b} is true
        if and only if C{a} is scheduled to run after C{b}.
        N(   RC   RD   RR   RS   (   R   RC   RD   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_gtä   s
    c         C   sc   |  j  |  j } } |  j | | k ƒ |  j | | k ƒ |  j | | k ƒ |  j | | k ƒ d S(   sµ   
        For two instances of L{DelayedCall} C{a} and C{b}, C{a > b} is true
        if and only if C{a} is scheduled to run after C{b} or at the same
        time as C{b}.
        N(   RC   RD   RR   RS   (   R   RC   RD   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_geð   s
    c         C   sO   |  j  |  j |  j k ƒ |  j |  j |  j k ƒ |  j |  j |  j k ƒ d S(   sD   
        A L{DelayedCall} instance is only equal to itself.
        N(   RS   RC   RD   RR   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_eqý   s    c         C   sO   |  j  |  j |  j k ƒ |  j |  j |  j k ƒ |  j |  j |  j k ƒ d S(   sM   
        A L{DelayedCall} instance is not equal to any other object.
        N(   RR   RC   RD   RS   (   R   (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   test_ne  s    (   R   R   R    RB   RE   RQ   RT   RU   RV   RW   RX   RY   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyR=      s   									(   R    R*   R    t   zope.interfaceR   t   twisted.python.threadpoolR   t   twisted.python.utilR   t   twisted.internet.interfacesR   R   t   twisted.internet.errorR   t   twisted.internet.baseR   R   t   twisted.internet.taskR	   t   twisted.trial.unittestR
   t   objectR   R!   R=   (    (    (    sC   /usr/lib/python2.7/dist-packages/twisted/internet/test/test_base.pyt   <module>   s    g