B
    0²ô`„8  ã               @   s>  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 dd
l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& G dd„ de&ƒZ'G dd„ de
ƒZ(G dd„ de
ƒZ)G dd„ de&ƒZ*G dd„ dƒZ+G dd„ de
ƒZ,dS )z)
Test for distributed trial worker side.
é    N)ÚBytesIOÚStringIO)ÚverifyObject)Ú
TestResult)ÚTestCase)ÚLocalWorkerÚLocalWorkerAMPÚLocalWorkerTransportÚWorkerProtocol)ÚmanagercommandsÚworkercommands)Útrial)ÚStringTransport)Ú
ITransportÚIAddress)ÚfailÚsucceed)ÚCONNECTION_DONE)ÚConnectionDone)ÚfullyQualifiedName)ÚFailure)ÚAMPc               @   s   e Zd ZdZdS )ÚFakeAMPz
    A fake amp protocol.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úS/home/dcms/DCMS/lib/python3.7/site-packages/twisted/trial/_dist/test/test_worker.pyr   #   s   r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚWorkerProtocolTestsz&
    Tests for L{WorkerProtocol}.
    c             C   s@   t ƒ | _t ƒ | _tƒ | _| j | j¡ tƒ | _| j | j¡ dS )zN
        Set up a transport, a result stream and a protocol instance.
        N)r   ÚserverTransportÚclientTransportr
   ÚserverÚmakeConnectionr   Úclient)Úselfr   r   r   ÚsetUp.   s    zWorkerProtocolTests.setUpc                sd   ˆ j jtjdd}‡ fdd„}| |¡ ˆ j ˆ j ¡ ¡ ˆ j 	¡  ˆ j  ˆ j
 ¡ ¡ ˆ j
 	¡  |S )z…
        Calling the L{workercommands.Run} command on the client returns a
        response with C{success} sets to C{True}.
        Zdoesntexist)ÚtestCasec                s   ˆ   | d ¡ d S )NÚsuccess)Ú
assertTrue)Úresult)r%   r   r   Úcheck@   s    z+WorkerProtocolTests.test_run.<locals>.check)r$   Ú
callRemoter   ZRunÚaddCallbackr"   ÚdataReceivedr!   ÚvalueÚclearr    )r%   Údr+   r   )r%   r   Útest_run9   s    


zWorkerProtocolTests.test_runc             C   sF   t j t jj¡}|  t j|¡ | j d¡ |  t j t jj¡|¡ dS )z@
        The C{start} command changes the current path.
        z..N)	ÚosÚpathÚrealpathÚcurdirÚ
addCleanupÚchdirr"   ÚstartZassertNotEqual)r%   r6   r   r   r   Ú
test_startJ   s    zWorkerProtocolTests.test_startN)r   r   r   r   r&   r2   r:   r   r   r   r   r   )   s   r   c               @   sh   e Zd Z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dd„ Zdd„ Zdd„ ZdS )ÚLocalWorkerAMPTestszR
    Test case for distributed trial's manager-side local worker AMP protocol
    c             C   s”   t ƒ | _tƒ | _| j | j¡ tƒ | _t ƒ | _tƒ | _	| j	 | j¡ t
 ¡ }d| _|d  | j¡ t
 |¡j ¡ | _| j | j| j¡ | j ¡  d S )Nztwisted.doesnexistÚtests)r   ÚmanagerTransportr   Ú
managerAMPr#   r   r*   ÚworkerTransportr   Úworkerr   ÚOptionsÚtestNameÚappendZ	_getSuiteZ_testsÚpopr'   Úrunr0   )r%   Úconfigr   r   r   r&   Y   s    zLocalWorkerAMPTests.setUpc             C   s2   | j  | j ¡ ¡ | j ¡  | j | j ¡ ¡ dS )zœ
        Sends data from C{self.workerTransport} to C{self.managerAMP}, and then
        data from C{self.managerTransport} back to C{self.worker}.
        N)r>   r.   r?   r/   r0   r@   r=   )r%   r   r   r   ÚpumpTransportsj   s    
z"LocalWorkerAMPTests.pumpTransportsc                s@   g ‰ | j jtj| jd}| ‡ fdd„¡ |  ¡  |  ˆ ¡ dS )z*
        Run a test, and succeed.
        )rB   c                s   ˆ   | d ¡S )Nr(   )rC   )r*   )Úresultsr   r   Ú<lambda>z   ó    z5LocalWorkerAMPTests.test_runSuccess.<locals>.<lambda>N)r@   r,   r   Z
AddSuccessrB   r-   rG   r)   )r%   r1   r   )rH   r   Útest_runSuccesss   s
    z#LocalWorkerAMPTests.test_runSuccessc                s^   g ‰ | j jtj| jddd}| ‡ fdd„¡ |  ¡  |  | j| j	j
d d ¡ |  ˆ ¡ dS )z2
        Run a test, and fail expectedly.
        ÚerrorZ
todoReason)rB   rL   Útodoc                s   ˆ   | d ¡S )Nr(   )rC   )r*   )rH   r   r   rI   ‹   rJ   z=LocalWorkerAMPTests.test_runExpectedFailure.<locals>.<lambda>r   N)r@   r,   r   ZAddExpectedFailurerB   r-   rG   ÚassertEqualr'   r*   ZexpectedFailuresr)   )r%   r1   r   )rH   r   Útest_runExpectedFailure   s    z+LocalWorkerAMPTests.test_runExpectedFailurec                sh   g ‰ t tƒ}| jjtj| jd|g d}| ‡ fdd„¡ |  ¡  |  	| j
| jjd d ¡ |  ˆ ¡ dS )z5
        Run a test, and encounter an error.
        rL   )rB   rL   Ú
errorClassÚframesc                s   ˆ   | d ¡S )Nr(   )rC   )r*   )rH   r   r   rI   ž   rJ   z3LocalWorkerAMPTests.test_runError.<locals>.<lambda>r   N)r   Ú
ValueErrorr@   r,   r   ÚAddErrorrB   r-   rG   rN   r'   r*   Úerrorsr)   )r%   rP   r1   r   )rH   r   Útest_runError‘   s    z!LocalWorkerAMPTests.test_runErrorc                s”   g ‰ t tƒ}| jjtj| jd|dddgd}| ‡ fdd„¡ |  ¡  |  	| j
| jjd d ¡ |  	ddd	g g fg| jjd d
 j¡ |  ˆ ¡ dS )zŒ
        L{LocalWorkerAMP._buildFailure} recreates the C{Failure.frames} from
        the C{frames} argument passed to C{AddError}.
        rL   zfile.pyzinvalid codeÚ3)rB   rL   rP   rQ   c                s   ˆ   | d ¡S )Nr(   )rC   )r*   )rH   r   r   rI   ²   rJ   z=LocalWorkerAMPTests.test_runErrorWithFrames.<locals>.<lambda>r   é   é   N)r   rR   r@   r,   r   rS   rB   r-   rG   rN   r'   r*   rT   rQ   r)   )r%   rP   r1   r   )rH   r   Útest_runErrorWithFrames¤   s    "z+LocalWorkerAMPTests.test_runErrorWithFramesc                sh   g ‰ t tƒ}| jjtj| jd|g d}| ‡ fdd„¡ |  ¡  |  	| j
| jjd d ¡ |  ˆ ¡ dS )z'
        Run a test, and fail.
        r   )rB   r   Ú	failClassrQ   c                s   ˆ   | d ¡S )Nr(   )rC   )r*   )rH   r   r   rI   È   rJ   z5LocalWorkerAMPTests.test_runFailure.<locals>.<lambda>r   N)r   ÚRuntimeErrorr@   r,   r   Z
AddFailurerB   r-   rG   rN   r'   r*   Zfailuresr)   )r%   rZ   r1   r   )rH   r   Útest_runFailure»   s    z#LocalWorkerAMPTests.test_runFailurec                s\   g ‰ | j jtj| jdd}| ‡ fdd„¡ |  ¡  |  | j| j	j
d d ¡ |  ˆ ¡ dS )z*
        Run a test, but skip it.
        Úreason)rB   r]   c                s   ˆ   | d ¡S )Nr(   )rC   )r*   )rH   r   r   rI   ×   rJ   z2LocalWorkerAMPTests.test_runSkip.<locals>.<lambda>r   N)r@   r,   r   ZAddSkiprB   r-   rG   rN   r'   r*   Úskipsr)   )r%   r1   r   )rH   r   Útest_runSkipÎ   s    z LocalWorkerAMPTests.test_runSkipc                s\   g ‰ | j jtj| jdd}| ‡ fdd„¡ |  ¡  |  | j| j	j
d d ¡ |  ˆ ¡ dS )z7
        Run a test, and succeed unexpectedly.
        rM   )rB   rM   c                s   ˆ   | d ¡S )Nr(   )rC   )r*   )rH   r   r   rI   æ   rJ   zALocalWorkerAMPTests.test_runUnexpectedSuccesses.<locals>.<lambda>r   N)r@   r,   r   ZAddUnexpectedSuccessrB   r-   rG   rN   r'   r*   ZunexpectedSuccessesr)   )r%   r1   r   )rH   r   Útest_runUnexpectedSuccessesÝ   s    z/LocalWorkerAMPTests.test_runUnexpectedSuccessesc                sd   g ‰ t ƒ }| j |¡ tj}| jj|dd}| ‡ fdd„¡ |  ¡  |  	d| 
¡ ¡ |  ˆ ¡ dS )zb
        L{LocalWorkerAMP.testWrite} writes the data received to its test
        stream.
        zSome output)Úoutc                s   ˆ   | d ¡S )Nr(   )rC   )r*   )rH   r   r   rI   ÷   rJ   z4LocalWorkerAMPTests.test_testWrite.<locals>.<lambda>zSome output
N)r   r>   ÚsetTestStreamr   Z	TestWriter@   r,   r-   rG   rN   Úgetvaluer)   )r%   ÚstreamÚcommandr1   r   )rH   r   Útest_testWriteì   s    z"LocalWorkerAMPTests.test_testWritec                sb   t ƒ ‰ g ‰‡ fdd„}|| j_G ‡fdd„dtƒ}| j | j|ƒ ¡}|  | jgˆ¡ | | jˆ ¡S )z|
        L{LocalWorkerAMP.run} calls C{stopTest} on its test result once the
        C{Run} commands has succeeded.
        c                s   t ˆ ƒS )N)r   )re   r'   )r*   r   r   ÚfakeCallRemote  s    z=LocalWorkerAMPTests.test_stopAfterRun.<locals>.fakeCallRemotec                   s   e Zd Z‡ fdd„ZdS )z=LocalWorkerAMPTests.test_stopAfterRun.<locals>.StopTestResultc                s   ˆ   |¡ d S )N)rC   )r%   Útest)Ústoppedr   r   ÚstopTest  s    zFLocalWorkerAMPTests.test_stopAfterRun.<locals>.StopTestResult.stopTestN)r   r   r   rj   r   )ri   r   r   ÚStopTestResult
  s   rk   )	Úobjectr>   r,   r   rE   r'   rN   r-   ÚassertIdentical)r%   rg   rk   r1   r   )r*   ri   r   Útest_stopAfterRuný   s    z%LocalWorkerAMPTests.test_stopAfterRunN)r   r   r   r   r&   rG   rK   rO   rU   rY   r\   r_   r`   rf   rn   r   r   r   r   r;   T   s   	r;   c               @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )	ÚFakeAMProtocolz6
    A fake implementation of L{AMP} for testing.
    r   rJ   c             C   s   |  j |7  _ d S )N)Ú
