§
    Ètºf�V  ã                   óR  — d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
mZmZmZ ddlmZ  G d„ d¦  «        Z ej        e¦  «          G d„ d	ej        ¦  «        Z G d
„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ d¦  «        Z G d„ dej        ¦  «        ZdS )z(
Tests for L{twisted.python.threadpool}
é    N)ÚTeamÚcreateMemoryWorker)ÚcontextÚfailureÚ
threadableÚ
threadpool)Úunittestc                   ó$   — e Zd ZdZd„ Zd„ ZdgZdS )ÚSynchronizationr   c                 ó`   — || _         || _        t          j        ¦   «         | _        g | _        d S ©N)ÚNÚwaitingÚ	threadingÚLockÚlockÚruns)Úselfr   r   s      ú[/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/twisted/test/test_threadpool.pyÚ__init__zSynchronization.__init__   s*   € ØˆŒØˆŒÝ”NÑ$Ô$ˆŒ	ØˆŒ	ˆ	ˆ	ó    c                 óè  — | j                              d¦  «        rEt          | j        ¦  «        dz  st	          j        d¦  «         | j                              ¦   «          n| xj        dz  c_        | j                              ¦   «          | j                             d ¦  «         t          | j        ¦  «        | j	        k    r| j
                             ¦   «          | j                              ¦   «          d S )NFé   g-Cëâ6*?é   )r   ÚacquireÚlenr   ÚtimeÚsleepÚreleaseÚfailuresÚappendr   r   ©r   s    r   ÚrunzSynchronization.run   sÙ   € ð Œ9×Ò˜UÑ#Ô#ð 	Ý�t”y‘>”> AÑ%ð #õ ”
˜6Ñ"Ô"Ð"ØŒI×ÒÑÔÐÐàˆMŒM˜QÑˆMŒMð
 	Œ	×ÒÑÔÐØŒ	×Ò˜ÑÔÐÝˆtŒy‰>Œ>˜TœVÒ#Ð#ØŒL× Ò Ñ"Ô"Ð"ØŒ	×ÒÑÔÐÐÐr   r#   N)Ú__name__Ú
__module__Ú__qualname__r    r   r#   Úsynchronized© r   r   r   r      s:   € € € € € Ø€Hðð ð ðð ð ð0 �7€L€L€Lr   r   c                   ó†   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zdd„ZdS )ÚThreadPoolTestsz
    Test threadpools.
    c                 ó   — dS )zD
        Return number of seconds to wait before giving up.
        r   r(   r"   s    r   Ú
getTimeoutzThreadPoolTests.getTimeout@   ó	   € ð ˆqr   c                 ó°   — t          d¦  «        }|D ].}|                     d¦  «        r d S t          j        d¦  «         Œ/|                      d¦  «         d S )Ni@B Fgñhãˆµøä>z%A long time passed without succeeding)Úranger   r   r   Úfail)r   r   ÚitemsÚis       r   Ú_waitForLockzThreadPoolTests._waitForLockF   sc   € Ý�g‘”ˆØð 	?ð 	?ˆAØ�|Š|˜EÑ"Ô"ð Ø��ÝŒJ�tÑÔÐÐà�IŠIÐ=Ñ>Ô>Ð>Ð>Ð>r   c                 óœ   — t          j        dd¦  «        }|                      |j        d¦  «         |                      |j        d¦  «         dS )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   Úpools     r   Útest_attributeszThreadPoolTests.test_attributesO   sL   € õ
 Ô$ R¨Ñ,Ô,ˆØ×Ò˜œ 2Ñ&Ô&Ð&Ø×Ò˜œ 2Ñ&Ô&Ð&Ð&Ð&r   c                 ó  — t          j        dd¦  «        }|                     ¦   «          |                      |j        ¦  «         |                      t          |j        ¦  «        d¦  «         t          j        dd¦  «        }|                      t          |j        ¦  «        d¦  «         |                     ¦   «          |                      |j        ¦  «         |                      t          |j        ¦  «        d¦  «         dS )zV
        L{ThreadPool.start} creates the minimum number of threads specified.
        r   r   é   é
   N)r   r7   ÚstartÚ
addCleanupÚstopr8   r   Úthreadsr;   s     r   Ú
test_startzThreadPoolTests.test_startX   sÐ   € õ Ô$ Q¨Ñ*Ô*ˆØ�
Š
‰ŒˆØ�Š˜œ	Ñ"Ô"Ð"Ø×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.åÔ$ Q¨Ñ+Ô+ˆØ×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.Ø�
Š
‰ŒˆØ�Š˜œ	Ñ"Ô"Ð"Ø×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.Ð.Ð.r   c                 óú   — t          j        dd¦  «        }|                     ¦   «          |                     ¦   «          |                     d¦  «         |                      t          |j        ¦  «        d¦  «         dS )z�
        L{ThreadPool.adjustPoolsize} only modifies the pool size and does not
        start new workers while the pool is not running.
        r   r   é   N)r   r7   rA   rC   ÚadjustPoolsizer8   r   rD   r;   s     r   Útest_adjustingWhenPoolStoppedz-ThreadPoolTests.test_adjustingWhenPoolStoppedg   sj   € õ
 Ô$ Q¨Ñ*Ô*ˆØ�
Š
‰ŒˆØ�	Š	‰ŒˆØ×Ò˜AÑÔÐØ×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.Ð.Ð.r   c                 ó¸  — t          j        dd¦  «        }|                     ¦   «          |                      |j        ¦  «         |                      |j        g ¦  «         d„ } G d„ d¦  «        } |¦   «         }t          j        |¦  «        }t          j        |¦  «        }| 	                    ||¦  «         t          j        ¦   «         }| 	                    |j        ¦  «         |                     |                      ¦   «         ¦  «         ~~t          j        ¦   «          |                       |¦   «         ¦  «         |                       |¦   «         ¦  «         dS )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.
        r   r   c                 ó   — d S r   r(   )Úargs    r   Úworkerz<ThreadPoolTests.test_threadCreationArguments.<locals>.worker�   s   € ØˆDr   c                   ó   — e Zd ZdS )ú:ThreadPoolTests.test_threadCreationArguments.<locals>.DumbN©r$   r%   r&   r(   r   r   ÚDumbrO   …   ó   € € € € € ØˆDr   rQ   N)r   r7   rA   rB   rC   r8   rD   ÚweakrefÚrefÚcallInThreadr   ÚEventÚsetÚwaitr,   ÚgcÚcollectÚassertIsNone)r   ÚtprM   rQ   ÚuniqueÚ	workerRefÚ	uniqueRefÚevents           r   Útest_threadCreationArgumentsz,ThreadPoolTests.test_threadCreationArgumentsr   sS  € õ Ô" 1 aÑ(Ô(ˆØ
�Š‰
Œ
ˆ
Ø�Š˜œÑ Ô Ð ð 	×Ò˜œ RÑ(Ô(Ð(ð	ð 	ð 	ð	ð 	ð 	ð 	ð 	ñ 	ô 	ð 	ð �‘”ˆå”K Ñ'Ô'ˆ	Ý”K Ñ'Ô'ˆ	ð 	�Š˜ Ñ'Ô'Ð'õ ”Ñ!Ô!ˆØ
�Š˜œ	Ñ"Ô"Ð"Ø�
Š
�4—?’?Ñ$Ô$Ñ%Ô%Ð%àØÝ
Œ
‰ŒˆØ×Ò˜)˜)™+œ+Ñ&Ô&Ð&Ø×Ò˜)˜)™+œ+Ñ&Ô&Ð&Ð&Ð&r   c                 óH  ‡ ‡‡‡‡	‡
‡‡— t          j        dd¦  «        }|                     ¦   «          ‰                      |j        ¦  «         ‰                      |j        g ¦  «         i Š	t          j        ¦   «         Št          j        ¦   «         Šg Š
ˆˆˆ	ˆ
ˆ ˆˆfd„}ˆfd„} G d„ d¦  «        Š ‰¦   «         }t          j
        |¦  «        }t          j
        |¦  «        Št          j
        |¦  «        Š|                     ||||¬¦  «         ~~‰                     ¦   «          ‰                     ‰                      ¦   «         ¦  «         t          j        ¦   «          ‰                       ‰¦   «         ¦  «         ‰                       ‰¦   «         ¦  «         ~t          j        ¦   «          ‰                       |¦   «         ¦  «         ‰                       ‰
d         ¦   «         ¦  «         ‰                      t%          ‰	                     ¦   «         ¦  «        ddg¦  «         dS )	ze
        As C{test_threadCreationArguments} above, but for
        callInThreadWithCallback.
        r   r   c                 ó&  •— t          j        ¦   «          ‰                     ‰                     ¦   «         ¦  «          ‰¦   «         ‰d<    ‰¦   «         ‰d<   ‰                     ¦   «          ‰                     t          j        |¦  «        ¦  «         d S )Nr^   r_   )rY   rZ   rX   r,   rW   r!   rS   rT   )	ÚsuccessÚresultÚonResultDoneÚonResultWaitÚrefdictÚ	resultRefr   r_   r^   s	     €€€€€€€r   ÚonResultzVThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.onResult²   s„   ø€ õ ŒJ‰LŒLˆLØ×Ò˜dŸošoÑ/Ô/Ñ0Ô0Ð0Ø#, 9¡;¤;ˆG�KÑ Ø#, 9¡;¤;ˆG�KÑ Ø×ÒÑÔÐØ×Ò�Wœ[¨Ñ0Ô0Ñ1Ô1Ð1Ð1Ð1r   c                 ó   •—  ‰¦   «         S r   r(   )rL   ÚtestrQ   s     €r   rM   zTThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.worker½   s   ø€ Ø�4‘6”6ˆMr   c                   ó   — e Zd ZdS )úRThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.DumbNrP   r(   r   r   rQ   rn   Á   rR   r   rQ   )rl   N)r   r7   rA   rB   rC   r8   rD   r   rV   rS   rT   ÚcallInThreadWithCallbackrW   rX   r,   rY   rZ   r[   ÚlistÚvalues)r   r\   rj   rM   r]   ÚonResultRefrQ   rf   rg   rh   ri   r_   r^   s   `     @@@@@@@r   Ú4test_threadCreationArgumentsCallInThreadWithCallbackzDThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallbackœ   sC  øøøøøøøø€ õ Ô" 1 aÑ(Ô(ˆØ
