
    VhI                        d Z ddl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mZmZmZ ddlmZmZ dd	l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#m$Z$m%Z%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/ ddl0m1Z1m2Z2m3Z3  G d de      Z4 G d de      Z5 G d de      Z6 G d de$      Z7 G d d      Z8 G d d e      Z9y)!z)
Test for distributed trial worker side.
    N)BytesIOStringIO)Type)TestCase)verifyObject)assert_thatequal_tohas_item
has_length)Deferredfail)ConnectionLostProcessDone)IAddress
ITransport)Failure)FilePath)connectedServerAndClient)managercommands)LocalWorkerLocalWorkerAMPLocalWorkerTransport
NotRunningWorkerExceptionWorkerProtocol)
TestResult)pyunitcasesskipping)r   makeTodo   )	isFailurematches_resultsimilarFramec                   >    e Zd ZU dZeed<   eed<   ddZddZddZ	y)	WorkerProtocolTestsz&
    Tests for L{WorkerProtocol}.
    workerserverNc                 n    t        t        t        d      \  | _        | _        }|j
                  | _        y)zN
        Set up a transport, a result stream and a protocol instance.
        FgreetN)r   r   r   r&   r'   flushselfpumps     T/home/dcms/DCMS/lib/python3.12/site-packages/twisted/trial/_dist/test/test_worker.pysetUpzWorkerProtocolTests.setUp.   s.     *BN%*
&T[$ ZZ
    c                     t        j                  | j                  j                  t	        j
                  d      t                           }| j                          | j                  ddi| j                  |             y)z
        Sending the L{workercommands.Run} command to the worker returns a
        response with C{success} sets to C{True}.
        	test_passsuccessTN)
r   fromCoroutiner'   runr   
PyUnitTestr   r+   assertEqualsuccessResultOf)r-   ds     r/   test_runzWorkerProtocolTests.test_run7   s^    
 ""KKOOK22;?N
 	

)T*D,@,@,CDr1   c                 v   t         j                  j                  t         j                  j                        }| j	                  t         j
                  |       | j                  j                  d       | j                  t         j                  j                  t         j                  j                        |       y)z@
        The C{start} command changes the current path.
        z..N)	ospathrealpathcurdir
addCleanupchdirr&   startassertNotEqual)r-   r@   s     r/   
test_startzWorkerProtocolTests.test_startB   sk     !!"''..1&)$BGG,,RWW^^<fEr1   returnN)
__name__
__module____qualname____doc__r   __annotations__r   r0   r;   rE    r1   r/   r%   r%   &   s&      	EFr1   r%   c                   b    e Zd ZdZdedee   ddfdZddZddZ	dd	Z
dd
ZddZddZddZy)WorkerProtocolErrorTestsz
    Tests for L{WorkerProtocol}'s handling of certain errors related to
    running the tests themselves (i.e., not test errors but test
    infrastructure/runner errors).
    brokenTestNameloggedExceptionTyperG   Nc                    t        t        t        d      \  }}}t        j                  |      }t               }t        j                  |j                  ||             |j                          t        |t        t        d                   |j                  \  \  }}	t        |t        |             t        | j                  |      t        d             y )NFr)   r    errors)r   r   r   r   BrokenRunInfrastructurer   r   r5   r6   r+   r   r"   r   rT   r	   flushLoggedErrors)
r-   rP   rQ   r&   r'   r.   expectedCaseresult
actualCaserT   s
             r/   _runErrorTestz&WorkerProtocolErrorTests._runErrorTestS   s      8N% 
 #::>Jvzz,?@

