
    Yf$                         d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZ  G d d	 eed
                    Z G d de	          ZdS )z&
Tests for L{twisted._threads._team}.
    )proxyForInterface)callget)Failure)SynchronousTestCase   )AlreadyQuitIWorkerTeamcreateMemoryWorkerc                   (     e Zd ZdZd Z fdZ xZS )ContextualWorkerz:
    A worker implementation that supplies a context.
    c                 "    || _         || _        dS )z:
        Create with a real worker and a context.
        N)_realWorker_context)self
realWorkerctxs      Q/var/www/html/env/lib/python3.11/site-packages/twisted/_threads/test/test_team.py__init__zContextualWorker.__init__   s     &    c                 V     t                                           fd           dS )z
        Perform the given work with the context given to __init__.

        @param work: the work to pass on to the real worker.
        c                  .    t           j                  S N)r   r   )r   works   r   <lambda>z%ContextualWorker.do.<locals>.<lambda>"   s    4t44 r   N)superdo)r   r   	__class__s   ``r   r   zContextualWorker.do   s0     	

4444455555r   )__name__
__module____qualname____doc__r   r   __classcell__)r   s   @r   r   r      sQ           6 6 6 6 6 6 6 6 6r   r   r   c                   r    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S )	TeamTestsz
    Tests for L{Team}
    c                      t                      \  } _        t          |d           _        g  _        g  _        g  _        g  _        d  _         fd}g  _	         fd}t          |||           _        dS )zl
        Set up a L{Team} with inspectable, synchronous workers that can be
        single-stepped.
        coordinatorworkerc                      dS )NF r,   r   r   r   z!TeamTests.setUp.<locals>.<lambda>5   s    U r   c                                                      rd S t                      \  } j                                       j                                       t          | t          j                            j                                       j                                       j	        fd}|_	        S )Nr)   c                                    j                                         j                                       d S r   )allUnquitWorkersremoveactivePerformers)cw	performerrealQuitr   s   r   quitAndRemovez<TeamTests.setUp.<locals>.createWorker.<locals>.quitAndRemoveB   sB    


%,,R000%,,Y77777r   )
noMoreWorkersr   workerPerformersappendr1   r   lenallWorkersEverr/   quit)r*   r5   r2   r3   r4   r   s     @@@r   createWorkerz%TeamTests.setUp.<locals>.createWorker7   s    !!## t 2 4 4FI!((333!((333!&T5J1K1KLLLB&&r***!((,,,wH8 8 8 8 8 8 8 8
 $BGIr   c                  T     j                             t                                 d S r   )failuresr8   r   r   s   r   logExceptionz%TeamTests.setUp.<locals>.logExceptionL   s#    M  +++++r   N)r   coordinateOncer   r(   r7   r:   r/   r1   r6   r>   r   team)r   r(   r<   r@   s   `   r   setUpzTeamTests.setUp*   s    
 ,>+?+?(T(+KNNN "  " "*]	 	 	 	 	& 	, 	, 	, 	, 	, lLAA			r   c                 ^    d}|                                  rd}|                                  |S )a  
        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}).
        @rtype: L{bool}
        FT)rA   )r   dids     r   
coordinatezTeamTests.coordinateQ   s=     !!## 	C !!## 	
r   c                     d}|rK|                                  }| j        D ]}|| j        v r
 |             |p|                                  }|IdS dS )zj
        Perform all work on the coordinator and worker performers that needs to
        be done.
        TN)rF   r7   r1   )r   
continuingr3   s      r   performAllOutstandingWorkz#TeamTests.performAllOutstandingWork_   s~    
 
 	9**J!2    	 555IKKK#8t'8'8J  	9 	9 	9 	9 	9r   c                    fd| j                                        |                                  |                     | j                                         j        d           |                                  |                     j        d           |                     | j                                         j        d           dS )zd
        L{Team.do} does the work in a worker created by the createWorker
        callable.
        c                  0    t          d           _        d S )Nr*   )r   who	somethings   r   rN   z4TeamTests.test_doDoesWorkInWorker.<locals>.somethingr   s    MMIMMMr      r   N)rB   r   rF   assertEqual
