HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/test/__pycache__/test_threads.cpython-38.pyc
U

��W[�3�@sFdZddlmZmZddlZddlZddlZddlmZddl	m
Z
ddlmZm
Z
mZmZmZmZddlmZmZmZmZGdd	�d	ej�ZGd
d�d�ZGdd
�d
ej�ZGdd�dej�ZdZGdd�dej�ZGdd�dej�Ze� ed�dk�reeefD]Z!de!_"�qnddl#Z#e�$ed�dk�rBefD]Z!de!_"�q4dS)zC
Test methods in twisted.internet.threads and reactor thread APIs.
�)�division�absolute_importN)�unittest)�range)�reactor�defer�
interfaces�threads�protocol�error)�failure�
threadable�log�
threadpoolc@s`eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�ReactorThreadsTestsz.
    Tests for the reactor threading API.
    cCs8t�d�|�tjjd�t�d�|�tjjd�dS)z:
        Try to change maximum number of threads.
        �"�N)r�suggestThreadPoolSize�assertEqualr�max��self�r�;/usr/lib/python3/dist-packages/twisted/test/test_threads.py�test_suggestThreadPoolSizes

z.ReactorThreadsTests.test_suggestThreadPoolSizecCst�tjd�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�sleeprrrr�_waitForThread$sz"ReactorThreadsTests._waitForThreadcs�fdd�}����|�S)z�
        Test callInThread functionality: set a C{threading.Event}, and check
        that it's not in the main thread.
        csTt���g���fdd�}t�|���d����sB��d�n���dg�dS)Ncs��t������dS�N)�appendr
ZisInIOThread�setr��result�waiterrr�threadedFunc5szGReactorThreadsTests.test_callInThread.<locals>.cb.<locals>.threadedFunc�xzTimed out waiting for event.F)�	threading�Eventr�callInThread�wait�isSet�failr��ignr%rr"r�cb2s

z1ReactorThreadsTests.test_callInThread.<locals>.cb�r�addCallback�rr/rrr�test_callInThread-s
z%ReactorThreadsTests.test_callInThreadcCsdd�}|���|�S)zk
        Test callFromThread functionality: from the main thread, and from
        another thread.
        csFt��}t����fdd�}t�|�t�|jd�tj|�gdd�S)Ncst��jd�dSr)r�callFromThread�callbackr�ZfiredByOtherThreadrrr%KszIReactorThreadsTests.test_callFromThread.<locals>.cb.<locals>.threadedFuncT)ZfireOnOneErrback)r�Deferredrr)r4r5ZDeferredList)r.ZfiredByReactorThreadr%rr6rr/Gs
�z3ReactorThreadsTests.test_callFromThread.<locals>.cbr0r2rrr�test_callFromThreadBs
z'ReactorThreadsTests.test_callFromThreadcs�fdd�}����|�S)zT
        Try to make an overflow on the reactor waker using callFromThread.
        cs\d�_t�����fdd�}t�|���d����sB��d��jdk	rXt��j�SdS)NcsJtd�D]4}zt�dd��Wqt���_Yq>YqXq���dS)Ni��cSsdSrrrrrr�<lambda>c�z^ReactorThreadsTests.test_wakerOverflow.<locals>.cb.<locals>.threadedFunction.<locals>.<lambda>)rrr4rZFailurer!)�i)rr$rr�threadedFunction^s
zLReactorThreadsTests.test_wakerOverflow.<locals>.cb.<locals>.threadedFunctionr&�Timed out waiting for event)	rr'r(rr)r*r+r,r)r.r<r)r$rr/[s




z2ReactorThreadsTests.test_wakerOverflow.<locals>.cbr0r2rrr�test_wakerOverflowWsz&ReactorThreadsTests.test_wakerOverflowcsJt���g�g������fdd�}����fdd�}����|��|�S)zK
        Utility method to test L{threads.blockingCallFromThread}.
        cs.����fdd�}t�|�t��j����S)Nc
sTzt�t��}Wn,tk
r<}z��|�W5d}~XYnX��|����dSr)r	ZblockingCallFromThreadr�	Exceptionr r!)�r�e)�errors�reactorFunc�resultsr$rrr%xs
zRReactorThreadsTests._testBlockingCallFromThread.<locals>.cb1.<locals>.threadedFunc)rr)r	rr*Z
getTimeoutr-�rBrCrDrr$rr�cb1ws	
z<ReactorThreadsTests._testBlockingCallFromThread.<locals>.cb1cs���s��d���fS)Nr=)r+r,�r.)rBrDrr$rr�cb2�s
z<ReactorThreadsTests._testBlockingCallFromThread.<locals>.cb2)r'r(rr1ZaddBoth)rrCrFrHrrEr�_testBlockingCallFromThreadps
z/ReactorThreadsTests._testBlockingCallFromThreadcs$dd�}�fdd�}��|��|�S)z�
        Test blockingCallFromThread facility: create a thread, call a function
        in the reactor using L{threads.blockingCallFromThread}, and verify the
        result returned.
        cSs
t�d�S)N�foo)rZsucceedrrrrrC�szDReactorThreadsTests.test_blockingCallFromThread.<locals>.reactorFunccs��|ddd�dS)NrrJ�r��resrrrr/�sz;ReactorThreadsTests.test_blockingCallFromThread.<locals>.cb�rIr1�rrCr/rrr�test_blockingCallFromThread�sz/ReactorThreadsTests.test_blockingCallFromThreadcs$dd�}�fdd�}��|��|�S)zx
        Test blockingCallFromThread as above, but be sure the resulting
        Deferred is not already fired.
        cSst��}t�d|jd�|S)N皙�����?�egg)rr7r�	callLaterr5��drrrrC�szIReactorThreadsTests.test_asyncBlockingCallFromThread.<locals>.reactorFunccs��|ddd�dS)NrrRrKrLrrrr/�sz@ReactorThreadsTests.test_asyncBlockingCallFromThread.<locals>.cbrNrOrrr� test_asyncBlockingCallFromThread�sz4ReactorThreadsTests.test_asyncBlockingCallFromThreadcs$dd�}�fdd�}��|��|�S)z?
        Test error report for blockingCallFromThread.
        cSst�td��S)N�bar)rr,�RuntimeErrorrrrrrC�szIReactorThreadsTests.test_errorBlockingCallFromThread.<locals>.reactorFunccs2��|ddt���|ddjdd�dS)N�rrW�ZassertIsInstancerXr�argsrLrrrr/�sz@ReactorThreadsTests.test_errorBlockingCallFromThread.<locals>.cbrNrOrrr� test_errorBlockingCallFromThread�sz4ReactorThreadsTests.test_errorBlockingCallFromThreadcs$dd�}�fdd�}��|��|�S)z�
        Test error report for blockingCallFromThread as above, but be sure the
        resulting Deferred is not already fired.
        cSs t��}t�d|jtd��|S)NrQ�spam)rr7rrSZerrbackrXrTrrrrC�szNReactorThreadsTests.test_asyncErrorBlockingCallFromThread.<locals>.reactorFunccs2��|ddt���|ddjdd�dS)NrYrr]rZrLrrrr/�szEReactorThreadsTests.test_asyncErrorBlockingCallFromThread.<locals>.cbrNrOrrr�%test_asyncErrorBlockingCallFromThread�sz9ReactorThreadsTests.test_asyncErrorBlockingCallFromThreadN)�__name__�
__module__�__qualname__�__doc__rrr3r8r>rIrPrVr\r^rrrrrs
	
rc@seZdZdZdZdd�ZdS)�CounterrcCs,|jd}||jdkr"d|_t�||_dS)zA non thread-safe method.rYN)�index�problem�
ValueError)r�nextrrr�add�s

zCounter.addN)r_r`rardrerhrrrrrc�srcc@s@eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dS)�DeferredResultTestsz(
    Test twisted.internet.threads.
    cCst�d�dS)N��rrrrrr�setUp�szDeferredResultTests.setUpcCst�d�dS)Nrrkrrrr�tearDown�szDeferredResultTests.tearDowncsRg�d�t�������fdd�}t��fdd�t��D�tj|fifg��S)zW
        L{threads.callMultipleInThread} calls multiple functions in a thread.
        �
cs"���tt������d�dSr)r�listrr5r��L�NrUrrr�finished�sz7DeferredResultTests.test_callMultiple.<locals>.finishedcsg|]}�j|fif�qSr)r )�.0r;)rqrr�
<listcomp>�sz9DeferredResultTests.test_callMultiple.<locals>.<listcomp>)rr7r	ZcallMultipleInThreadrrr4�rrsrrpr�test_callMultiple�s��z%DeferredResultTests.test_callMultiplecCs(tjddd�ddd�}|�|jd�|S)	z�
        L{threads.deferToThread} executes the function passed, and correctly
        handles the positional and keyword arguments given.
        �cSs||Srr��x�yrrrr9�r:z9DeferredResultTests.test_deferredResult.<locals>.<lambda>�r�r{�)rx)r	rr1r�rrUrrr�test_deferredResult�sz'DeferredResultTests.test_deferredResultcs2Gdd�dt���fdd�}t�|�}|�|��S)z�
        Check that L{threads.deferToThread} return a failure object
        with an appropriate exception instance when the called
        function raises an exception.
        c@seZdZdS)z:DeferredResultTests.test_deferredFailure.<locals>.NewErrorN�r_r`rarrrr�NewErrorsr�cs
���dSrrr�r�rr�
raiseErrorsz<DeferredResultTests.test_deferredFailure.<locals>.raiseError)r?r	r�
assertFailure�rr�rUrr�r�test_deferredFailure�s
z(DeferredResultTests.test_deferredFailurecCs(t�dd��}|�dd��|�|t�S)z�
        Check that a successful L{threads.deferToThread} followed by a one
        that raises an exception correctly result as a failure.
        cSsdSrrrrrrr9r:zFDeferredResultTests.test_deferredFailureAfterSuccess.<locals>.<lambda>cSst�dd��S)NcSsddS)NrYrrrrrrr9r:zXDeferredResultTests.test_deferredFailureAfterSuccess.<locals>.<lambda>.<locals>.<lambda>)r	rrGrrrr9r:)r	rr1r��ZeroDivisionErrorrrrr� test_deferredFailureAfterSuccesssz4DeferredResultTests.test_deferredFailureAfterSuccessN)
r_r`rarbrlrmrwr�r�r�rrrrri�s
ric@s0eZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�DeferToThreadPoolTestsz=
    Test L{twisted.internet.threads.deferToThreadPool}.
    cCst�dd�|_|j��dS)Nrrj)rZ