FN*Q-@A!'	*fJ 67 	D**+>?AOr1   c                 0    | j                  dt               y)zi
        If there is an error reporting success then the test run is marked as
        an error.
        test_addSuccessNrZ   AttributeErrorr-   s    r/   test_addSuccessErrorz-WorkerProtocolErrorTests.test_addSuccessErrorj       
 	,n=r1   c                 0    | j                  dt               y)zj
        If there is an error reporting an error then the test run is marked as
        an error.
        test_addErrorNr]   r_   s    r/   test_addErrorErrorz+WorkerProtocolErrorTests.test_addErrorErrorq   s    
 	?N;r1   c                 0    | j                  dt               y)zk
        If there is an error reporting a failure then the test run is marked
        as an error.
        test_addFailureNr]   r_   s    r/   test_addFailureErrorz-WorkerProtocolErrorTests.test_addFailureErrorx   ra   r1   c                 0    | j                  dt               y)zh
        If there is an error reporting a skip then the test run is marked
        as an error.
        test_addSkipNr]   r_   s    r/   test_addSkipErrorz*WorkerProtocolErrorTests.test_addSkipError   s    
 	>>:r1   c                 0    | j                  dt               y)zu
        If there is an error reporting an expected failure then the test
        run is marked as an error.
        test_addExpectedFailureNr]   r_   s    r/   rl   z0WorkerProtocolErrorTests.test_addExpectedFailure   s    
 	4nEr1   c                 0    | j                  dt               y)zu
        If there is an error reporting an unexpected ccess then the test
        run is marked as an error.
        test_addUnexpectedSuccessNr]   r_   s    r/   rn   z2WorkerProtocolErrorTests.test_addUnexpectedSuccess   s    
 	6Gr1   c                 >   t        t        t        d      \  }}}d|_        t	        j
                  d      }t               }t        j                  |j                  ||             |j                          t        | j                  t              t        d             y)zV
        A failure encountered while reporting a reporting failure is logged.
        Fr)   Nr3      )r   r   r   	transportr   r7   r   r   r5   r6   r+   r   rV   r   r   )r-   r&   r'   r.   rW   rX   s         r/   test_failedFailureReportz1WorkerProtocolErrorTests.test_failedFailureReport   sz      8N% 
  "--k:vzz,?@


 	D**>:JqMJr1   rF   )rH   rI   rJ   rK   strr   BaseExceptionrZ   r`   rd   rg   rj   rl   rn   rr   rM   r1   r/   rO   rO   L   sS    P!P8<]8KP	P.><>;FHKr1   rO   c                   v    e Zd ZdZddZefdedee   defdZddZ	dd	Z
dd
ZddZddZddZddZddZy)LocalWorkerAMPTestszR
    Test case for distributed trial's manager-side local worker AMP protocol
    rG   Nc                 n    t        t        t        d      \  | _        | _        }|j
                  | _        y )NFr)   )r   r   r   r&   
managerAMPr+   r,   s     r/   r0   zLocalWorkerAMPTests.setUp   s+    -EN%.
*T_d ZZ
r1   testCase
makeResultc                      |       }t        j                  | j                  j                  ||            }| j	                          | j                  ddi| j                  |             |S )Nr4   T)r   r5   rx   r6   r+   r8   r9   )r-   ry   rz   rX   r:   s        r/   workerRunTestz!LocalWorkerAMPTests.workerRunTest   sZ     ""4??#6#6x#HI

)T*D,@,@,CDr1   c                     | j                  t        j                  d            }t        |t	        t        d                   y)z*
        Run a test, and succeed.
        r3   r    )	successesN)r|   r   r7   r   r"   r	   )r-   rX   s     r/   test_runSuccessz#LocalWorkerAMPTests.test_runSuccess   s2     ##K$:$:;$GHFNXa[ABr1   c                 B   t        j                  d      }| j                  |      }t        |t	        t        d                   |j                  \  \  }}}t        |t        |             t        |t        d             t        |t        t        d                   y)z2
        Run a test, and fail expectedly.
        
test_todo1r    )expectedFailureszexpected failuretodo1N)	r   SynchronousStrictTodor|   r   r"   r   r   r	   r   )r-   rW   rX   rY   exceptionMessage
todoReasons         r/   test_runExpectedFailurez+LocalWorkerAMPTests.test_runExpectedFailure   s      55lC##L1FNJqMJK7=7N7N4	3*&
J 67 	$h/A&BCJ'): ;<r1   c                 n   t        j                  d      }| j                  |      }t        |t	        t        d                   |j                  \  \  }}t        |t        |             t        |t        t        t              t        t        d            t        t        dd                         y)z5
        Run a test, and encounter an error.
        
test_errorr    rS   zpyunit errorzpyunitcases.py)typevalueframesN)r   r7   r|   r   r"   r   rT   r	   r!   	Exceptionr   r
   r#   r-   rW   rX   rY   failures        r/   test_runErrorz!LocalWorkerAMPTests.test_runError   s     #--l;##L1FN*Q-@A"(--	*gL(:"67i(~>?\;K LM	
r1   c                 F   t        j                  d      }| j                  |      }t        |t	        t        d                   |j                  \  \  }}t        |t        |             t        |t        t        t              t        t        d                         y)z'
        Run a test, and fail.
        	test_failr    )failureszpyunit failure)r   r   N)r   r7   r|   r   r"   r   r   r	   r!   AssertionErrorr   r   s        r/   test_runFailurez#LocalWorkerAMPTests.test_runFailure   s     #--k:##L1FNJqMBC"(//	*gL(:"67n-/?@A	
r1   c                    t        j                  d      }| j                  |      }t        |t	        t        d                   |j                  \  \  }}t        |t        |             t        |t        d             y)z*
        Run a test, but skip it.
        	test_skipr    )skipszpyunit skipN)r   r7   r|   r   r"   r   r   r	   )r-   rW   rX   rY   skips        r/   test_runSkipz LocalWorkerAMPTests.test_runSkip   sf     #--k:##L1FNA?@%||	*dL(:"67D(=12r1   c                    t        j                  d      }| j                  |      }t        |t	        t        d                   |j                  \  \  }}t        |t        |             t        |t        d             y)z7
        Run a test, and succeed unexpectedly.
        
test_todo7r    )unexpectedSuccessestodo7N)r   r   r|   r   r"   r   r   r	   )r-   rW   rX   rY   unexpectedSuccesss        r/   test_runUnexpectedSuccessesz/LocalWorkerAMPTests.test_runUnexpectedSuccesses  sj      55lC##L1FNz!}MN,2,F,F)	(*'L(:"67%x'89r1   c                 J   t               }| j                  j                  |       | j                  j	                  t
        j                  d      }| j                          | j                  ddi| j                  |             | j                  d|j                                y)zb
        L{LocalWorkerAMP.testWrite} writes the data received to its test
        stream.
        zSome output)outr4   TzSome output
N)r   rx   setTestStreamr&   
callRemoter   	TestWriter+   r8   r9   getvalue)r-   streamr:   s      r/   test_testWritez"LocalWorkerAMPTests.test_testWrite  s|    
 %%f-KK""?#<#<-"P

)T*D,@,@,CD&//*;<r1   c                     g  G fddt               }t        j                  d      }| j                  ||       t	        t        |g             y)z|
        L{LocalWorkerAMP.run} calls C{stopTest} on its test result once the
        C{Run} commands has succeeded.
        c                   "    e Zd Zdeddf fdZy)=LocalWorkerAMPTests.test_stopAfterRun.<locals>.StopTestResulttestrG   Nc                 (    j                  |       y N)append)r-   r   stoppeds     r/   stopTestzFLocalWorkerAMPTests.test_stopAfterRun.<locals>.StopTestResult.stopTest#  s    t$r1   )rH   rI   rJ   PyUnitTestCaser   )r   s   r/   StopTestResultr   "  s    %^ % %r1   r   r3   N)r   r   r7   r|   r   r	   )r-   r   caser   s      @r/   test_stopAfterRunz%LocalWorkerAMPTests.test_stopAfterRun  sI    
 	%Z 	% %%k240GXtf-.r1   rF   )rH   rI   rJ   rK   r0   r   r   r   r|   r   r   r   r   r   r   r   r   rM   r1   r/   rv   rv      s^      HR&484D	C=
$
$	3	:
=/r1   rv   c                       e Zd ZdZdZdZd Zy)SpyDataLocalWorkerAMPz
    A fake implementation of L{LocalWorkerAMP} that records the received
    data and doesn't automatically dispatch any command..
    r   r1   c                 .    | xj                   |z  c_         y r   
dataString)r-   datas     r/   dataReceivedz"SpyDataLocalWorkerAMP.dataReceived4      4r1   N)rH   rI   rJ   rK   idr   r   rM   r1   r/   r   r   +  s    
 
BJ r1   r   c                   $    e Zd ZdZdZdZd Zd Zy)FakeTransportz>
    A fake process transport implementation for testing.
    r1   r   c                 .    | xj                   |z  c_         y r   r   )r-   fdr   s      r/   writeToChildzFakeTransport.writeToChild@  r   r1   c                 .    | xj                   dz  c_         y )Nr    )callsr_   s    r/   loseConnectionzFakeTransport.loseConnectionC  s    

a
r1   N)rH   rI   rJ   rK   r   r   r   r   rM   r1   r/   r   r   8  s     JE r1   r   c                   j    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y)LocalWorkerTestsz?
    Tests for L{LocalWorker} and L{LocalWorkerTransport}.
    c                     t        |i |}|j                  t                      | j                  |j                  j
                         | j                  |j                  j
                         |S )z
        Create a L{LocalWorker}, connect it to a transport, and ensure
        its log files are closed.

        @param args: See L{LocalWorker}

        @param kwargs: See L{LocalWorker}

        @return: a L{LocalWorker} instance
        )r   makeConnectionr   rA   _outLogclose_errLog)r-   argskwargsr&   s       r/   tidyLocalWorkerz LocalWorkerTests.tidyLocalWorkerL  sT     d-f-mo.,,-,,-r1   c                     t        t               t        | j                               t	                     }| j                  |j                         t               y)z
        L{LocalWorker.exit} fails with L{NotRunning} if it is called before the
        protocol is connected to a transport.
        N)r   r   r   mktempr   failureResultOfexitr   r-   r&   s     r/   test_exitBeforeConnectedz)LocalWorkerTests.test_exitBeforeConnected]  s=    
 !#Xdkkm%<hj
 	V[[]J7r1   c                 F   | j                  t               t        | j                               t	                     }|j                  t        t        d                   | j                  |j                  t               | j                  |j                         t               y)z
        L{LocalWorker.exit} fails with L{NotRunning} if it is called after the the
        protocol is disconnected from its transport.
        r   N)r   r   r   r   r   processEndedr   r   r   endDeferredr   r   r   s     r/   test_exitAfterDisconnectedz+LocalWorkerTests.test_exitAfterDisconnectedg  st    
 %%!#Xdkkm%<hj
 	GKN34 	V//= 	V[[]J7r1   c                 n   | j                  t               t        | j                               d      }t	               |_        |j                  dd       |j                  dd       | j                  d|j                  j                         | j                  d|j
                  j                                y)z
        L{LocalWorker.childDataReceived} forwards the received data to linked
        L{AMP} protocol if the right file descriptor, otherwise forwards to
        C{ProcessProtocol.childDataReceived}.
        test.log   s   foor    s   barN)r   r   r   r   r   r   childDataReceivedr8   _ampProtocolr   r   r-   localWorkers     r/   test_childDataReceivedz'LocalWorkerTests.test_childDataReceivedx  s     **!#Xdkkm%<j
 &i%%a0%%a0!9!9!D!DE!4!4!=!=!?@r1   c                 l   t               }t        | j                               }|j                          |j	                  d      }t        |j                  dd      5 }t        |||      }|j                  t                      | j                  |j                  j                         | j                  |j                  j                         d}|j                  |       ddd       | j                  t         j"                  z   |j%                         j'                  d             y# 1 sw Y   JxY w)zI
        L{LocalWorker} writes the log data with local newlines.
        r   wtzutf-8)encodingu   Here comes the ☉!N)r   r   r   makedirschildopenr>   r   r   r   rA   r   r   r   	testWriter8   r=   linesep
getContentdecode)r-   amptempDirlogPathlogFiler&   expecteds          r/   test_newlineStylez"LocalWorkerTests.test_newlineStyle  s     $%4;;=)--
+',,w7 	$7 gw7F!!-/2OOFNN001OOFNN0010HMM(#	$ 	

"  ''0	
	$ 	$s   BD**D3c                     | j                  t               t        | j                               d      }t	               |_        d}|j                  |       | j                  ||j
                  j                                y)zb
        L{LocalWorker.outReceived} logs the output into its C{_outLog} log
        file.
        r   +   The quick brown fox jumps over the lazy dogN)	r   r   r   r   r   r   outReceivedr8   r   r-   r   r   s      r/   test_outReceivedz!LocalWorkerTests.test_outReceived  g    
 **!#Xdkkm%<j
 &i=%{22;;=>r1   c                     | j                  t               t        | j                               d      }t	               |_        d}|j                  |       | j                  ||j
                  j                                y)zb
        L{LocalWorker.errReceived} logs the errors into its C{_errLog} log
        file.
        r   r   N)	r   r   r   r   r   r   errReceivedr8   r   r   s      r/   test_errReceivedz!LocalWorkerTests.test_errReceived  r   r1   c                     t               }t        |      }d}|j                  |       | j                  ||j                         y)zi
        L{LocalWorkerTransport.write} forwards the written data to the given
        transport.
        r   N)r   r   writer8   r   r-   rq   localTransportr   s       r/   
test_writezLocalWorkerTests.test_write  s>    
 "O	-i8=T"y334r1   c                     t               }t        |      }d}|j                  |       | j                  dj	                  |      |j
                         y)zq
        L{LocalWorkerTransport.writeSequence} forwards the written data to the
        given transport.
        )s
   The quick s   brown fox jumps s   over the lazy dogr1   N)r   r   writeSequencer8   joinr   r   s       r/   test_writeSequencez#LocalWorkerTests.test_writeSequence  sF    
 "O	-i8I$$T*$)=)=>r1   c                     t               }t        |      }|j                          | j                  |j                  d       y)zj
        L{LocalWorkerTransport.loseConnection} forwards the call to the given
        transport.
        r    N)r   r   r   r8   r   )r-   rq   r   s      r/   test_loseConnectionz$LocalWorkerTests.test_loseConnection  s5    
 "O	-i8%%'!,r1   c                    | j                  t               t        | j                               d      }|j	                  d       | j                  |j                  j                         | j                  |j                  j                         y)zR
        L{LocalWorker.connectionLost} closes the per-worker log streams.
        r   N)	r   r   r   r   connectionLost
assertTruer   closedr   r   s     r/   test_connectionLostz$LocalWorkerTests.test_connectionLost  si    
 **!#Xdkkm%<j
 	""4(++223++223r1   c                    t               }t               }t        |t        | j	                               d      }|j                  |       |j                  t        t        d                   | j                  |j                  j                         | j                  |j                  j                         | j                  d|j                         | j                  |j                   t              S )zs
        L{LocalWorker.processEnded} calls C{connectionLost} on itself and on
        the L{AMP} protocol.
        r   r   N)r   r   r   r   r   r   r   r   r   r	  r   r
  r   assertIdenticalrq   assertFailurer   )r-   rq   protocolr   s       r/   test_processEndedz"LocalWorkerTests.test_processEnded  s    
 "O	(*!(HT[[],CZP""9-  Q!89++223++223T8#5#56!!+"9"9;GGr1   c                     t        d      }| j                  t        t        |j	                                      | j                  t        t        |j                                      y)zy
        L{LocalWorkerTransport.getPeer} and L{LocalWorkerTransport.getHost}
        return L{IAddress} objects.
        N)r   r	  r   r   getPeergetHostr-   r   s     r/   test_addresseszLocalWorkerTests.test_addresses  sF    
 .d3X~/E/E/GHIX~/E/E/GHIr1   c                 X    t        d      }| j                  t        t        |             y)zk
        L{LocalWorkerTransport} implements L{ITransport} to be able to be used
        by L{AMP}.
        N)r   r	  r   r   r  s     r/   test_transportzLocalWorkerTests.test_transport   s!    
 .d3Z@Ar1   c                     d }t               }||_        | j                  |t        | j	                               d       | j                  g | j                  t                     y)z
        L{LocalWorker} swallows the exceptions returned by the L{AMP} protocol
        start method, as it generates unnecessary errors.
        c                 *    t        t        d            S )Noops)r   RuntimeError)command	directorys     r/   failCallRemotez8LocalWorkerTests.test_startError.<locals>.failCallRemote  s    V,--r1   r   N)r   r   r   r   r   r8   rV   r  )r-   r  r  s      r/   test_startErrorz LocalWorkerTests.test_startError  sQ    	. )*,Xx'>
KT33LABr1   N)rH   rI   rJ   rK   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  rM   r1   r/   r   r   G  sW    "88"A
4??	5	?	-
4HJBCr1   r   ):rK   r=   ior   r   typingr   unittestr   r   zope.interface.verifyr   hamcrestr   r	   r
   r   twisted.internet.deferr   r   twisted.internet.errorr   r   twisted.internet.interfacesr   r   twisted.python.failurer   twisted.python.filepathr   twisted.test.iosimr   twisted.trial._distr   twisted.trial._dist.workerr   r   r   r   r   r   twisted.trial.reporterr   twisted.trial.testr   r   twisted.trial.unittestr   matchersr!   r"   r#   r%   rO   rv   r   r   r   rM   r1   r/   <module>r1     s    
    / . @ @ 1 > < * , 7 /  . 4 5 = =#F( #FL]Kx ]K@|/( |/~
 N 
  MCx MCr1   