o
    ¯bd  ã                   @   sÀ   d Z ddl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mZmZ ddlmZmZ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dS )z/
Tests for L{twisted.trial._dist.workertrial}.
é    N)ÚBytesIO)ÚAMP)ÚStringTransport)Ú_WORKER_AMP_STDINÚ_WORKER_AMP_STDOUTÚmanagercommandsÚworkercommandsÚworkertrial)ÚWorkerLogObserverÚ
_setupPathÚmain)ÚTestCasec                   @   s   e Zd ZdZdS )ÚFakeAMPz
    A fake amp protocol.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úK/usr/lib/python3/dist-packages/twisted/trial/_dist/test/test_workertrial.pyr      s    r   c                   @   ó   e Zd ZdZdd„ ZdS )ÚWorkerLogObserverTestsz)
    Tests for L{WorkerLogObserver}.
    c                    sL   g ‰ G ‡ fdd„dƒ}t |ƒ ƒ}| ddgi¡ |  ˆ tjddifg¡ dS )zU
        L{WorkerLogObserver} forwards data to L{managercommands.TestWrite}.
        c                       s   e Zd Z‡ fdd„ZdS )z4WorkerLogObserverTests.test_emit.<locals>.FakeClientc                    s   ˆ   ||f¡ d S ©N)Úappend)ÚselfÚmethodÚkwargs©Úcallsr   r   Ú
callRemote,   s   z?WorkerLogObserverTests.test_emit.<locals>.FakeClient.callRemoteN)r   r   r   r   r   r   r   r   Ú
FakeClient+   s    r   ÚmessagezSome logÚoutN)r
   ÚemitÚassertEqualr   Ú	TestWrite)r   r   Úobserverr   r   r   Ú	test_emit%   s
   
z WorkerLogObserverTests.test_emitN)r   r   r   r   r&   r   r   r   r   r       s    r   c                   @   sJ   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )Ú	MainTestsz
    Tests for L{main}.
    c                 C   s>   t ƒ | _t ƒ | _|  td| j¡ |  ttdtj	¡ dgt_	d S )NÚstartLoggingWithObserverÚargvÚtrial)
r   Ú
readStreamÚwriteStreamÚpatchr	   r(   Ú
addCleanupÚsetattrÚsysr)   ©r   r   r   r   ÚsetUp9   s   ÿzMainTests.setUpNc                 C   sB   |t kr|  d|¡ | jS |tkr|  d|¡ | jS td|›�ƒ‚)z—
        Fake C{os.fdopen} implementation which returns C{self.readStream} for
        the stdin fd and C{self.writeStream} for the stdout fd.
        ÚrbÚwbzUnexpected fd )r   ÚassertIdenticalr+   r   r#   r,   ÚAssertionError)r   ÚfdÚmoder   r   r   ÚfdopenB   s   zMainTests.fdopenc                 C   s   |   |¡ dS )zP
        Override C{startLoggingWithObserver} for not starting logging.
        N)ÚassertFalse)r   r"   Ú	setStdoutr   r   r   r(   P   s   z"MainTests.startLoggingWithObserverc                 C   s    t | jƒ |  d| j ¡ ¡ dS )zU
        If no data is ever written, L{main} exits without writing data out.
        ó    N)r   r9   r#   r,   Úgetvaluer1   r   r   r   Ú
test_emptyV   s   
zMainTests.test_emptyc                 C   s\   t ƒ }tƒ }| |¡ |jtjdd� |j| _| j dd¡ t	| j
ƒ |  d| j ¡ ¡ dS )z�
        L{main} forwards data from its input stream to a L{WorkerProtocol}
        instance which writes data to the output stream.
        Údoesntexist)ÚtestCaser   s   No module named 'doesntexist'N)r   r   ÚmakeConnectionr   r   ÚRunÚior+   Úseekr   r9   ÚassertInr,   r=   )r   ÚclientÚclientTransportr   r   r   Útest_forwardCommand]   s   

zMainTests.test_forwardCommandc                    sL   g ‰ G ‡ fdd„dƒ}|ƒ | _ t| jƒ |  d| j ¡ ¡ |  dgˆ ¡ dS )zŒ
        If reading the input stream fails with a C{IOError} with errno
        C{EINTR}, L{main} ignores it and continues reading.
        c                       s   e Zd ZdZ‡ fdd„ZdS )z2MainTests.test_readInterrupted.<locals>.FakeStreamr   c                    s4   |  j d7  _ | j dkrttjƒ‚ˆ  t ¡ ¡ dS )Né   r<   )ÚcountÚOSErrorÚerrnoÚEINTRr   r0   Úexc_info©ÚoselfÚsize©ÚexcInfosr   r   Úreadu   s
   

z7MainTests.test_readInterrupted.<locals>.FakeStream.readN©r   r   r   rJ   rT   r   rR   r   r   Ú
FakeStreamr   s    rV   r<   )NNNN)r+   r   r9   r#   r,   r=   ©r   rV   r   rR   r   Útest_readInterruptedk   s   
zMainTests.test_readInterruptedc                 C   s*   G dd„ dƒ}|ƒ | _ |  tt| j¡ dS )zm
        L{main} only ignores C{IOError} with C{EINTR} errno: otherwise, the
        error pops out.
        c                   @   r   )z1MainTests.test_otherReadError.<locals>.FakeStreamr   c                 S   s$   |  j d7  _ | j dkrtdƒ‚dS )NrI   zSomething elseÚ )rJ   rK   rO   r   r   r   rT   ‹   s   
z6MainTests.test_otherReadError.<locals>.FakeStream.readNrU   r   r   r   r   rV   ˆ   s    rV   N)r+   ÚassertRaisesÚIOErrorr   r9   rW   r   r   r   Útest_otherReadError‚   s   	zMainTests.test_otherReadErrorr   )r   r   r   r   r2   r9   r(   r>   rH   rX   r\   r   r   r   r   r'   4   s    
	r'   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚSetupPathTestsz;
    Tests for L{_setupPath} C{sys.path} manipulation.
    c                 C   s   |   ttdtjd d … ¡ d S )NÚpath)r.   r/   r0   r^   r1   r   r   r   r2   š   s   zSetupPathTests.setUpc                 C   s2   dt j ddg¡i}t|ƒ |  ddgtj¡ dS )zu
        L{_setupPath} overrides C{sys.path} if B{TRIAL_PYTHONPATH} is specified
        in the environment.
        ÚTRIAL_PYTHONPATHÚfooÚbarN)ÚosÚpathsepÚjoinr   r#   r0   r^   )r   Úenvironr   r   r   Útest_overridePath�   s   z SetupPathTests.test_overridePathc                 C   s(   t jdd… }ti ƒ |  |t j¡ dS )z|
        L{_setupPath} doesn't change C{sys.path} if B{TRIAL_PYTHONPATH} is not
        present in the environment.
        N)r0   r^   r   r#   )r   ÚoriginalPathr   r   r   Útest_noVariable¦   s   zSetupPathTests.test_noVariableN)r   r   r   r   r2   rf   rh   r   r   r   r   r]   •   s
    	r]   )r   rL   rb   r0   rC   r   Útwisted.protocols.ampr   Útwisted.test.proto_helpersr   Útwisted.trial._distr   r   r   r   r	   Útwisted.trial._dist.workertrialr
   r   r   Útwisted.trial.unittestr   r   r   r'   r]   r   r   r   r   Ú<module>   s   a