
    Yf&k                        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 ddl	m
Z
mZmZ ddlmZ ddlmZmZ dd	lmZmZmZmZ dd
lmZmZmZmZmZmZmZ ddl m!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. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddlBmCZCmDZDmEZE ddlFmGZGmHZHmIZImJZJmKZK ddlLmMZMmNZNmOZOmPZP ddlQmRZRmSZSmTZTmUZU ddlVmWZWmXZX ddlYmZZZmZ d d!l[m\Z\m]Z] d"d#l^m_Z_ e G d$ d%                      Z` ee(ja                   G d& d'e7                      Zb G d( d)eZ          Zc G d* d+e          Zd G d, d-e          Ze G d. d/e          Zf G d0 d1eg          Zh G d2 d3          Zie G d4 d5                      Zj G d6 d7          Zk G d8 d9eg          Zl G d: d;          Zme G d< d=                      Zne G d> d?                      ZodS )@z-
Tests for L{twisted.trial._dist.disttrial}.
    N)partial)StringIO)sep)CallableListSetTestCase)implementerverify)Factoryassocdefinefield)assert_thatcontains	ends_withequal_to
has_lengthnonestarts_with)AllOf)given)booleanssampled_from)
interfaces)ReactorBase)CancelledErrorDeferredsucceed)ProcessDone)ProcessProtocolProtocol)AlternateReactor)MemoryReactorClock)Failure)FilePath)FilesystemLock)_WORKER_AMP_STDIN)DistReporter)DistTrialRunner
WorkerPoolWorkerPoolConfig)countingCallsdiscardResultfromOptionaliterateWhilesequence)LocalWorker	RunResultWorkerWorkerAction)Reporter
TestResultTreeReporterUncleanWarningsReporterWrapper)ErrorHolder
TrialSuite)SynchronousTestCaser
      )	erroneoussample   )matches_resultc                   `    e Zd ZU dZ e ee                    Zee	         e
d<   d Zd ZdS )FakeTransportz*
    A simple fake process transport.
    default_closedc                     dS )z%
        Ignore write calls.
        N )selffddatas      Y/var/www/html/env/lib/python3.11/site-packages/twisted/trial/_dist/test/test_disttrial.pywriteToChildzFakeTransport.writeToChildI   s          c                 :    | j                             |           dS )z>
        Mark one of the child descriptors as closed.
        N)rG   add)rJ   rK   s     rM   closeChildFDzFakeTransport.closeChildFDN   s      	rO   N)__name__
__module____qualname____doc__r   r   setrG   r   int__annotations__rN   rR   rI   rO   rM   rD   rD   A   si           ggcll333GSX333  
    rO   rD   c                   F    e Zd ZdZdZdZdZd Zdi dddddfdZd Z	d Z
dS )	CountingReactorz
    A fake reactor that counts the calls to L{IReactorCore.run},
    L{IReactorCore.stop}, and L{IReactorProcess.spawnProcess}.
    r   c                 <    t          j        |            || _        d S N)r%   __init___workers)rJ   workerss     rM   r^   zCountingReactor.__init__`   s    #D)))rO   rI   Nc
                     | j                             |           |                    t                                 | xj        dz  c_        dS )z
        See L{IReactorProcess.spawnProcess}.

        @param workerProto: See L{IReactorProcess.spawnProcess}.
        @param args: See L{IReactorProcess.spawnProcess}.
        @param kwargs: See L{IReactorProcess.spawnProcess}.
        rA   N)r_   appendmakeConnectionrD   