dataString)r%   Údatar   r   r   r.     s    zFakeAMProtocol.dataReceivedc             C   s
   || _ d S )N)Z
testStream)r%   rd   r   r   r   rb     s    zFakeAMProtocol.setTestStreamN)r   r   r   r   Úidrp   r.   rb   r   r   r   r   ro     s
   ro   c               @   s(   e Zd ZdZdZdZdd„ Zdd„ ZdS )	ÚFakeTransportz>
    A fake process transport implementation for testing.
    rJ   r   c             C   s   |  j |7  _ d S )N)rp   )r%   Úfdrq   r   r   r   ÚwriteToChild*  s    zFakeTransport.writeToChildc             C   s   |  j d7  _ d S )NrX   )Úcalls)r%   r   r   r   ÚloseConnection-  s    zFakeTransport.loseConnectionN)r   r   r   r   rp   rv   ru   rw   r   r   r   r   rs   "  s
   rs   c               @   sp   e Zd Z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dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚLocalWorkerTestsz?
    Tests for L{LocalWorker} and L{LocalWorkerTransport}.
    c             O   sD   t ||Ž}| 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   r#   rs   r7   Ú_testLogÚcloseÚ_outLogÚ_errLog)r%   ÚargsÚkwargsr@   r   r   r   ÚtidyLocalWorker6  s    
