o
    pjn=  ã                   @   sR  d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	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 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  ddl!m"Z"m#Z#m$Z$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)ÚfailÚsucceed)ÚConnectionDone)ÚIAddressÚ
ITransport)ÚCONNECTION_DONE)ÚAMP)ÚFailure)ÚFilePath)ÚfullyQualifiedName)Útrial)ÚStringTransport)ÚmanagercommandsÚworkercommands)ÚLocalWorkerÚLocalWorkerAMPÚLocalWorkerTransportÚWorkerProtocol)Ú
TestResult)ÚTestCasec                   @   s   e Zd ZdZdS )ÚFakeAMPz
    A fake amp protocol.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úF/usr/lib/python3/dist-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}.
        Údoesntexist)ÚtestCasec                    s   ˆ   | d ¡ d S ©NÚsuccess)Ú
assertTrue©Úresultr&   r   r   Úcheck@   ó   z+WorkerProtocolTests.test_run.<locals>.check)r%   Ú
callRemoter   ÚRunÚaddCallbackr#   ÚdataReceivedr"   ÚvalueÚclearr!   )r'   Údr0   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#   ÚstartÚassertNotEqual)r'   r=   r   r   r   Ú
test_startJ   s   zWorkerProtocolTests.test_startN)r   r   r   r   r(   r9   rB   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ÚappendÚ	_getSuiteÚ_testsÚpopr*   Úrunr7   )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)rF   r5   rG   r6   r7   rH   rE   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.
        )rJ   c                    ó   ˆ   | d ¡S r+   ©rK   r.   ©Úresultsr   r   Ú<lambda>z   ó    z5LocalWorkerAMPTests.test_runSuccess.<locals>.<lambda>N)rH   r2   r   Ú
AddSuccessrJ   r4   rQ   r-   ©r'   r8   r   rT   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.
        ÚerrorÚ
todoReason)rJ   r[   Útodoc                    rR   r+   rS   r.   rT   r   r   rV   ‹   rW   z=LocalWorkerAMPTests.test_runExpectedFailure.<locals>.<lambda>r   N)rH   r2   r   ÚAddExpectedFailurerJ   r4   rQ   ÚassertEqualr*   r/   ÚexpectedFailuresr-   rY   r   rT   r   Útest_runExpectedFailure   s   üz+LocalWorkerAMPTests.test_runExpectedFailurec                    óh   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.
        r[   ©rJ   r[   Ú
errorClassÚframesc                    rR   r+   rS   r.   rT   r   r   rV   ž   rW   z3LocalWorkerAMPTests.test_runError.<locals>.<lambda>r   N)r   Ú
ValueErrorrH   r2   r   ÚAddErrorrJ   r4   rQ   r_   r*   r/   Úerrorsr-   ©r'   rd   r8   r   rT   r   Útest_runError‘   ó   ûz!LocalWorkerAMPTests.test_runErrorc                    s’   g ‰ t tƒ}| jjtj| jd|g d¢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}.
        r[   )úfile.pyúinvalid codeÚ3rc   c                    rR   r+   rS   r.   rT   r   r   rV   ²   rW   z=LocalWorkerAMPTests.test_runErrorWithFrames.<locals>.<lambda>r   rl   rm   é   é   N)r   rf   rH   r2   r   rg   rJ   r4   rQ   r_   r*   r/   rh   re   r-   ri   r   rT   r   Útest_runErrorWithFrames¤   s    ûÿz+LocalWorkerAMPTests.test_runErrorWithFramesc                    rb   )z'
        Run a test, and fail.
        r   )rJ   r   Ú	failClassre   c                    rR   r+   rS   r.   rT   r   r   rV   È   rW   z5LocalWorkerAMPTests.test_runFailure.<locals>.<lambda>r   N)r   ÚRuntimeErrorrH   r2   r   Ú