�Š‰
Œ
ˆ
Ø�Š˜œÑ Ô Ð ð 	×Ò˜œ RÑ(Ô(Ð(ð ˆå ”Ñ(Ô(ˆÝ ”Ñ(Ô(ˆàˆ	ð	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð	ð 	ð 	ð 	ð 	ð	ð 	ð 	ð 	ð 	ñ 	ô 	ð 	ð �‘”ˆå”k (Ñ+Ô+ˆÝ”K Ñ'Ô'ˆ	Ý”K Ñ'Ô'ˆ	ð 	×#Ò# H¨f°fÀ6Ð#ÑJÔJÐJàØð 	×ÒÑÔÐà×Ò˜$Ÿ/š/Ñ+Ô+Ñ,Ô,Ð,Ý
Œ
‰Œˆà×Ò˜)˜)™+œ+Ñ&Ô&Ð&Ø×Ò˜)˜)™+œ+Ñ&Ô&Ð&ð Ý
Œ
‰ŒˆØ×Ò˜+˜+™-œ-Ñ(Ô(Ð(Ø×Ò˜,˜) Aœ,™.œ.Ñ)Ô)Ð)ð 	×Ò�˜gŸnšnÑ.Ô.Ñ/Ô/°$¸°Ñ>Ô>Ð>Ð>Ð>r   c                 óT  — t          j        dd¦  «        }|                      |j        d¦  «         |                      |j        d¦  «         t          j        t          j        |¦  «        ¦  «        }|                      |j        d¦  «         |                      |j        d¦  «         dS )z…
        Threadpools can be pickled and unpickled, which should preserve the
        number of threads and other parameters.
        é   é   N)r   r7   r8   r9   r:   ÚpickleÚloadsÚdumps)r   r<   Úcopys      r   Útest_persistencez ThreadPoolTests.test_persistenceæ   s—   € õ
 Ô$ Q¨Ñ+Ô+ˆà×Ò˜œ 1Ñ%Ô%Ð%Ø×Ò˜œ 2Ñ&Ô&Ð&õ Œ|�FœL¨Ñ.Ô.Ñ/Ô/ˆà×Ò˜œ 1Ñ%Ô%Ð%Ø×Ò˜œ 2Ñ&Ô&Ð&Ð&Ð&r   c                 ó¨  — d}t          j        ¦   «         }|                     ¦   «          |                      |j        ¦  «         t          j        ¦   «         }|                     ¦   «          t          ||¦  «        }t          |¦  «        D ]} |||¦  «         Œ|  
                    |¦  «         |                      |j        d|j        › d�¦  «         dS )z 
        Test synchronization of calls made with C{method}, which should be
        one of the mechanisms of the threadpool to execute work in threads.
        r@   zrun() re-entered z timesN)r   r7   rA   rB   rC   r   r   r   r   r/   r3   ÚassertFalser    )r   Úmethodr   r\   r   Úactorr2   s          r   Ú_threadpoolTestzThreadPoolTests._threadpoolTestö   sÎ   € ð ˆåÔ"Ñ$Ô$ˆØ