ThreadPool�tp�startrrrrrl&szDeferToThreadPoolTests.setUpcCs|j��dSr)r��stoprrrrrm+szDeferToThreadPoolTests.tearDowncCs.tjt|jddd�ddd�}|�|jd�|S)	z�
        L{threads.deferToThreadPool} executes the function passed, and
        correctly handles the positional and keyword arguments given.
        rxcSs||Srrryrrrr95r:z<DeferToThreadPoolTests.test_deferredResult.<locals>.<lambda>r|rr}r~)rx)r	�deferToThreadPoolrr�r1rrrrrr�/s
�z*DeferToThreadPoolTests.test_deferredResultcs8Gdd�dt���fdd�}t�t|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@seZdZdS)z=DeferToThreadPoolTests.test_deferredFailure.<locals>.NewErrorNr�rrrrr�@sr�cs
���dSrrrr�rrr�Bsz?DeferToThreadPoolTests.test_deferredFailure.<locals>.raiseError)r?r	r�rr�r�r�rr�rr�:sz+DeferToThreadPoolTests.test_deferredFailureN)r_r`rarbrlrmr�r�rrrrr�!s
r�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@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�ThreadStartupProcessProtocolcCs||_g|_g|_dSr)rs�out�errrvrrr�__init__]sz%ThreadStartupProcessProtocol.__init__cCs|j�|�dSr)r�r )rr�rrr�outReceivedbsz(ThreadStartupProcessProtocol.outReceivedcCs|j�|�dSr)r�r )rr�rrr�errReceivedesz(ThreadStartupProcessProtocol.errReceivedcCs|j�|j|j|f�dSr)rsr5r�r�)r�reasonrrr�processEndedhsz)ThreadStartupProcessProtocol.processEndedN)r_r`rar�r�r�r�rrrrr�\sr�c@seZdZdZdd�ZdS)�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	s����}t|d��}|�tdtji�W5QRX�fdd�}�fdd�}tj��}tj	�
tj�|d<t
���||�}t|��t��tjd|f|�|S)	N�wrcsR|\}}}|�tj�r(��d||f�|r<t�d|f���|d|f�dS)Nz.Process did not exit cleanly (out: %s err: %s)z'Unexpected output on standard error: %sz(Expected no output, instead received:
%s)ZcheckrZProcessTerminatedr,r�msgZassertFalse)r#r�r�r�rrr�programFinished�s
�zMStartupBehaviorTests.testCallBeforeStartupUnexecuted.<locals>.programFinishedcs|�tj���d�|S)NZKILL)Ztrapr�TimeoutErrorZ
signalProcess)r�)�protorr�programTimeout�s
zLStartupBehaviorTests.testCallBeforeStartupUnexecuted.<locals>.programTimeout�
PYTHONPATH�python)Zmktemp�open�write�_callBeforeStartupProgramrr`�os�environ�copy�pathsep�join�sys�pathrr7ZaddCallbacksr�ZspawnProcess�
executable)rZprognameZprogfiler�r��envrUr)r�rr�testCallBeforeStartupUnexecuted|s
z4StartupBehaviorTests.testCallBeforeStartupUnexecutedN)r_r`rarbr�rrrrr�msr�z(No thread support, nothing to test here.z7No process support, cannot run subprocess thread tests.)%rbZ
__future__rrr�r�rZ
twisted.trialrZtwisted.python.compatrZtwisted.internetrrrr	r
rZtwisted.pythonrr
rrZTestCaserrcrir�r�ZProcessProtocolr�r�ZIReactorThreads�cls�skipr'ZIReactorProcessrrrr�<module>s0 .L(-