HOME


Mini Shell 1.0
DIR: /usr/lib/python3/dist-packages/twisted/internet/test/__pycache__/
Upload File :
Current File : //usr/lib/python3/dist-packages/twisted/internet/test/__pycache__/reactormixins.cpython-312.pyc
�

Ϫ�f/@��@�dZgd�ZddlZddlZddlZddlmZmZmZm	Z	m
Z
mZmZm
Z
ddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZmZddlmZm Z erdd
l!m"Z"	ddl!m#Z$e$Z#Gd�de&�Z'd�Z(dd�Z)Gd�d�Z*de+ddfd�Z,e,Z-y#e%$rdZ#Y�0wxYw)a�
Utilities for unit testing reactor implementations.

The main feature of this module is L{ReactorBuilder}, a base class for use when
writing interface/blackbox tests for reactor implementations.  Test case classes
for reactor features should subclass L{ReactorBuilder} instead of
L{SynchronousTestCase}.  All of the features of L{SynchronousTestCase} will be
available.  Additionally, the tests will automatically be applied to all
available reactor implementations.
)�TestTimeoutError�ReactorBuilder�needsRunningReactor�N)�
TYPE_CHECKING�Callable�Dict�Optional�Sequence�Type�Union�cast)�	Interface��log)�_fullyQualifiedName)�Failure)�namedAny)�platform)�SkipTest�SynchronousTestCase)�DEFAULT_TIMEOUT_DURATION�acquireAttribute��asyncioreactor)�processc��eZdZdZy)rzm
    The reactor was still running after the timeout period elapsed in
    L{ReactorBuilder.runReactor}.
    N)�__name__�
__module__�__qualname__�__doc__���E/usr/lib/python3/dist-packages/twisted/internet/test/reactormixins.pyrr/s��r"rc�&�|j|�y)a*
    Various functions within these tests need an already-running reactor at
    some point.  They need to stop the reactor when the test has completed, and
    that means calling reactor.stop().  However, reactor.stop() raises an
    exception if the reactor isn't already running, so if the L{Deferred} that
    a particular API under test returns fires synchronously (as especially an
    endpoint's C{connect()} method may do, if the connect is to a local
    interface address) then the test won't be able to stop the reactor being
    tested and finish.  So this calls C{thunk} only once C{reactor} is running.

    (This is just an alias for
    L{twisted.internet.interfaces.IReactorCore.callWhenRunning} on the given
    reactor parameter, in order to centrally reference the above paragraph and
    repeating it everywhere as a comment.)

    @param reactor: the L{twisted.internet.interfaces.IReactorCore} under test

    @param thunk: a 0-argument callable, which eventually finishes the test in
        question, probably in a L{Deferred} callback.
    N)�callWhenRunning)�reactor�thunks  r#rr6s��*���E�"r"c����|�ddlm}dg���fd�}|j|�|j|j|�y)a�
    Stop the reactor as soon as any error is logged on the given publisher.

    This is beneficial for tests which will wait for a L{Deferred} to fire
    before completing (by passing or failing).  Certain implementation bugs may
    prevent the L{Deferred} from firing with any result at all (consider a
    protocol's {dataReceived} method that raises an exception: this exception
    is logged but it won't ever cause a L{Deferred} to fire).  In that case the
    test would have to complete by timing out which is a much less desirable
    outcome than completing as soon as the unexpected error is encountered.

    @param case: A L{SynchronousTestCase} to use to clean up the necessary log
        observer when the test is over.
    @param reactor: The reactor to stop.
    @param publisher: A L{LogPublisher} to watch for errors.  If L{None}, the
        global log publisher will be watched.
    Nrrc�p���r3|jd�r!�j��j�yyy)N�isError)�get�pop�stop)�eventr&�runnings ��r#�stopIfErrorz stopOnError.<locals>.stopIfErrords*����u�y�y��+��K�K�M��L�L�N�,�7r")�twisted.pythonr�addObserver�
addCleanup�removeObserver)�caser&�	publisherr0r/s `  @r#�stopOnErrorr7Ns>���$��3��f�G��
���+�&��O�O�I�,�,�k�:r"c
��eZdZUdZdgZej�rejgd��n|ejdg�ejd�ej�rejd�n6ejddg�ej�sejd	g�d
Zee
gefed<d
Zd
Zeeeeed<iZeeefed
<d�Zd�Zd�Zd�Zd�Zdd�Zededdeeeedee fffd��Z!y
)ra�
    L{SynchronousTestCase} mixin which provides a reactor-creation API.  This
    mixin defines C{setUp} and C{tearDown}, so mix it in before
    L{SynchronousTestCase} or call its methods from the overridden ones in the
    subclass.

    @cvar skippedReactors: A dict mapping FQPN strings of reactors for
        which the tests defined by this class will be skipped to strings
        giving the skip message.
    @cvar requiredInterfaces: A C{list} of interfaces which the reactor must
        provide or these tests will be skipped.  The default, L{None}, means
        that no interfaces are required.
    @ivar reactorFactory: A no-argument callable which returns the reactor to
        use for testing.
    @ivar originalHandler: The SIGCHLD handler which was installed when setUp
        ran and which will be re-installed when tearDown runs.
    @ivar _reactors: A list of FQPN strings giving the reactors for which
        L{SynchronousTestCase}s will be created.
    z,twisted.internet.selectreactor.SelectReactor)z,twisted.internet.gireactor.PortableGIReactorz/twisted.internet.win32eventreactor.Win32Reactorz0twisted.internet.iocpreactor.reactor.IOCPReactorz$twisted.internet.gireactor.GIReactorz:twisted.internet.test.reactormixins.AsyncioSelectorReactorz$twisted.internet.cfreactor.CFReactorz(twisted.internet.pollreactor.PollReactorz*twisted.internet.epollreactor.EPollReactorz(twisted.internet.kqreactor.KQueueReactorN�reactorFactory�requiredInterfaces�skippedReactorsc��tj�s8tjtjtj�|_yy)z�
        Clear the SIGCHLD handler, if there is one, to ensure an environment
        like the one which exists prior to a call to L{reactor.run}.
        N)r�	isWindows�signal�SIGCHLD�SIG_DFL�originalHandler��selfs r#�setUpzReactorBuilder.setUp�s0��
�!�!�#�#)�=�=�������#P�D� �$r"c�n�|j�.tjtj|j�t��t	j�}tj
r�t
jdtj
���tj�t	jd�t	j�|z
dkDrUtj
D]&}tj|tj��(tdtj
����tj
r��yyy)z|
        Restore the original SIGCHLD handler and reap processes as long as
        there seem to be any remaining.
        Nz/ReactorBuilder.tearDown reaping some processes g����MbP?�<z-Timeout waiting for child processes to exit: )rAr>r?r�time�reapProcessHandlersr�msg�reapAllProcesses�sleep�os�kill�SIGKILL�	Exception)rC�begin�pids   r#�tearDownzReactorBuilder.tearDown�s���
���+��M�M�&�.�.�$�*>�*>�?����I�I�K�E��-�-�����2�2�5���(�(�*��
�
�5�!��9�9�;��&��+�&�:�:�5������V�^�^�4�5�#�"�6�6�9����-�-�r"c��|j�t|dd��M|jD]$}|j|�|j	d��&|jj�|j
�|j�}|D]}|j��ddl	m
}|j|_|j|_y)z�
        Clean up any resources which may have been allocated for the given
        reactor by its creation or by a test which used it.
        �_internalReadersNr�r&)�_uninstallHandler�getattrrT�removeReader�connectionLost�clear�
disconnectAll�getDelayedCalls�cancel�twisted.internetr&�_originalReactorDict�__dict__�_originalReactorClass�	__class__)rCr&�reader�calls�c�
globalReactors      r#�_unbuildReactorzReactorBuilder._unbuildReactor�s���	�!�!�#��7�.��5�A�!�2�2�
,���$�$�V�,��%�%�d�+�
,�
�$�$�*�*�,�	�����'�'�)���	�A�
�H�H�J�	�	>�!(�!=�!=�
��")�"?�"?�
�r"c�h�	ddlm}ddlm}t	||�r|j
|urt
d��	|j
�J�|j�}j|_	|j|_|j|_|j|_
|j��|jD�cgc]}|j|�s|��}}|rZ|j|�t
t|j��ddj!|D�cgc]
}t|���c}�����|j/|j|�|S#t$rY��wxYwcc}wcc}w#t"$rHt%j&dd�|j)�t
t+�j-���wxYw)	zK
        Create and return a reactor using C{self.reactorFactory}.
        rrU)�	CFReactorzuCFReactor uses APIs which manipulate global state, so it's not safe to run its own reactor-builder tests under itselfNz does not provide �,zFailed to install reactor)r^r&�twisted.internet.cfreactorri�
isinstancer9r�ImportErrorr`r_rbrar:�
providedByrg�fullyQualifiedName�join�
BaseExceptionr�err�flushLoggedErrorsr�getErrorMessager3)rCrfrir&�required�missing�xs       r#�buildReactorzReactorBuilder.buildReactors���	�A�<�
�=�)�4��'�'�9�4��#���
"	��&�&�2�2�2��)�)�+�G�+8�+A�+A�G�(�,9�,C�,C�G�)�&-�%5�%5�M�"�&-�&7�&7�M�#��&�&�2�%)�$;�$;�� �#�.�.�w�7�����
��(�(��1�"�/�w�/@�/@�A��H�H�W�%M��&8��&;�%M�N����	
����,�,�g�6����a�	��	��B��&N��/�		8�
�G�G�D�5�6��"�"�$��7�9�4�4�6�7�7�		8�s*�E�A"E �0E�	E�	E�E� AF1c�8�t|jdt�S)z�
        Determine how long to run the test before considering it failed.

        @return: A C{int} or C{float} giving a number of seconds.
        �timeout)r�_parentsrrBs r#�
getTimeoutzReactorBuilder.getTimeout:s�� ��
�
�y�:R�S�Sr"c�����|�|j�}g���fd�}�j||�}�j��rtd|�d���|j	�y)a�
        Run the reactor for at most the given amount of time.

        @param reactor: The reactor to run.

        @type timeout: C{int} or C{float}
        @param timeout: The maximum amount of time, specified in seconds, to
            allow the reactor to run.  If the reactor is still running after
            this much time has elapsed, it will be stopped and an exception
            raised.  If L{None}, the default test method timeout imposed by
            Trial will be used.  This depends on the L{IReactorTime}
            implementation of C{reactor} for correct operation.

        @raise TestTimeoutError: If the reactor is still running after
            C{timeout} seconds.
        Nc�H���jd��j�y�N)�appendr-)r&�timedOuts��r#r-z'ReactorBuilder.runReactor.<locals>.stopXs����O�O�D�!��L�L�Nr"zreactor still running after z seconds)r|�	callLater�runrr])rCr&rzr-�timedOutCallr�s `   @r#�
runReactorzReactorBuilder.runReactorBsa���"�?��o�o�'�G���	��(�(��$�7�����
��"�%A�'��(�#S�T�T����!r"�cls�returnc�^���i}�jD]���jd�d}�jdz|zdzjdd�}G��fd�d�t�}||_dj�jj�dd|gz�|_|||j<��|S)z�
        Create a L{SynchronousTestCase} subclass which mixes in C{cls} for each
        known reactor and return a dict mapping their names to them.
        �.����Tests�_c���eZdZW�jZW�W�jvrW�jW�Z	eW��Zy#e$re�j�ZYywxYw)�4ReactorBuilder.makeTestCaseClasses.<locals>.testcaseN)
rrrr;�skiprr9rqrrt)r�r&s��r#�testcaser�rsX��� �^�^�
��c�1�1�1��.�.�w�7�D�7�%-�g�%6�N��$�7�"�9�4�4�6�D�7�s�	?�A�Ar�r)�	_reactors�splitr�replacerrpr)r��classes�shortReactorName�namer�r&s`    @r#�makeTestCaseClassesz"ReactorBuilder.makeTestCaseClassescs����
�	��}�}�	2�G�&�}�}�S�1�"�5���L�L�3�&�)9�9�G�C�L�L�S�RU�V�D�
7�3� 3�
7�!%�H��$'�H�H�S�-=�-=�-C�-C�-E�a��-K�t�f�-T�$U�H�!�)1�G�H�%�%�&�	2� �r"r)"rrrr r�rr=�extendr��isMacOSX�isLinuxr9r	r�object�__annotations__rAr:r
rrr;r�strrDrRrgrxr|r��classmethodrrr�r!r"r#rrmsz���,	7��I�
�x����	���
�	
�	���6�
�	
�	���U�V��8�������C�D����>�@��
�$�8�#�#�%�� � �C���6:�N�H�X�b�&�j�1�2�9��O�>B����$�y�/�!:�;�B�&(�O�T�#�s�(�^�(�Q��:#@�J7�rT�"�B��
�"�
#��	
�c�5��.�/��6I�1J�J�K�K�	L���r"rrCr�z%asyncioreactor.AsyncioSelectorReactorc������ddlm}m}m�ddlm}t
t|�}|��|�����|j���fd��}|j��S)a
    Make a new asyncio reactor associated with a new event loop.

    The test suite prefers this constructor because having a new event loop
    for each reactor provides better test isolation.  The real constructor
    prefers to re-use (or create) a global loop because of how this interacts
    with other asyncio-based libraries and applications (though maybe it
    shouldn't).

    @param self: The L{ReactorBuilder} subclass this is being called on.  We
        don't use this parameter but we get called with it anyway.
    r)�get_event_loop�new_event_loop�set_event_looprc�6���j����yr)�close)�loop�originalLoopr�s���r#�cleanUpz'asyncioSelectorReactor.<locals>.cleanUp�s����
�
���|�$r")
�asyncior�r�r�r^rr
rr3�AsyncioSelectorReactor)	rCr�r�r�
asTestCaser�r�r�r�s	      @@@r#�asyncioSelectorReactorr��sa���G�F�/��)�4�0�J�!�#�L���D��4�����%��%�1�>�0�0��6�6r"r).r �__all__rLr>rG�typingrrrr	r
rrr
�zope.interfacerr1r�twisted.python.deprecaterro�twisted.python.failurer�twisted.python.reflectr�twisted.python.runtimer�twisted.trial.unittestrr�twisted.trial.utilrrr^rr�_processrmrOrrr7rr�r�r�r!r"r#�<module>r�s���	�H��	�
��W�W�W�$��N�*�+�+�@�I��0��4��G��y��#�0;�>Q�Q�h7��7�,S�7�@0���q���G��s�$B�B�B