ó
    ×›‘h�V  ã                   óX  • S r SSKrSSKrSSKrSSKrSSKrSSKJrJr  SSK	J
r
JrJrJr  SSKJr   " S S5      r\R"                  " \5         " S S	\R$                  5      r " S
 S\R$                  5      r " S S\R*                  5      r " S S5      r " S S\R$                  5      rg)z(
Tests for L{twisted.python.threadpool}
é    N)ÚTeamÚcreateMemoryWorker)ÚcontextÚfailureÚ
threadableÚ
threadpool)Úunittestc                   ó*   • \ rS rSrSrS rS rS/rSrg)ÚSynchronizationé   r   c                 ó^   • Xl         X l        [        R                  " 5       U l        / U l        g ©N)ÚNÚwaitingÚ	threadingÚLockÚlockÚruns)Úselfr   r   s      Ú`C:\Users\psant\Downloads\Noiseback\school\venv\Lib\site-packages\twisted/test/test_threadpool.pyÚ__init__ÚSynchronization.__init__   s!   € ØŒØŒÜ—N’NÓ$ˆŒ	Øˆ�	ó    c                 ó"  • U R                   R                  S5      (       aN  [        U R                  5      S-  (       d  [        R
                  " S5        U R                   R                  5         OU =R                  S-  sl        U R                   R                  5         U R                  R                  S 5        [        U R                  5      U R                  :X  a  U R                  R                  5         U R                   R                  5         g )NFé   g-Cëâ6*?é   )r   ÚacquireÚlenr   ÚtimeÚsleepÚreleaseÚfailuresÚappendr   r   ©r   s    r   ÚrunÚSynchronization.run   s­   € ð �9‰9×Ñ˜U×#Ñ#Ü�t—y‘y“> A×%ô —
’
˜6Ô"Ø�I‰I×ÑÕà�MŠM˜QÑ�Mð
 	�	‰	×ÑÔØ�	‰	×Ñ˜ÔÜˆt�y‰y‹>˜TŸV™VÓ#Ø�L‰L× Ñ Ô"Ø�	‰	×ÑÕr   r%   )r   r   r   r   N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r"   r   r%   ÚsynchronizedÚ__static_attributes__© r   r   r   r      s   † Ø€Hòòð0 �7ƒLr   r   c                   óŽ   • \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rSS jrSrg)ÚThreadPoolTestsé;   z
Test threadpools.
c                 ó   • g)z4
Return number of seconds to wait before giving up.
r   r-   r$   s    r   Ú
getTimeoutÚThreadPoolTests.getTimeout@   ó   € ð r   c                 óª   • [        S5      nU H1  nUR                  S5      (       a    g [        R                  " S5        M3     U R	                  S5        g )Ni@B Fgñhãˆµøä>z%A long time passed without succeeding)Úranger   r   r    Úfail)r   r   ÚitemsÚis       r   Ú_waitForLockÚThreadPoolTests._waitForLockF   sB   € Ü�g“ˆÛˆAØ�|‰|˜E×"Ñ"ÙÜ�JŠJ�tÖñ ð
 �I‰IÐ=Õ>r   c                 ó¢   • [         R                  " SS5      nU R                  UR                  S5        U R                  UR                  S5        g)za
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_attributesÚThreadPoolTests.test_attributesO   s>   € ô
 ×$Ò$ R¨Ó,ˆØ×Ñ˜Ÿ™ 2Ô&Ø×Ñ˜Ÿ™ 2Õ&r   c                 óê  • [         R                  " SS5      nUR                  5         U R                  UR                  5        U R                  [        UR                  5      S5        [         R                  " SS5      nU R                  [        UR                  5      S5        UR                  5         U R                  UR                  5        U R                  [        UR                  5      S5        g)zF
L{ThreadPool.start} creates the minimum number of threads specified.
r   r   é   é
   N)r   r?   ÚstartÚ
addCleanupÚstopr@   r   ÚthreadsrC   s     r   Ú
test_startÚThreadPoolTests.test_startX   s¥   € ô ×$Ò$ Q¨Ó*ˆØ�
‰
ŒØ�‰˜Ÿ	™	Ô"Ø×Ñœ˜TŸ\™\Ó*¨AÔ.ä×$Ò$ Q¨Ó+ˆØ×Ñœ˜TŸ\™\Ó*¨AÔ.Ø�
‰
ŒØ�‰˜Ÿ	™	Ô"Ø×Ñœ˜TŸ\™\Ó*¨AÕ.r   c                 óÞ   • [         R                  " SS5      nUR                  5         UR                  5         UR	                  S5        U R                  [        UR                  5      S5        g)zx
L{ThreadPool.adjustPoolsize} only modifies the pool size and does not
start new workers while the pool is not running.
r   r   é   N)r   r?   rJ   rL   ÚadjustPoolsizer@   r   rM   rC   s     r   Útest_adjustingWhenPoolStoppedÚ-ThreadPoolTests.test_adjustingWhenPoolStoppedg   sO   € ô
 ×$Ò$ Q¨Ó*ˆØ�
‰
ŒØ�	‰	ŒØ×Ñ˜AÔØ×Ñœ˜TŸ\™\Ó*¨AÕ.r   c                 ó†  • [         R                  " SS5      nUR                  5         U R                  UR                  5        U R                  UR                  / 5        S n " S S5      nU" 5       n[        R                  " U5      n[        R                  " U5      nUR                  X$5        [        R                  " 5       nUR                  UR                  5        UR                  U R                  5       5        AA[        R                   " 5         U R#                  U" 5       5        U R#                  U" 5       5        g)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                 ó   • g r   r-   )Úargs    r   ÚworkerÚ<ThreadPoolTests.test_threadCreationArguments.<locals>.worker�   s   € Ør   c                   ó   • \ rS rSrSrg)Ú:ThreadPoolTests.test_threadCreationArguments.<locals>.Dumbé…   r-   N©r'   r(   r)   r*   r,   r-   r   r   ÚDumbr[   …   ó   † Úr   r^   N)r   r?   rJ   rK   rL   r@   rM   ÚweakrefÚrefÚcallInThreadr   ÚEventÚsetÚwaitr2   ÚgcÚcollectÚassertIsNone)r   ÚtprX   r^   ÚuniqueÚ	workerRefÚ	uniqueRefÚevents           r   Útest_threadCreationArgumentsÚ,ThreadPoolTests.test_threadCreationArgumentsr   sç   € ô ×"Ò" 1 aÓ(ˆØ
�‰Œ
Ø�‰˜Ÿ™Ô ð 	×Ñ˜Ÿ™ RÔ(ò	÷	ñ 	ñ “ˆä—K’K Ó'ˆ	Ü—K’K Ó'ˆ	ð 	�‰˜Ô'ô —’Ó!ˆØ
�‰˜Ÿ	™	Ô"Ø�
‰
�4—?‘?Ó$Ô%àØÜ
�
Š
ŒØ×Ñ™)›+Ô&Ø×Ñ™)›+Õ&r   c                 óÚ  ^ ^^^^	^
^^• [         R                  " SS5      nUR                  5         T R                  UR                  5        T R                  UR                  / 5        0 m	[        R                  " 5       m[        R                  " 5       m/ m
UUU	U
U UU4S jnU4S jn " S S5      mT" 5       n[        R                  " U5      n[        R                  " U5      m[        R                  " U5      mUR                  X#XDS9  AATR                  5         TR                  T R                  5       5        [        R                   " 5         T R#                  T" 5       5        T R#                  T" 5       5        A[        R                   " 5         T R#                  U" 5       5        T R#                  T
S   " 5       5        T R                  [%        T	R'                  5       5      SS/5        g)	zM
As C{test_threadCreationArguments} above, but for
callInThreadWithCallback.
r   r   c                 ó   >• [         R                  " 5         TR                  TR                  5       5        T" 5       TS'   T" 5       TS'   TR	                  5         TR                  [        R                  " U5      5        g )Nrk   rl   )rf   rg   re   r2   rd   r#   r`   ra   )	ÚsuccessÚresultÚonResultDoneÚonResultWaitÚrefdictÚ	resultRefr   rl   rk   s	     €€€€€€€r   ÚonResultÚVThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.onResult²   s^   ø€ ô �JŠJŒLØ×Ñ˜dŸo™oÓ/Ô0Ù#,£;ˆG�KÑ Ù#,£;ˆG�KÑ Ø×ÑÔØ×ÑœWŸ[š[¨Ó0Õ1r   c                 ó   >• T" 5       $ r   r-   )rW   Útestr^   s     €r   rX   ÚTThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.worker½   s
   ø€ Ù“6ˆMr   c                   ó   • \ rS rSrSrg)ÚRThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallback.<locals>.DumbéÁ   r-   Nr]   r-   r   r   r^   r~   Á   r_   r   r^   )r{   N)r   r?   rJ   rK   rL   r@   rM   r   rc   r`   ra   ÚcallInThreadWithCallbackrd   re   r2   rf   rg   rh   ÚlistÚvalues)r   ri   rx   rX   rj   ÚonResultRefr^   rt   ru   rv   rw   rl   rk   s   `     @@@@@@@r   Ú4test_threadCreationArgumentsCallInThreadWithCallbackÚDThreadPoolTests.test_threadCreationArgumentsCallInThreadWithCallbackœ   sq  ÿ€ ô ×"Ò" 1 aÓ(ˆØ
�‰Œ
Ø�‰˜Ÿ™Ô ð 	×Ñ˜Ÿ™ RÔ(ð ˆä —’Ó(ˆÜ —’Ó(ˆàˆ	÷	2ó 	2õ	÷	ñ 	ñ “ˆä—k’k (Ó+ˆÜ—K’K Ó'ˆ	Ü—K’K Ó'ˆ	ð 	×#Ñ# H°fÐ#ÑJàØð 	×ÑÔà×Ñ˜$Ÿ/™/Ó+Ô,Ü
�
Š
Œà×Ñ™)›+Ô&Ø×Ñ™)›+Ô&ð Ü
�
Š
ŒØ×Ñ™+›-Ô(Ø×Ñ˜) Aš,›.Ô)ð 	×Ñœ˜gŸn™nÓ.Ó/°$¸°Õ>r   c                 óf  • [         R                  " SS5      nU R                  UR                  S5        U R                  UR                  S5        [
        R                  " [
        R                  " U5      5      nU R                  UR                  S5        U R                  UR                  S5        g)zm
Threadpools can be pickled and unpickled, which should preserve the
number of threads and other parameters.
é   r   N)r   r?   r@   rA   rB   ÚpickleÚloadsÚdumps)r   rD   Úcopys      r   Útest_persistenceÚ ThreadPoolTests.test_persistenceæ   s   € ô
 ×$Ò$ Q¨Ó+ˆà×Ñ˜Ÿ™ 1Ô%Ø×Ñ˜Ÿ™ 2Ô&ô �|Š|œFŸLšL¨Ó.Ó/ˆà×Ñ˜Ÿ™ 1Ô%Ø×Ñ˜Ÿ™ 2Õ&r   c                 ó’  • Sn[         R                  " 5       nUR                  5         U R                  UR                  5        [
        R                  " 5       nUR                  5         [        X$5      n[        U5       H  nU" X55        M     U R                  U5        U R                  UR                  SUR                   S35        g)zˆ
Test synchronization of calls made with C{method}, which should be
one of the mechanisms of the threadpool to execute work in threads.
rI   zrun() re-entered z timesN)r   r?   rJ   rK   rL   r   r   r   r   r6   r:   ÚassertFalser"   )r   Úmethodr   ri   r   Úactorr9   s          r   Ú_threadpoolTestÚThreadPoolTests._threadpoolTestö   s™   € ð ˆä×"Ò"Ó$ˆØ
�‰Œ
Ø�‰˜Ÿ™Ô ä—.’.Ó"ˆØ�‰ÔÜ Ó+ˆä�q–ˆAÙ�2Öñ ð 	×Ñ˜'Ô"à×Ñ˜Ÿ™Ð+<¸U¿^¹^Ð<LÈFÐ)SÕTr   c                 ó&   • U R                  S 5      $ )z/
Call C{_threadpoolTest} with C{callInThread}.
c                 ó8   • U R                  UR                  5      $ r   )rb   r%   )ri   r‘   s     r   Ú<lambda>Ú3ThreadPoolTests.test_callInThread.<locals>.<lambda>  s   € °b·o±oÀeÇiÁiÔ6Pr   )r’   r$   s    r   Útest_callInThreadÚ!ThreadPoolTests.test_callInThread  s   € ð ×#Ñ#Ñ$PÓQÐQr   c                 ó  ^•  " S S[         5      mU4S jn[        R                  " SS5      nUR                  U5        UR	                  5         UR                  5         U R                  T5      nU R                  [        U5      S5        g)zQ
L{ThreadPool.callInThread} logs exceptions raised by the callable it
is passed.
c                   ó   • \ rS rSrSrg)Ú<ThreadPoolTests.test_callInThreadException.<locals>.NewErrori  r-   Nr]   r-   r   r   ÚNewErrorrœ     r_   r   r�   c                  ó   >• T " 5       er   r-   ©r�   s   €r   Ú
raiseErrorÚ>ThreadPoolTests.test_callInThreadException.<locals>.raiseError  ó   ø€ Ù“*Ðr   r   r   N)	Ú	Exceptionr   r?   rb   rJ   rL   ÚflushLoggedErrorsr@   r   )r   r    ri   Úerrorsr�   s       @r   Útest_callInThreadExceptionÚ*ThreadPoolTests.test_callInThreadException  sj   ø€ ô	”yô 	õ	ô ×"Ò" 1 aÓ(ˆØ
�‰˜
Ô#Ø
�‰Œ
Ø
�‰Œ	à×'Ñ'¨Ó1ˆØ×Ñœ˜V› aÕ(r   c                 ó˜  ^^• [         R                  " 5       mTR                  5         / mUU4S jn[        R                  " SS5      nUR                  US 5        UR                  5          U R                  T5        UR                  5         U R                  TS   5        U R                  TS   S5        g! UR                  5         f = f)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                 ój   >• TR                  5         TR                  U 5        TR                  U5        g r   ©r!   r#   ©rr   rs   ÚresultsÚwaiters     €€r   rx   Ú?ThreadPoolTests.test_callInThreadWithCallback.<locals>.onResult5  ó%   ø€ Ø�N‰NÔØ�N‰N˜7Ô#Ø�N‰N˜6Õ"r   r   r   c                  ó   • g)Nr{   r-   r-   r   r   r–   Ú?ThreadPoolTests.test_callInThreadWithCallback.<locals>.<lambda>;  s   € °fr   r{   N)r   r   r   r   r?   r€   rJ   r:   rL   Ú
assertTruer@   )r   rx   ri   r¬   r­   s      @@r   Útest_callInThreadWithCallbackÚ-ThreadPoolTests.test_callInThreadWithCallback*  sŸ   ù€ ô —’Ó!ˆØ�‰Ôàˆö	#ô
 ×"Ò" 1 aÓ(ˆØ
×#Ñ# H©nÔ=Ø
�‰Œ
ð	Ø×Ñ˜fÔ%à�G‰GŒIà�‰˜ ™
Ô#Ø×Ñ˜ ™ VÕ,øð �G‰G�Iús   Á,B7 Â7C	c                 ó,  ^^^•  " S S[         5      mU4S jn[        R                  " 5       mTR                  5         / mUU4S jn[        R
                  " SS5      nUR                  X!5        UR                  5          U R                  T5        UR                  5         U R                  TS   5        U R                  TS   [        R                  5        U R                  [        TS   R                   T5      5        g! UR                  5         f = f)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                   ó   • \ rS rSrSrg)ÚRThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.NewErroriM  r-   Nr]   r-   r   r   r�   r·   M  r_   r   r�   c                  ó   >• T " 5       er   r-   rŸ   s   €r   r    ÚTThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.raiseErrorP  r¢   r   c                 ój   >• TR                  5         TR                  U 5        TR                  U5        g r   rª   r«   s     €€r   rx   ÚRThreadPoolTests.test_callInThreadWithCallbackExceptionInCallback.<locals>.onResultX  r¯   r   r   r   N)r£   r   r   r   r   r?   r€   rJ   r:   rL   r�   ÚassertIsInstancer   ÚFailurer²   Ú
issubclassÚtype)r   r    rx   ri   r�   r¬   r­   s       @@@r   Ú0test_callInThreadWithCallbackExceptionInCallbackÚ@ThreadPoolTests.test_callInThreadWithCallbackExceptionInCallbackF  sÏ   ú€ ô	”yô 	õ	ô —’Ó!ˆØ�‰Ôàˆö	#ô
 ×"Ò" 1 aÓ(ˆØ
×#Ñ# HÔ9Ø
�‰Œ
ð	Ø×Ñ˜fÔ%à�G‰GŒIà×Ñ˜ ™Ô$Ø×Ñ˜g a™j¬'¯/©/Ô:Ø�‰œ
 7¨1¡:§?¡?°HÓ=Õ>øð	 �G‰G�Iús   Â D ÄDc                 óB  ^^•  " S S[         5      m[        R                  " 5       nUR                  5         / mUU4S jn[        R
                  " SS5      nUR                  US 5        UR                  UR                  5        UR                  5          U R                  U5        UR                  5         U R                  T5      nU R                  [        U5      S5        U R                  TS   5        U R!                  TS   5        g! UR                  5         f = f)zR
L{ThreadPool.callInThreadWithCallback} logs the exception raised by
C{onResult}.
c                   ó   • \ rS rSrSrg)ÚRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.NewErrorip  r-   Nr]   r-   r   r   r�   rÄ   p  r_   r   r�   c                 óV   >• TR                  U 5        TR                  U5        T" 5       er   )r#   )rr   rs   r�   r¬   s     €€r   rx   ÚRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.onResultx  s#   ø€ Ø�N‰N˜7Ô#Ø�N‰N˜6Ô"Ù“*Ðr   r   r   c                  ó   • g r   r-   r-   r   r   r–   ÚRThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResult.<locals>.<lambda>~  s   € °dr   N)r£   r   r   r   r   r?   r€   rb   r!   rJ   r:   rL   r¤   r@   r   r²   rh   )r   r­   rx   ri   r¥   r�   r¬   s        @@r   Ú0test_callInThreadWithCallbackExceptionInOnResultÚ@ThreadPoolTests.test_callInThreadWithCallbackExceptionInOnResultj  sÝ   ù€ ô	”yô 	ô —’Ó!ˆØ�‰Ôàˆö	ô
 ×"Ò" 1 aÓ(ˆØ
×#Ñ# H©lÔ;Ø
�‰˜Ÿ™Ô'Ø
�‰Œ
ð	Ø×Ñ˜fÔ%à�G‰GŒIà×'Ñ'¨Ó1ˆØ×Ñœ˜V› aÔ(à�‰˜ ™
Ô#Ø×Ñ˜' !™*Õ%øð �G‰G�Iús   ÂD ÄDc                 óš  ^^• / m[         R                  " 5       mUU4S jnU4S jn[        R                  " SS5      nUR	                  X5        UR                  5         U R                  UR                  5        TR                  U R                  5       5        U R                  [        T5      S5        U R                  TS   TS   5        g)zx
L{ThreadPool.callInThreadWithCallback} calls the function it is
given and the C{onResult} callback in the same thread.
c                 ó‚   >• TR                  [        R                  " 5       R                  5        TR	                  5         g r   )r#   r   Úcurrent_threadÚidentrd   )rr   rs   rm   Ú	threadIdss     €€r   rx   Ú5ThreadPoolTests.test_callbackThread.<locals>.onResult–  s)   ø€ Ø×ÑœY×5Ò5Ó7×=Ñ=Ô>Ø�I‰I�Kr   c                  ób   >• T R                  [        R                  " 5       R                  5        g r   )r#   r   rÍ   rÎ   )rÏ   s   €r   ÚfuncÚ1ThreadPoolTests.test_callbackThread.<locals>.funcš  s    ø€ Ø×ÑœY×5Ò5Ó7×=Ñ=Õ>r   r   r   rQ   N)r   rc   r   r?   r€   rJ   rK   rL   re   r2   r@   r   )r   rx   rÒ   ri   rm   rÏ   s       @@r   Útest_callbackThreadÚ#ThreadPoolTests.test_callbackThread�  sš   ù€ ð
 ˆ	ä—’Ó!ˆö	õ	?ô ×"Ò" 1 aÓ(ˆØ
×#Ñ# HÔ3Ø
�‰Œ
Ø�‰˜Ÿ™Ô à�
‰
�4—?‘?Ó$Ô%Ø×Ñœ˜Y›¨Ô+Ø×Ñ˜ 1™ y°¡|Õ4r   c                 ó  ^^• [         R                  R                  5       R                  S   nSUS'   / m[        R
                  " 5       mUU4S jnU4S jn[        R                  " SS5      nUR                  X#5        UR                  5         U R                  UR                  5        TR                  U R                  5       5        U R                  [        T5      S5        U R                  UTS   5        U R                  UTS   5        g	)
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                 óž   >• [         R                  R                  5       R                  S   nTR	                  U5        TR                  5         g ©Nr×   )r   ÚtheContextTrackerÚcurrentContextÚcontextsr#   rd   )rr   rs   ÚctxrÝ   rm   s      €€r   rx   Ú6ThreadPoolTests.test_callbackContext.<locals>.onResult³  s7   ø€ Ü×+Ñ+×:Ñ:Ó<×EÑEÀbÑIˆCØ�O‰O˜CÔ Ø�I‰I�Kr   c                  ó~   >• [         R                  R                  5       R                  S   n TR	                  U 5        g rÚ   )r   rÛ   rÜ   rÝ   r#   )rÞ   rÝ   s    €r   rÒ   Ú2ThreadPoolTests.test_callbackContext.<locals>.func¸  s.   ø€ Ü×+Ñ+×:Ñ:Ó<×EÑEÀbÑIˆCØ�O‰O˜CÕ r   r   r   rQ   N)r   rÛ   rÜ   rÝ   r   rc   r   r?   r€   rJ   rK   rL   re   r2   r@   r   )r   Úmyctxrx   rÒ   ri   rÝ   rm   s        @@r   Útest_callbackContextÚ$ThreadPoolTests.test_callbackContext¦  sÓ   ù€ ô ×)Ñ)×8Ñ8Ó:×CÑCÀBÑGˆØ1ˆˆiÑàˆä—’Ó!ˆö	õ
	!ô ×"Ò" 1 aÓ(ˆØ
×#Ñ# HÔ3Ø
�‰Œ
Ø�‰˜Ÿ™Ô à�
‰
�4—?‘?Ó$Ô%à×Ñœ˜X›¨Ô*Ø×Ñ˜ ¨¡Ô,Ø×Ñ˜ ¨¡Õ,r   c                 ó@  • [         R                  " 5       nUR                  5         [        R                  " SS5      nUR                  UR                  5        UR                  5          U R                  U5        UR                  5         g! UR                  5         f = f)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   r?   rb   r!   rJ   r:   rL   )r   r­   ri   s      r   Útest_existingWorkÚ!ThreadPoolTests.test_existingWorkÇ  sk   € ô —’Ó!ˆØ�‰Ôä×"Ò" 1 aÓ(ˆØ
�‰˜Ÿ™Ô'Ø
�‰Œ
ð	Ø×Ñ˜fÔ%à�G‰G�IøˆB�G‰G�Iús   Á)B ÂBc                 ó  ^^• [         R                  " SS5      nUR                  5         U R                  UR                  5        U R                  UR                  S5        U R                  [        UR                  5      S5        U R                  [        UR                  5      S5        [        R                  " 5       m[        R                  " 5       mUU4S jnUR                  U5        TR                  S5        U R                  UR                  S5        U R                  [        UR                  5      S5        U R                  [        UR                  5      S5        TR                  5         [        UR                  5      (       d2  [        R                   " S5        [        UR                  5      (       d  M2  U R                  [        UR                  5      S5        U R                  [        UR                  5      S5        g)zc
As the worker receives and completes work, it transitions between
the working and waiting states.
r   r   c                  óH   >• TR                  5         T R                  S5        g )NrI   )rd   re   )ÚthreadFinishÚthreadWorkings   €€r   Ú_threadÚ;ThreadPoolTests.test_workerStateTransition.<locals>._threadë  s   ø€ Ø×ÑÔØ×Ñ˜bÕ!r   rI   gü©ñÒMb@?N)r   r?   rJ   rK   rL   r@   Úworkersr   ÚwaitersÚworkingr   rc   rb   re   rd   r   r    )r   rD   rì   rê   rë   s      @@r   Útest_workerStateTransitionÚ*ThreadPoolTests.test_workerStateTransitionÙ  si  ù€ ô
 ×$Ò$ Q¨Ó*ˆØ�
‰
ŒØ�‰˜Ÿ	™	Ô"ð 	×Ñ˜Ÿ™ qÔ)Ø×Ñœ˜TŸ\™\Ó*¨AÔ.Ø×Ñœ˜TŸ\™\Ó*¨AÔ.ô "ŸšÓ)ˆÜ —’Ó(ˆö	"ð 	×Ñ˜'Ô"Ø×Ñ˜2ÔØ×Ñ˜Ÿ™ qÔ)Ø×Ñœ˜TŸ\™\Ó*¨AÔ.Ø×Ñœ˜TŸ\™\Ó*¨AÔ.ð 	×ÑÔÜ�d—l‘l×#Ñ#Ü�JŠJ�vÔô �d—l‘l×#Ó#ð 	×Ñœ˜TŸ\™\Ó*¨AÔ.Ø×Ñœ˜TŸ\™\Ó*¨AÕ.r   Nc                 ó†   • [         R                  " SS5      nU R                  UR                  R	                  5       S5        g)z2
There is a property '_queue' for legacy purposes
r   r   N)r   r?   r@   Ú_queueÚqsizerC   s     r   Útest_qÚThreadPoolTests.test_qþ  s3   € ô ×$Ò$ Q¨Ó*ˆØ×Ñ˜Ÿ™×*Ñ*Ó,¨aÕ0r   r-   )ÚreturnN)r'   r(   r)   r*   Ú__doc__r2   r:   rE   rN   rS   rn   r„   rŒ   r’   r˜   r¦   r³   rÀ   rÉ   rÔ   rã   ræ   rñ   rö   r,   r-   r   r   r/   r/   ;   so   † ñòò?ò'ò/ò	/ò('òTH?òT'ò Uò4Rò)ò(-ò8"?òH!&òF5ò2-òBò$#/÷J1r   r/   c                   ó&   • \ rS rSrS rS rS rSrg)ÚRaceConditionTestsi  c                 óÔ   ^ • [         R                  " SS5      T l         [        R                  " 5       T l        T R                   R                  5         U 4S jnT R                  U5        g )Nr   rI   c                  ó>   >• T R                   R                  5         T ? g r   )r   rL   r$   s   €r   ÚdoneÚ&RaceConditionTests.setUp.<locals>.done  s   ø€ Ø�O‰O× Ñ Ô"Ø‘r   )r   r?   r   rc   rm   rJ   rK   )r   rþ   s   ` r   ÚsetUpÚRaceConditionTests.setUp  sH   ø€ Ü$×/Ò/°°2Ó6ˆŒÜ—_’_Ó&ˆŒ
Ø�‰×ÑÔõ	 ð 	�‰˜Õr   c                 ó   • g)z-
A reasonable number of seconds to time out.
r   r-   r$   s    r   r2   ÚRaceConditionTests.getTimeout  r4   r   c                 ó˜  • U R                  5       nU R                  R                  U R                  R                  5        U R                  R                  U5        U R                  R                  5         [        S5       H2  nU R                  R                  U R                  R
                  5        M4     U R                  R                  U R                  R                  5        U R                  R                  U5        U R                  R                  5       (       d,  U R                  R	                  5         U R                  S5        gg)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.
rH   z9'set' did not run in thread; timed out waiting on 'wait'.N)
r2   r   rb   rm   rd   re   Úclearr6   ÚisSetr7   )r   Útimeoutr9   s      r   Útest_synchronizationÚ'RaceConditionTests.test_synchronization  sÏ   € ð" —/‘/Ó#ˆØ�‰×$Ñ$ T§Z¡Z§^¡^Ô4Ø�
‰
�‰˜Ô Ø�
‰
×ÑÔÜ�q–ˆAØ�O‰O×(Ñ(¨¯©¯©Ö9ñ à�‰×$Ñ$ T§Z¡Z§^¡^Ô4Ø�
‰
�‰˜Ô Ø�z‰z×Ñ×!Ñ!Ø�J‰J�N‰NÔØ�I‰IÐQÕRð "r   )rm   r   N)r'   r(   r)   r*   r   r2   r  r,   r-   r   r   rû   rû     s   † ò	òõSr   rû   c                   ó$   • \ rS rSrSrS rS rSrg)Ú
MemoryPooli6  zs
A deterministic threadpool that uses in-memory data structures to queue
work rather than threads to execute work.
c                 óp   • Xl         X l        X0l        [        R                  R
                  " U /UQ70 UD6  g)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   r?   r   )r   ÚcoordinatorÚfailTestÚ	newWorkerÚargsÚkwargss         r   r   ÚMemoryPool.__init__<  s2   € ð$ (ÔØ!ŒØ#ŒÜ×Ñ×&Ò& tÐ=¨dÒ=°fÓ=r   c                 óZ   ^ ^^• UU U4S jn[        T R                  UT R                  S9mT$ )ag  
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                  óŠ   >• TR                  5       n U R                  U R                  -   T" 5       :¼  a  g TR                  5       $ r   )Ú
statisticsÚbusyWorkerCountÚidleWorkerCountr  )ÚstatsÚcurrentLimitr   Úteams    €€€r   ÚrespectLimitÚ&MemoryPool._pool.<locals>.respectLimita  s<   ø€ ð —O‘OÓ%ˆEØ×%Ñ%¨×(=Ñ(=Ñ=Á,Ã.ÓPØØ—?‘?Ó$Ð$r   )r  ÚcreateWorkerÚlogException)r   r  r  )r   r  ÚthreadFactoryr  r  s   ``  @r   Ú_poolÚMemoryPool._poolS  s.   ú€ ÷	%ô Ø×)Ñ)Ø%ØŸ™ñ
ˆð
 ˆr   )r  r  r  N)r'   r(   r)   r*   rù   r   r#  r,   r-   r   r   r  r  6  s   † ñò
>õ.r   r  c                   ó$   • \ rS rSrSrS rS rSrg)Ú
PoolHelperir  aÖ  
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                 ó‚   ^ • [        5       u  nT l        / T l        U 4S jn[        XAR                  U/UQ70 UD6T l        g)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                  ón   >• T R                   R                  [        5       5        T R                   S   S   $ )Nr×   r   )rî   r#   r   r$   s   €r   r  Ú&PoolHelper.__init__.<locals>.newWorker‘  s-   ø€ Ø�L‰L×ÑÔ 2Ó 4Ô5Ø—<‘< Ñ# AÑ&Ð&r   N)r   ÚperformCoordinationrî   r  r7   r   )r   ÚtestCaser  r  r  r  s   `     r   r   ÚPoolHelper.__init__„  sI   ø€ ô 1CÓ0DÑ-ˆ�TÔ-ØˆŒõ	'ô %ØŸ™¨	ð
Ø48ò
Ø<Bñ
ˆ�r   c                 ó`   • U R                  5       (       a   U R                  5       (       a  M  gg)zq
Perform all currently scheduled "coordination", which is the work
involved in delegating work to other threads.
N)r*  r$   s    r   ÚperformAllCoordinationÚ!PoolHelper.performAllCoordination™  s'   € ð
 ×&Ñ&×(Ñ(Øð ×&Ñ&×(Õ(r   )r*  r   rî   N)r'   r(   r)   r*   rù   r   r.  r,   r-   r   r   r&  r&  r  s   † ñò"
õ*r   r&  c                   ó$   • \ rS rSrSrS rS rSrg)ÚMemoryBackedTestsi¢  zb
Tests using L{PoolHelper} to deterministically test properties of the
threadpool implementation.
c                 ót  • [        U SS5      nSn[        U5       H  nUR                  R                  S 5        M!     UR	                  5         U R                  UR                  / 5        UR                  R                  5         UR	                  5         U R                  [        UR                  5      U5        g)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   rI   r   c                  ó   • g r   r-   r-   r   r   r–   Ú;MemoryBackedTests.test_workBeforeStarting.<locals>.<lambda>±  ó   € °4r   N)	r&  r6   r   rb   r.  r@   rî   rJ   r   ©r   ÚhelperÚnÚxs       r   Útest_workBeforeStartingÚ)MemoryBackedTests.test_workBeforeStarting¨  s�   € ô ˜D ! RÓ(ˆØˆÜ�q–ˆAØ×Ñ×*Ñ*©<Ö8ñ à×%Ñ%Ô'Ø×Ñ˜Ÿ™¨Ô,Ø×Ñ×ÑÔ!Ø×%Ñ%Ô'Ø×Ñœ˜VŸ^™^Ó,¨aÕ0r   c                 óœ  • [        U SS5      nSn[        U5       H  nUR                  R                  S 5        M!     UR	                  5         U R                  UR                  / 5        UR                  R                  5         UR	                  5         U R                  [        UR                  5      UR                  R                  5        g)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   rI   é2   c                  ó   • g r   r-   r-   r   r   r–   ÚBMemoryBackedTests.test_tooMuchWorkBeforeStarting.<locals>.<lambda>Á  r5  r   N)
r&  r6   r   rb   r.  r@   rî   rJ   r   rB   r6  s       r   Útest_tooMuchWorkBeforeStartingÚ0MemoryBackedTests.test_tooMuchWorkBeforeStarting¸  sœ   € ô ˜D ! RÓ(ˆØˆÜ�q–ˆAØ×Ñ×*Ñ*©<Ö8ñ à×%Ñ%Ô'Ø×Ñ˜Ÿ™¨Ô,Ø×Ñ×ÑÔ!Ø×%Ñ%Ô'Ø×Ñœ˜VŸ^™^Ó,¨f×.?Ñ.?×.CÑ.CÕDr   r-   N)r'   r(   r)   r*   rù   r:  r@  r,   r-   r   r   r1  r1  ¢  s   † ñò
1õ Er   r1  )rù   rf   rˆ   r   r   r`   Útwisted._threadsr   r   Útwisted.pythonr   r   r   r   Útwisted.trialr	   r   ÚsynchronizeÚSynchronousTestCaser/   rû   r?   r  r&  r1  r-   r   r   Ú<module>rG     sž   ðñó
 
Û Û Û Û ç 5ß CÓ CÝ "÷!ñ !ðH × Ò �Ô 'ôH1�h×2Ñ2ô H1ôV-S˜×5Ñ5ô -Sô`9�×&Ñ&ô 9÷x-ñ -ô`$E˜×4Ñ4õ $Er   