z LocalWorkerTests.tidyLocalWorkerc             C   sV   |   tƒ dd¡}tƒ |_| dd¡ | dd¡ |  d|jj¡ |  d|j ¡ ¡ dS )zÑ
        L{LocalWorker.childDataReceived} forwards the received data to linked
        L{AMP} protocol if the right file descriptor, otherwise forwards to
        C{ProcessProtocol.childDataReceived}.
        Ú.ztest.logé   s   foorX   s   barN)	r   ro   r   r{   ZchildDataReceivedrN   Z_ampProtocolrp   rc   )r%   ÚlocalWorkerr   r   r   Útest_childDataReceivedH  s    z'LocalWorkerTests.test_childDataReceivedc             C   s<   |   tƒ dd¡}tƒ |_d}| |¡ |  ||j ¡ ¡ dS )zb
        L{LocalWorker.outReceived} logs the output into its C{_outLog} log
        file.
        r€   ztest.logs+   The quick brown fox jumps over the lazy dogN)r   ro   r   r{   ZoutReceivedrN   rc   )r%   r‚   rq   r   r   r   Útest_outReceivedU  s
    
z!LocalWorkerTests.test_outReceivedc             C   s<   |   tƒ dd¡}tƒ |_d}| |¡ |  ||j ¡ ¡ dS )zb
        L{LocalWorker.errReceived} logs the errors into its C{_errLog} log
        file.
        r€   ztest.logs+   The quick brown fox jumps over the lazy dogN)r   ro   r   r|   ZerrReceivedrN   rc   )r%   r‚   rq   r   r   r   Útest_errReceived`  s
    
z!LocalWorkerTests.test_errReceivedc             C   s.   t ƒ }t|ƒ}d}| |¡ |  ||j¡ dS )zi
        L{LocalWorkerTransport.write} forwards the written data to the given
        transport.
        s+   The quick brown fox jumps over the lazy dogN)rs   r	   ÚwriterN   rp   )r%   Ú	transportÚlocalTransportrq   r   r   r   Ú
test_writek  s
    
zLocalWorkerTests.test_writec             C   s4   t ƒ }t|ƒ}d}| |¡ |  d |¡|j¡ dS )zq
        L{LocalWorkerTransport.writeSequence} forwards the written data to the
        given transport.
        )s
   The quick s   brown fox jumps s   over the lazy dogrJ   N)rs   r	   ZwriteSequencerN   Újoinrp   )r%   r‡   rˆ   rq   r   r   r   Útest_writeSequencev  s
    
z#LocalWorkerTests.test_writeSequencec             C   s(   t ƒ }t|ƒ}| ¡  |  |jd¡ dS )zj
        L{LocalWorkerTransport.loseConnection} forwards the call to the given
        transport.
        rX   N)rs   r	   rw   rN   rv   )r%   r‡   rˆ   r   r   r   Útest_loseConnection  s    z$LocalWorkerTests.test_loseConnectionc             C   sH   |   tƒ dd¡}| d¡ |  |jj¡ |  |jj¡ |  |jj¡ dS )zG
        L{LocalWorker.connectionLost} closes the log streams.
        r€   ztest.logN)r   ro   ZconnectionLostr)   r{   Úclosedr|   ry   )r%   r‚   r   r   r   Útest_connectionLostŒ  s
    
z$LocalWorkerTests.test_connectionLostc             C   sv   t ƒ }tƒ }t|ddƒ}| |¡ | ttƒ¡ |  |jj	¡ |  |j
j	¡ |  |jj	¡ |  d|j¡ |  |jt¡S )zs
        L{LocalWorker.processEnded} calls C{connectionLost} on itself and on
        the L{AMP} protocol.
        r€   ztest.logN)rs   ro   r   r#   ZprocessEndedr   r   r)   r{   r   r|   ry   rm   r‡   ZassertFailureZendDeferredr   )r%   r‡   Úprotocolr‚   r   r   r   Útest_processEnded—  s    
z"LocalWorkerTests.test_processEndedc             C   s4   t dƒ}|  tt| ¡ ƒ¡ |  tt| ¡ ƒ¡ dS )zy
        L{LocalWorkerTransport.getPeer} and L{LocalWorkerTransport.getHost}
        return L{IAddress} objects.
        N)r	   r)   r   r   ZgetPeerZgetHost)r%   rˆ   r   r   r   Útest_addresses¨  s    zLocalWorkerTests.test_addressesc             C   s   t dƒ}|  tt|ƒ¡ dS )zk
        L{LocalWorkerTransport} implements L{ITransport} to be able to be used
        by L{AMP}.
        N)r	   r)   r   r   )r%   rˆ   r   r   r   Útest_transport±  s    zLocalWorkerTests.test_transportc             C   s8   dd„ }t ƒ }||_|  |dd¡ |  g |  t¡¡ dS )z’
        L{LocalWorker} swallows the exceptions returned by the L{AMP} protocol
        start method, as it generates unnecessary errors.
        c             S   s   t tdƒƒS )NZoops)r   r[   )re   Ú	directoryr   r   r   ÚfailCallRemote¿  s    z8LocalWorkerTests.test_startError.<locals>.failCallRemoter€   ztest.logN)ro   r,   r   rN   ZflushLoggedErrorsr[   )r%   r”   r   r   r   r   Útest_startError¹  s
    z LocalWorkerTests.test_startErrorN)r   r   r   r   r   rƒ   r„   r…   r‰   r‹   rŒ   rŽ   r   r‘   r’   r•   r   r   r   r   rx   1  s   	rx   )-r   r3   Úior   r   Zzope.interface.verifyr   Ztwisted.trial.reporterr   Ztwisted.trial.unittestr   Ztwisted.trial._dist.workerr   r   r	   r
   Ztwisted.trial._distr   r   Ztwisted.scriptsr   Ztwisted.test.proto_helpersr   Ztwisted.internet.interfacesr   r   Ztwisted.internet.deferr   r   Ztwisted.internet.mainr   Ztwisted.internet.errorr   Ztwisted.python.reflectr   Ztwisted.python.failurer   Ztwisted.protocols.ampr   r   r   r;   ro   rs   rx   r   r   r   r   Ú<module>   s.   + @