+
    Dfj&                        R t ^ RIHt ^ RIHt ^ RIHt ^ RIHtH	t	 ^ RI
Ht ^ RIHt ^RIHtHtHtHt  ! R R	]! ]R
4      4      t ! R R]4      tR# )z&
Tests for L{twisted._threads._team}.
)annotations)Callable)proxyForInterface)callget)Failure)SynchronousTestCase)AlreadyQuitIWorkerTeamcreateMemoryWorkerc                  >   a  ] tR t^tRtR R ltR V 3R lltRtV ;t# )ContextualWorkerz2
A worker implementation that supplies a context.
c               $    V ^8  d   QhRRRRRR/# )   
realWorkerr
   ctxobjectreturnNone )formats   "A/usr/lib/python3/dist-packages/twisted/_threads/test/test_team.py__annotate__ContextualWorker.__annotate__   s!      7 6 d     c                    Wn         W n        R# )z*
Create with a real worker and a context.
N)_realWorker_context)selfr   r   s   &&,r   __init__ContextualWorker.__init__   s     &r   c                    V ^8  d   QhRRRR/# )r   workzCallable[[], object]r   r   r   )r   s   "r   r   r      s     6 6+ 6 6r   c                4   <a a \         SS `  V V3R l4       R# )zr
Perform the given work with the context given to __init__.

