
    Yf@                        d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZmZ ddlmZmZmZ ddlmZ dd	lmZmZmZ dd
lm Z m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6m7Z7 ddl8m9Z9m:Z: ddl;m<Z< ddl=m>Z>m?Z?m@Z@mAZA ddlBmCZCmDZDmEZE  G d de e!          ZFdeFfdZGe G d d                       ZHe G d! d"                      ZIe G d# d$                      ZJd%eKd&e/deKfd'ZLd(eId)ee0         d&e<d*e
e<ee0         eDge	d         f         ddf
d+ZMe G d, d-                      ZNdS ).z
This module contains the trial distributed runner, the management class
responsible for coordinating all of trial's behavior at the highest level.

@since: 12.3
    N)partial)isabs)
Any	AwaitableCallableIterableListOptionalSequenceTextIOUnioncast)TestCase	TestSuite)definefieldfrozen)default_if_none)DeferredDeferredListgatherResults)IReactorCoreIReactorProcess)Logger)Failure)FilePath)FilesystemLock)theSystemPath   )_iterateTests)	IReporter	ITestCase)UncleanWarningsReporterWrapper)
TestHolder)_unusedTestDirectoryopenTestLog   )_WORKER_AMP_STDIN_WORKER_AMP_STDOUT)DistReporter)countingCallsdiscardResultiterateWhile	takeWhile)LocalWorkerLocalWorkerAMPWorkerActionc                       e Zd ZdZdS )IDistTrialReactorz7
    The reactor interfaces required by disttrial.
    N)__name__
__module____qualname____doc__     O/var/www/html/env/lib/python3.11/site-packages/twisted/trial/_dist/disttrial.pyr3   r3   3   s           r9   r3   returnc                      ddl mc m}  t          t	          j        |           t          j        |           g          rt          t          |           S t          d          )zR
    Get the default reactor, ensuring it is suitable for use with disttrial.
    r   Nz-Reactor does not provide the right interfaces)
twisted.internet.reactorinternetreactorallr   
providedByr   r   r3   	TypeError)defaultReactors    r:   _defaultReactorrD   9   st     655555555
#N33&~66	
  	7 %~666
C
D
DDr9   c                   T    e Zd ZU dZeed<   ee         ed<   ee	         ed<   e	ed<   dS )WorkerPoolConfiga  
    Configuration parameters for a pool of test-running workers.

    @ivar numWorkers: The number of workers in the pool.

    @ivar workingDirectory: A directory in which working directories for each
        of the workers will be created.

    @ivar workerArguments: Extra arguments to pass the worker process in its
        argv.

    @ivar logFile: The basename of the overall test log file.
    
numWorkersworkingDirectoryworkerArgumentslogFileN)
r4   r5   r6   r7   int__annotations__r   r   r   strr8   r9   r:   rF   rF   M   sN           OOOsm###c]"""LLLLLr9   rF   c                       e Zd ZU dZee         ed<   eed<   eed<   e	e
         ed<   e	e         ed<    e            Zdee         dd	fd
ZddZd	S )StartedWorkerPoolaE  
    A pool of workers which have already been started.

    @ivar workingDirectory: A directory holding the working directories for
        each of the workers.

    @ivar testDirLock: An object representing the cooperative lock this pool
        holds on its working directory.

    @ivar testLog: The open overall test log file.

    @ivar workers: Objects corresponding to the worker child processes and
        adapting between process-related interfaces and C{IProtocol}.

    @ivar ampWorkers: AMP protocol instances corresponding to the worker child
        processes.
    rH   testDirLocktestLogworkers
