o
    >h@                     @   s2  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 de e!ZFdeFfddZGeG d d! d!ZHeG d"d# d#ZIeG d$d% d%ZJd&eKd'e/deKfd(d)ZLd*eId+ee0 d'e<d,e
e<ee0 eDge	d f ddf
d-d.ZMeG d/d0 d0ZNdS )1z
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                   @   s   e Zd ZdZdS )IDistTrialReactorz7
    The reactor interfaces required by disttrial.
    N)__name__
__module____qualname____doc__ r6   r6   }/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/trial/_dist/disttrial.pyr1   3   s    r1   returnc                  C   s<   ddl m  m}  tt| t| 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   r1   	TypeError)defaultReactorr6   r6   r7   _defaultReactor9   s   
	r@   c                   @   s:   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)
r2   r3   r4   r5   int__annotations__r   r   r
   strr6   r6   r6   r7   rA   M   s   
 rA   c                   @   sl   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
dZd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.
    rC   testDirLocktestLogworkers
ampWorkersworkerActionr8   Nc                    s$   t  fdd| jD I dH  dS )zB
        Run an action on all of the workers in the pool.
        c                 3   s    | ]	}t  |V  qd S N)r+   .0workerrN   r6   r7   	<genexpr>   s    
z(StartedWorkerPool.run.<locals>.<genexpr>N)r   rM   )selfrN   r6   rS   r7   run   s
   zStartedWorkerPool.runc                    s   t dd | jD ddI dH }t|D ]\}\}}|s'| jd| d| q| 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                 S   s   g | ]	}t | qS r6   )r   fromCoroutineexitrP   r6   r6   r7   
<listcomp>   s    z*StartedWorkerPool.join.<locals>.<listcomp>T)consumeErrorsNzjoining disttrial worker #z failed)
r   rL   	enumerate_loggerfailurerM   rK   closerJ   unlock)rU   resultsn	succeededr]   r6   r6   r7   join   s   
zStartedWorkerPool.join)r8   N)r2   r3   r4   r5   r   r   rG   r   r   r   r.   r/   r   r\   r0   rV   rc   r6   r6   r6   r7   rI   c   s   
 	rI   c                	   @   sZ   e Zd ZU dZeed< dee dee	 de
dee fddZd	d
 Zdedefd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	protocolsrC   rE   r8   c                    s    fd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                    s&   g | ]\}}t |t| qS r6   )r.   childrH   )rQ   xprotocolrE   rC   r6   r7   rY      s    z2WorkerPool._createLocalWorkers.<locals>.<listcomp>)r[   )rU   rf   rC   rE   r6   rj   r7   _createLocalWorkers   s   zWorkerPool._createLocalWorkersc           	   
   C   sv   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 q"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pathseprc   sys
executableextend)	rU   spawnerrf   	argumentsworkertrialPathrp   ru   rR   ro   r6   r6   r7   _launchWorkerProcesses   s   



z!WorkerPool._launchWorkerProcessesr;   c                    s   t | jj\}}t| jjrt| jj}n|| jj}t|}d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                 S   s   g | ]}t  qS r6   )r/   )rQ   rh   r6   r6   r7   rY      s    z$WorkerPool.start.<locals>.<listcomp>)r$   re   rC   r   rE   r   preauthChildr%   rangerB   rk   r~   spawnProcessrD   rI   )rU   r;   testDirrJ   testLogPathrK   rM   rL   r6   r6   r7   start   s4   zWorkerPool.startN)r2   r3   r4   r5   rA   rG   r   r/   r   r   r   r   r.   rk   r~   r   rI   r   r6   r6   r6   r7   rd      s   
 
rd   untilFailureresultc                 C   s   | 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   r6   r6   r7   shouldContinue  s   
r   pool	testCasesdriveWorkerc                    sJ   z|  t|||I d H  W d S  ty$   |jtdt  Y d S w )Nz
<runTests>)rV   r   	ExceptionoriginaladdErrorr#   r   )r   r   r   r   r6   r6   r7   runTests  s   r   c                   @   sz  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d	d
Zee
d< edeej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< dZee
d< eZee gef e
d< defddZ!dd Z"dede#e$ de%ddfddZ&	d+d e'e(e)f d!edefd"d#Z*d$e'e(e)f d!ede	fd%d&Z+d$e'e(e)f de	fd'd(Z,d$e'e(e)f de	fd)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_workerPoolFactoryr8   c                 C   s.   | j | j| j| jd}| jrt|}| |S )zL
        Make reporter factory, and wrap it with a L{DistReporter}.
        )realtime)r   r   r   r   r   r"   _distReporterFactory)rU   reporterr6   r6   r7   _makeResultJ  s   