�Š‰
Œ
ˆ
Ø�Š˜œÑ Ô Ð å”.Ñ"Ô"ˆØ�ŠÑÔÐÝ  7Ñ+Ô+ˆå�q‘”ð 	ð 	ˆAØˆF�2�uÑÔÐÐà×Ò˜'Ñ"Ô"Ð"à×Ò˜œÐ)S¸U¼^Ð)SÐ)SÐ)SÑTÔTÐTÐTÐTr   c                 ó.   — |                       d„ ¦  «        S )z?
        Call C{_threadpoolTest} with C{callInThread}.
        c                 ó6   — |                       |j        ¦  «        S r   )rU   r#   )r\   r   s     r   ú<lambda>z3ThreadPoolTests.test_callInThread.<locals>.<lambda>  s   € °b·o²oÀeÄiÑ6PÔ6P€ r   )r€   r"   s    r   Útest_callInThreadz!ThreadPoolTests.test_callInThread  s   € ð ×#Ò#Ð$PÐ$PÑQÔQÐQr   c                 óL  ‡—  G d„ dt           ¦  «        Šˆfd„}t          j        dd¦  «        }|                     |¦  «         |                     ¦   «          |                     ¦   «          |                      ‰¦  «        }|                      t          |¦  «        d¦  «         dS )zi
        L{ThreadPool.callInThread} logs exceptions raised by the callable it
        is passed.
        c                   ó   — e Zd ZdS )ú<ThreadPoolTests.test_callInThreadException.<locals>.NewErrorNrP   r(   r   r   ÚNewErrorr‡     rR   r   rˆ   c                  ó   •—  ‰ ¦   «         ‚r   r(   ©rˆ   s   €r   Ú
raiseErrorz>ThreadPoolTests.test_callInThreadException.<locals>.raiseError  ó   ø€ Ø�(‘*”*Ðr   r   r   N)	Ú	Exceptionr   r7   rU   rA   rC   ÚflushLoggedErrorsr8   r   )r   r‹   r\   Úerrorsrˆ   s       @r   Útest_callInThreadExceptionz*ThreadPoolTests.test_callInThreadException  s»   ø€ ð	ð 	ð 	ð 	ð 	•yñ 	ô 	ð 	ð	ð 	ð 	ð 	ð 	õ Ô" 1 aÑ(Ô(ˆØ
�Š˜
Ñ#Ô#Ð#Ø
�Š‰
Œ
ˆ
Ø
�Š‰	Œ	ˆ	à×'Ò'¨Ñ1Ô1ˆØ×Ò�˜V™œ aÑ(Ô(Ð(Ð(Ð(r   c                 óÞ  ‡‡— t          j        ¦   «         Š‰                     ¦   «          g Šˆˆfd„}t          j        dd¦  «        }|                     |d„ ¦  «         |                     ¦   «          	 |                      ‰¦  «         |                     ¦   «          n# |                     ¦   «          w xY w|  	                    ‰d         ¦  «         |  
                    ‰d         d¦  «         dS )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                 ó„   •— ‰                      ¦   «          ‰                     | ¦  «         ‰                     |¦  «         d S r   ©r   r!   ©rd   re   ÚresultsÚwaiters     €€r   rj   z?ThreadPoolTests.test_callInThreadWithCallback.<locals>.onResult5  ó=   ø€ Ø�NŠNÑÔÐØ�NŠN˜7Ñ#Ô#Ð#Ø�NŠN˜6Ñ"Ô"Ð"Ð"Ð"r   r   r   c                  ó   — dS )Nrl   r(   r(   r   r   rƒ   z?ThreadPoolTests.test_callInThreadWithCallback.<locals>.<lambda>;  s   € °f€ r   rl   N)r   r   r   r   r7   ro   rA   r3   rC   Ú
assertTruer8   )r   rj   r\   r•   r–   s      @@r   Útest_callInThreadWithCallbackz-ThreadPoolTests.test_callInThreadWithCallback*  sï   øø€ õ ”Ñ!Ô!ˆØ�ŠÑÔÐàˆð	#ð 	#ð 	#ð 	#ð 	#ð 	#õ
 Ô" 1 aÑ(Ô(ˆØ
×#Ò# H¨n¨nÑ=Ô=Ð=Ø
�Š‰
Œ
ˆ
ð	Ø×Ò˜fÑ%Ô%Ð%à�GŠG‰IŒIˆIˆIøˆB�GŠG‰IŒIˆIˆIøøøà�Š˜ œ
Ñ#Ô#Ð#Ø×Ò˜ œ VÑ,Ô,Ð,Ð,Ð,s   Á3B ÂB3c                 ó~  ‡‡‡—  G d„ dt           ¦  «        Šˆfd„}t          j        ¦   «         Š‰                     ¦   «          g Šˆˆfd„}t	          j        dd¦  «        }|                     ||¦  «         |                     ¦   «          	 |                      ‰¦  «         | 	                    ¦   «          n# | 	                    ¦   «          w xY w|  
                    ‰d         ¦  «         |                      ‰d         t          j        ¦  «         |                      t          ‰d         j        ‰¦  «        ¦  «         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                   ó   — e Zd ZdS )úRThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.NewErrorNrP   r(   r   r   rˆ   r�   M  rR   r   rˆ   c                  ó   •—  ‰ ¦   «         ‚r   r(   rŠ   s   €r   r‹   zTThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.raiseErrorP  rŒ   r   c                 ó„   •— ‰                      ¦   «          ‰                     | ¦  «         ‰                     |¦  «         d S r   r“   r”   s     €€r   rj   zRThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.onResultX  r—   r   r   r   N)r�   r   r   r   r   r7   ro   rA   r3   rC   r}   ÚassertIsInstancer   ÚFailurer™   Ú
issubclassÚtype)r   r‹   rj   r\   rˆ   r•   r–   s       @@@r   Ú0test_callInThreadWithCallbackExceptionInCallbackz@ThreadPoolTests.test_callInThreadWithCallbackExceptionInCallbackF  sW  øøø€ ð	ð 	ð 	ð 	ð 	•yñ 	ô 	ð 	ð	ð 	ð 	ð 	ð 	õ ”Ñ!Ô!ˆØ�ŠÑÔÐàˆð	#ð 	#ð 	#ð 	#ð 	#ð 	#õ
 Ô" 1 aÑ(Ô(ˆØ
