B
    18™\o$  ã               @   s~   d dl Zd dlZd dlZd dlZd dlZd dlmZ d dlmZ d Z	G dd„ dej
ƒZG dd„ dej
ƒZG dd	„ d	ej
ƒZdS )
é    N)Úsupport)Úmockc               @   sz   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e d¡dd„ ƒZdd„ ZdS )Ú	LockTestszTest lock objects.c             C   s   t  ¡ | _d S )N)Ú_threadÚallocate_lockÚlock)Úself© r	   úZ/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_dummy_thread.pyÚsetUp   s    zLockTests.setUpc             C   s   |   | j ¡ d¡ d S )Nz(Lock object is not initialized unlocked.)ÚassertFalser   Úlocked)r   r	   r	   r
   Útest_initlock   s    zLockTests.test_initlockc             C   s*   | j  ¡  | j  ¡  |  | j  ¡ d¡ d S )Nz%Lock object did not release properly.)r   ÚacquireÚreleaser   r   )r   r	   r	   r
   Útest_release   s    

zLockTests.test_releasec          	   C   s*   t  ¡  W d Q R X |  | j ¡ d¡ d S )NzAcquired Lock was not released)r   ÚLockTyper   r   r   )r   r	   r	   r
   Útest_LockType_context_manager   s    

z'LockTests.test_LockType_context_managerc             C   s   |   t| jj¡ d S )N)ÚassertRaisesÚRuntimeErrorr   r   )r   r	   r	   r
   Útest_improper_release%   s    zLockTests.test_improper_releasec             C   s   |   | j d¡d¡ d S )Nr   z)Conditional acquiring of the lock failed.)Ú
assertTruer   r   )r   r	   r	   r
   Útest_cond_acquire_success)   s    z#LockTests.test_cond_acquire_successc             C   s$   | j  d¡ |  | j  d¡d¡ d S )Nr   z=Conditional acquiring of a locked lock incorrectly succeeded.)r   r   r   )r   r	   r	   r
   Útest_cond_acquire_fail.   s    z LockTests.test_cond_acquire_failc             C   s    | j  ¡  |  | j  ¡ d¡ d S )NzUncondional locking failed.)r   r   r   r   )r   r	   r	   r
   Útest_uncond_acquire_success5   s    
z%LockTests.test_uncond_acquire_successc             C   s,   |   | j d¡dd¡ |   | j ¡ d¡ d S )Né   Tz*Unconditional locking did not return True.)ÚassertIsr   r   )r   r	   r	   r
   Útest_uncond_acquire_return_val;   s    z(LockTests.test_uncond_acquire_return_valc             C   s‚   dd„ }| j  ¡  tt ¡ ƒ}t || j tf¡ tj	rHt
ƒ  t
dt ƒ | j  ¡  tt ¡ ƒ}tj	rlt
dƒ |  || td¡ d S )Nc             S   s   t  |¡ |  ¡  dS )z:Hold on to lock for a set amount of time before unlocking.N)ÚtimeÚsleepr   )Z	to_unlockÚdelayr	   r	   r
   Údelay_unlockC   s    
z<LockTests.test_uncond_acquire_blocking.<locals>.delay_unlockz@*** Waiting for thread to release the lock (approx. %s sec.) ***Údonez+Blocking by unconditional acquiring failed.)r   r   Úintr   Ú	monotonicr   Ústart_new_threadÚDELAYr   ÚverboseÚprintÚassertGreaterEqual)r   r!   Ú
start_timeZend_timer	   r	   r
   Útest_uncond_acquire_blockingA   s    

z&LockTests.test_uncond_acquire_blockingz
time.sleepc             C   s@   | j  ¡  | j jddd}|  |j¡ | d¡ |  |d¡ dS )z¹Test invoking acquire() with a positive timeout when the lock is
        already acquired. Ensure that time.sleep() is invoked with the given
        timeout and that False is returned.r   r   )ZwaitflagÚtimeoutFN)r   r   r   ÚcalledZassert_called_once_withÚassertEqual)r   Z
mock_sleepÚretvalr	   r	   r
   Útest_acquire_timeoutV   s
    

zLockTests.test_acquire_timeoutc             C   s<   | j  ¡  |  dt| j ƒ¡ | j  ¡  |  dt| j ƒ¡ d S )Nr   Úunlocked)r   r   ÚassertInÚreprr   )r   r	   r	   r
   Útest_lock_representationb   s    

z"LockTests.test_lock_representationN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r+   r   Úpatchr0   r4   r	   r	   r	   r
   r      s   r   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S )Ú	MiscTestszMiscellaneous tests.c             C   s   |   ttj¡ d S )N)r   Ú
SystemExitr   Úexit)r   r	   r	   r
   Ú	test_exitl   s    zMiscTests.test_exitc             C   s&   |   t ¡ td¡ |  t ¡ d¡ d S )Nz*_thread.get_ident() returned a non-integerr   )ÚassertIsInstancer   Ú	get_identr#   ÚassertGreater)r   r	   r	   r
   Ú