spawnCount)
rJ   workerProto
executableargsenvpathuidgidusePTYchildFDss
             rM   spawnProcesszCountingReactor.spawnProcessd   sF    & 	[)))""=??3331rO   c                     t          j        |            d| j        v r' | j        d         d         d         d                      | xj        dz  c_        dS )z+
        See L{IReactorCore.stop}.
        beforeshutdownr   rA   N)r%   stoptriggers	stopCountrJ   s    rM   rr   zCountingReactor.stop{   s\     	%%% t}$$5DM(#J/215777!rO   c                     | xj         dz  c_         d| _        d| _        | j        D ]\  }}} ||i | |                                  | xj        dz  c_        dS )z*
        See L{IReactorCore.run}.
        rA   TN)runCountrunninghasRunwhenRunningHooksrr   rt   )rJ   frg   kwargss       rM   runzCountingReactor.run   su     	 #4 	 	OAtVAtv		!rO   )rS   rT   rU   rV   rd   rt   rw   r^   rn   rr   r}   rI   rO   rM   r[   r[   U   s         
 JIH         .
 
 
    rO   r[   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )CountingReactorTestsz'
    Tests for L{CountingReactor}.
    c                 F    g | _         t          | j                   | _        d S r]   )r`   r[   reactorru   s    rM   setUpzCountingReactorTests.setUp   s    &t|44rO   c                 N    t          j        t          j        | j                   dS )zJ
        L{CountingReactor} instances provide L{IReactorProcess}.
        N)r   verifyObjectr   IReactorProcessr   ru   s    rM   test_providesIReactorProcessz1CountingReactorTests.test_providesIReactorProcess   s"     	J6EEEEErO   c                    |                      | j        j                   t                      }dD ]}| j                            |t
          j        t
          j        g           |                     |j                   | 	                    | j
        |g|z             | 	                    | j        j        |           dS )z
        The process protocol for a spawned process is connected to a
        transport and appended onto the provided C{workers} list, and
        the reactor's C{spawnCount} increased.
        rA      )rg   N)assertFalser   rd   r#   rn   sysrf   
assertTrue	transportassertEqualr`   )rJ   protocounts      rM   test_spawnProcessz&CountingReactorTests.test_spawnProcess   s     	0111

 	= 	=EL%%eS^3>BR%SSSOOEO,,,T\E7U?;;;T\4e<<<<		= 	=rO   c                     |                      | j        j                   dD ];}| j                                         |                     | j        j        |           <dS )zB
        Stopping the reactor increments its C{stopCount}
        r   N)r   r   rt   rr   r   )rJ   r   s     rM   	test_stopzCountingReactorTests.test_stop   sh     	/000 	< 	<ELT\3U;;;;	< 	<rO   c                    |                      | j        j                   g }| j                            |j        d           dD ]~}| j                                         |                     | j        j        |           |                     | j        j        d           |                     t          |          |           dS )z
        Running the reactor increments its C{runCount}, does not imply
        C{stop}, and calls L{IReactorCore.callWhenRunning} hooks.
        Nr   r   )	r   r   rw   callWhenRunningrb   r}   r   rt   len)rJ   whenRunningCallsr   s      rM   test_runzCountingReactorTests.test_run   s    
 	.///$$%5%<dCCC 	; 	;ELT\2E:::T\3Q777S!122E::::		; 	;rO   N)	rS   rT   rU   rV   r   r   r   r   r   rI   rO   rM   r   r      sl         5 5 5F F F= = =< < <; ; ; ; ;rO   r   c            	           e Zd ZdZd Zd Zd Zd Zd Zd Z	 e
 e             edd	e dg                    d
ededdfd            ZdS )WorkerPoolTestsz"
    Tests for L{WorkerPool}.
    c                     t          |                                           | _        | j                            d          | _        t          d| j        g d          | _        t          | j                  | _        d S )N_trial_temp   out.log)
numWorkersworkingDirectoryworkerArgumentslogFile)	r'   mktempparentchildr   r-   configr,   poolru   s    rM   r   zWorkerPoolTests.setUp   sm    t{{}}-- $ 1 1- @ @&!2	
 
 
 t{++			rO   c                 (   d t          d          D             }| j                            |t          d          t	                                }|D ]}|                     |t                     |                     dt          |                     dS )zw
        C{_createLocalWorkers} iterates the list of protocols and create one
        L{LocalWorker} for each.
        c                 *    g | ]}t                      S rI   )object).0xs     rM   