×#Ò# H¨jÑ9Ô9Ð9Ø
�Š‰
Œ
ˆ
ð	Ø×Ò˜fÑ%Ô%Ð%à�GŠG‰IŒIˆIˆIøˆB�GŠG‰IŒIˆIˆIøøøà×Ò˜ œÑ$Ô$Ð$Ø×Ò˜g aœj­'¬/Ñ:Ô:Ð:Ø�Š�
 7¨1¤:¤?°HÑ=Ô=Ñ>Ô>Ð>Ð>Ð>s   ÂB5 Â5Cc                 ó¦  ‡‡—  G d„ dt           ¦  «        Št          j        ¦   «         }|                     ¦   «          g Šˆˆfd„}t	          j        dd¦  «        }|                     |d„ ¦  «         |                     |j        ¦  «         | 	                    ¦   «          	 |  
                    |¦  «         |                     ¦   «          n# |                     ¦   «          w xY w|                      ‰¦  «        }|                      t          |¦  «        d¦  «         |                      ‰d         ¦  «         |                      ‰d         ¦  «         dS )zj
        L{ThreadPool.callInThreadWithCallback} logs the exception raised by
        C{onResult}.
        c                   ó   — e Zd ZdS )úRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.NewErrorNrP   r(   r   r   rˆ   r§   p  rR   r   rˆ   c                 ól   •— ‰                      | ¦  «         ‰                      |¦  «          ‰¦   «         ‚r   )r!   )rd   re   rˆ   r•   s     €€r   rj   zRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.onResultx  s3   ø€ Ø�NŠN˜7Ñ#Ô#Ð#Ø�NŠN˜6Ñ"Ô"Ð"Ø�(‘*”*Ðr   r   r   c                  ó   — d S r   r(   r(   r   r   rƒ   zRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.<lambda>~  s   € °d€ r   N)r�   r   r   r   r   r7   ro   rU   r   rA   r3   rC   rŽ   r8   r   r™   r[   )r   r–   rj   r\   r�   rˆ   r•   s        @@r   Ú0test_callInThreadWithCallbackExceptionInOnResultz@ThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResultj  sZ  øø€ ð	ð 	ð 	ð 	ð 	•yñ 	ô 	ð 	õ ”Ñ!Ô!ˆØ�ŠÑÔÐàˆð	ð 	ð 	ð 	ð 	ð 	õ
 Ô" 1 aÑ(Ô(ˆØ