zDistTrialRunner._makeResultc                 C   s   |   dS )z
        Write test run final outcome to result.

        @param result: A C{TestResult} which will print errors and the summary.
        N)done)rU   r   r6   r6   r7   writeResultsU  s   zDistTrialRunner.writeResultsr   r   rR   c                    s,    fdd}|D ]	}||I dH  q
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                    s@   z |  I d H  W d S  ty    j| t  Y d S w rO   )rV   r   r   r   r   )caser   rR   r6   r7   taskq  s   z*DistTrialRunner._driveWorker.<locals>.taskNr6   )rU   r   r   rR   r   r   r6   r   r7   _driveWorker]  s
   zDistTrialRunner._driveWorkersuiter   c                    s   t t| ttt jt j j	 j
} jd|  d | jI dH tt}tdtdtf fdd}zt||I dH W  I dH  S  I dH  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.
Nra   r8   c                    sl   rj d| d  d   jr fdd}ndd }tt| jI d H     S )Nz
Test Pass r&   
c                    s
    j  S rO   )r   r   _r   r6   r7   <lambda>  s   
 z@DistTrialRunner.runAsync.<locals>.runAndReport.<locals>.<lambda>c                 S   s   dS NTr6   r   r6   r6   r7   r     s    )r   writer   r   r   r-   r   r   )ra   casesConditionrU   startedPoolr   r   r   r7   runAndReport  s   

z.DistTrialRunner.runAsync.<locals>.runAndReport)listr   r   rA   minlenr   r   r   r   r   r   r   countTestCasesr   r   r   r   r*   rF   r)   r,   rc   )rU   r   r   poolStarter	conditionr   r6   r   r7   runAsyncz  s$   
 zDistTrialRunner.runAsynctestc                    s   d d dt ttf dd ffdd}dttt  f fdd}dtdtf fd	d
}jdd| t	||
|
|j  ttrS  tts_J  dttjS )NFrm   r8   c                    s   |  d S rO   r6   )rm   r   r6   r7   capture  s   z%DistTrialRunner._run.<locals>.capturec                      s   d d u r   S d S r   )cancelr6   )reactorStoppingr   testsInProgressr6   r7   maybeStopTests  s
   z,DistTrialRunner._run.<locals>.maybeStopTestsr   c                    s    sj   | S rO   )r   stopr   )r   rU   r6   r7   maybeStopReactor  s   
z.DistTrialRunner._run.<locals>.maybeStopReactorbeforeshutdownz is not DistReporter)r   r   r)   r	   r   objectr   addSystemEventTriggerrW   r   addBothrV   
isinstanceraiseExceptionr   r    r   )rU   r   r   r   r   r   r6   )r   r   rU   r   r7   _run  s   

zDistTrialRunner._runc                 C      | j |ddS )za
        Run a reactor and a test suite.

        @param test: The test or suite to run.
        Fr   r   rU   r   r6   r6   r7   rV        zDistTrialRunner.runc                 C   r   )z|
        Run the tests with local worker processes until they fail.

        @param test: The test or suite to run.
        Tr   r   r   r6   r6   r7   runUntilFailure  r   zDistTrialRunner.runUntilFailure)F).r2   r3   r4   r5   r)   r   r   r\   r   r    rG   rF   r   rH   r   boolr   r   r@   r   r1   rx   stdoutr   r   r   r   r   r   r   rd   r   rA   r   r   r
   r!   r/   r   r   r   r   r   r   rV   r   r6   r6   r6   r7   r   !  sR   
 
 

L,r   )Or5   rt   rx   	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-   rR   r.   r/   r0   r1   r@   rA   rI   rd   r   r   r   r   r6   r6   r6   r7   <module>   sb   08e
