B
    u9aÇ&  ã               @   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G d
d„ dej
ƒZedkrœe ¡  dS )é    N)Úsupport)Úmockc               @   sv   e 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 )Ú	LockTestsc             C   s   t  ¡ | _d S )N)Ú_threadÚallocate_lockÚlock)Úself© r	   ú'/usr/lib/python3.7/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.)Z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 )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   Z	monotonicr   Ústart_new_threadÚDELAYr   ÚverboseÚprintZassertGreaterEqual)r   r    Z
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 )Nr   r   )ZwaitflagZtimeoutF)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__r   r   r   r   r   r   r   r   r   r'   r   Úpatchr+   r/   r	   r	   r	   r
   r      s   r   c               @   s   e Zd Zdd„ Zdd„ ZdS )Ú
RLockTestsc             C   s   t  ¡ | _d S )N)r   ÚRLockÚrlock)r   r	   r	   r
   r   l   s    zRLockTests.setUpc             C   s„   |   dt| jƒ¡ | j ¡  | j ¡  |   dt| jƒ¡ | j ¡  |   dt| jƒ¡ | j ¡  |   dt| jƒ¡ |  t| jj¡ d S )Nr,   r   )r-   r.   r6   r   r   r   r   )r   r	   r	   r
   Útest_multiple_acquireo   s    



z RLockTests.test_multiple_acquireN)r0   r1   r2   r   r7   r	   r	   r	   r
   r4   i   s   r4   c               @   sL   e 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 )Ú	MiscTestsc             C   s   |   ttj¡ d S )N)r   Ú
SystemExitr   Úexit)r   r	   r	   r
   Ú	test_exit~   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"   ZassertGreater)r   r	   r	   r
   Ú
test_ident   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_LockType†   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_sentinel‹   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   rC   r	   r	   r
   Útest_interrupt_main   s
    zMiscTests.test_interrupt_mainc             C   s   |   ttj¡ d S )N)r   rD   r   rB   )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   ÚerrorrH   r)   Ú	exceptionÚargs)r   Úcmr	   r	   r
   Útest_stack_size_not_None¢   s    z"MiscTests.test_stack_size_not_NoneN)r0   r1   r2   r;   r>   r?   rA   rF   rG   rI   rO   r	   r	   r	   r
   r8   {   s   r8   c               @   sF   e 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 )ÚThreadTestsc             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 )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)rR   rS   rT   z8Argument passing for thread creation using kwargs failedrT   zGArgument passing for thread creation using both tuple and kwargs failed)FF)rR   ÚQueuer   r#   Úgetr   rE   )r   rU   Ú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   rQ   r   r=   )rR   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.)rR   rV   r   r%   r&   r$   ÚrangeÚroundÚrandomr   r#   r   r   r)   Zqsize)r   r[   Zthread_countrX   Ú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 )Nr   z2nd arg must be a tuple)	r   Ú	TypeErrorr   r#   r   ÚMockr)   rL   rM   )r   rN   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 )N)Úkwargsr   z3rd arg must be a dict)
r   rb   r   r#   r   rc   rE   r)   rL   rM   )r   rN   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 )N)Úside_effectz#start_new_thread raised SystemExit.)r   rc   r9   r   r#   rE   Z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 )N)rg   )r   rc   Ú	Exceptionr   r#   rE   r   r(   )r   Zmock_print_excrh   r	   r	   r
   Útest_RaiseException  s    zThreadTests.test_RaiseExceptionN)r0   r1   r2   rZ   ra   rd   rf   ri   r   r3   rk   r	   r	   r	   r
   rP   ©   s   !		rP   Ú__main__)Z_dummy_threadr   r   rR   r_   ZunittestZtestr   r   r$   ZTestCaser   r4   r8   rP   r0   Úmainr	   r	   r	   r
   Ú<module>   s   ].j