×#Ò# H¨l¨lÑ;Ô;Ð;Ø
�Š˜œÑ'Ô'Ð'Ø
�Š‰
Œ
ˆ
ð	Ø×Ò˜fÑ%Ô%Ð%à�GŠG‰IŒIˆIˆIøˆB�GŠG‰IŒIˆIˆIøøøà×'Ò'¨Ñ1Ô1ˆØ×Ò�˜V™œ aÑ(Ô(Ð(à�Š˜ œ
Ñ#Ô#Ð#Ø×Ò˜' !œ*Ñ%Ô%Ð%Ð%Ð%s   Â C
 Ã
C c                 óÔ  ‡‡— g Št          j        ¦   «         Šˆˆfd„}ˆfd„}t          j        dd¦  «        }|                     ||¦  «         |                     ¦   «          |                      |j        ¦  «         ‰                     |  	                    ¦   «         ¦  «         |  
                    t          ‰¦  «        d¦  «         |  
                    ‰d         ‰d         ¦  «         dS )z�
        L{ThreadPool.callInThreadWithCallback} calls the function it is
        given and the C{onResult} callback in the same thread.
        c                 ó†   •— ‰                      t          j        ¦   «         j        ¦  «         ‰                     ¦   «          d S r   )r!   r   Úcurrent_threadÚidentrW   )rd   re   r`   Ú	threadIdss     €€r   rj   z5ThreadPoolTests.test_callbackThread.<locals>.onResult–  s4   ø€ Ø×Ò�YÔ5Ñ7Ô7Ô=Ñ>Ô>Ð>Ø�IŠI‰KŒKˆKˆKˆKr   c                  ó^   •— ‰                       t          j        ¦   «         j        ¦  «         d S r   )r!   r   r­   r®   )r¯   s   €r   Úfuncz1ThreadPoolTests.test_callbackThread.<locals>.funcš  s)   ø€ Ø×Ò�YÔ5Ñ7Ô7Ô=Ñ>Ô>Ð>Ð>Ð>r   r   r   rG   N)r   rV   r   r7   ro   rA   rB   rC   rX   r,   r8   r   )r   rj   r±   r\   r`   r¯   s       @@r   Útest_callbackThreadz#ThreadPoolTests.test_callbackThread�  sö   øø€ ð
 ˆ	å”Ñ!Ô!ˆð	ð 	ð 	ð 	ð 	ð 	ð	?ð 	?ð 	?ð 	?ð 	?õ Ô" 1 aÑ(Ô(ˆØ
×#Ò# H¨dÑ3Ô3Ð3Ø
�Š‰
Œ
ˆ
Ø�Š˜œÑ Ô Ð à�
Š
�4—?’?Ñ$Ô$Ñ%Ô%Ð%Ø×Ò�˜Y™œ¨Ñ+Ô+Ð+Ø×Ò˜ 1œ y°¤|Ñ4Ô4Ð4Ð4Ð4r   c                 ó\  ‡‡— t           j                             ¦   «         j        d         }d|d<   g Št	          j        ¦   «         Šˆˆfd„}ˆfd„}t          j        dd¦  «        }|                     ||¦  «         | 	                    ¦   «          |  
                    |j        ¦  «         ‰                     |                      ¦   «         ¦  «         |                      t          ‰¦  «        d¦  «         |                      |‰d         ¦  «         |                      |‰d         ¦  «         d	S )
z±
        The context L{ThreadPool.callInThreadWithCallback} is invoked in is
        shared by the context the callable and C{onResult} callback are
        invoked in.
        éÿÿÿÿzthis must be presentÚtestingc                 ó¬   •— t           j                             ¦   «         j        d         }‰                     |¦  «         ‰                     ¦   «          d S ©Nr´   )r   ÚtheContextTrackerÚcurrentContextÚcontextsr!   rW   )rd   re   Úctxrº   r`   s      €€r   rj   z6ThreadPoolTests.test_callbackContext.<locals>.onResult³  sB   ø€ ÝÔ+×:Ò:Ñ<Ô<ÔEÀbÔIˆCØ�OŠO˜CÑ Ô Ð Ø�IŠI‰KŒKˆKˆKˆKr   c                  ó„   •— t           j                             ¦   «         j        d         } ‰                     | ¦  «         d S r·   )r   r¸   r¹   rº   r!   )r»   rº   s    €r   r±   z2ThreadPoolTests.test_callbackContext.<locals>.func¸  s7   ø€ ÝÔ+×:Ò:Ñ<Ô<ÔEÀbÔIˆCØ�OŠO˜CÑ Ô Ð Ð Ð r   r   r   rG   N)r   r¸   r¹   rº   r   rV   r   r7   ro   rA   rB   rC   rX   r,   r8   r   )r   Úmyctxrj   r±   r\   rº   r`   s        @@r   Útest_callbackContextz$ThreadPoolTests.test_callbackContext¦  s2  øø€ õ Ô)×8Ò8Ñ:Ô:ÔCÀBÔGˆØ1ˆˆiÑàˆå”Ñ!Ô!ˆð	ð 	ð 	ð 	ð 	ð 	ð
	!ð 	!ð 	!ð 	!ð 	!õ Ô" 1 aÑ(Ô(ˆØ