<listcomp>z;WorkerPoolTests.test_createLocalWorkers.<locals>.<listcomp>   s    000!VXX000rO   r   ri   N)	ranger   _createLocalWorkersr'   r   assertIsInstancer3   r   r   )rJ   	protocolsr`   ss       rM   test_createLocalWorkersz'WorkerPoolTests.test_createLocalWorkers   s    
 10uQxx000	)//	8F;K;KXZZXX 	2 	2A!!![1111CLL)))))rO   c                    d t          d          D             }g i di dddddffd	}| j                            ||dg           |                     d         d                    |                     t
          j                            d	                              |                     dd
                    |                     t
          j        	                    t          j                  d                    dS )z
        Given a C{spawnProcess} function, C{_launchWorkerProcess} launches a
        python process with an existing path as its argument.
        c                 *    g | ]}t                      S rI   )r"   )r   is     rM   r   z>WorkerPoolTests.test_launchWorkerProcesses.<locals>.<listcomp>   s    9991_&&999rO   r   rI   Nr   c	                     	                     |           	                    |           
                    |           d S r]   )rb   extendupdate)processProtocolrf   rg   rh   ri   rj   rk   rl   rm   	argumentsenvironments            rM   fakeSpawnProcesszDWorkerPoolTests.test_launchWorkerProcesses.<locals>.fakeSpawnProcess   sG     Z(((T"""s#####rO   foorA   r   r>   
PYTHONPATH)r   r   _launchWorkerProcessesr   r   osri   existspathsepjoinr   )rJ   r   r   r   r   s      @@rM   test_launchWorkerProcessesz*WorkerPoolTests.test_launchWorkerProcesses   s   
 :9a999		
 	$ 	$ 	$ 	$ 	$ 	$ 	$ 		(()99ugNNN1y|444y|44555	!--- 	22K4MNNNNNrO   c                 ^   | j                                          g | j                            t	          g                     }|                     |          }|                    fd          }|                     |           t          t          | j	        j
                             dS )zY
        C{run} dispatches the given action to each of its workers exactly once.
        c                 H    t                              |                     S r]   )r    rb   )wr`   s    rM   <lambda>z*WorkerPoolTests.test_run.<locals>.<lambda>  s    q0A0A(B(B rO   N)r   makedirsr   startr[   successResultOfr}   r   r   r   r   )rJ   startingstartedrx   r`   s       @rM   r   zWorkerPoolTests.test_run  s     	9???2#6#677&&x00++BBBBCCW%%%GZ(>??@@@@@rO   c                    | j                                          t          | j        j        dz             }|                     |                                           |                     |j                   t          g           }| 
                    | j                            |                    }|                     |j        | j                            d                     dS )z
        L{WorkerPool.start} checks if the test directory is already locked, and if
        it is generates a name based on it.
        z.lockz_trial_temp-1N)r   r   r(   r   ri   r   lock
addCleanupunlockr[   r   r   r   r   sibling)rJ   r   fakeReactorr   s       rM   test_runUsedDirectoryz%WorkerPoolTests.test_runUsedDirectory  s     	 d387BCC		$$$$$$ &b))&&ty{'C'CDD 	$!))/::	
 	
 	
 	
 	
rO   c           	      @   | j                                          t          g           }|                     | j                            |                    }t          j        |                                          }| 	                    |           |j
        D ]}t          |j        j        t          t                               |j        j        D ]}|                    |           |j        |j        fD ]'} |t'          t)          d                               (t          |                     |          t+                                 t          |j        j        t1          d                     t          |j        j        t1          d                     dS )z
        L{StartedWorkerPool.join} causes all of the workers to exit, closes the
        log file, and unlocks the test directory.
        r   TFN)r   r   r[   r   r   r   r   fromCoroutiner   assertNoResultr_   r   r   rG   r   r)   childConnectionLostprocessExitedprocessEndedr&   r!   r   testLogclosedr   testDirLocklocked)rJ   r   r   joiningr   rK   r{   s          rM   	test_joinzWorkerPoolTests.test_join3  sj   
 	!"%%&&tyw'?'?@@(88G$$$! 	+ 	+A+X6G-H-HIIIk) * *%%b))))oq~6 + +'+a..))****+D((11466:::GO*HTNN;;;G'.@@@@@rO   r   subdirabsoluter   returnNc                    |r| j         j        t          z   |z   }t          | j        |          }|rt          |          }n<t          t          |j        j                  t          t          |z                       }t          |          }|                     |                    t          g                               }t          |j        j        |           dS )z
        L{WorkerPool.start} creates a L{StartedWorkerPool} configured with a
        log file based on the L{WorkerPoolConfig.logFile}.
        )r   N)r   ri   r   r   r   r   r   r   r   r   r,   r   r   r[   r   r   name)rJ   r   r   r   matchesr   r   s          rM   test_logFilezWorkerPoolTests.test_logFileH  s      	7k&,w6Gt{G444 		w''GG F3899#-(( G &!!&&tzz/"2E2E'F'FGGGO('22222rO   )rS   rT   rU   rV   r   r   r   r   r   r   r   r   r   r   boolstrr   rI   rO   rM   r   r      s         	, 	, 	,	* 	* 	*O O OBA A A
 
 
0A A A* U

%%%%	
 	
 3T 3C 3D 3 3 3 3 3 3rO   r   c                       e Zd ZdZ e ej        d          g          Zd Zd Z	d Z
ddZd	 Zd
 ZddZddZddZddZd Zd ZddZddZddZd ZdS )DistTrialRunnerTestsz'
    Tests for L{DistTrialRunner}.
    test_fooc                     t          t          |                                 t                      dg t	          t
          d          t          g                     }|                    |           t          di |S )z.
        Create a runner for testing.
        r   T)autostop)reporterFactoryr   stream
maxWorkersr   workerPoolFactoryr   rI   )	dictr9   r   r   r   LocalWorkerPoolr[   r   r+   )rJ   	overridesrg   s      rM   	getRunnerzDistTrialRunnerTests.getRunnerr  sv     (![[]]::%oEEE#B''
 
 
 	I&&&&&rO   c                    t                      }t          t          |                    }|                                 }|                    |           |                     |                                dk               dS )ze
        L{DistTrialRunner.writeResults} writes to the stream specified in the
        init.
        r   N)r   r*   r7   r   writeResultsr   tell)rJ   stringIOresultrunners       rM   test_writeResultsz&DistTrialRunnerTests.test_writeResults  sj    
 ::hx0011!!F###!+,,,,,rO   c                 B   dfd}d}d}|                      ||          }t          d t          |          D                       }|                     |                    |                     t          j        d         j        t          |                     dS )zq
        L{DistTrialRunner.runAsync} doesn't try to start more workers than the
        number of tests.
        Nc                  $    t          | ddi|S )Nr   Tr   akwr   s     rM   recordingFactoryzADistTrialRunnerTests.test_minimalWorker.<locals>.recordingFactory  s    "A;;;;DKrO      r>   )r   r   c                 *    g | ]}t                      S rI   r	   )r   ns     rM   r   z;DistTrialRunnerTests.test_minimalWorker.<locals>.<listcomp>  s    @@@1HJJ@@@rO   r   )	r   r<   r   r   runAsyncr   _startedr`   r   )rJ   r  r   numTestsr   suiter   s         @rM   test_minimalWorkerz'DistTrialRunnerTests.test_minimalWorker  s    
 	 	 	 	 	
 
!5E   
 
 @@h@@@AAV__U33444DM!$,j.B.BCCCCCrO   r   Nc                    |                      d          }|                    | j                  }|                     |          }|                     |t
                     |                     |j        t                     dS )z
        Running with the C{unclean-warnings} option makes L{DistTrialRunner} uses
        the L{UncleanWarningsReporterWrapper}.
        T)uncleanWarningsN)r   r	  r  r   r   r*   originalr:   )rJ   r   dr   s       rM   test_runUncleanWarningsz,DistTrialRunnerTests.test_runUncleanWarnings  sr    
 55OODJ''%%a((fl333fo/MNNNNNrO   c                 l   t                      }|                     |          }|                     |                    t	                                          }|                     |t                     |                                }|                     d|           |                     d|           dS )zA
        L{DistTrialRunner} can run an empty test suite.
        r   Running 0 testPASSEDN)	r   r   r   r	  r<   r   r*   getvalueassertIn)rJ   r   r   r   outputs        rM   test_runWithoutTestz(DistTrialRunnerTests.test_runWithoutTest  s     v..%%foojll&C&CDDfl333""&///h'''''rO   c                 ,   t          dt          t          d                              }t                      }|                     |          }|                     |                    |                    }|                     |t                     |	                                }| 
                    d|           | 
                    d|           | 
                    d|           | 
                    d|           | 
                    d|           dS )z
        Even if there is no test, the suite can contain an error (most likely,
        an import error): this should make the run fail, and the error should
        be printed.
        zan errorzfoo barr  r  zerrors=1FAILEDN)r;   r&   RuntimeErrorr   r   r   r	  r   r*   r  r  )rJ   errr   r   r   r  s         rM   !test_runWithoutTestButWithAnErrorz6DistTrialRunnerTests.test_runWithoutTestButWithAnError  s     *gl9.E.E&F&FGGv..%%fooc&:&:;;fl333""&///i(((j&)))j&)))h'''''rO   c                 N   |                      t                    }|                     |                    | j                            }|j        j        }t          |t          d                     t          |d         d         j	        t          t                               dS )z
        If for some reasons we can't connect to the worker process, the error is
        recorded in the result object.
        r   rA   r   N)r   BrokenWorkerPoolr   r	  r  r  errorsr   r   typer   WorkerPoolBrokenrJ   r   r   r#  s       rM   test_runUnexpectedErrorz,DistTrialRunnerTests.test_runUnexpectedError  s    
 2BCC%%foodj&A&ABB'FJqMM***F1IaL%x0@'A'ABBBBBrO   c                     |                      t                    }|                     t                    5  |                    | j                   ddd           dS # 1 swxY w Y   dS )z
        If the reactor is stopped by C-c (i.e. `run` returns before the test
        case's Deferred has been fired) we should cancel the pending test run.
        r!  N)r   r   assertRaisesr   r}   r  )rJ   r   s     rM   test_runUnexpectedErrorCtrlCz1DistTrialRunnerTests.test_runUnexpectedErrorCtrlC  s    
 /BB~.. 	# 	#JJtz"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   AA"%A"c                 x   |                      t          t          t          d                    }|                     |                    | j                            }|j        j        }t          |t          d                     t          |d         d         j        t          t                               dS )z~
        If for some reason the worker process cannot run a test, the error is
        recorded in the result object.
        T)workerFactoryr   r!  rA   r   N)r   r   r   _BrokenLocalWorkerr   r	  r  r  r#  r   r   r$  r   WorkerBrokenr&  s       rM   test_runUnexpectedWorkerErrorz2DistTrialRunnerTests.test_runUnexpectedWorkerError  s    
 %/AD     
 

 %%foodj&A&ABB'FJqMM***F1IaL%x'='=>>>>>rO   c                    dfd}|                      |          }t          j        |                    | j                            }|                     d           J j        d         j        }|                     |           |	                    d           | 
                    |          }|                     |t                     dS )zG
        L{DistTrialRunner} waits for the worker pool to stop.
        Nc                  $    t          | ddi|S )Nr   Fr  r  s     rM   r  zPDistTrialRunnerTests.test_runWaitForProcessesDeferreds.<locals>.recordingFactory  s    "A<<<<DKrO   r!  zworker pool was never createdr   )r   r   r   r	  r  failr
  _stoppedr   callbackr   r   r*   )rJ   r  r   r  stoppedr   r   s         @rM   !test_runWaitForProcessesDeferredsz6DistTrialRunnerTests.test_runWaitForProcessesDeferreds  s     	 	 	 	 	
 .   
 
 "6??4:#>#>??<II5666-"+A%%a((fl33333rO   c           	         t                      }t          t          j        d          t	          j        d          t          j        d          g          }|                     |dd          }|                    |          }|                     |          }t          |j
        t          dt          d                               d	S )
z
        L{DistTrialRunner} can run in C{exitFirst} mode where it will run until a
        test fails and then abandon the rest of the suite.
        r   	test_failtest_barTr   )r   	exitFirstr   rA   )	successesfailuresN)r   r<   r@   FooTestr?   TestRegularFailr   r	  r   r   r  rB   r   )rJ   r   r  r   r  r   s         rM   test_exitFirstz#DistTrialRunnerTests.test_exitFirst	  s    
 z**)+66z**
 
 v!LLOOE""%%a((O#A  	
 	
 	
 	
 	
rO   c                    t                      }t          j        d          }|                     |          }|                    |d          }|                     |          }|                     d|j                   |                     |	                                           |
                                }|                     |                    d          |j        dz
  d           |                     d	|           t          dd
          D ]}|                     d| |           |                     |                    d          |j        d           dS )z
        L{DistTrialRunner} can run in C{untilFailure} mode where it will run
        the given tests until they fail.
        test_itr  T)untilFailure   r  rA   z expected to see PASSED in outputFAIL   z
Test Pass zRan 1 tests inz2expected to see per-iteration test count in outputN)r   r?   EventuallyFailingTestCaser   r	  r   r   r  r   wasSuccessfulr  r   r  r   )rJ   r   caser   r  r   r  r   s           rM   test_runUntilFailurez)DistTrialRunnerTests.test_runUntilFailure"  s_   
 29==v..OODtO44%%a((DF###--//000"" 	LL""FQJ.	
 	
 	
 	ff%%% q! 	4 	4AMM*q**F3333 	LL)**F@	
 	
 	
 	
 	
rO   c                    |                                  }|                    | j                  }t          |                                t          d                     t          |j        t          d                     dS )zw
        L{DistTrialRunner.run} returns a L{DistReporter} containing the result of
        the test suite run.
        TrA   N)r   r}   r  r   rG  r   r;  )rJ   r   r   s      rM   r   zDistTrialRunnerTests.test_runE  sg    
 !!DJ''F((**HTNN;;;F$hqkk22222rO   c                    t          g           }t          |          5  |                     d          }ddd           n# 1 swxY w Y   |                    | j                  }t          |j        t          g                      t          |j        t          g                      t          |	                                t          d                     t          |j
        t          d                     t          |j        t          d                     t          |j        t          d                     dS )z
        L{DistTrialRunner.run} uses the installed reactor L{DistTrialRunner} was
        constructed without a reactor.
        Nr   TrA   )r[   r$   r   r}   r  r   r#  r   r<  rG  r;  rw   rt   )rJ   r   r   r   s       rM   test_installedReactorz*DistTrialRunnerTests.test_installedReactorO  s=   
 ""%%g&& 	2 	2^^D^11F	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2DJ''FM8B<<000FOXb\\222F((**HTNN;;;F$hqkk222G$hqkk222G%x{{33333s   AA	Ac                 j    G d dt                     }t          t          j                   G d d                      } G d d          }t	           |                      5  |                     t                    5  |                     d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   t	           |                      5  |                     t                    5  |                     d           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   t	           |                      5  |                     t                    5  |                     d           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )	z
        L{DistTrialRunner} raises L{TypeError} if the installed reactor provides
        neither L{IReactorCore} nor L{IReactorProcess} and no other reactor is
        given.
        c                       e Zd Zd ZdS )=DistTrialRunnerTests.test_wrongInstalledReactor.<locals>.Corec                     d S r]   rI   ru   s    rM   installWakerzJDistTrialRunnerTests.test_wrongInstalledReactor.<locals>.Core.installWakerg  s    rO   N)rS   rT   rU   rR  rI   rO   rM   CorerP  f  s#            rO   rS  c                   "    e Zd Z	 	 	 	 	 	 ddZdS )@DistTrialRunnerTests.test_wrongInstalledReactor.<locals>.ProcessNFc
                     d S r]   rI   )
rJ   r   rf   rg   rh   ri   rj   rk   rl   rm   s
             rM   rn   zMDistTrialRunnerTests.test_wrongInstalledReactor.<locals>.Process.spawnProcessl  s	     rO   )NNNNFN)rS   rT   rU   rn   rI   rO   rM   ProcessrU  j  s<              rO   rW  c                       e Zd ZdS )@DistTrialRunnerTests.test_wrongInstalledReactor.<locals>.NeitherNrS   rT   rU   rI   rO   rM   NeitherrY  z          DrO   r[  NrL  )r   r   r   r   r$   r)  	TypeErrorr   )rJ   rS  rW  r[  s       rM   test_wrongInstalledReactorz/DistTrialRunnerTests.test_wrongInstalledReactor_  s[   	 	 	 	 	; 	 	 	 
Z/	0	0	 	 	 	 	 	 	 
1	0		 	 	 	 	 	 	 	 ggii(( 	- 	-""9-- - -t,,,- - - - - - - - - - - - - - -	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-
 ddff%% 	- 	-""9-- - -t,,,- - - - - - - - - - - - - - -	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-
 ggii(( 	- 	-""9-- - -t,,,- - - - - - - - - - - - - - -	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-s   $B9?B"B9"B&	&B9)B&	*B99B= B=D06DD0D	D0 D	!D00D47D4F(-FF(F	F(F	F((F,/F,c                      G d dt                     fd}|                     |          }|                               5  |                    | j                   ddd           dS # 1 swxY w Y   dS )z
        If there is an unexpected exception running the test suite then it is
        re-raised by L{DistTrialRunner.run}.
        c                       e Zd ZdS );DistTrialRunnerTests.test_runFailure.<locals>.BrokenFactoryNrZ  rI   rO   rM   BrokenFactoryra    r\  rO   rb  c                                   r]   rI   )rg   r|   rb  s     rM   brokenFactoryz;DistTrialRunnerTests.test_runFailure.<locals>.brokenFactory  s    -//!rO   r!  N)	Exceptionr   r)  r}   r  )rJ   rd  r   rb  s      @rM   test_runFailurez$DistTrialRunnerTests.test_runFailure  s    	 	 	 	 	I 	 	 		" 	" 	" 	" 	" -@@}-- 	# 	#JJtz"""	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	#s   A--A14A1r   N)rS   rT   rU   rV   r<   r@   r=  r  r   r   r  r  r  r  r'  r*  r/  r6  r?  rI  r   rM  r^  rf  rI   rO   rM   r   r   k  sc         Jz22344E' ' ' 	- 	- 	-D D D,	O 	O 	O 	O
( 
( 
(( ( (&	C 	C 	C 	C# # # #? ? ? ?4 4 4 42
 
 
2!
 !
 !
F3 3 3 34 4 4 4 +- +- +- +-Z# # # # #rO   r   c                   :    e Zd ZdZd	dZd	dZd	dZd	dZd	dZdS )
FunctionalTestsz8
    Tests for the functional helpers that need it.
    r   Nc                     t          t          dd          t          d                     t          t          dd          t          d                     dS )z
        ``fromOptional`` accepts a default value and an ``Optional`` value of the
        same type and returns the default value if the optional value is
        ``None`` or the optional value otherwise.
        rA   Nr   )r   r0   r   ru   s    rM   test_fromOptionalz!FunctionalTests.test_fromOptional  sJ     	LD))8A;;777LA&&44444rO   c                     t                      }t          |          }|                     |           |                    d           t	          |                     |          t                                 dS )z
        ``discardResult`` accepts an awaitable and returns a ``Deferred`` that
        fires with ``None`` after the awaitable completes.
        r   N)r   r/   r   r4  r   r   r   )rJ   r  r  s      rM   test_discardResultz"FunctionalTests.test_discardResult  sh    
 $::!A	

