Edit File: test_utils.cpython-36.opt-1.pyc
3 � \�:��������������� ���@���s���d�Z�ddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddlZddlZddl Z ddlZddl mZ�ddlmZ�ddlmZmZ�yddlZW�n�ek r����dZY�nX�ddlmZ�ddlmZ�dd lmZ�dd lmZ�ddlmZ�ddlmZ�dd lmZ�ddl m!Z!�ddl"m#Z#�e j$dk�rHddl%m&Z&�nddlm&Z&�dd��Z'e'd�Z(e'd�Z)dd��Z*dd��Z+dRdd�Z,dd��Z-G�dd ��d e�Z.G�d!d"��d"e�Z/G�d#d$��d$�Z0G�d%d&��d&e0e/�Z1d'd(�d)d*�Z2e3ed+��rZG�d,d-��d-ej4e�Z5G�d.d/��d/e5e�Z6G�d0d1��d1e6�Z7G�d2d3��d3e0e7�Z8d4d5��Z9ej:d6d7���Z;ej:d'd(�d8d9��Z<ej:d:dd'd;�d<d=��Z=d>d?��Z>G�d@dA��dAej?�Z@G�dBdC��dCejA�ZBdDdE��ZCG�dFdG��dGeD�ZEdHdI��ZFG�dJdK��dKe jG�ZGej:dLdM���ZHejIejJejKfdNdO�ZLdPdQ��ZMdS�)SzUtilities shared by tests.�����N)�mock)� HTTPServer)�WSGIRequestHandler� WSGIServer����)�base_events)�compat)�events)�futures)� selectors)�tasks)� coroutine)�logger)�supportZwin32)� socketpairc�������������C���s`���t�td�r*tjjtj|��}tjj|�r*|S�tjjtjjtj�d|��}tjj|�rT|S�t |���d�S�)N� TEST_HOME_DIR�test) �hasattrr����os�path�joinr����isfile�dirname�__file__�FileNotFoundError)�filename�fullname��r����*/usr/lib64/python3.6/asyncio/test_utils.py� data_file-���s���� r���zssl_cert.pemzssl_key.pemc���������������C���s���t�d�krd�S�t�jt�j�S�d�S�)N)�ssl� SSLContextZPROTOCOL_SSLv23r���r���r���r����dummy_ssl_context<���s����r"���c���������� ���C���s@���t�dd���}|��}|�j|�}d|_z|�j|��W�d�|j���X�d�S�)Nc���������������S���s���d�S�)Nr���r���r���r���r����onceD���s����zrun_briefly.<locals>.onceF)r ���Zcreate_taskZ_log_destroy_pending�run_until_complete�close)�loopr#����gen�tr���r���r����run_brieflyC���s���� r)�������c�������������C���sT���t�j���|�}xB|��sN|d�k r8|t�j����}|dkr8tj���|�jtjd|�d���qW�d�S�)Nr���g����MbP?)r&���)�timer ����TimeoutErrorr$���r���Zsleep)r&���Zpred�timeoutZdeadliner���r���r���� run_untilR���s����r.���c�������������C���s���|�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)r&���r���r���r����run_once\���s����r0���c���������������@���s���e�Zd�Zdd��Zdd��ZdS�)�SilentWSGIRequestHandlerc�������������C���s���t�j��S�)N)�io�StringIO)�selfr���r���r���� get_stderri���s����z#SilentWSGIRequestHandler.get_stderrc�������������G���s���d�S�)Nr���)r4����format�argsr���r���r����log_messagel���s����z$SilentWSGIRequestHandler.log_messageN)�__name__� __module__�__qualname__r5���r8���r���r���r���r���r1���g���s���r1���c�������������������s(���e�Zd�ZdZ��fdd�Zdd��Z���ZS�)�SilentWSGIServer����c����������������s"���t���j��\}}|j|�j��||fS�)N)�super�get_request� settimeout�request_timeout)r4����request�client_addr)� __class__r���r���r?���t���s����zSilentWSGIServer.get_requestc�������������C���s���d�S�)Nr���)r4���rB����client_addressr���r���r����handle_errory���s����zSilentWSGIServer.handle_error)r9���r:���r;���rA���r?���rF���� __classcell__r���r���)rD���r���r<���p���s���r<���c���������������@���s���e�Zd�Zdd��ZdS�)�SSLWSGIServerMixinc�������������C���s^���t�}t}tj��}|j||��|j|dd�}y|�j|||���|j���W�n�tk rX���Y�nX�d�S�)NT)Zserver_side) �ONLYKEY�ONLYCERTr ���r!���Zload_cert_chainZwrap_socketZRequestHandlerClassr%����OSError)r4���rB���rE���ZkeyfileZcertfile�contextZssockr���r���r����finish_request���s����z!SSLWSGIServerMixin.finish_requestN)r9���r:���r;���rM���r���r���r���r���rH���}���s���rH���c���������������@���s���e�Zd�ZdS�)� SSLWSGIServerN)r9���r:���r;���r���r���r���r���rN�������s���rN���F)�use_sslc���������� ���#���sv���dd��}|r|n|}||�t������j|����j��_tj��fdd�d�}|j���z ��V��W�d���j�����j���|j ���X�d�S�)Nc�������������S���s���d}dg}|||��dgS�)Nz200 OK�Content-type� text/plains���Test message)rP���rQ���r���)�environZstart_responseZstatusZheadersr���r���r����app����s���� z_run_test_server.<locals>.appc������������������s�����j�dd�S�)Ng�������?)Z poll_interval)Z serve_foreverr���)�httpdr���r����<lambda>����s����z"_run_test_server.<locals>.<lambda>)�target) r1���Zset_appZserver_address�address� threadingZThread�start�shutdownZserver_closer���)rW���rO���� server_cls�server_ssl_clsrS���Zserver_classZ server_threadr���)rT���r����_run_test_server����s���� r]���ZAF_UNIXc���������������@���s���e�Zd�Zdd��ZdS�)�UnixHTTPServerc�������������C���s���t�jj|���d|�_d|�_d�S�)Nz 127.0.0.1�P���)�socketserver�UnixStreamServer�server_bindZserver_nameZserver_port)r4���r���r���r���rb�������s����zUnixHTTPServer.server_bindN)r9���r:���r;���rb���r���r���r���r���r^�������s���r^���c�������������������s(���e�Zd�ZdZdd��Z��fdd�Z���ZS�)�UnixWSGIServerr=���c�������������C���s���t�j|���|�j���d�S�)N)r^���rb���Z setup_environ)r4���r���r���r���rb�������s���� zUnixWSGIServer.server_bindc����������������s"���t���j��\}}|j|�j��|dfS�)N� 127.0.0.1��)rd���re���)r>���r?���r@���rA���)r4���rB���rC���)rD���r���r���r?�������s����zUnixWSGIServer.get_request)r9���r:���r;���rA���rb���r?���rG���r���r���)rD���r���rc�������s���rc���c���������������@���s���e�Zd�Zdd��ZdS�)�SilentUnixWSGIServerc�������������C���s���d�S�)Nr���)r4���rB���rE���r���r���r���rF�������s����z!SilentUnixWSGIServer.handle_errorN)r9���r:���r;���rF���r���r���r���r���rf�������s���rf���c���������������@���s���e�Zd�ZdS�)�UnixSSLWSGIServerN)r9���r:���r;���r���r���r���r���rg�������s���rg���c����������� ���C���s���t�j���}�|�jS�Q�R�X�d�S�)N)�tempfileZNamedTemporaryFile�name)�filer���r���r����gen_unix_socket_path����s���� rk���c��������������c���s<���t���}�z |�V��W�d�ytj|���W�n�tk r4���Y�nX�X�d�S�)N)rk���r����unlinkrK���)r���r���r���r����unix_socket_path����s���� rm���c���������� ���c���s,���t����}t||�ttd�E�d�H��W�d�Q�R�X�d�S�)N)rW���rO���r[���r\���)rm���r]���rf���rg���)rO���r���r���r���r����run_test_unix_server����s����rn���z 127.0.0.1)�host�portrO���c�������������c���s���t�|�|f|ttd�E�d�H��d�S�)N)rW���rO���r[���r\���)r]���r<���rN���)ro���rp���rO���r���r���r����run_test_server����s���� rq���c�������������C���sP���i�}x4t�|��D�](}|jd�r(|jd�r(qtd�d�||<�qW�td|�f|�j�|���S�)N�__)�return_valueZTestProtocol)�dir� startswith�endswith�MockCallback�type� __bases__)�baseZdctri���r���r���r����make_test_protocol����s����r{���c���������������@���s6���e�Zd�Zdd��Zddd�Zdd��Zdd ��Zd d��ZdS�) �TestSelectorc�������������C���s ���i�|�_�d�S�)N)�keys)r4���r���r���r����__init__��s����zTestSelector.__init__Nc�������������C���s���t�j|d||�}||�j|<�|S�)Nr���)r���ZSelectorKeyr}���)r4����fileobjr ����data�keyr���r���r����register��s���� zTestSelector.registerc�������������C���s���|�j�j|�S�)N)r}����pop)r4���r���r���r���r���� unregister��s����zTestSelector.unregisterc�������������C���s���g�S�)Nr���)r4���r-���r���r���r����select��s����zTestSelector.selectc�������������C���s���|�j�S�)N)r}���)r4���r���r���r����get_map��s����zTestSelector.get_map)N)r9���r:���r;���r~���r����r����r����r����r���r���r���r���r|�����s ��� r|���c�������������������s����e�Zd�ZdZd-��fdd� Zdd��Zdd��Z��fd d �Zdd��Zd d��Z dd��Z dd��Zdd��Zdd��Z dd��Zdd��Zdd��Zdd��Zdd ��Zd!d"��Zd#d$��Z��fd%d&�Z��fd'd(�Zd)d*��Zd+d,��Z���ZS�).�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. Nc����������������sv���t���j���|d�kr"dd��}d|�_nd|�_|��|�_t|�j��d|�_d|�_g�|�_t��|�_ i�|�_ i�|�_|�j���t j��|�_d�S�)Nc���������������s���s ���d�V��d�S�)Nr���r���r���r���r���r'���,��s����zTestLoop.__init__.<locals>.genFTr���g��&�.>)r>���r~����_check_on_close�_gen�next�_timeZ_clock_resolution�_timersr|���Z _selector�readers�writers�reset_counters�weakref�WeakValueDictionary�_transports)r4���r'���)rD���r���r���r~���(��s���� zTestLoop.__init__c�������������C���s���|�j�S�)N)r����)r4���r���r���r���r+���?��s����z TestLoop.timec�������������C���s���|r|��j�|7��_�dS�)zMove test time forward.N)r����)r4����advancer���r���r����advance_timeB��s����zTestLoop.advance_timec����������������sB���t���j���|�jr>y|�jjd��W�n�tk r4���Y�n X�td��d�S�)Nr���zTime generator is not finished)r>���r%���r����r�����send� StopIteration�AssertionError)r4���)rD���r���r���r%���G��s���� zTestLoop.closec�������������G���s���t�j|||��|�j|<�d�S�)N)r ����Handler����)r4����fd�callbackr7���r���r���r����_add_readerQ��s����zTestLoop._add_readerc�������������C���s0���|�j�|��d7��<�||�jkr(|�j|=�dS�dS�d�S�)Nr���TF)�remove_reader_countr����)r4���r����r���r���r����_remove_readerT��s ���� zTestLoop._remove_readerc�������������G���sh���||�j�krtd|��d���|�j�|�}|j|krDtd|j��d|�����|j|krdtd|j��d|�����d�S�)Nzfd z is not registeredzunexpected callback: z != zunexpected callback args: )r����r����Z _callbackZ_args)r4���r����r����r7����handler���r���r���� assert_reader\��s���� zTestLoop.assert_readerc�������������C���s���||�j�krtd|��d���d�S�)Nzfd z is registered)r����r����)r4���r����r���r���r����assert_no_readerg��s���� zTestLoop.assert_no_readerc�������������G���s���t�j|||��|�j|<�d�S�)N)r ���r����r����)r4���r����r����r7���r���r���r����_add_writerk��s����zTestLoop._add_writerc�������������C���s0���|�j�|��d7��<�||�jkr(|�j|=�dS�dS�d�S�)Nr���TF)�remove_writer_countr����)r4���r����r���r���r����_remove_writern��s ���� zTestLoop._remove_writerc�������������G���s���|�j�|�}d�S�)N)r����)r4���r����r����r7���r����r���r���r���� assert_writerv��s���� zTestLoop.assert_writerc�������������C���s8���y|�j�|�}W�n�tk r"���Y�nX�tdj||���d�S�)Nz.File descriptor {!r} is used by transport {!r})r�����KeyError�RuntimeErrorr6���)r4���r����Z transportr���r���r����_ensure_fd_no_transport~��s����z TestLoop._ensure_fd_no_transportc�������������G���s���|�j�|��|�j||f|���S�)zAdd a reader callback.)r����r����)r4���r����r����r7���r���r���r���� add_reader���s���� zTestLoop.add_readerc�������������C���s���|�j�|��|�j|�S�)zRemove a reader callback.)r����r����)r4���r����r���r���r���� remove_reader���s���� zTestLoop.remove_readerc�������������G���s���|�j�|��|�j||f|���S�)zAdd a writer callback..)r����r����)r4���r����r����r7���r���r���r���� add_writer���s���� zTestLoop.add_writerc�������������C���s���|�j�|��|�j|�S�)zRemove a writer callback.)r����r����)r4���r����r���r���r���� remove_writer���s���� zTestLoop.remove_writerc�������������C���s���t�jt�|�_t�jt�|�_d�S�)N)�collections�defaultdict�intr����r����)r4���r���r���r���r�������s����zTestLoop.reset_countersc����������������s:���t���j���x$|�jD�]}|�jj|�}|�j|��qW�g�|�_d�S�)N)r>���� _run_oncer����r����r����r����)r4����whenr����)rD���r���r���r�������s ���� zTestLoop._run_oncec����������������s ���|�j�j|��t��j||f|���S�)N)r�����appendr>����call_at)r4���r����r����r7���)rD���r���r���r�������s����zTestLoop.call_atc�������������C���s���d�S�)Nr���)r4���Z event_listr���r���r����_process_events���s����zTestLoop._process_eventsc�������������C���s���d�S�)Nr���)r4���r���r���r����_write_to_self���s����zTestLoop._write_to_self)N)r9���r:���r;����__doc__r~���r+���r����r%���r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����r����rG���r���r���)rD���r���r������s,��� r����c��������������K���s���t�jf�ddgi|���S�)N�spec�__call__)r���ZMock)�kwargsr���r���r���rw������s����rw���c���������������@���s���e�Zd�ZdZdd��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')) c�������������C���s���t�tjt|��|tj��S�)N)�bool�re�search�str�S)r4����otherr���r���r����__eq__���s����zMockPattern.__eq__N)r9���r:���r;���r����r����r���r���r���r���r�������s���r����c�������������C���s$���t�j|��}|d�kr td|�f���|S�)Nzunable to get the source of %r)r ���Z_get_function_source� ValueError)�func�sourcer���r���r����get_function_source���s���� r����c���������������@���sV���e�Zd�Zedd���Zdd�dd�Zddd �Zd d��Zdd ��Zdd��Z e jsRdd��ZdS�)�TestCasec�������������C���s&���|�j�}|d�k r|jdd��|�j���d�S�)NT)�wait)Z_default_executorrZ���r%���)r&���Zexecutorr���r���r���� close_loop���s����zTestCase.close_loopT)�cleanupc������������C���s ���t�jd���|r|�j|�j|��d�S�)N)r ����set_event_loopZ addCleanupr����)r4���r&���r����r���r���r���r�������s���� zTestCase.set_event_loopNc�������������C���s���t�|�}|�j|��|S�)N)r����r����)r4���r'���r&���r���r���r���� new_test_loop���s���� zTestCase.new_test_loopc�������������C���s���|�j�t_�d�S�)N)�_get_running_loopr ���)r4���r���r���r����unpatch_get_running_loop���s����z!TestCase.unpatch_get_running_loopc�������������C���s ���t�j|�_dd��t�_tj��|�_d�S�)Nc���������������S���s���d�S�)Nr���r���r���r���r���rU������s����z TestCase.setUp.<locals>.<lambda>)r ���r����r���Zthreading_setup�_thread_cleanup)r4���r���r���r����setUp���s���� zTestCase.setUpc�������������C���sB���|�j����tjd���|�jtj��d��|�j���tj|�j ���tj ���d�S�)N)NNN)r����r ���r����ZassertEqual�sys�exc_infoZ doCleanupsr���Zthreading_cleanupr����Z reap_children)r4���r���r���r����tearDown���s���� zTestCase.tearDownc�������������O���s���G�dd��d�}|��S�)Nc���������������@���s���e�Zd�Zdd��Zdd��ZdS�)z!TestCase.subTest.<locals>.EmptyCMc�������������S���s���d�S�)Nr���)r4���r���r���r���� __enter__���s����z+TestCase.subTest.<locals>.EmptyCM.__enter__c�������������W���s���d�S�)Nr���)r4����excr���r���r����__exit__���s����z*TestCase.subTest.<locals>.EmptyCM.__exit__N)r9���r:���r;���r����r����r���r���r���r����EmptyCM���s���r����r���)r4���r7���r����r����r���r���r����subTest���s����zTestCase.subTest)N) r9���r:���r;����staticmethodr����r����r����r����r����r����r���ZPY34r����r���r���r���r���r�������s��� r����c��������������c���s2���t�j}�zt�jtjd���dV��W�dt�j|���X�dS�)zrContext manager to disable asyncio logger. For example, it can be used to ignore warnings in debug mode. r���N)r����levelZsetLevel�loggingZCRITICAL)Z old_levelr���r���r����disable_logger���s ���� r����c�������������C���s*���t�jtj�}|�|_||_||_d|j_|S�)z'Create a mock of a non-blocking socket.g��������)r���Z MagicMock�socket�protorx����familyZ gettimeoutrs���)r����rx���r����Zsockr���r���r����mock_nonblocking_socket��s����r����c���������������C���s���t�jddd�S�)Nz'asyncio.sslproto._is_sslproto_availableF)rs���)r���Zpatchr���r���r���r����force_legacy_ssl_support��s����r����)r*���)Nr����r����� contextlibr2���r����r���r����r����r`���r����rh���rX���r+���Zunittestr����r���Zhttp.serverr���Zwsgiref.simple_serverr���r���r ����ImportErrorre���r���r���r ���r ���r���r���Z coroutinesr ����logr���r���r����platformZ windows_utilsr���r���rJ���rI���r"���r)���r.���r0���r1���r<���rH���rN���r]���r���ra���r^���rc���rf���rg���rk����contextmanagerrm���rn���rq���r{���ZBaseSelectorr|���Z BaseEventLoopr����rw���r����r����r����r����r����ZIPPROTO_TCPZSOCK_STREAMZAF_INETr����r����r���r���r���r����<module>���s���� � 4