×#Ò# H¨dÑ3Ô3Ð3Ø
�Š‰
Œ
ˆ
Ø�Š˜œÑ Ô Ð à�
Š
�4—?’?Ñ$Ô$Ñ%Ô%Ð%à×Ò�˜X™œ¨Ñ*Ô*Ð*Ø×Ò˜ ¨¤Ñ,Ô,Ð,Ø×Ò˜ ¨¤Ñ,Ô,Ð,Ð,Ð,r   c                 ó`  — t          j        ¦   «         }|                     ¦   «          t          j        dd¦  «        }|                     |j        ¦  «         |                     ¦   «          	 |                      |¦  «         | 	                    ¦   «          dS # | 	                    ¦   «          w xY 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.
        r   r   N)
r   r   r   r   r7   rU   r   rA   r3   rC   )r   r–   r\   s      r   Útest_existingWorkz!ThreadPoolTests.test_existingWorkÇ  s�   € õ ”Ñ!Ô!ˆØ�ŠÑÔÐåÔ" 1 aÑ(Ô(ˆØ
�Š˜œÑ'Ô'Ð'Ø
�Š‰
Œ
ˆ
ð	Ø×Ò˜fÑ%Ô%Ð%à�GŠG‰IŒIˆIˆIˆIøˆB�GŠG‰IŒIˆIˆIøøøs   Á,B ÂB-c                 ó(  ‡‡— t          j        dd¦  «        }|                     ¦   «          |                      |j        ¦  «         |                      |j        d¦  «         |                      t          |j        ¦  «        d¦  «         |                      t          |j	        ¦  «        d¦  «         t          j        ¦   «         Št          j        ¦   «         Šˆˆfd„}|                     |¦  «         ‰                     d¦  «         |                      |j        d¦  «         |                      t          |j        ¦  «        d¦  «         |                      t          |j	        ¦  «        d¦  «         ‰                     ¦   «          t          |j        ¦  «        s(t          j        d¦  «         t          |j        ¦  «        ¯(|                      t          |j        ¦  «        d¦  «         |                      t          |j	        ¦  «        d¦  «         dS )z{
        As the worker receives and completes work, it transitions between
        the working and waiting states.
        r   r   c                  óZ   •— ‰                      ¦   «          ‰                      d¦  «         d S )Nr@   )rW   rX   )ÚthreadFinishÚthreadWorkings   €€r   Ú_threadz;ThreadPoolTests.test_workerStateTransition.<locals>._threadë  s/   ø€ Ø×ÒÑÔÐØ×Ò˜bÑ!Ô!Ð!Ð!Ð!r   r@   gü©ñÒMb@?N)r   r7   rA   rB   rC   r8   Úworkersr   ÚwaitersÚworkingr   rV   rU   rX   rW   r   r   )r   r<   rÅ   rÃ   rÄ   s      @@r   Útest_workerStateTransitionz*ThreadPoolTests.test_workerStateTransitionÙ  sá  øø€ õ
 Ô$ Q¨Ñ*Ô*ˆØ�
Š
‰ŒˆØ�Š˜œ	Ñ"Ô"Ð"ð 	×Ò˜œ qÑ)Ô)Ð)Ø×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.Ø×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.õ "œÑ)Ô)ˆÝ ”Ñ(Ô(ˆð	"ð 	"ð 	"ð 	"ð 	"ð 	"ð 	×Ò˜'Ñ"Ô"Ð"Ø×Ò˜2ÑÔÐØ×Ò˜œ qÑ)Ô)Ð)Ø×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.Ø×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.ð 	×ÒÑÔÐÝ�d”lÑ#Ô#ð 	ÝŒJ�vÑÔÐõ �d”lÑ#Ô#ð 	ð 	×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.Ø×Ò�˜Tœ\Ñ*Ô*¨AÑ.Ô.Ð.Ð.Ð.r   ÚreturnNc                 óŠ   — t          j        dd¦  «        }|                      |j                             ¦   «         d¦  «         dS )zB
        There is a property '_queue' for legacy purposes
        r   r   N)r   r7   r8   Ú_queueÚqsizer;   s     r   Útest_qzThreadPoolTests.test_qþ  s@   € õ Ô$ Q¨Ñ*Ô*ˆØ×Ò˜œ×*Ò*Ñ,Ô,¨aÑ0Ô0Ð0Ð0Ð0r   )rÊ   N)r$   r%   r&   Ú__doc__r,   r3   r=   rE   rI   ra   rs   r{   r€   r„   r�   rš   r¤   rª   r²   r¾   rÀ   rÉ   rÎ   r(   r   r   r*   r*   ;   sO  € € € € € ðð ðð ð ð?ð ?ð ?ð'ð 'ð 'ð/ð /ð /ð	/ð 	/ð 	/ð('ð ('ð ('ðTH?ð H?ð H?ðT'ð 'ð 'ð Uð Uð Uð4Rð Rð Rð)ð )ð )ð(-ð -ð -ð8"?ð "?ð "?ðH!&ð !&ð !&ðF5ð 5ð 5ð2-ð -ð -ðBð ð ð$#/ð #/ð #/ðJ1ð 1ð 1ð 1ð 1ð 1r   r*   c                   ó    — e Zd Zd„ Zd„ Zd„ ZdS )ÚRaceConditionTestsc                 óÒ   ‡ — t          j        dd¦  «        ‰ _         t          j        ¦   «         ‰ _        ‰ j                              ¦   «          ˆ fd„}‰                      |¦  «         d S )Nr   r@   c                  ó>   •— ‰ j                              ¦   «          ‰ ` d S r   )r   rC   r"   s   €r   Údonez&RaceConditionTests.setUp.<locals>.done  s    ø€ ØŒO× Ò Ñ"Ô"Ð"Ø���r   )r   r7   r   rV   r`   rA   rB   )r   rÔ   s   ` r   ÚsetUpzRaceConditionTests.setUp  sm   ø€ Ý$Ô/°°2Ñ6Ô6ˆŒÝ”_Ñ&Ô&ˆŒ