AddFailurerJ   r4   rQ   r_   r*   r/   Úfailuresr-   )r'   rr   r8   r   rT   r   Útest_runFailure»   rk   z#LocalWorkerAMPTests.test_runFailurec                    ó\   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)rJ   rx   c                    rR   r+   rS   r.   rT   r   r   rV   ×   rW   z2LocalWorkerAMPTests.test_runSkip.<locals>.<lambda>r   N)rH   r2   r   ÚAddSkiprJ   r4   rQ   r_   r*   r/   Úskipsr-   rY   r   rT   r   Útest_runSkipÎ   ó   
ÿz LocalWorkerAMPTests.test_runSkipc                    rw   )z7
        Run a test, and succeed unexpectedly.
        r]   )rJ   r]   c                    rR   r+   rS   r.   rT   r   r   rV   æ   rW   zALocalWorkerAMPTests.test_runUnexpectedSuccesses.<locals>.<lambda>r   N)rH   r2   r   ÚAddUnexpectedSuccessrJ   r4   rQ   r_   r*   r/   ÚunexpectedSuccessesr-   rY   r   rT   r   Útest_runUnexpectedSuccessesÝ   r|   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                    rR   r+   rS   r.   rT   r   r   rV   ÷   rW   z4LocalWorkerAMPTests.test_testWrite.<locals>.<lambda>zSome output
N)r   rF   ÚsetTestStreamr   Ú	TestWriterH   r2   r4   rQ   r_   Úgetvaluer-   )r'   ÚstreamÚcommandr8   r   rT   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   )r…   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 r‡   rS   )r'   Útest©Ústoppedr   r   ÚstopTest  s   zFLocalWorkerAMPTests.test_stopAfterRun.<locals>.StopTestResult.stopTestN)r   r   r   rŒ   r   rŠ   r   r   ÚStopTestResult
  s    r�   )	ÚobjectrF   r2   r   rO   r*   r_   r4   ÚassertIdentical)r'   rˆ   r�   r8   r   )r/   r‹   r   Útest_stopAfterRuný   s   z%LocalWorkerAMPTests.test_stopAfterRunN)r   r   r   r   r(   rQ   rZ   ra   rj   rq   rv   r{   r   r†   r�   r   r   r   r   rC   T   s    	rC   c                   @   s    e Zd ZdZdZdZdd„ ZdS )ÚSpyDataLocalWorkerAMPzˆ
    A fake implementation of L{LocalWorkerAMP} that records the received
    data and doesn't automatically dispatch any command..
    r   ó    c                 C   s   |  j |7  _ d S r‡   ©Ú
dataString)r'   Údatar   r   r   r5     r1   z"SpyDataLocalWorkerAMP.dataReceivedN)r   r   r   r   Úidr”   r5   r   r   r   r   r‘     s
    r‘   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.
    r’   r   c                 C   s   |  j |7  _ d S r‡   r“   )r'   Úfdr•   r   r   r   ÚwriteToChild(  r1   zFakeTransport.writeToChildc                 C   s   |  j d7  _ d S )Nrp   )Úcallsr&   r   r   r   ÚloseConnection+  r1   zFakeTransport.loseConnectionN)r   r   r   r   r”   rš   r™   r›   r   r   r   r   r—      s    r—   c                   @   sx   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d„ ZdS )ÚLocalWorkerTestsz?
    Tests for L{LocalWorker} and L{LocalWorkerTransport}.
    c                 O   sH   t |i |¤Ž}| 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$   r—   r>   Ú_testLogÚcloseÚ_outLogÚ_errLog)r'   ÚargsÚkwargsrH   r   r   r   ÚtidyLocalWorker4  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}.
        Ú.útest.logé   s   foorp   s   barN)	r£   r‘   r   rŸ   ÚchildDataReceivedr_   Ú_ampProtocolr”   rƒ   ©r'   ÚlocalWorkerr   r   r   Útest_childDataReceivedF  s   z'LocalWorkerTests.test_childDataReceivedc                 C   sÀ   t ƒ }t|  ¡ ƒ}| d¡}t ¡ }|  tjtj|¡ t tjd¡ t	||j
