Your IP : 18.116.24.148


Current Path : /opt/alt/python35/lib64/python3.5/asyncio/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/asyncio/__pycache__/test_utils.cpython-35.opt-1.pyc



��Yf�8�@sdZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddl
mZddlmZddlmZmZyddlZWnek
rdZYnXddlmZddlmZdd	lmZdd
lmZddlmZddlmZdd
lmZddl m!Z!ddl"m#Z#e	j$dkr�ddl%m&Z&nddlm&Z&dd�Z'dd�Z(ddd�Z)dd�Z*Gdd�de�Z+Gdd�de�Z,Gdd �d �Z-Gd!d"�d"e-e,�Z.d#d$d%d&�Z/e0ed'�rGd(d)�d)ej1e�Z2Gd*d+�d+e2e�Z3Gd,d-�d-e3�Z4Gd.d/�d/e-e4�Z5d0d1�Z6ej7d2d3��Z8ej7d#d$d4d5��Z9ej7d6d7d8dd#d$d9d:��Z:d;d<�Z;Gd=d>�d>ej<�Z=Gd?d@�d@ej>�Z?dAdB�Z@GdCdD�dDeA�ZBdEdF�ZCGdGdH�dHe
jD�ZDej7dIdJ��ZEejFejGejHdKdL�ZIdMdN�ZJdS)OzUtilities shared by tests.�N)�mock)�
HTTPServer)�WSGIRequestHandler�
WSGIServer�)�base_events)�compat)�events)�futures)�	selectors)�tasks)�	coroutine)�logger)�support�win32)�
socketpaircCs$tdkrdStjtj�SdS)N)�sslZ
SSLContextZPROTOCOL_SSLv23�rr�7/opt/alt/python35/lib64/python3.5/asyncio/test_utils.py�dummy_ssl_context-src
CsVtdd��}|�}|j|�}d|_z|j|�Wd|j�XdS)NcSsdS)Nrrrrr�once5szrun_briefly.<locals>.onceF)r
Zcreate_taskZ_log_destroy_pending�run_until_complete�close)�loopr�gen�trrr�run_briefly4s		r�cCsttj�|}x]|�so|dk	rP|tj�}|dkrPtj��|jtjdd|��qWdS)Nrg����MbP?r)�timer
�TimeoutErrorrrZsleep)rZpred�timeoutZdeadlinerrr�	run_untilCsr!cCs|j|j�|j�dS)z�Legacy API to run once through the event loop.

    This is the recommended pattern for test code.  It will poll the
    selector once and run all callbacks scheduled in response to I/O
    events.
    N)Z	call_soon�stopZrun_forever)rrrr�run_onceMsr#c@s(eZdZdd�Zdd�ZdS)�SilentWSGIRequestHandlercCs
tj�S)N)�io�StringIO)�selfrrr�
get_stderrZsz#SilentWSGIRequestHandler.get_stderrcGsdS)Nr)r'�format�argsrrr�log_message]sz$SilentWSGIRequestHandler.log_messageN)�__name__�
__module__�__qualname__r(r+rrrrr$Xsr$cs4eZdZdZ�fdd�Zdd�Z�S)�SilentWSGIServer�cs/t�j�\}}|j|j�||fS)N)�super�get_request�
settimeout�request_timeout)r'�request�client_addr)�	__class__rrr2eszSilentWSGIServer.get_requestcCsdS)Nr)r'r5�client_addressrrr�handle_errorjszSilentWSGIServer.handle_error)r,r-r.r4r2r9rr)r7rr/asr/c@seZdZdd�ZdS)�SSLWSGIServerMixincCs�tjjtjjt�dd�}tjj|�s]tjjtjjtj�dd�}tjj|d�}tjj|d�}tj|d|d|d	d
�}y!|j|||�|j	�Wnt
k
r�YnXdS)Nz..Ztests�testZtest_asynciozssl_key.pemzssl_cert.pem�keyfile�certfileZserver_sideT)�os�path�join�dirname�__file__�isdirrZwrap_socketZRequestHandlerClassr�OSError)r'r5r8�herer<r=Zssockrrr�finish_requestps$	
z!SSLWSGIServerMixin.finish_requestN)r,r-r.rFrrrrr:nsr:c@seZdZdS)�
SSLWSGIServerN)r,r-r.rrrrrG�srG�use_sslFc
#s�dd�}|r|n|}||t���j|��j�_tjd�fdd��}|j�z	�VWd�j��j�|j	�XdS)NcSs#d}dg}|||�dgS)Nz200 OK�Content-type�
text/plainsTest message)rIrJr)�environZstart_responseZstatusZheadersrrr�app�s	
z_run_test_server.<locals>.app�targetcs�jdd�S)NZ
poll_intervalg�������?)Z
serve_foreverr)�httpdrr�<lambda>�sz"_run_test_server.<locals>.<lambda>)
r$Zset_appZserver_address�address�	threadingZThread�startZshutdownZserver_closer@)rPrH�
server_cls�server_ssl_clsrLZserver_classZ
server_threadr)rNr�_run_test_server�s
	
	

