관리-도구
편집 파일: twistedtools.cpython-37.pyc
B .�Se� � @ sf d Z ddlZddlmZmZ ddlmZmZ ddddd gZda d d� Z e � \ZZdd � Z d dd�ZdS )av 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: http://twistedmatrix.com/trac/ � N)�Queue�Empty)�make_decorator�TimeExpired�threaded_reactor�reactor�deferredr �stop_reactorc sn yddl m� W n tk r$ dS X tsfddlm} ddlm} |� fdd�d�at�d � t� � � tfS ) 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)� threadable)�Threadc s � j dd�S )NF)ZinstallSignalHandlers)�run� )r r �B/opt/alt/python37/lib/python3.7/site-packages/nose/twistedtools.py�<lambda>8 s z"threaded_reactor.<locals>.<lambda>)�targetT) Ztwisted.internetr �ImportError�_twisted_threadZtwisted.pythonr � threadingr � setDaemon�start)r r r )r r r * s c C sD dd� } t �| � t�� x t �� D ]}|�� r$|�� q$W dadS )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 �� dS )z0Helper for calling stop from withing the thread.N)r �stopr r r r r J s z"stop_reactor.<locals>.stop_reactorN)r �callFromThread�reactor_thread�joinZgetDelayedCallsZactive�cancelr )r �pr r r r B s c s^ t � \� }� dkrtd��y�dkp*�d W n tk rJ td��Y nX � �fdd�}|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 desireable 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 � ��fdd�}t � �|�}|S )Nc s� t � ��fdd���fdd��� �����fdd�}��|� y�j�d�}W n tk rn td� ��Y nX |d k r�|\}}}||��|��d S ) Nc s � � d � d S )N)�put)�value)�qr r �callback� s z=deferred.<locals>.decorate.<locals>.wrapper.<locals>.callbackc s, y| � � W n � �t�� � Y nX d S )N)ZraiseExceptionr �sys�exc_info)Zfailure)r r r �errback� s z<deferred.<locals>.decorate.<locals>.wrapper.<locals>.errbackc s\ y<�� ��} y| � ��� W n tk r8 td��Y nX W n ��t�� � Y nX d S )Nz7you must return a twisted Deferred from your test case!)ZaddCallbacks�AttributeError� TypeErrorr r r! )�d)�argsr r"