statisticsbusyWorkerCountrI   rL   )r   rN   s    @r   test_doDoesWorkInWorkerz!TeamTests.test_doDoesWorkInWorkerl   s    	* 	* 	* 	* 	* 		Y--//?CCC&&(((***--//?CCCCCr   c                     | j                                         }|                     |j        d           |                     |j        d           |                     |j        d           dS )z
        L{Team.statistics} returns an object with idleWorkerCount,
        busyWorkerCount, and backloggedWorkCount integer attributes.
        r   N)rB   rQ   rP   idleWorkerCountrR   backloggedWorkCount)r   statss     r   test_initialStatisticsz TeamTests.test_initialStatistics|   sh    
 	$$&&.222.2222A66666r   c                     | j                             d           |                                  |                     t	          | j                  d           dS )zN
        L{Team.grow} increases the number of available idle workers.
           N)rB   growrI   rP   r9   r7   r?   s    r   test_growCreatesIdleWorkersz%TeamTests.test_growCreatesIdleWorkers   sQ     		q&&(((T233Q77777r   c                 ,     fd _          j                            d                                                                  t           j                  d                                 j                                        j        d           dS )z
        L{Team.grow} increases the number of available idle workers until the
        C{createWorker} callable starts returning None.
        c                  4    t           j                  dk    S )N   )r9   r:   r?   s   r   r   z0TeamTests.test_growCreateLimit.<locals>.<lambda>   s    S)<%=%=%B r   rZ   r_   N)	r6   rB   r[   rI   rP   r9   r:   rQ   rU   r?   s   `r   test_growCreateLimitzTeamTests.test_growCreateLimit   s    
 CBBB	q&&(((T0111555--//?CCCCCr   c                    | j                             d           |                                  | j                             d           |                                  |                     t          | j                  d           dS )zG
        L{Team.shrink} will quit the given number of workers.
        rZ   r_   r   N)rB   r[   rI   shrinkrP   r9   r/   r?   s    r   test_shrinkQuitsWorkersz!TeamTests.test_shrinkQuitsWorkers   sy     		q&&(((	&&(((T233Q77777r   c                    | j                             d           |                                  |                     t	          | j                  d           | j                                          |                     t	          | j                  d           |                                  |                     t	          | j                  d           dS )zU
        L{Team.shrink} with no arguments will stop all outstanding workers.
        
   r   N)rB   r[   rI   rP   r9   r/   rb   r?   s    r   test_shrinkToZerozTeamTests.test_shrinkToZero   s     		r&&(((T233R888	T233R888&&(((T233Q77777r   c                    | j                             d           |                                  fdd_        |                     | j                                         j        d           t          d          D ]}| j                                        |                                  |                     | j                                         j        d           d | _	        | j                                        |                                  |                     | j                                         j        d           |                     | j                                         j
        d           |                                  |                     | j                                         j
        d           |                     j        d           dS )z
        When no additional workers are available, the given work is backlogged,
        and then performed later when the work was.
        r_   c                  (     xj         dz  c_         d S )NrO   )timesrM   s   r   rN   z@TeamTests.test_moreWorkWhenNoWorkersAvailable.<locals>.something   s    OOq OOOOr   r   c                      dS )NTr,   r,   r   r   r   z?TeamTests.test_moreWorkWhenNoWorkersAvailable.<locals>.<lambda>   s    T r   rO      N)rB   r[   rF   ri   rP   rQ   rU   ranger   r6   rV   rI   )r   irN   s     @r   #test_moreWorkWhenNoWorkersAvailablez-TeamTests.test_moreWorkWhenNoWorkersAvailable   s   
 		q	! 	! 	! 	! 	! 	--//?CCCq 	$ 	$AILL#### 	--//?CCC)\	Y--//?CCC--//CQGGG&&(((--//CQGGG!,,,,,r   c                 
   | j                             d            |                                  |                     t	          | j                  d           |                     | j        d         j        t                     dS )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                      ddz  S )NrO   r   r,   r,   r   r   r   z0TeamTests.test_exceptionInTask.<locals>.<lambda>   s
    QU r   rO   r   N)rB   r   rI   rP   r9   r>   typeZeroDivisionErrorr?   s    r   test_exceptionInTaskzTeamTests.test_exceptionInTask   ss     		]]###&&(((T]++Q///q).0ABBBBBr   c                     | j                                          |                     t          | j         j                   |                     t          | j         j        t
                     dS )zx
        L{Team.quit} causes future invocations of L{Team.do} and L{Team.quit}
        to raise L{AlreadyQuit}.
        N)rB   r;   assertRaisesr	   r   listr?   s    r   	test_quitzTeamTests.test_quit   sR    
 		+ty~666+ty|T:::::r   c                    t          d          D ]!}| j                            t                     "|                                  | j                                         |                                  |                     t          | j                  d           | 	                    t          | j        j                   dS )zk
        L{Team.quit} causes all idle workers, as well as the coordinator
        worker, to quit.
        re   r   N)rl   rB   r   rv   rI   r;   rP   r9   r/   ru   r	   r(   r   xs     r   test_quitQuitszTeamTests.test_quitQuits   s    
 r 	 	AILL&&(((	&&(((T233Q777+t'7'<=====r   c                 0   | j                             d           t          d          D ]!}| j                             t                     "|                                  | j                                          |                                  |                     t          | j	                  d           | 
                                 |                     t          | j	                  d           |                     t          | j        j                   dS )z|
        L{Team.quit} causes all busy workers to be quit once they've finished
        the work they've been given.
        re   rZ   r   N)rB   r[   rl   r   rv   rF   r;   rP   r9   r/   rI   ru   r	   r(   ry   s     r   test_quitQuitsLaterWhenBusyz%TeamTests.test_quitQuitsLaterWhenBusy   s    
 		rq 	 	AILL	T233Q777&&(((T233Q777+t'7'<=====r   c                 j     j                             t                      j         j        j         fd}| j         j        _         j                                                               t           j         j                                        t           j         j        t                     dS )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                  D                                                     d S r   )rI   )originalSetr   s   r   performWorkConcurrentlyzOTeamTests.test_quitConcurrentWithWorkHappening.<locals>.performWorkConcurrently  s&    KMMM**,,,,,r   N)rB   r   rv   _quitsetr;   ru   r	   )r   r   r   s   ` @r   $test_quitConcurrentWithWorkHappeningz.TeamTests.test_quitConcurrentWithWorkHappening   s    
 		Tio)	- 	- 	- 	- 	- 	- 6		+ty~666+ty|T:::::r   c                    t          d          D ]!}| j                            t                     "|                                  |                     t          | j                  d           | j                            d           | 	                                 |                     t          | j                  d           dS )zl
        L{Team.shrink} will wait for busy workers to finish being busy and then
        quit them.
        re      r_   N)
rl   rB   r   rv   rF   rP   r9   r/   rb   rI   ry   s     r   test_shrinkWhenBusyzTeamTests.test_shrinkWhenBusy  s    
 r 	 	AILLT233R888	&&(((T233Q77777r   N)r    r!   r"   r#   rC   rF   rI   rS   rX   r\   r`   rc   rf   rn   rs   rw   r{   r}   r   r   r,   r   r   r&   r&   %   s        %B %B %BN  9 9 9D D D 7 7 78 8 8	D 	D 	D8 8 8
8 
8 
8- - -8	C 	C 	C; ; ;> > >> > > ; ; ;"8 8 8 8 8r   r&   N)r#   twisted.python.componentsr   twisted.python.contextr   r   twisted.python.failurer   twisted.trial.unittestr    r	   r
   r   r   r   r&   r,   r   r   <module>r      s    
 8 7 7 7 7 7 , , , , , , , , * * * * * * 6 6 6 6 6 6 = = = = = = = = = = = =6 6 6 6 6((-@@ 6 6 6*t8 t8 t8 t8 t8# t8 t8 t8 t8 t8r   