dƒ}| tƒ ¡ |  |jj¡ |  |jj¡ z| d¡ W |j ¡  n|j ¡  w |  dtj d¡ | ¡ ¡ dS )z‰
        L{LocalWorker} write the log data with local newlines but
        in UTF-8 encoding regardless of the default encoding.
        r¥   )ÚCÚasciiu   Here comes the â˜‰!s   Here comes the â˜‰!r­   N)r‘   r   ÚmktempÚchildÚlocaleÚ	getlocaler>   Ú	setlocaleÚLC_ALLr   r;   r$   r—   rŸ   rž   r    Ú	testWriter�   r_   r:   ÚlinesepÚencodeÚ
getContent)r'   ÚampÚtempDirÚlogFileÚcurrentLocalerH   r   r   r   Ú_test_unicodeLogFileUTF8S  s"   
þz)LocalWorkerTests._test_unicodeLogFileUTF8c                 C   ó<   |   tƒ dd¡}tƒ |_d}| |¡ |  ||j ¡ ¡ dS )zb
        L{LocalWorker.outReceived} logs the output into its C{_outLog} log
        file.
        r¤   r¥   ó+   The quick brown fox jumps over the lazy dogN)r£   r‘   r   rŸ   ÚoutReceivedr_   rƒ   ©r'   rª   r•   r   r   r   Útest_outReceivedr  ó
   
z!LocalWorkerTests.test_outReceivedc                 C   r½   )zb
        L{LocalWorker.errReceived} logs the errors into its C{_errLog} log
        file.
        r¤   r¥   r¾   N)r£   r‘   r   r    ÚerrReceivedr_   rƒ   rÀ   r   r   r   Útest_errReceived}  rÂ   z!LocalWorkerTests.test_errReceivedc                 C   s.   t ƒ }t|ƒ}d}| |¡ |  ||j¡ dS )zi
        L{LocalWorkerTransport.write} forwards the written data to the given
        transport.
        r¾   N)r—   r   Úwriter_   r”   ©r'   Ú	transportÚlocalTransportr•   r   r   r   Ú
test_writeˆ  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 dogr’   N)r—   r   ÚwriteSequencer_   Újoinr”   rÆ   r   r   r   Útest_writeSequence“  s
   
z#LocalWorkerTests.test_writeSequencec                 C   s(   t ƒ }t|ƒ}| ¡  |  |jd¡ dS )zj
        L{LocalWorkerTransport.loseConnection} forwards the call to the given
        transport.
        rp   N)r—   r   r›   r_   rš   )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¤   r¥   N)r£   r‘   ÚconnectionLostr-   rŸ   Úclosedr    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¤   r¥   N)r—   r‘   r   r$   ÚprocessEndedr   r
   r-   rŸ   rÏ   r    r�   r�   rÇ   ÚassertFailureÚ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   ÚgetPeerÚ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   Ú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 )NÚoops)r   rs   )r…   Ú	directoryr   r   r   ÚfailCallRemoteÜ  s   z8LocalWorkerTests.test_startError.<locals>.failCallRemoter¤   r¥   N)r‘   r2   r£   r_   ÚflushLoggedErrorsrs   )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   r   rœ   /  s    	rœ   )0r   r°   r:   Úior   r   Úzope.interface.verifyr   Útwisted.internet.deferr   r   Útwisted.internet.errorr   Útwisted.internet.interfacesr   r	   Útwisted.internet.mainr
   Útwisted.protocols.ampr   Útwisted.python.failurer   Útwisted.python.filepathr   Útwisted.python.reflectr   Útwisted.scriptsr   Útwisted.test.proto_helpersr   Útwisted.trial._distr   r   Útwisted.trial._dist.workerr   r   r   r   Útwisted.trial.reporterr   Útwisted.trial.unittestr   r   r    rC   r‘   r—   rœ   r   r   r   r   Ú<module>   s4   + @