%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/test/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/test/__pycache__/test_threads.cpython-312.pyc

�

Ϫ�f�3���dZddlZddlZddlZddlmZddlmZmZm	Z	m
Z
mZmZddl
mZmZmZmZddlmZ	ddlZee	j,ed�d�Gd�d	e��ZGd
�d�Zee	j,ed�d�Gd�d
e��ZGd�de�ZdZGd�de
j8�Zee	j,ed�d�ee	j<ed�d�Gd�de���Zy#e$rY��wxYw)zC
Test methods in twisted.internet.threads and reactor thread APIs.
�N)�skipIf)�defer�error�
interfaces�protocol�reactor�threads)�failure�log�
threadable�
threadpool)�TestCasez(No thread support, nothing to test here.c�L�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
y)
�ReactorThreadsTestsz.
    Tests for the reactor threading API.
    c��tjd�|jtjjd�tjd�|jtjjd�y)z:
        Try to change maximum number of threads.
        �"�N)r�suggestThreadPoolSize�assertEqualr
�max��selfs �;/usr/lib/python3/dist-packages/twisted/test/test_threads.py�test_suggestThreadPoolSizez.ReactorThreadsTests.test_suggestThreadPoolSize"sX��	�%�%�b�)�����+�+�/�/��4��%�%�a�(�����+�+�/�/��3�c�J�tjtjd�S)z�
        The reactor's threadpool is only available when the reactor is running,
        so to have a sane behavior during the tests we make a dummy
        L{threads.deferToThread} call.
        r)r	�
deferToThread�time�sleeprs r�_waitForThreadz"ReactorThreadsTests._waitForThread+s���$�$�T�Z�Z��3�3rc�L���fd�}�j�j|�S)z�
        Test callInThread functionality: set a C{threading.Event}, and check
        that it's not in the main thread.
        c�����tj��g���fd�}tj|��j	d��j�s�j