test_idento   s    zMiscTests.test_identc             C   s   |   t ¡ tjd¡ d S )NzR_thread.LockType is not an instance of what is returned by _thread.allocate_lock())r>   r   r   r   )r   r	   r	   r
   Útest_LockTypet   s    zMiscTests.test_LockTypec             C   s   |   t ¡ tjd¡ d S )Nz;_thread._set_sentinel() did not return a LockType instance.)r>   r   Ú_set_sentinelr   )r   r	   r	   r
   Útest_set_sentinely   s    zMiscTests.test_set_sentinelc             C   s    dd„ }|   ttj|tƒ ¡ d S )Nc               S   s   t  ¡  d S )N)r   Úinterrupt_mainr	   r	   r	   r
   Úcall_interrupt   s    z5MiscTests.test_interrupt_main.<locals>.call_interrupt)r   ÚKeyboardInterruptr   r%   Útuple)r   rF   r	   r	   r
   Útest_interrupt_main~   s
    zMiscTests.test_interrupt_mainc             C   s   |   ttj¡ d S )N)r   rG   r   rE   )r   r	   r	   r
   Útest_interrupt_in_main‰   s    z MiscTests.test_interrupt_in_mainc             C   s   t  d ¡}|  |d¡ d S )Nr   )r   Ú
stack_sizer.   )r   r/   r	   r	   r
   Útest_stack_size_NoneŒ   s    
zMiscTests.test_stack_size_Nonec          	   C   s:   |   tj¡}t d¡ W d Q R X |  |jjd d¡ d S )NÚ r   z'setting thread stack size not supported)r   r   ÚerrorrK   r.   Ú	exceptionÚargs)r   Úcmr	   r	   r
   Útest_stack_size_not_None   s    z"MiscTests.test_stack_size_not_NoneN)r5   r6   r7   r8   r=   rA   rB   rD   rI   rJ   rL   rR   r	   r	   r	   r
   r:   i   s   r:   c               @   sJ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Ze	 
d¡dd„ ƒZdS )ÚThreadTestszTest thread creation.c             C   s¸   ddd„}t  d¡}t ||ddf¡ | ¡ }|  |d o@|d d¡ t |tƒ |dddœ¡ | ¡ }|  |d ox|d d	¡ t ||dfd
di¡ | ¡ }|  |d o®|d d¡ d S )NFc             S   s   |   ||f¡ dS )z<Use to test _thread.start_new_thread() passes args properly.N)Úput)ÚqueueÚarg1Úarg2r	   r	   r
   Ú
arg_testerœ   s    z0ThreadTests.test_arg_passing.<locals>.arg_testerr   Tr   z7Argument passing for thread creation using tuple failed)rU   rV   rW   z8Argument passing for thread creation using kwargs failedrW   zGArgument passing for thread creation using both tuple and kwargs failed)FF)rU   ÚQueuer   r%   Úgetr   rH   )r   rX   Útesting_queueÚresultr	   r	   r
   Útest_arg_passingš   s(    


zThreadTests.test_arg_passingc             C   s¢   dd„ }d}t  |¡}tjr2tƒ  tdt|f ƒ x8t|ƒD ],}trTtt ¡ dƒ}nd}t	 
|||f¡ q<W t t¡ tjr„tdƒ |  | ¡ |d|tf ¡ d S )	Nc             S   s   t  |¡ |  t ¡ ¡ d S )N)r   r   rT   r   r?   )rU   r    r	   r	   r
   Ú
queue_mark¼   s    
z:ThreadTests.test_multi_thread_creation.<locals>.queue_marké   zJ*** Testing multiple thread creation (will take approx. %s to %s sec.) ***r   r   r"   z2Not all %s threads executed properly after %s sec.)rU   rY   r   r'   r(   r&   ÚrangeÚroundÚrandomr   r%   r   r   r.   Úqsize)r   r^   Zthread_countr[   ÚcountZlocal_delayr	   r	   r
   Útest_multi_thread_creation»   s&    

z&ThreadTests.test_multi_thread_creationc          	   C   s>   |   t¡}t t ¡ g ¡ W dQ R X |  |jjd d¡ dS )zœ
        Test invoking start_new_thread() with a non-tuple value for "args".
        Expect TypeError with a meaningful error message to be raised.
        Nr   z2nd arg must be a tuple)	r   Ú	TypeErrorr   r%   r   ÚMockr.   rO   rP   )r   rQ   r	   r	   r
   Útest_args_not_tuple×   s    zThreadTests.test_args_not_tuplec          	   C   sD   |   t¡}tjt ¡ tƒ g d W dQ R X |  |jj	d d¡ dS )z
        Test invoking start_new_thread() with a non-dict value for "kwargs".
        Expect TypeError with a meaningful error message to be raised.
        )ÚkwargsNr   z3rd arg must be a dict)
r   rf   r   r%   r   rg   rH   r.   rO   rP   )r   rQ   r	   r	   r
   Útest_kwargs_not_dictà   s     z ThreadTests.test_kwargs_not_dictc             C   sD   t jtƒ d}yt |tƒ ¡ W n tk
r>   |  d¡ Y nX dS )z
        Test invoking start_new_thread() with a function that raises
        SystemExit.
        The exception should be discarded.
        )Úside_effectz#start_new_thread raised SystemExit.N)r   rg   r;   r   r%   rH   Úfail)r   Úfuncr	   r	   r
   Útest_SystemExité   s
    zThreadTests.test_SystemExitztraceback.print_excc             C   s*   t jtd}t |tƒ ¡ |  |j¡ dS )zÊ
        Test invoking start_new_thread() with a function that raises exception.

        The exception should be discarded and the traceback should be printed
        via traceback.print_exc()
        )rk   N)r   rg   Ú	Exceptionr   r%   rH   r   r-   )r   Zmock_print_excrm   r	   r	   r
   Útest_RaiseExceptionõ   s    zThreadTests.test_RaiseExceptionN)r5   r6   r7   r8   r]   re   rh   rj   rn   r   r9   rp   r	   r	   r	   r
   rS   —   s   !		rS   )Ú_dummy_threadr   r   rU   rb   ÚunittestÚtestr   r   r&   ÚTestCaser   r:   rS   r	   r	   r	   r
   Ú<module>   s   ].