ampWorkersworkerActionr;   Nc                 X   K   t          fd| j        D                        d{V  dS )zB
        Run an action on all of the workers in the pool.
        c              3   H   K   | ]}t           |                    V  d S N)r,   ).0workerrT   s     r:   	<genexpr>z(StartedWorkerPool.run.<locals>.<genexpr>   sF       
 
4:M,,v..//
 
 
 
 
 
r9   N)r   rS   )selfrT   s    `r:   runzStartedWorkerPool.run   sh        
 
 
 
>Bo
 
 
 
 
 	
 	
 	
 	
 	
 	
 	
 tr9   c                 T  K   t          d | j        D             d           d{V }t          |          D ])\  }\  }}|s| j                            d| d|           *| j        dd= | j        dd= | j                                         | j        	                                 dS )zv
        Shut down all of the workers in the pool.

        The pool is unusable after this method is called.
        c                 Z    g | ](}t          j        |                                          )S r8   )r   fromCoroutineexit)rX   rY   s     r:   
<listcomp>z*StartedWorkerPool.join.<locals>.<listcomp>   s+    NNNvX#FKKMM22NNNr9   T)consumeErrorsNzjoining disttrial worker #z failed)
r   rR   	enumerate_loggerfailurerS   rQ   closerP   unlock)r[   resultsn	succeededre   s        r:   joinzStartedWorkerPool.join   s       %NNNNN
 
 
 
 
 
 
 
 
 (1'9'9 	W 	W#A#	7 W$$%L!%L%L%LgVVVLOOAAA!!!!!r9   )r;   N)r4   r5   r6   r7   r   r   rL   r   r   r	   r/   r0   r   rd   r1   r\   rk   r8   r9   r:   rO   rO   c   s          $ sm###OOO+^$$$$fhhGl3&7 D    " " " " " "r9   rO   c            	       p    e Zd ZU dZeed<   dee         dee	         de
dee         fdZd Zd	edefd
ZdS )
WorkerPoolz
    Manage a fixed-size collection of child processes which can run tests.

    @ivar _config: Configuration for the precise way in which the pool is run.
    _config	protocolsrH   rJ   r;   c                 >    fdt          |          D             S )a  
        Create local worker protocol instances and return them.

        @param protocols: The process/protocol adapters to use for the created
        workers.

        @param workingDirectory: The base path in which we should run the
            workers.

        @param logFile: The test log, for workers to write to.

        @return: A list of C{quantity} C{LocalWorker} instances.
        c           
      x    g | ]6\  }}t          |                    t          |                              7S r8   )r/   childrM   )rX   xprotocolrJ   rH   s      r:   ra   z2WorkerPool._createLocalWorkers.<locals>.<listcomp>   sN     
 
 
8 "2"8"8Q"@"@'JJ
 
 
r9   )rc   )r[   ro   rH   rJ   s     ``r:   _createLocalWorkerszWorkerPool._createLocalWorkers   s<    &
 
 
 
 
(33
 
 
 	
r9   c           
      x   t           d         j        j        }ddddddt          dt          di}t
          j                                        }t
          j        	                    t          j                  |d<   |D ]?}t          j        |g}|                    |            ||t          j        |||           @d	S )
a  
        Spawn processes from a list of process protocols.

        @param spawner: A C{IReactorProcess.spawnProcess} implementation.

        @param protocols: An iterable of C{ProcessProtocol} instances.

        @param arguments: Extra arguments passed to the processes.
        ztwisted.trial._dist.workertrialr   wr'   rr   
PYTHONPATH)argschildFDsenvN)r   filePathpathr(   r)   osenvironcopypathseprk   sys
executableextend)	r[   spawnerro   	argumentsworkertrialPathr{   r   rY   rz   s	            r:   _launchWorkerProcessesz!WorkerPool._launchWorkerProcesses   s     ((IJSXssss
 *//## !#
 9 9 	W 	WFNO4DKK	"""GFCNgVVVVV	W 	Wr9   r?   c                   K   t          | j        j                  \  }}t          | j        j                  rt          | j        j                  }n|                    | j        j                  }t          |          }d t          | j        j	                  D             }| 
                    |||          }|                     |j        || j        j                   t          |||||          S )z
        Launch all of the workers for this pool.

        @return: A started pool object that can run jobs using the workers.
        c                 *    g | ]}t                      S r8   )r0   )rX   rs   s     r:   ra   z$WorkerPool.start.<locals>.<listcomp>   s    OOO1n&&OOOr9   )r%   rn   rH   r   rJ   r   preauthChildr&   rangerG   ru   r   spawnProcessrI   rO   )r[   r?   testDirrP   testLogPathrQ   rS   rR   s           r:   startzWorkerPool.start   s       4L) 
  
 %&& 	E"4<#788KK
 "..t|/CDDKk**OOdl6M0N0NOOO
**
 

 	## L(	
 	
 	
 !
 
 	
r9   N)r4   r5   r6   r7   rF   rL   r   r0   r   r   r   r	   r/   ru   r   r   rO   r   r8   r9   r:   rm   rm      s           
N+
 #3-
 	

 
k	
 
 
 
0W W W6&
? &
7H &
 &
 &
 &
 &
 &
r9   rm   untilFailureresultc                 .    | o|                                 S )z
    Determine whether the test suite should be iterated again.

    @param untilFailure: C{True} if the suite is supposed to run until
        failure.

    @param result: The test result of the test suite iteration which just
        completed.
    )wasSuccessful)r   r   s     r:   shouldContinuer     s     2F00222r9   pool	testCasesdriveWorkerc                    K   	 |                      t          |||                     d {V  d S # t          $ r8 |j                            t          d          t                                 Y d S w xY w)Nz
<runTests>)r\   r   	ExceptionoriginaladdErrorr$   r   )r   r   r   r   s       r:   runTestsr     s      Fhhw{FI>>??????????? F F F 	  L!9!9799EEEEEE	Fs   *0 >A21A2c                      e Zd ZU dZeZ e            Zede	f         e
d<   ee
d<   ee         e
d<   dZee
d<    ed ee	          
          Zee
d<    ed eej                  
          Zee
d<   dZee
d<   dZee
d<   dZee
d<   dZee
d<   dZee
d<   eZee gef         e
d<   defdZ!d Z"dede#e$         de%ddfdZ&	 d$de'e(e)f         dedefdZ*d e'e(e)f         dede	fd!Z+d e'e(e)f         de	fd"Z,d e'e(e)f         de	fd#Z-dS )%DistTrialRunnera  
    A specialized runner for distributed trial. The runner launches a number of
    local worker processes which will run tests.

    @ivar _maxWorkers: the number of workers to be spawned.

    @ivar _exitFirst: ``True`` to stop the run as soon as a test case fails.
        ``False`` to run through the whole suite and report all of the results
        at the end.

    @ivar stream: stream which the reporter will use.

    @ivar _reporterFactory: the reporter class to be used.
    ._reporterFactory_maxWorkers_workerArgumentsF
_exitFirstN)factory)default	converter_reactorstreamr   _tracebackFormat_realTimeErrors_uncleanWarningsztest.log_logfile_trial_temp_workingDirectory_workerPoolFactoryr;   c                     |                      | j        | j        | j                  }| j        rt          |          }|                     |          S )zL
        Make reporter factory, and wrap it with a L{DistReporter}.
        )realtime)r   r   r   r   r   r#   _distReporterFactory)r[   reporters     r:   _makeResultzDistTrialRunner._makeResultJ  s\     ((K.9M ) 
 
   	@5h??H((222r9   c                 .    |                                  dS )z
        Write test run final outcome to result.

        @param result: A C{TestResult} which will print errors and the summary.
        N)done)r[   r   s     r:   writeResultszDistTrialRunner.writeResultsU  s     	r9   r   r   rY   c                 F   K   fd}|D ]} ||           d{V  dS )a  
        Drive a L{LocalWorkerAMP} instance, iterating the tests and calling
        C{run} for every one of them.

        @param worker: The L{LocalWorkerAMP} to drive.

        @param result: The global L{DistReporter} instance.

        @param testCases: The global list of tests to iterate.

        @return: A coroutine that completes after all of the tests have
            completed.
        c                    K   	                      |            d {V  d S # t          $ r+ j                            | t	                                 Y d S w xY wrW   )r\   r   r   r   r   )caser   rY   s    r:   taskz*DistTrialRunner._driveWorker.<locals>.taskq  sx      :jjv........... : : :((wyy999999:s   # 1AANr8   )r[   r   r   rY   r   r   s    ` `  r:   _driveWorkerzDistTrialRunner._driveWorker]  sd      (	: 	: 	: 	: 	: 	:  	 	D$t**	 	r9   suiter   c           	         K   t          t          |                                         t          t	          t                     j                  t           j                   j	         j
                            } j                            d|                                 d           |                     j                   d{V t!          t"                    }t$          dt&          dt(          f fd            }	 t+          ||           d{V 	                                  d{V  S #                                  d{V  w xY w)a*  
        Spawn local worker processes and load tests. After that, run them.

        @param suite: A test or suite to be run.

        @param untilFailure: If C{True}, continue to run the tests until they
            fail.

        @return: A coroutine that completes with the test result.
        zRunning z tests.
Nri   r;   c                   K   r!j                             d| dz    d                                           j        rfd}nd }t	          t          |          j                   d {V                                 S )Nz
Test Pass r'   
c                 6    j                                         S rW   )r   r   )_r   s    r:   <lambda>z@DistTrialRunner.runAsync.<locals>.runAndReport.<locals>.<lambda>  s    6?+H+H+J+J r9   c                     dS NTr8   )r   s    r:   r   z@DistTrialRunner.runAsync.<locals>.runAndReport.<locals>.<lambda>  s    4 r9   )r   writer   r   r   r.   r   r   )ri   casesConditionr   r[   startedPoolr   r   s     @r:   runAndReportz.DistTrialRunner.runAsync.<locals>.runAndReport  s       : !!"8q1u"8"8"8999%%''F 0 "K!J!J!J!/.)44!	         f%%%Mr9   )listr    r   rF   minlenr   r   r   r   r   r   r   countTestCasesr   r   r   r   r+   rK   r*   r-   rk   )r[   r   r   poolStarter	conditionr   r   r   s   ` `   @@r:   runAsynczDistTrialRunner.runAsyncz  s     " u--..	 --C	NND$455/00% 
 
 	FU%9%9%;%;FFFGGG (--dm<<<<<<<< NL99	 
	# 	, 	 	 	 	 	 	 	 	 
	0	% &i>>>>>>>>> ""$$$$$$$$$$+""$$$$$$$$$$s   D? ?Etestc                     d ddt           t          t          f         dd ffd}dt          t          t
                            ffd}dt
          dt
          f fd} j                            dd	|           t	          j         	                    ||                    
                    |          
                    |           j                                         t          t                    r                                 t          t                    sJ  d
            t          t          j                  S )NFrx   r;   c                     | d S rW   r8   )rx   r   s    r:   capturez%DistTrialRunner._run.<locals>.capture  s    FFFr9   c                  <    d                                   S d S r   )cancel)reactorStoppingr   testsInProgresss   r:   maybeStopTestsz,DistTrialRunner._run.<locals>.maybeStopTests  s*    "O~&&(((&&4r9   r   c                 >    sj                                          | S rW   )r   stop)r   r   r[   s    r:   maybeStopReactorz.DistTrialRunner._run.<locals>.maybeStopReactor  s$    " %""$$$Mr9   beforeshutdownz is not DistReporter)r   r   r*   r
   r   objectr   addSystemEventTriggerr_   r   addBothr\   
isinstanceraiseExceptionr   r!   r   )	r[   r   r   r   r   r   r   r   r   s	   `     @@@r:   _runzDistTrialRunner._run  s   59 %	uWl23 	 	 	 	 	 	 		&)9 : 	 	 	 	 	 	 	 		V 	 	 	 	 	 	 	 	
 	++Hj.QQQ "4==|#D#DEEWWW%&& 	 	fg&& 	$!!###
 &,//PPF1P1P1PPP/ Iv///r9   c                 0    |                      |d          S )za
        Run a reactor and a test suite.

        @param test: The test or suite to run.
        Fr   r   r[   r   s     r:   r\   zDistTrialRunner.run  s     yyEy222r9   c                 0    |                      |d          S )z|
        Run the tests with local worker processes until they fail.

        @param test: The test or suite to run.
        Tr   r   r   s     r:   runUntilFailurezDistTrialRunner.runUntilFailure  s     yyDy111r9   )F).r4   r5   r6   r7   r*   r   r   rd   r   r!   rL   rK   r	   rM   r   boolr   r   rD   r   r3   r   stdoutr   r   r   r   r   r   r   rm   r   rF   r   r   r   r"   r0   r   r   r   r   r   r   r\   r   r8   r9   r:   r   r   !  s          (fhhG sI~....3iJ"'%!//:::# # #H    U4??3:3N3NOOOFFOOO%c%%%!OT!!!"d"""Hc*s***CM"2!3Z!?@MMM	3\ 	3 	3 	3 	3   I& 	
 
   @ #J% J%Xy()J% J% 
	J% J% J% J%X*0x23 *04 *0I *0 *0 *0 *0X3h	12 3y 3 3 3 32E(I*=$> 29 2 2 2 2 2 2r9   r   )Or7   r   r   	functoolsr   os.pathr   typingr   r   r   r   r	   r
   r   r   r   r   unittestr   r   attrsr   r   r   attrs.convertersr   twisted.internet.deferr   r   r   twisted.internet.interfacesr   r   twisted.loggerr   twisted.python.failurer   twisted.python.filepathr   twisted.python.lockfiler   twisted.python.modulesr   _asyncrunnerr    itrialr!   r"   r   r#   runnerr$   utilr%   r&    r(   r)   distreporterr*   
functionalr+   r,   r-   r.   rY   r/   r0   r1   r3   rD   rF   rO   rm   r   r   r   r   r8   r9   r:   <module>r     s<  
  
			 



                                    ) ( ( ( ( ( ( ( ' ' ' ' ' ' ' ' ' ' , , , , , , H H H H H H H H H H E E E E E E E E ! ! ! ! ! ! * * * * * * , , , , , , 2 2 2 2 2 2 0 0 0 0 0 0 ( ( ( ( ( ( ) ) ) ) ) ) ) ) 5 5 5 5 5 5       4 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 & & & & & & M M M M M M M M M M M M = = = = = = = = = =    o   E* E E E E(        * 5" 5" 5" 5" 5" 5" 5" 5"p b
 b
 b
 b
 b
 b
 b
 b
J
3 
3y 
3T 
3 
3 
3 
3F
F	"F F 	x	*N;Yt_L	F 
F F F F$ ^2 ^2 ^2 ^2 ^2 ^2 ^2 ^2 ^2 ^2r9   