d�y�j�dg�y)Nc�l���jtj���j�y�N)�appendr�isInIOThread�set)�result�waiters��r�threadedFunczGReactorThreadsTests.test_callInThread.<locals>.cb.<locals>.threadedFunc=s!����
�
�j�5�5�7�8��
�
�r�xzTimed out waiting for event.F)�	threading�Eventr�callInThread�wait�isSet�failr)�ignr*r(r)rs  @@�r�cbz1ReactorThreadsTests.test_callInThread.<locals>.cb9s]����_�_�&�F��F�
�
� � ��.��K�K����<�<�>��	�	�8�9�� � ��%��1r�r �addCallback�rr3s` r�test_callInThreadz%ReactorThreadsTests.test_callInThread3s$���
	2��"�"�$�0�0��4�4rc�F�d�}|j�j|�S)zk
        Test callFromThread functionality: from the main thread, and from
        another thread.
        c���tj�}tj���fd�}tj|�tj|j
d�tj|�gd��S)Nc�F��tj�jd�yr$)r�callFromThread�callback)�firedByOtherThreads�rr*zIReactorThreadsTests.test_callFromThread.<locals>.cb.<locals>.threadedFuncTs����&�&�'9�'B�'B�D�IrT)�fireOnOneErrback)r�Deferredrr.r;r<�DeferredList)r2�firedByReactorThreadr*r=s   @rr3z3ReactorThreadsTests.test_callFromThread.<locals>.cbPsh���#(�>�>�#3� �!&���!1��
J�
� � ��.��"�"�#7�#@�#@�$�G��%�%�%�'9�:�T��
rr4r6s  r�test_callFromThreadz'ReactorThreadsTests.test_callFromThreadJs#��	��"�"�$�0�0��4�4rc�L���fd�}�j�j|�S)zT
        Try to make an overflow on the reactor waker using callFromThread.
        c�.���d�_tj����fd�}tj|��jd��j
�s�jd��j�tj�j�Sy)Nc����td�D]}	tjd����j
�y#t$rt	j
��_Y�6wxYw)Ni��c��yr$�rGrr�<lambda>z^ReactorThreadsTests.test_wakerOverflow.<locals>.cb.<locals>.threadedFunction.<locals>.<lambda>n��r)�rangerr;�
BaseExceptionr
�Failurer')�irr)s ��r�threadedFunctionzLReactorThreadsTests.test_wakerOverflow.<locals>.cb.<locals>.threadedFunctionisX����v���A���.�.�|�<���
�
���)��'.���'8�����s�:�"A �A r+�Timed out waiting for event)	r
r,r-rr.r/r0r1r)r2rNr)rs  @�rr3z2ReactorThreadsTests.test_wakerOverflow.<locals>.cbesp����D�L��_�_�&�F�	
�
� � �!1�2��K�K����<�<�>��	�	�7�8��|�|�'��z�z�$�,�,�/�/�(rr4r6s` r�test_wakerOverflowz&ReactorThreadsTests.test_wakerOverflow`s$���
	0�,�"�"�$�0�0��4�4rc�������tj��g�g������fd�}����fd�}�j�j|�j	|�S)zK
        Utility method to test L{threads.blockingCallFromThread}.
        c�������fd�}tj|�tj�j�j��S)Nc����	tjt��}�j|��j�y#t$r}�j|�Yd}~�0d}~wwxYwr$)r	�blockingCallFromThreadrr%�	Exceptionr')�r�e�errors�reactorFunc�resultsr)s  ����rr*zRReactorThreadsTests._testBlockingCallFromThread.<locals>.cb1.<locals>.threadedFunc�sR���&��6�6�w��L�A��N�N�1�%��
�
���	!�%��M�M�!�$�$��%�s�?�	A#�A�A#)rr.r	rr/�
getTimeout)r2r*rXrYrZrr)s  �����r�cb1z<ReactorThreadsTests._testBlockingCallFromThread.<locals>.cb1�s6���
�
� � ��.��(�(����d�o�o�6G�H�Hrc�N���j�s�jd���fS)NrO)r0r1)r2rXrZrr)s ����r�cb2z<ReactorThreadsTests._testBlockingCallFromThread.<locals>.cb2�s#����<�<�>��	�	�7�8��F�?�"r)r,r-r r5�addBoth)rrYr\r^rXrZr)s``  @@@r�_testBlockingCallFromThreadz/ReactorThreadsTests._testBlockingCallFromThread}sS������"������	I�	I�	#�
�"�"�$�0�0��5�=�=�c�B�Brc�T��d�}�fd�}�j|�j|�S)z�
        Test blockingCallFromThread facility: create a thread, call a function
        in the reactor using L{threads.blockingCallFromThread}, and verify the
        result returned.
        c�,�tjd�S)N�foo)r�succeedrGrrrYzDReactorThreadsTests.test_blockingCallFromThread.<locals>.reactorFunc�s���=�=��'�'rc�6���j|ddd�y)Nrrc�r��resrs �rr3z;ReactorThreadsTests.test_blockingCallFromThread.<locals>.cb��������S��V�A�Y��.r�r`r5�rrYr3s`  r�test_blockingCallFromThreadz/ReactorThreadsTests.test_blockingCallFromThread�s+���	(�	/��/�/��<�H�H��L�Lrc�T��d�}�fd�}�j|�j|�S)zx
        Test blockingCallFromThread as above, but be sure the resulting
        Deferred is not already fired.
        c�p�tj�}tjd|jd�|S)N皙�����?�egg)rr?r�	callLaterr<��ds rrYzIReactorThreadsTests.test_asyncBlockingCallFromThread.<locals>.reactorFunc�s)����� �A����c�1�:�:�u�5��Hrc�6���j|ddd�y)Nrrprfrgs �rr3z@ReactorThreadsTests.test_asyncBlockingCallFromThread.<locals>.cb�rirrjrks`  r� test_asyncBlockingCallFromThreadz4ReactorThreadsTests.test_asyncBlockingCallFromThread�s+���	�
	/��/�/��<�H�H��L�Lrc�T��d�}�fd�}�j|�j|�S)z?
        Test error report for blockingCallFromThread.
        c�>�tjtd��S)N�bar)rr1�RuntimeErrorrGrrrYzIReactorThreadsTests.test_errorBlockingCallFromThread.<locals>.reactorFunc�s���:�:�l�5�1�2�2rc����j|ddt��j|ddjdd�y)N�rrx��assertIsInstanceryr�argsrgs �rr3z@ReactorThreadsTests.test_errorBlockingCallFromThread.<locals>.cb�s<����!�!�#�a�&��)�\�:����S��V�A�Y�^�^�A�.��6rrjrks`  r� test_errorBlockingCallFromThreadz4ReactorThreadsTests.test_errorBlockingCallFromThread�s+���
	3�	7��/�/��<�H�H��L�Lrc�T��d�}�fd�}�j|�j|�S)z�
        Test error report for blockingCallFromThread as above, but be sure the
        resulting Deferred is not already fired.
        c��tj�}tjd|jtd��|S)Nro�spam)rr?rrq�errbackryrrs rrYzNReactorThreadsTests.test_asyncErrorBlockingCallFromThread.<locals>.reactorFunc�s.����� �A����c�1�9�9�l�6�.B�C��Hrc����j|ddt��j|ddjdd�y)Nr{rr�r|rgs �rr3zEReactorThreadsTests.test_asyncErrorBlockingCallFromThread.<locals>.cb�s<����!�!�#�a�&��)�\�:����S��V�A�Y�^�^�A�.��7rrjrks`  r�%test_asyncErrorBlockingCallFromThreadz9ReactorThreadsTests.test_asyncErrorBlockingCallFromThread�s+���	�
	8��/�/��<�H�H��L�LrN)�__name__�
__module__�__qualname__�__doc__rr r7rBrPr`rlrurr�rGrrrrs>��
�4�4�5�.5�,5�:C�8
M�M� M�Mrrc��eZdZdZdZd�Zy)�Counterrc�n�|jdz}||jdzk7r
d|_t�||_y)zA non thread-safe method.r{N)�index�problem�
ValueError)r�nexts  r�addzCounter.add�s5���z�z�A�~���4�:�:��>�!��D�L���
��
rN)r�r�r�r�r�r�rGrrr�r��s��
�E��G�rr�c�4�eZdZdZd�Zd�Zd�Zd�Zd�Zd�Z	y)	�DeferredResultTestsz(
    Test twisted.internet.threads.
    c�.�tjd�y)N��rrrs r�setUpzDeferredResultTests.setUp�����%�%�a�(rc�.�tjd�y)Nrr�rs r�tearDownzDeferredResultTests.tearDown�r�rc������g�d�tj������fd�}tjt	��D�cgc]}�j
|fif��c}tj|fifgz��Scc}w)zW
        L{threads.callMultipleInThread} calls multiple functions in a thread.
        �
c�p���j�tt�����jd�yr$)r�listrJr<)�L�Nrsrs����r�finishedz7DeferredResultTests.test_callMultiple.<locals>.finisheds'������Q��U�1�X��/�
�J�J�t�r)rr?r	�callMultipleInThreadrJr%rr;)rr�rMr�r�rss`  @@@r�test_callMultiplez%DeferredResultTests.test_callMultiple�su���
�����N�N���	�	�$�$�+0��8�4�a�a�h�h���b�
!�4��&�&���R�8�9�
:�	
����
5s�A7c�r�tjdd�dd��}|j|jd�|S)z�
        L{threads.deferToThread} executes the function passed, and correctly
        handles the positional and keyword arguments given.
        c��||zSr$rG��x�ys  rrHz9DeferredResultTests.test_deferredResult.<locals>.<lambda>s
���Q��r�r�r����)r	rr5r�rrss  r�test_deferredResultz'DeferredResultTests.test_deferredResults2��

�!�!�"6��Q�?��	�
�
�d�&�&��*��rc�z��Gd�dt���fd�}tj|�}|j|��S)z�
        Check that L{threads.deferToThread} return a failure object
        with an appropriate exception instance when the called
        function raises an exception.
        c��eZdZy)�:DeferredResultTests.test_deferredFailure.<locals>.NewErrorN�r�r�r�rGrr�NewErrorr����rr�c������r$rG�r�s�r�
raiseErrorz<DeferredResultTests.test_deferredFailure.<locals>.raiseError�����*�r)rUr	r�
assertFailure�rr�rsr�s   @r�test_deferredFailurez(DeferredResultTests.test_deferredFailures9���	�y�	�	�
�!�!�*�-���!�!�!�X�.�.rc�~�tjd��}|jd��|j|t�S)z�
        Check that a successful L{threads.deferToThread} followed by a one
        that raises an exception correctly result as a failure.
        c��yr$rGrGrrrHzFDeferredResultTests.test_deferredFailureAfterSuccess.<locals>.<lambda>3rIrc�.�tjd��S)Nc��ddzS)Nr{rrGrGrrrHzXDeferredResultTests.test_deferredFailureAfterSuccess.<locals>.<lambda>.<locals>.<lambda>4s
���Q��r)r	r)r2s rrHzFDeferredResultTests.test_deferredFailureAfterSuccess.<locals>.<lambda>4s��'�"7�"7��"G�r)r	rr5r��ZeroDivisionErrorr�s  r� test_deferredFailureAfterSuccessz4DeferredResultTests.test_deferredFailureAfterSuccess$s5��
�!�!�,�/��	�
�
�G�H��!�!�!�%6�7�7rN)
r�r�r�r�r�r�r�r�r�r�rGrrr�r��s%��
�)�)��$�/� 8rr�c�(�eZdZdZd�Zd�Zd�Zd�Zy)�DeferToThreadPoolTestsz=
    Test L{twisted.internet.threads.deferToThreadPool}.
    c�n�tjdd�|_|jj�y)Nrr�)r
�
ThreadPool�tp�startrs rr�zDeferToThreadPoolTests.setUp=s#���'�'��1�-������
�
�rc�8�|jj�yr$)r��stoprs rr�zDeferToThreadPoolTests.tearDownAs�������rc��tjt|jdd�dd��}|j	|j
d�|S)z�
        L{threads.deferToThreadPool} executes the function passed, and
        correctly handles the positional and keyword arguments given.
        c��||zSr$rGr�s  rrHz<DeferToThreadPoolTests.test_deferredResult.<locals>.<lambda>Is
��q�1�u�rr�rr�r�r�)r	�deferToThreadPoolrr�r5rr�s  rr�z*DeferToThreadPoolTests.test_deferredResultDs;��

�%�%�g�t�w�w�8L�a�ST�U��	�
�
�d�&�&��*��rc���Gd�dt���fd�}tjt|j|�}|j|��S)z�
        Check that L{threads.deferToThreadPool} return a failure object with an
        appropriate exception instance when the called function raises an
        exception.
        c��eZdZy)�=DeferToThreadPoolTests.test_deferredFailure.<locals>.NewErrorNr�rGrrr�r�Tr�rr�c������r$rGr�s�rr�z?DeferToThreadPoolTests.test_deferredFailure.<locals>.raiseErrorWr�r)rUr	r�rr�r�r�s   @rr�z+DeferToThreadPoolTests.test_deferredFailureMsA���	�y�	�	�
�%�%�g�t�w�w�
�C���!�!�!�X�.�.rN)r�r�r�r�r�r�r�r�rGrrr�r�8s������/rr�a_
import time
import %(reactor)s
%(reactor)s.install()

from twisted.internet import reactor

def threadedCall():
    print('threaded call')

reactor.callInThread(threadedCall)

# Spin very briefly to try to give the thread a chance to run, if it
# is going to.  Is there a better way to achieve this behavior?
for i in range(100):
    time.sleep(0.0)