ØŒ×ÒÑÔÐð	 ð 	 ð 	 ð 	 ð 	 ð 	�Š˜ÑÔÐÐÐr   c                 ó   — dS )z=
        A reasonable number of seconds to time out.
        r   r(   r"   s    r   r,   zRaceConditionTests.getTimeout  r-   r   c                 óV  — |                       ¦   «         }| j                             | j        j        ¦  «         | j                             |¦  «         | j                             ¦   «          t          d¦  «        D ]&}| j                             | j        j        ¦  «         Œ'| j                             | j        j        ¦  «         | j                             |¦  «         | j                             ¦   «         s0| j                             ¦   «          |  	                    d¦  «         dS dS )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.
        r?   z9'set' did not run in thread; timed out waiting on 'wait'.N)
r,   r   rU   r`   rW   rX   Úclearr/   ÚisSetr0   )r   Útimeoutr2   s      r   Útest_synchronizationz'RaceConditionTests.test_synchronization  s  € ð" —/’/Ñ#Ô#ˆØŒ×$Ò$ T¤Z¤^Ñ4Ô4Ð4ØŒ
�Š˜Ñ Ô Ð ØŒ
×ÒÑÔÐÝ�q‘”ð 	:ð 	:ˆAØŒO×(Ò(¨¬¬Ñ9Ô9Ð9Ð9ØŒ×$Ò$ T¤Z¤^Ñ4Ô4Ð4ØŒ
�Š˜Ñ Ô Ð ØŒz×ÒÑ!Ô!ð 	SØŒJ�NŠNÑÔÐØ�IŠIÐQÑRÔRÐRÐRÐRð	Sð 	Sr   N)r$   r%   r&   rÕ   r,   rÛ   r(   r   r   rÑ   rÑ     sF   € € € € € ð	ð 	ð 	ðð ð ðSð Sð Sð Sð Sr   rÑ   c                   ó   — e Zd ZdZd„ Zd„ ZdS )Ú
MemoryPoolz
    A deterministic threadpool that uses in-memory data structures to queue
    work rather than threads to execute work.
    c                 ód   — || _         || _        || _        t          j        j        | g|¢R i |¤Ž dS )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Ú
_newWorkerr   r7   r   )r   ÚcoordinatorÚfailTestÚ	newWorkerÚargsÚkwargss         r   r   zMemoryPool.__init__<  sD   € ð$ (ˆÔØ!ˆŒØ#ˆŒÝÔÔ& tÐ=¨dÐ=Ð=Ð=°fÐ=Ð=Ð=Ð=Ð=r   c                 óR   ‡ ‡‡— ˆˆ ˆfd„}t          ‰ j        |‰ j        ¬¦  «        Š‰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__}.
        c                  óŽ   •— ‰                      ¦   «         } | j        | j        z    ‰¦   «         k    rd S ‰                     ¦   «         S r   )Ú
statisticsÚbusyWorkerCountÚidleWorkerCountrá   )ÚstatsÚcurrentLimitr   Úteams    €€€r   ÚrespectLimitz&MemoryPool._pool.<locals>.respectLimita  sE   ø€ ð —O’OÑ%Ô%ˆEØÔ%¨Ô(=Ñ=À,À,Á.Ä.ÒPÐPØ�tØ—?’?Ñ$Ô$Ð$r   )râ   ÚcreateWorkerÚlogException)r   rß   rà   )r   rí   ÚthreadFactoryrï   rî   s   ``  @r   Ú_poolzMemoryPool._poolS  sS   øøø€ ð	%ð 	%ð 	%ð 	%ð 	%ð 	%ð 	%õ ØÔ)Ø%Øœð
ñ 
ô 
ˆð
 ˆr   N)r$   r%   r&   rÏ   r   ró   r(   r   r   rÝ   rÝ   6  s<   € € € € € ðð ð
>ð >ð >ð.ð ð ð ð r   rÝ   c                   ó   — e Zd ZdZd„ Zd„ ZdS )Ú
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}
    c                 ó„   ‡ — t          ¦   «         \  }‰ _        g ‰ _        ˆ fd„}t          ||j        |g|¢R i |¤Ž‰ _        dS )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}
        c                  óv   •— ‰ j                              t          ¦   «         ¦  «         ‰ j         d         d         S )Nr´   r   )rÆ   r!   r   r"   s   €r   rä   z&PoolHelper.__init__.<locals>.newWorker‘  s2   ø€ ØŒL×ÒÕ 2Ñ 4Ô 4Ñ5Ô5Ð5Ø”< Ô# AÔ&Ð&r   N)r   ÚperformCoordinationrÆ   rÝ   r0   r   )r   ÚtestCaserå   ræ   râ   rä   s   `     r   r   zPoolHelper.__init__„  ss   ø€ õ 1CÑ0DÔ0DÑ-ˆ�TÔ-ØˆŒð	'ð 	'ð 	'ð 	'ð 	'õ %Ø˜œ¨	ð
