a
    I_                     @   sD   d Z ddlZddlmZ G dd dZddd	ZG d
d deZdS )z7
Blackbox tests for blackboxtest check output methods.
    N)BlackboxTestCasec                   @   s@   e Zd ZdZG dd deZdd Zdd Zdd	 Zd
d Z	dS )TimeoutHelperz
    Timeout class using alarm signal.

    Raise a Timeout exception if a function timeout.
    Usage:

        try:
            with Timeout(3):
                foobar("Request 1")
        except TimeoutHelper.Timeout:
            print("Timeout")
    c                   @   s   e Zd ZdS )zTimeoutHelper.TimeoutN)__name__
__module____qualname__ r   r   C/usr/lib/python3/dist-packages/samba/tests/blackbox/check_output.pyTimeout&   s   r	   c                 C   s
   || _ d S N)sec)selfr   r   r   r   __init__)   s    zTimeoutHelper.__init__c                 C   s    t  t j| j t | j d S r
   )signalSIGALRMraise_timeoutalarmr   r   r   r   r   	__enter__,   s    zTimeoutHelper.__enter__c                 G   s   t d d S )Nr   )r   r   r   argsr   r   r   __exit__0   s    zTimeoutHelper.__exit__c                 G   s   t  d S r
   )r   r	   r   r   r   r   r   3   s    zTimeoutHelper.raise_timeoutN)
r   r   r   __doc__	Exceptionr	   r   r   r   r   r   r   r   r   r      s   r   $  P c                 C   s   d | ||S )z>Build a command to call gen_output.py to generate large outputz3gen_output.py --data {0} --repeat {1} --retcode {2})format)datarepeatretcoder   r   r   _make_cmdline7   s    r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )CheckOutputTestsa   
    Blackbox tests for check_xxx methods.

    The check_xxx methods in BlackboxTestCase will deadlock
    on large output from command which caused by Popen.wait().

    This is a test case to show the deadlock issue,
    will fix in another commit.
    c                 C   s^   z8t d | t  W d   n1 s,0    Y  W n  t jyX   | jdd Y n0 dS )z!Call check_run with large output.
   NTimeout!msg)r   Z	check_runr   r	   failr   r   r   r   test_check_run_timeoutI   s
    
.z'CheckOutputTests.test_check_run_timeoutc                 C   sd   z>t d" | tddd W d    n1 s20    Y  W n  t jy^   | jdd Y n0 d S )Nr!   r   r   r"   r#   r   Zcheck_exit_coder   r	   r%   r   r   r   r   .test_check_exit_code_with_large_output_successQ   s
    
4z?CheckOutputTests.test_check_exit_code_with_large_output_successc                 C   sd   z>t d" | tddd W d    n1 s20    Y  W n  t jy^   | jdd Y n0 d S )Nr!      r'   r"   r#   r(   r   r   r   r   .test_check_exit_code_with_large_output_failureX   s
    
4z?CheckOutputTests.test_check_exit_code_with_large_output_failurec                 C   s   d}d}|| }t ||d}zHtd, | |}| ||d W d    n1 sX0    Y  W n  tjy   | jdd Y n0 d S )N@r   )r   r   r!   zutf-8r"   r#   )r   r   Zcheck_outputZassertEqualencoder	   r%   )r   r   r   ZexpectedZcmdlineZactualr   r   r   #test_check_output_with_large_output_   s    

4z4CheckOutputTests.test_check_output_with_large_outputN)r   r   r   r   r&   r)   r+   r.   r   r   r   r   r    >   s
   
r    )r   r   r   )r   r   Zsamba.testsr   r   r   r    r   r   r   r   <module>   s
   