@param work: the work to pass on to the real worker.
c                 0   < \        S P                  S4      # N)r   r   )r   r#   s   r   <lambda>%ContextualWorker.do.<locals>.<lambda>%   s    4t4r   N)superdo)r   r#   	__class__s   ffr   r*   ContextualWorker.do   s     	
45r   )r   r   )	__name__
__module____qualname____firstlineno____doc__r    r*   __static_attributes____classcell__)r+   s   @r   r   r      s    6 6r   r   r   c                      ] tR t^(tRtR R ltR R ltR R ltR R	 ltR
 R lt	R R lt
R R ltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR R ltR  R! ltR"tR## )$	TeamTestsz
Tests for L{Team}
c                   V ^8  d   QhRR/# r   r   r   r   )r   s   "r   r   TeamTests.__annotate__-   s     %B %Bt %Br   c                   a  \        4       w  pS n        \        VRR7      S n        . S n        . S n        . S n        . S n        R S n        R V 3R llp. S n	        R V 3R llp\        WV4      S n        R# )	zT
Set up a L{Team} with inspectable, synchronous workers that can be
single-stepped.
coordinatorworkerc                     R # )Fr   r   r   r   r'   !TeamTests.setUp.<locals>.<lambda>8   s    Ur   c                   V ^8  d   QhRR/# )r   r   zContextualWorker | Noner   )r   s   "r   r   %TeamTests.setUp.<locals>.__annotate__:   s     	 	5 	r   c                   <aaa SP                  4       '       d   R # \        4       w  p oSP                  P                  S4       SP                  P                  S4       \        V \        SP                  4      R7      oSP                  P                  S4       SP                  P                  S4       SP                  oR VVVV3R llpVSn	        S# )Nr;   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   ;TeamTests.setUp.<locals>.createWorker.<locals>.__annotate__E   s     8 84 8r   c                    < S! 4        SP                   P                  S 4       SP                  P                  S4       R # r&   )allUnquitWorkersremoveactivePerformers)cw	performerrealQuitr   s   r   quitAndRemove<TeamTests.setUp.<locals>.createWorker.<locals>.quitAndRemoveE   s0    
%%,,R0%%,,Y7r   )
noMoreWorkersr   workerPerformersappendrG   r   lenallWorkersEverrE   quit)r<   rK   rH   rI   rJ   r   s     @@@r   createWorker%TeamTests.setUp.<locals>.createWorker:   s    !!## 2 4FI!!((3!!((3!&T5J5J1KLB&&r*!!((,wwH8 8
 $BGIr   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r@   O   s     	, 	,d 	,r   c                 N   < S P                   P                  \        4       4       R # r&   )failuresrO   r   r   s   r   logException%TeamTests.setUp.<locals>.logExceptionO   s    MM  +r   N)r   coordinateOncer   r:   rN   rQ   rE   rG   rM   rW   r   team)r   r:   rS   rY   s   f   r   setUpTeamTests.setUp-   sy    
 ,>+?(T(+KN<>688:<>*	 	& (*	, 	, LA	r   c                   V ^8  d   QhRR/# )r   r   boolr   )r   s   "r   r   r8   T   s      D r   c                >    RpV P                  4       '       d   RpK  V# )z
Perform all work currently scheduled in the coordinator.

@return: whether any coordination work was performed; if the
    coordinator was idle when this was called, return L{False}
    (otherwise L{True}).
FT)r[   )r   dids   & r   
coordinateTeamTests.coordinateT   s"     !!##C
r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8   a   s     9 94 9r   c                    RpV'       dY   V P                  4       pV P                   F  pW P                  9   g   K  V! 4        K  	  T;'       g    V P                  4       pK`  R# )zR
Perform all work on the coordinator and worker performers that needs to
be done.
TN)rc   rN   rG   )r   
continuingrI   s   &  r   performAllOutstandingWork#TeamTests.performAllOutstandingWorka   sT    
 
*J!22	 5 55K 3 $88t'8J r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8   n   s     D D Dr   c                  a RoR V3R llpV P                   P                  V4       V P                  4        V P                  V P                   P	                  4       P
                  ^4       V P                  4        V P                  S^4       V P                  V P                   P	                  4       P
                  ^ 4       R# )zL
L{Team.do} does the work in a worker created by the createWorker
callable.
Nc                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   7TeamTests.test_doDoesWorkInWorker.<locals>.__annotate__u   s     	  	 4 	 r   c                    < \        R 4      o R# )r<   N)r   )whos   r   	something4TeamTests.test_doDoesWorkInWorker.<locals>.somethingu   s    h-Cr   )r\   r*   rc   assertEqual
statisticsbusyWorkerCountrh   )r   rp   ro   s   & @r   test_doDoesWorkInWorker!TeamTests.test_doDoesWorkInWorkern   s    
 	  	  			Y--/??C&&(a --/??Cr   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     7 7 7r   c                    V P                   P                  4       pV P                  VP                  ^ 4       V P                  VP                  ^ 4       V P                  VP
                  ^ 4       R# )zy
L{Team.statistics} returns an object with idleWorkerCount,
busyWorkerCount, and backloggedWorkCount integer attributes.
N)r\   rs   rr   idleWorkerCountrt   backloggedWorkCount)r   statss   & r   test_initialStatistics TeamTests.test_initialStatistics   sX    
 		$$&..2..222A6r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     8 8T 8r   c                    V P                   P                  ^4       V P                  4        V P                  \	        V P
                  4      ^4       R# )z>
L{Team.grow} increases the number of available idle workers.
N)r\   growrh   rr   rP   rN   rX   s   &r   test_growCreatesIdleWorkers%TeamTests.test_growCreatesIdleWorkers   s;     			q&&(T223Q7r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     	D 	Dd 	Dr   c                &  a  V 3R lS n         S P                  P                  ^4       S P                  4        S P	                  \        S P                  4      ^4       S P	                  S P                  P                  4       P                  ^4       R# )zw
L{Team.grow} increases the number of available idle workers until the
C{createWorker} callable starts returning None.
c                 4   < \        S P                  4      ^8  # )   )rP   rQ   rX   s   r   r'   0TeamTests.test_growCreateLimit.<locals>.<lambda>   s    S)<)<%=%Br   N)	rM   r\   r   rh   rr   rP   rQ   rs   ry   rX   s   fr   test_growCreateLimitTeamTests.test_growCreateLimit   sh    
 C		q&&(T00115--/??Cr   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     8 8 8r   c                    V P                   P                  ^4       V P                  4        V P                   P                  ^4       V P                  4        V P	                  \        V P                  4      ^4       R# )z7
L{Team.shrink} will quit the given number of workers.
N)r\   r   rh   shrinkrr   rP   rE   rX   s   &r   test_shrinkQuitsWorkers!TeamTests.test_shrinkQuitsWorkers   sY     			q&&(		&&(T223Q7r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     
8 
84 
8r   c                   V P                   P                  ^
4       V P                  4        V P                  \	        V P
                  4      ^
4       V P                   P                  4        V P                  \	        V P
                  4      ^
4       V P                  4        V P                  \	        V P
                  4      ^ 4       R# )zE
L{Team.shrink} with no arguments will stop all outstanding workers.
N)r\   r   rh   rr   rP   rE   r   rX   s   &r   test_shrinkToZeroTeamTests.test_shrinkToZero   s     			r&&(T223R8		T223R8&&(T223Q7r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     # #T #r   c                  a V P                   P                  ^4       V P                  4        ^ oR V3R llpV P                  V P                   P	                  4       P
                  ^4       \        ^4       F  pV P                   P                  V4       K   	  V P                  4        V P                  V P                   P	                  4       P
                  ^ 4       R V n        V P                   P                  V4       V P                  4        V P                  V P                   P	                  4       P
                  ^ 4       V P                  V P                   P	                  4       P                  ^4       V P                  4        V P                  V P                   P	                  4       P                  ^ 4       V P                  S^4       R# )zu
When no additional workers are available, the given work is backlogged,
and then performed later when the work was.
c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   CTeamTests.test_moreWorkWhenNoWorkersAvailable.<locals>.__annotate__   s     	 	4 	r   c                    < S ^,          o R# )   Nr   )timess   r   rp   @TeamTests.test_moreWorkWhenNoWorkersAvailable.<locals>.something   s    QJEr   c                     R # )Tr   r   r   r   r'   ?TeamTests.test_moreWorkWhenNoWorkersAvailable.<locals>.<lambda>   s    Tr   N)r\   r   rc   rr   rs   ry   ranger*   rM   rz   rh   )r   rp   ir   s   &  @r   #test_moreWorkWhenNoWorkersAvailable-TeamTests.test_moreWorkWhenNoWorkersAvailable   sG   
 			q	 	 	--/??CqAIILL#  	--/??C)		Y--/??C--/CCQG&&(--/CCQG"r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     	C 	Cd 	Cr   c                
   V P                   P                  R 4       V P                  4        V P                  \	        V P
                  4      ^4       V P                  V P
                  ^ ,          P                  \        4       R# )z
When an exception is raised in a task passed to L{Team.do}, the
C{logException} given to the L{Team} at construction is invoked in the
exception context.
c                     ^^ ,          # )r   r   r   r   r   r'   0TeamTests.test_exceptionInTask.<locals>.<lambda>   s    QUr   N)r\   r*   rh   rr   rP   rW   typeZeroDivisionErrorrX   s   &r   test_exceptionInTaskTeamTests.test_exceptionInTask   sY     			]#&&(T]]+Q/q)..0ABr   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     ; ;4 ;r   c                    V P                   P                  4        V P                  \        V P                   P                  4       V P                  \        V P                   P                  \
        4       R# )z`
L{Team.quit} causes future invocations of L{Team.do} and L{Team.quit}
to raise L{AlreadyQuit}.
N)r\   rR   assertRaisesr	   r*   listrX   s   &r   	test_quitTeamTests.test_quit   sD    
 			+tyy~~6+tyy||T:r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     > > >r   c                z   \        ^
4       F"  pV P                  P                  \        4       K$  	  V P	                  4        V P                  P                  4        V P	                  4        V P                  \        V P                  4      ^ 4       V P                  \        V P                  P
                  4       R# )zS
L{Team.quit} causes all idle workers, as well as the coordinator
worker, to quit.
N)r   r\   r*   r   rh   rR   rr   rP   rE   r   r	   r:   r   xs   & r   test_quitQuitsTeamTests.test_quitQuits   s    
 rAIILL &&(		&&(T223Q7+t'7'7'<'<=r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8      s     > >T >r   c                   V P                   P                  ^
4       \        ^4       F"  pV P                   P                  \        4       K$  	  V P                  4        V P                   P                  4        V P                  4        V P                  \        V P                  4      ^4       V P                  4        V P                  \        V P                  4      ^ 4       V P                  \        V P                  P                  4       R# )zd
L{Team.quit} causes all busy workers to be quit once they've finished
the work they've been given.
N)r\   r   r   r*   r   rc   rR   rr   rP   rE   rh   r   r	   r:   r   s   & r   test_quitQuitsLaterWhenBusy%TeamTests.test_quitQuitsLaterWhenBusy   s    
 			rqAIILL 		T223Q7&&(T223Q7+t'7'7'<'<=r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8     s     ; ;d ;r   c                  a a S P                   P                  \        4       S P                   P                  P                  oR VV 3R llpVS P                   P                  n        S P                   P                  4        S P                  \        S P                   P
                  4       S P                  \        S P                   P                  \        4       R# )z
If work happens after L{Team.quit} sets its C{Quit} flag, but before
any other work takes place, the L{Team} should still exit gracefully.
c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   DTeamTests.test_quitConcurrentWithWorkHappening.<locals>.__annotate__	  s     	- 	- 	-r   c                 6   < S ! 4        SP                  4        R # r&   )rh   )originalSetr   s   r   performWorkConcurrentlyOTeamTests.test_quitConcurrentWithWorkHappening.<locals>.performWorkConcurrently	  s    M**,r   N)r\   r*   r   _quitsetrR   r   r	   )r   r   r   s   f @r   $test_quitConcurrentWithWorkHappening.TeamTests.test_quitConcurrentWithWorkHappening  s    
 			Tiioo))	- 	- 6				+tyy~~6+tyy||T:r   c                   V ^8  d   QhRR/# r7   r   )r   s   "r   r   r8     s     8 8T 8r   c                r   \        ^
4       F"  pV P                  P                  \        4       K$  	  V P	                  4        V P                  \        V P                  4      ^
4       V P                  P                  ^4       V P                  4        V P                  \        V P                  4      ^4       R# )zT
L{Team.shrink} will wait for busy workers to finish being busy and then
quit them.
N)
r   r\   r*   r   rc   rr   rP   rE   r   rh   r   s   & r   test_shrinkWhenBusyTeamTests.test_shrinkWhenBusy  s    
 rAIILL T223R8		&&(T223Q7r   )	rG   rE   rQ   r[   r:   rW   rM   r\   rN   N)r-   r.   r/   r0   r1   r]   rc   rh   ru   r|   r   r   r   r   r   r   r   r   r   r   r   r2   r   r   r   r5   r5   (   sa    %BN9D$78	D8
8#:	C;>> ;"8 8r   r5   N)r1   
__future__r   typingr   twisted.python.componentsr   twisted.python.contextr   r   twisted.python.failurer   twisted.trial.unittestr    r	   r
   r   r   r   r5   r   r   r   <module>r      sG    #  7 , * 6 = =6(-@ 6*v8# v8r   