rUZAF_UNIXc@seZdZdd�ZdS)�UnixHTTPServercCs&tjj|�d|_d|_dS)Nz	127.0.0.1�P)�socketserver�UnixStreamServer�server_bindZserver_nameZserver_port)r'rrrrZ�s	zUnixHTTPServer.server_bindN)r,r-r.rZrrrrrV�srVcs4eZdZdZdd�Z�fdd�Z�S)�UnixWSGIServerr0cCstj|�|j�dS)N)rVrZZ
setup_environ)r'rrrrZ�s
zUnixWSGIServer.server_bindcs/t�j�\}}|j|j�|dfS)N�	127.0.0.1�)r\r])r1r2r3r4)r'r5r6)r7rrr2�szUnixWSGIServer.get_request)r,r-r.r4rZr2rr)r7rr[�sr[c@seZdZdd�ZdS)�SilentUnixWSGIServercCsdS)Nr)r'r5r8rrrr9�sz!SilentUnixWSGIServer.handle_errorN)r,r-r.r9rrrrr^�sr^c@seZdZdS)�UnixSSLWSGIServerN)r,r-r.rrrrr_�sr_c	Cs!tj��}|jSWdQRXdS)N)�tempfileZNamedTemporaryFile�name)�filerrr�gen_unix_socket_path�srcccs@t�}z	|VWdytj|�Wntk
r:YnXXdS)N)rcr>�unlinkrD)r?rrr�unix_socket_path�s		
reccs;t��+}td|d|dtdt�EdHWdQRXdS)NrPrHrSrT)rerUr^r_)rHr?rrr�run_test_unix_server�srf�hostz	127.0.0.1�portc	cs.td||fd|dtdt�EdHdS)NrPrHrSrT)rUr/rG)rgrhrHrrr�run_test_server�sricCsni}xHt|�D]:}|jd�r:|jd�r:qtdd�||<qWtd|f|j|��S)N�__�return_valueZTestProtocol)�dir�
startswith�endswith�MockCallback�type�	__bases__)�baseZdctrarrr�make_test_protocol�srsc@sOeZdZdd�Zddd�Zdd�Zdd	�Zd
d�ZdS)�TestSelectorcCs
i|_dS)N)�keys)r'rrr�__init__�szTestSelector.__init__NcCs)tj|d||�}||j|<|S)Nr)rZSelectorKeyru)r'�fileobjr	�data�keyrrr�register�s
zTestSelector.registercCs|jj|�S)N)ru�pop)r'rwrrr�
unregisterszTestSelector.unregistercCsgS)Nr)r'r rrr�selectszTestSelector.selectcCs|jS)N)ru)r'rrr�get_mapszTestSelector.get_map)r,r-r.rvrzr|r}r~rrrrrt�s
rtcs!eZdZdZd�fdd�Zdd�Zdd�Z�fd	d
�Zdd�Zd
d�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Z�fd#d$�Z�fd%d&�Zd'd(�Zd)d*�Z�S)+�TestLoopa�Loop for unittests.

    It manages self time directly.
    If something scheduled to be executed later then
    on next loop iteration after all ready handlers done
    generator passed to __init__ is calling.

    Generator should be like this:

        def gen():
            ...
            when = yield ...
            ... = yield time_advance

    Value returned by yield is absolute time of next scheduled handler.
    Value passed to yield is time advance to move loop's time forward.
    Ncs�t�j�|dkr1dd�}d|_n	d|_|�|_t|j�d|_d|_g|_t�|_	i|_