c�$�eZdZd�Zd�Zd�Zd�Zy)�ThreadStartupProcessProtocolc�.�||_g|_g|_yr$)r��out�err)rr�s  r�__init__z%ThreadStartupProcessProtocol.__init__rs�� ��
������rc�:�|jj|�yr$)r�r%)rr�s  r�outReceivedz(ThreadStartupProcessProtocol.outReceivedw���������rc�:�|jj|�yr$)r�r%)rr�s  r�errReceivedz(ThreadStartupProcessProtocol.errReceivedzr�rc�h�|jj|j|j|f�yr$)r�r<r�r�)r�reasons  r�processEndedz)ThreadStartupProcessProtocol.processEnded}s#���
�
������$�(�(�F�;�<rN)r�r�r�r�r�r�r�rGrrr�r�qs���
��=rr�z7No process support, cannot run subprocess thread tests.c��eZdZdZd�Zy)�StartupBehaviorTestsa
    Test cases for the behavior of the reactor threadpool near startup
    boundary conditions.

    In particular, this asserts that no threaded calls are attempted
    until the reactor starts up, that calls attempted before it starts
    are in fact executed once it has started, and that in both cases,
    the reactor properly cleans itself up (which is tested for
    somewhat implicitly, by requiring a child process be able to exit,
    something it cannot do unless the threadpool has been properly
    torn down).
    c�����j�}t|d�5}|jtdtj
iz�ddd��fd�}�fd�}tjj�}tjjtj�|d<tj�j||�}t!|��t	j"�tj$d|f|�|S#1swY��xYw)N�wrc����|\}}}|jtj�r�jd|�d|�d��|rt	j
d|����j
|d|���y)Nz#Process did not exit cleanly (out: z err: �)z%Unexpected output on standard error: z&Expected no output, instead received:
)�checkr�ProcessTerminatedr1r�msg�assertFalse)r(r�r�r�rs    �r�programFinishedzMStartupBehaviorTests.testCallBeforeStartupUnexecuted.<locals>.programFinished�sl���!'��S�#�v��|�|�E�3�3�4��	�	�?��u�F�3�%�q�Q�R�����?��u�E�F����S�$K�C�5�"Q�Rrc�h��|jtj��jd�|S)N�KILL)�trapr�TimeoutError�
signalProcess)r��protos �r�programTimeoutzLStartupBehaviorTests.testCallBeforeStartupUnexecuted.<locals>.programTimeout�s(����H�H�U�'�'�(�����'��Jr�
PYTHONPATH�python)�mktemp�open�write�_callBeforeStartupProgramrr��os�environ�copy�pathsep�join�sys�pathrr?�addCallbacksr��spawnProcess�
executable)r�progname�progfiler�r��envrsr�s`      @r�testCallBeforeStartupUnexecutedz4StartupBehaviorTests.testCallBeforeStartupUnexecuted�s�����;�;�=��
�(�C�
 �	X�H��N�N�4�	�7�CU�CU�7V�V�W�	X�	S�	�
�j�j�o�o����J�J�O�O�C�H�H�5��L���N�N��)�)�/�>�J��,�Q�/�����U�C�N�N�X�x�4H�#�N���-	X�	X�s�)D�D
N)r�r�r�r�rrGrrr�r��s���rr�) r�r�r�r�unittestr�twisted.internetrrrrrr	�twisted.pythonr
rrr
�twisted.trial.unittestrr,�ImportError�IReactorThreadsrr�r�r�r��ProcessProtocolr��IReactorProcessr�rGrr�<module>rsO��
�

�
���Q�Q�?�?�+�	��
�"�
�"�"�7�D�1�1�.��xM�(�xM�	�xM�v��$�"�
�"�"�7�D�1�1�.��G8�(�G8�	�G8�T#/�X�#/�L��&
=�8�#;�#;�
=� �"�
�"�"�7�D�1�1�.���"�
�"�"�7�D�1�1�=��&�8�&�	�	�&��i�	��	�s�D�D
�	D


Zerion Mini Shell 1.0