Ø48ð
ð 
ð 
Ø<Bð
ð 
ˆŒˆˆr   c                 ó\   — |                       ¦   «         r	 |                       ¦   «         °dS dS )z‰
        Perform all currently scheduled "coordination", which is the work
        involved in delegating work to other threads.
        N)rø   r"   s    r   ÚperformAllCoordinationz!PoolHelper.performAllCoordination™  sE   € ð
 ×&Ò&Ñ(Ô(ð 	Øð ×&Ò&Ñ(Ô(ð 	ð 	ð 	ð 	ð 	r   N)r$   r%   r&   rÏ   r   rû   r(   r   r   rõ   rõ   r  s<   € € € € € ðð ð"
ð 
ð 
ð*ð ð ð ð r   rõ   c                   ó   — e Zd ZdZd„ Zd„ ZdS )ÚMemoryBackedTestszn
    Tests using L{PoolHelper} to deterministically test properties of the
    threadpool implementation.
    c                 óŽ  — t          | dd¦  «        }d}t          |¦  «        D ]}|j                             d„ ¦  «         Œ|                     ¦   «          |                      |j        g ¦  «         |j                             ¦   «          |                     ¦   «          |                      t          |j        ¦  «        |¦  «         dS )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.
        r   r@   r   c                  ó   — d S r   r(   r(   r   r   rƒ   z;MemoryBackedTests.test_workBeforeStarting.<locals>.<lambda>±  ó   € °4€ r   N)	rõ   r/   r   rU   rû   r8   rÆ   rA   r   ©r   ÚhelperÚnÚxs       r   Útest_workBeforeStartingz)MemoryBackedTests.test_workBeforeStarting¨  sÂ   € õ ˜D ! RÑ(Ô(ˆØˆÝ�q‘”ð 	9ð 	9ˆAØÔ×*Ò*¨<¨<Ñ8Ô8Ð8Ð8Ø×%Ò%Ñ'Ô'Ð'Ø×Ò˜œ¨Ñ,Ô,Ð,ØÔ×ÒÑ!Ô!Ð!Ø×%Ò%Ñ'Ô'Ð'Ø×Ò�˜Vœ^Ñ,Ô,¨aÑ0Ô0Ð0Ð0Ð0r   c                 ó¢  — t          | dd¦  «        }d}t          |¦  «        D ]}|j                             d„ ¦  «         Œ|                     ¦   «          |                      |j        g ¦  «         |j                             ¦   «          |                     ¦   «          |                      t          |j        ¦  «        |j        j	        ¦  «         dS )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.
        r   r@   é2   c                  ó   — d S r   r(   r(   r   r   rƒ   zBMemoryBackedTests.test_tooMuchWorkBeforeStarting.<locals>.<lambda>Á  r   r   N)
rõ   r/   r   rU   rû   r8   rÆ   rA   r   r:   r  s       r   Útest_tooMuchWorkBeforeStartingz0MemoryBackedTests.test_tooMuchWorkBeforeStarting¸  sÈ   € õ ˜D ! RÑ(Ô(ˆØˆÝ�q‘”ð 	9ð 	9ˆAØÔ×*Ò*¨<¨<Ñ8Ô8Ð8Ð8Ø×%Ò%Ñ'Ô'Ð'Ø×Ò˜œ¨Ñ,Ô,Ð,ØÔ×ÒÑ!Ô!Ð!Ø×%Ò%Ñ'Ô'Ð'Ø×Ò�˜Vœ^Ñ,Ô,¨fÔ.?Ô.CÑDÔDÐDÐDÐDr   N)r$   r%   r&   rÏ   r  r	  r(   r   r   rý   rý   ¢  sA   € € € € € ðð ð
1ð 1ð 1ð Eð Eð Eð Eð Er   rý   )rÏ   rY   rw   r   r   rS   Útwisted._threadsr   r   Útwisted.pythonr   r   r   r   Útwisted.trialr	   r   ÚsynchronizeÚSynchronousTestCaser*   rÑ   r7   rÝ   rõ   rý   r(   r   r   ú<module>r     sÑ  ððð ð
 
€	€	€	Ø €€€Ø Ð Ð Ð Ø €€€Ø €€€à 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ "Ð "Ð "Ð "Ð "Ð "ð!ð !ð !ð !ð !ñ !ô !ð !ðH €
Ô �Ñ 'Ô 'Ð 'ðH1ð H1ð H1ð H1ð H1�hÔ2ñ H1ô H1ð H1ðV-Sð -Sð -Sð -Sð -S˜Ô5ñ -Sô -Sð -Sð`9ð 9ð 9ð 9ð 9�Ô&ñ 9ô 9ð 9ðx-ð -ð -ð -ð -ñ -ô -ð -ð`$Eð $Eð $Eð $Eð $E˜Ô4ñ $Eô $Eð $Eð $Eð $Er   