i|_|j�t
j�|_dS)Ncss	dVdS)Nrrrrrr!szTestLoop.__init__.<locals>.genFTrg��&�.>)r1rv�_check_on_close�_gen�next�_timeZ_clock_resolution�_timersrtZ	_selector�readers�writers�reset_counters�weakref�WeakValueDictionary�_transports)r'r)r7rrrvs
	
					
zTestLoop.__init__cCs|jS)N)r�)r'rrrr4sz
TestLoop.timecCs|r|j|7_dS)zMove test time forward.N)r�)r'�advancerrr�advance_time7szTestLoop.advance_timecsOt�j�|jrKy|jjd�Wntk
r>Yn
Xtd��dS)NrzTime generator is not finished)r1rr�r��send�
StopIteration�AssertionError)r')r7rrr<s
	
zTestLoop.closecGs tj|||�|j|<dS)N)r	�Handler�)r'�fd�callbackr*rrr�_add_readerFszTestLoop._add_readercCs6|j|d7<||jkr.|j|=dSdSdS)NrTF)�remove_reader_countr�)r'r�rrr�_remove_readerIs

zTestLoop._remove_readercGs|j|}dS)N)r�)r'r�r�r*�handlerrr�
assert_readerQs
zTestLoop.assert_readercGs tj|||�|j|<dS)N)r	r�r�)r'r�r�r*rrr�_add_writerYszTestLoop._add_writercCs6|j|d7<||jkr.|j|=dSdSdS)NrTF)�remove_writer_countr�)r'r�rrr�_remove_writer\s

zTestLoop._remove_writercGs|j|}dS)N)r�)r'r�r�r*r�rrr�
assert_writerds
zTestLoop.assert_writercCsBy|j|}Wntk
r%YnXtdj||���dS)Nz.File descriptor {!r} is used by transport {!r})r��KeyError�RuntimeErrorr))r'r�Z	transportrrr�_ensure_fd_no_transportls
z TestLoop._ensure_fd_no_transportcGs |j|�|j|||�S)zAdd a reader callback.)r�r�)r'r�r�r*rrr�
add_readervs
zTestLoop.add_readercCs|j|�|j|�S)zRemove a reader callback.)r�r�)r'r�rrr�
remove_reader{s
zTestLoop.remove_readercGs |j|�|j|||�S)zAdd a writer callback..)r�r�)r'r�r�r*rrr�
add_writer�s
zTestLoop.add_writercCs|j|�|j|�S)zRemove a writer callback.)r�r�)r'r�rrr�
remove_writer�s
zTestLoop.remove_writercCs(tjt�|_tjt�|_dS)N)�collections�defaultdict�intr�r�)r'rrrr��szTestLoop.reset_counterscsMt�j�x0|jD]%}|jj|�}|j|�qWg|_dS)N)r1�	_run_oncer�r�r�r�)r'�whenr�)r7rrr��s

zTestLoop._run_oncecs&|jj|�t�j|||�S)N)r��appendr1�call_at)r'r�r�r*)r7rrr��szTestLoop.call_atcCsdS)Nr)r'Z
event_listrrr�_process_events�szTestLoop._process_eventscCsdS)Nr)r'rrr�_write_to_self�szTestLoop._write_to_self)r,r-r.�__doc__rvrr�rr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr)r7rr
s*

