a
    ñ»jt  ã                   @   s`   d Z ddlZddlmZmZ ddlmZmZ g d¢Zda	dd„ Z
e
ƒ \ZZdd	„ Zdd
d„ZdS )af  
Twisted integration
-------------------
This module provides a very simple way to integrate your tests with the
Twisted_ event loop.
You must import this module *before* importing anything from Twisted itself!

Example:

  from nose.twistedtools import reactor, deferred

  @deferred()
  def test_resolve():
      return reactor.resolve("www.python.org")

Or, more realistically:

  @deferred(timeout=5.0)
  def test_resolve():
      d = reactor.resolve("www.python.org")
      def check_ip(ip):
          assert ip == "67.15.36.43"
      d.addCallback(check_ip)
      return d

.. _Twisted: https://twisted.org/ é    N)ÚQueueÚEmpty)Úmake_decoratorÚTimeExpired)Úthreaded_reactorÚreactorÚdeferredr   Ústop_reactorc                     sb   zddl m‰  W n ty$   Y dS 0 tsZddlm}  | ‡ fdd„d�at d¡ t ¡  ˆ tfS )	z©Start the Twisted reactor in a separate thread, if not already done.
    Returns the reactor.
    The thread will automatically be destroyed when all the tests are done.r   ©r   )NN©ÚThreadc                      s   ˆ j dd�S )NF)ZinstallSignalHandlers)Úrun© r
   r   ú7E:\soccer\ml_env\Lib\site-packages\nose/twistedtools.pyÚ<lambda>3   ó    z"threaded_reactor.<locals>.<lambda>)ÚtargetT)Ztwisted.internetr   ÚImportErrorÚ_twisted_threadÚ	threadingr   Ú	setDaemonÚstartr   r   r
   r   r   '   s    
ÿ
r   c                  C   s@   dd„ } t  | ¡ t ¡  t  ¡ D ]}| ¡ r"| ¡  q"dadS )a	  Stop the reactor and join the reactor thread until it stops.
    Call this function in teardown at the module or package level to
    reset the twisted system after your tests. You *must* do this if
    you mix tests using these tools and tests using twisted.trial.c                   S   s   t  ¡  d S ©N)r   Ústopr   r   r   r   r	   D   s    z"stop_reactor.<locals>.stop_reactorN)r   ÚcallFromThreadÚreactor_threadÚjoinZgetDelayedCallsZactiveÚcancelr   )r	   Úpr   r   r   r	   =   s    

r	   c                    s\   t ƒ \‰ }ˆ du rtdƒ‚zˆdu p*ˆd  W n tyH   tdƒ‚Y n0 ‡ ‡fdd„}|S )að  By wrapping a test function with this decorator, you can return a
    twisted Deferred and the test will wait for the deferred to be triggered.
    The whole test function will run inside the Twisted event loop.

    The optional timeout parameter specifies the maximum duration of the test.
    The difference with timed() is that timed() will still wait for the test
    to end, while deferred() will stop the test when its timeout has expired.
    The latter is more desirable when dealing with network tests, because
    the result may actually never arrive.

    If the callback is triggered, the test has passed.
    If the errback is triggered or the timeout expires, the test has failed.

    Example:

        @deferred(timeout=5.0)
        def test_resolve():
            return reactor.resolve("www.python.org")

    Attention! If you combine this decorator with other decorators (like
    "raises"), deferred() must be called *first*!

    In other words, this is good:

        @raises(DNSLookupError)
        @deferred()
        def test_error():
            return reactor.resolve("xxxjhjhj.biz")

    and this is bad:

        @deferred()
        @raises(DNSLookupError)
        def test_error():
            return reactor.resolve("xxxjhjhj.biz") Nz1twisted is not available or could not be importedr   z+'timeout' argument must be a number or Nonec                    s    ‡ ‡‡fdd„}t ˆ ƒ|ƒ}|S )Nc                     s’   t ƒ ‰‡fdd„‰‡fdd„‰‡ ‡‡‡‡‡fdd„}ˆ |¡ zˆjˆd�}W n tyl   tdˆ ƒ‚Y n0 |d urŽ|\}}}||ƒ |¡‚d S )	Nc                    s   ˆ   d ¡ d S r   )Úput)Úvalue©Úqr   r   Úcallback€   s    z=deferred.<locals>.decorate.<locals>.wrapper.<locals>.callbackc                    s2   z|   ¡  W n  ty,   ˆ  t ¡ ¡ Y n0 d S r   )ZraiseExceptionÚ	Exceptionr   ÚsysÚexc_info)Zfailurer!   r   r   Úerrbackƒ   s    z<deferred.<locals>.decorate.<locals>.wrapper.<locals>.errbackc                     sd   z>ˆˆ i ˆ¤Ž} z|   ˆˆ¡ W n ty:   tdƒ‚Y n0 W n  ty^   ˆ t ¡ ¡ Y n0 d S )Nz7you must return a twisted Deferred from your test case!)ZaddCallbacksÚAttributeErrorÚ	TypeErrorr$   r   r%   r&   )Úd)Úargsr#   r'   ÚfuncÚkargsr"   r   r   Úg‰   s    z6deferred.<locals>.decorate.<locals>.wrapper.<locals>.g)Útimeoutz*timeout expired before end of test (%f s.))r   r   Úgetr   r   Úwith_traceback)r+   r-   r.   ÚerrorÚexc_typeÚ	exc_valueÚtb)r,   r   r/   )r+   r#   r'   r-   r"   r   Úwrapper}   s    
ÿ

z+deferred.<locals>.decorate.<locals>.wrapper)r   )r,   r6   ©r   r/   )r,   r   Údecorate|   s    "zdeferred.<locals>.decorate)r   r   r)   )r/   r   r8   r   r7   r   r   O   s    $
%r   )N)Ú__doc__r%   Úqueuer   r   Z
nose.toolsr   r   Ú__all__r   r   r   r   r	   r   r   r   r   r   Ú<module>   s   