8D((++TVV44444rO   c                 `   t                      }t                      }t          j        t          ||                    }|                    d           |                     |           |                    d           t          |                     |          t          d                     dS )z
        ``sequence`` accepts two awaitables and returns an awaitable that waits
        for the first one to complete and then completes with the result of
        the second one.
        *   helloN)r   r   r2   r4  r   r   r   r   )rJ   r  bcs       rM   test_sequencezFunctionalTests.test_sequence  s     $::#::"8Aq>>22	

2A	

7D((++Xb\\:::::rO   c                    t                      t                      t                      g}d }t          j        t          |t          |          j                            }|                                                    d           |                     |           |                                                    d           t          |                     |          t          d                     dS )z
        ``iterateWhile`` executes the actions from its factory until the predicate
        does not match an action result.
        c                     | dk    S )Nro  rI   )values    rM   	predicatez4FunctionalTests.test_iterateWhile.<locals>.predicate  s    B;rO   r  ro  N)
r   r   r1   listpopr4  r   r   r   r   )rJ   actionsrw  r  s       rM   test_iterateWhilez!FunctionalTests.test_iterateWhile  s    
 )1

HJJ

'K	 	 	 $1DMM$566
 
 	q!!! 	A 	r"""D((++Xb\\:::::rO   c                     t           dt          dt          fd            }t          dd          D ]'}t           |            t	          |                     (dS )z
        ``countingCalls`` decorates a function so that it is called with an
        increasing counter and passes the return value through.
        r  r   c                     | dz   S )NrA   rI   )r  s    rM   targetz2FunctionalTests.test_countingCalls.<locals>.target  s    q5LrO   rA   
   N)r.   rX   r   r   r   )rJ   r~  expecteds      rM   test_countingCallsz"FunctionalTests.test_countingCalls  st     
	c 	c 	 	 	 
	 a 	6 	6H(8"4"45555	6 	6rO   rg  )	rS   rT   rU   rV   rk  rm  rs  r{  r  rI   rO   rM   ri  ri    s         5 5 5 5	5 	5 	5 	5; ; ; ;; ; ; ;26 6 6 6 6 6rO   ri  c                       e Zd ZdZdS )r%  zx
    An exception for ``StartedWorkerPoolBroken`` to fail with to allow tests
    to exercise exception code paths.
    NrS   rT   rU   rV   rI   rO   rM   r%  r%    s           rO   r%  c                   6    e Zd ZdZded         ddfdZddZdS )StartedWorkerPoolBrokenzn
    A broken, started worker pool.  Its workers cannot run actions.  They
    always raise an exception.
    workerActionNr   c                 "   K   t                      r]   )r%  )rJ   r  s     rM   r}   zStartedWorkerPoolBroken.run  s         rO   c                 
   K   d S r]   rI   ru   s    rM   r   zStartedWorkerPoolBroken.join  s      trO   rg  )rS   rT   rU   rV   r6   r}   r   rI   rO   rM   r  r    sY         
!l4&8 !T ! ! ! !     rO   r  c                   8    e Zd ZU dZeed<   dej        defdZ	dS )r"  zF
    A worker pool that has workers with a broken ``run`` method.
    _configr   r   c                 "   K   t                      S r]   )r  )rJ   r   s     rM   r   zBrokenWorkerPool.start	  s       '(((rO   N)
rS   rT   rU   rV   r-   rY   r   r   r  r   rI   rO   rM   r"  r"    sU           )!1)	 ) ) ) ) ) )rO   r"  c                   &    e Zd ZdZdededefdZdS )_LocalWorkerz
    A L{Worker} that runs tests in this process in the usual way.

    This is a test double for L{LocalWorkerAMP} which allows testing worker
    pool logic without sending tests over an AMP connection to be run
    somewhere else..
    rH  r   r   c                 T   K   t          |g                              |           ddiS )z8
        Directly run C{case} in the usual way.
        successT)r<   r}   rJ   rH  r   s      rM   r}   z_LocalWorker.run  s1       	D6v&&&4  rO   N)rS   rT   rU   rV   PyUnitTestCaser8   r4   r}   rI   rO   rM   r  r    sG         !n !j !Y ! ! ! ! ! !rO   r  c                       e Zd ZdZdS )r.  zE
    A worker tried to run a test case but the worker is broken.
    Nr  rI   rO   rM   r.  r.     s           rO   r.  c                   &    e Zd ZdZdededdfdZdS )r-  z:
    A L{Worker} that always fails to run test cases.
    rH  r   r   Nc                 "   K   t                      )z@
        Raise an exception instead of running C{case}.
        )r.  r  s      rM   r}   z_BrokenLocalWorker.run+  s       nnrO   )rS   rT   rU   rV   r  r8   r}   rI   rO   rM   r-  r-  &  sG         n j T      rO   r-  c                   x    e Zd ZU dZee         ed<   ee         ed<   e	d         ed<   de
d         ddfdZd	 ZdS )
StartedLocalWorkerPoolz'
    A started L{LocalWorkerPool}.
    r   r`   Nr3  r  r   c                 @   K   | j         D ]} ||           d{V  dS )z8
        Run the action with each local worker.
        N)r`   )rJ   r  workers      rM   r}   zStartedLocalWorkerPool.run<  sH       l 	' 	'F,v&&&&&&&&&&	' 	'rO   c                 $   K   | j          d {V  d S r]   )r3  ru   s    rM   r   zStartedLocalWorkerPool.joinC  s&      mrO   )rS   rT   rU   rV   r'   r   rY   r   r5   r   r6   r}   r   rI   rO   rM   r  r  2  s           sm###&\tn'l4&8 'T ' ' ' '    rO   r  c                       e Zd ZU dZeed<    e ee                    Z	e
e         ed<   dZeed<   eZeg ef         ed<   dej        d	efd
ZdS )r   z_
    Implement a worker pool that runs tests in-process instead of in child
    processes.
    r  rE   r
  F	_autostop_workerFactoryr   r   c                    K    fdt           j        j                  D             }t           j        j        | j        rt          d           nt                                } j        	                    |           |S )Nc                 8    g | ]}                                 S rI   )r  )r   r   rJ   s     rM   r   z)LocalWorkerPool.start.<locals>.<listcomp>V  s%    QQQQ4&&((QQQrO   )
r   r  r   r  r   r  r    r   r
  rb   )rJ   r   r`   r   s   `   rM   r   zLocalWorkerPool.startS  s       RQQQ%8O2P2PQQQ(L)"n<WT]]](**
 

 	W%%%rO   N)rS   rT   rU   rV   r-   rY   r   r   rx  r
  r   r  r  r   r  r  r   r5   r   r   r   rI   rO   rM   r   r   G  s          
 -2U774==-I-I-IHd)*IIIIt+7NHRZ(777
!1
	
 
 
 
 
 
rO   r   )prV   r   r   	functoolsr   ior   os.pathr   typingr   r   r   unittestr
   r  zope.interfacer   r   attrsr   r   r   r   hamcrestr   r   r   r   r   r   r   hamcrest.core.core.allofr   
hypothesisr   hypothesis.strategiesr   r   twisted.internetr   twisted.internet.baser   twisted.internet.deferr   r   r    twisted.internet.errorr!   twisted.internet.protocolr"   r#   #twisted.internet.test.modulehelpersr$   twisted.internet.testingr%   twisted.python.failurer&   twisted.python.filepathr'   twisted.python.lockfiler(   twisted.trial._distr)    twisted.trial._dist.distreporterr*   twisted.trial._dist.disttrialr+   r,   r-   twisted.trial._dist.functionalr.   r/   r0   r1   r2   twisted.trial._dist.workerr3   r4   r5   r6   twisted.trial.reporterr7   r8   r9   r:   twisted.trial.runnerr;   r<   twisted.trial.unittestr=   testr?   r@   matchersrB   rD   r   r[   r   r   r   ri  re  r%  r  r"  r  r.  r-  r  r   rI   rO   rM   <module>r     s    
			 



                   & & & & & & & & & & / / / / / / . . . . . . . . / / / / / / / / / / / /                  + * * * * *       8 8 8 8 8 8 8 8 ' ' ' ' ' ' - - - - - - D D D D D D D D D D . . . . . . ? ? ? ? ? ? ? ? @ @ @ @ @ @ 7 7 7 7 7 7 * * * * * * , , , , , , 2 2 2 2 2 2 1 1 1 1 1 1 9 9 9 9 9 9 W W W W W W W W W W              T S S S S S S S S S S S            9 8 8 8 8 8 8 8 @ @ @ @ @ @ @ @ % % % % % % % % $ $ $ $ $ $        & Z'((? ? ? ? ?( ? ? )(?D5; 5; 5; 5; 5;. 5; 5; 5;pX3 X3 X3 X3 X3h X3 X3 X3vq# q# q# q# q#8 q# q# q#h	K6 K6 K6 K6 K6h K6 K6 K6\    y   
 
 
 
 
 
 
 
 
) 
) 
) 
) 
) 
) 
) 
)! ! ! ! ! ! ! !"    9   	 	 	 	 	 	 	 	        (          rO   