rcKstjddg|�S)N�spec�__call__)rZMock)�kwargsrrrro�sroc@s"eZdZdZdd�ZdS)�MockPatternz�A regex based str with a fuzzy __eq__.

    Use this helper with 'mock.assert_called_with', or anywhere
    where a regex comparison between strings is needed.

    For instance:
       mock_call.assert_called_with(MockPattern('spam.*ham'))
    cCs"ttjt|�|tj��S)N)�bool�re�search�str�S)r'�otherrrr�__eq__�szMockPattern.__eq__N)r,r-r.r�r�rrrrr��sr�cCs2tj|�}|dkr.td|f��|S)Nzunable to get the source of %r)r	Z_get_function_source�
ValueError)�func�sourcerrr�get_function_source�sr�c@sjeZdZdddd�Zddd�Zdd	�Zd
d�Zdd
�Zej	sfdd�Z
dS)�TestCase�cleanupTcCs'tjd�|r#|j|j�dS)N)r	�set_event_loopZ
addCleanupr)r'rr�rrrr��s
zTestCase.set_event_loopNcCst|�}|j|�|S)N)rr�)r'rrrrr�
new_test_loop�s
zTestCase.new_test_loopcCs|jt_dS)N)�_get_running_loopr	)r'rrr�unpatch_get_running_loop�sz!TestCase.unpatch_get_running_loopcCs.tj|_dd�t_tj�|_dS)NcSsdS)NrrrrrrO�sz TestCase.setUp.<locals>.<lambda>)r	r�rZthreading_setup�_thread_cleanup)r'rrr�setUp�szTestCase.setUpcCsU|j�tjd�|jtj�d�|j�tj|j	�tj
�dS)N)NNN)r�r	r�ZassertEqual�sys�exc_infoZ
doCleanupsrZthreading_cleanupr�Z
reap_children)r'rrr�tearDown�s


zTestCase.tearDowncOsGdd�d�}|�S)Nc@s(eZdZdd�Zdd�ZdS)z!TestCase.subTest.<locals>.EmptyCMcSsdS)Nr)r'rrr�	__enter__�sz+TestCase.subTest.<locals>.EmptyCM.__enter__cWsdS)Nr)r'�excrrr�__exit__�sz*TestCase.subTest.<locals>.EmptyCM.__exit__N)r,r-r.r�r�rrrr�EmptyCM�sr�r)r'r*r�r�rrr�subTest�szTestCase.subTest)r,r-r.r�r�r�r�r�rZPY34r�rrrrr��s
	r�ccs;tj}ztjtjd�dVWdtj|�XdS)zrContext manager to disable asyncio logger.

    For example, it can be used to ignore warnings in debug mode.
    rN)r�levelZsetLevel�loggingZCRITICAL)Z	old_levelrrr�disable_logger�s
		r�cCs=tjtj�}||_||_||_d|j_|S)z'Create a mock of a non-blocking socket.g)rZ	MagicMock�socket�protorp�familyZ
gettimeoutrk)r�rpr�Zsockrrr�mock_nonblocking_socket�s			r�cCstjddd�S)Nz'asyncio.sslproto._is_sslproto_availablerkF)rZpatchrrrr�force_legacy_ssl_support�sr�)Kr�r��
contextlibr%r�r>r�r�rXr�r`rQrZunittestr�rZhttp.serverrZwsgiref.simple_serverrrr�ImportErrorr]rrr	r
rrZ
coroutinesr
�logrr;r�platformZ
windows_utilsrrrr!r#r$r/r:rGrU�hasattrrYrVr[r^r_rc�contextmanagerrerfrirsZBaseSelectorrtZ
BaseEventLooprror�r�r�r�r�ZIPPROTO_TCPZSOCK_STREAMZAF_INETr�r�rrrr�<module>s�

	
		
�
-

?>