o
    U˟i5                     @   s$  d dl mZ d dl m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 G dd dejZG d	d
 d
ejZG dd deZedG dd dejZG dd deZG dd dejZG dd deZdd Zdd Zdd ZG dd dejZedkre  dS dS )    )print_function)absolute_importN	SemaphoreBoundedSemaphore)timingc                   @   s:   e Zd Zdd Zdd Zdd Zejdd Zd	d
 Z	dS )TestSemaphorec                 C   s.   t dd}|jdd}|du sJ t|d S )Nr   )valueg{Gz?timeoutF)r   acquirerepr)selfsresult r   d/var/www/apps/myagent/mysuperagent/venv/lib/python3.10/site-packages/gevent/tests/test__semaphore.py(test_acquire_returns_false_after_timeout   s   
z6TestSemaphore.test_acquire_returns_false_after_timeoutc                    s`   t  }g  | fdd |  | fdd |  td | t ddg d S )Nc                    
     dS )Naappendr   r   r   r   <lambda>      
 z2TestSemaphore.test_release_twice.<locals>.<lambda>c                    r   )Nbr   r   r   r   r   r   !   r   gMbP?r   r   )r   rawlinkreleasegeventsleepassertEqualsortedr   r   r   r   r   test_release_twice   s   
z TestSemaphore.test_release_twicec                 C   s"   t  }t|}| ||  d S N)r   weakrefrefr"   )r   r   rr   r   r   test_semaphore_weakref'   s   
z$TestSemaphore.test_semaphore_weakrefc                 C   s2   G dd dt }|  dd l}|  |  d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z9TestSemaphore.test_semaphore_in_class_with_del.<locals>.Xc                 S   s   t  | _d S r&   )r   r   r   r   r   r   __init__2   s   zBTestSemaphore.test_semaphore_in_class_with_del.<locals>.X.__init__c                 S   s   | j   d S r&   )r   r   r+   r   r   r   __del__5      zATestSemaphore.test_semaphore_in_class_with_del.<locals>.X.__del__N)__name__
__module____qualname__r,   r-   r   r   r   r   X1   s    r2   r   )objectgccollect)r   r2   r4   r   r   r    test_semaphore_in_class_with_del,   s
   z.TestSemaphore.test_semaphore_in_class_with_delc                 C   s   t  }t|g d S r&   )r   r    waitr$   r   r   r   )test_rawlink_on_unacquired_runs_notifiers>   s   z7TestSemaphore.test_rawlink_on_unacquired_runs_notifiersN)
r/   r0   r1   r   r%   r*   	greentestignores_leakcheckr6   r8   r   r   r   r   r	      s    
r	   c                   @   sz   e Zd Zdd Zdd Zdd ZdZ			dd
dZdd Zdd Z	dd Z
dd ZeddddZdd Zdd ZdS )TestSemaphoreMultiThreadc                 C      t S r&   r   r+   r   r   r   _getTargetClassN      z(TestSemaphoreMultiThread._getTargetClassc                 C   s   |   dS )N   )r=   r+   r   r   r   _makeOneQ   s   z!TestSemaphoreMultiThread._makeOnec                    s2   ddl m dd l fdd}|S )Nr   get_hub_if_existsc               	      s      z2z jdi  W n    d d <  W  } | d ur1|   | jdd    d S  } | d urH|   | jdd    w )NTdestroy_loopr   )setr   r   exc_infojoindestroy)hubacquiredrF   rB   semsysthread_acquire_kwargsthread_acquiredthread_runningr   r   thread_main^   s&   
z=TestSemaphoreMultiThread._makeThreadMain.<locals>.thread_main)gevent._hub_localrB   rM   )r   rP   rO   rL   rK   rF   rN   rQ   r   rJ   r   _makeThreadMainX   s   z(TestSemaphoreMultiThread._makeThreadMain   TFc                 K   sB  ddl m} | |d dd l}| }| }|  }|  g }	g }
|j| j	||||
|	fi |d}d|_
|  |d |rd|  t| jD ]}t   |tjr\ nqN| |
dg |s|drt| jD ]}t   |tjr~ nqp|tjd  |r| |  z| |	g  W d }	||
fS d }	w )	Nr   monkey	threadingtargetT
   r   rT   )r    rV   assertFalseis_module_patchedrW   Eventr@   r   ThreadrS   daemonstartr7   r   rangeIDLE_ITERATIONSidler   
LARGE_TICKr"   get
assertTrueis_set)r   r   !require_thread_acquired_to_finishrN   rV   rW   rP   rO   rL   rF   rK   t_r   r   r   $_do_test_acquire_in_one_then_anotherq   sP   

	z=TestSemaphoreMultiThread._do_test_acquire_in_one_then_anotherc                 C      | j dd d S )NT)r   )rk   r+   r   r   r    test_acquire_in_one_then_another      z9TestSemaphoreMultiThread.test_acquire_in_one_then_anotherc                 C   sF   | j ddtjd\}}| dg| |  t|dd }| | d S )NFT)r   rh   r   	_notifier)rk   r   SMALLEST_RELIABLE_DELAYr"   r   getattrassertIsNone)r   rL   acquired_in_threadnotifierr   r   r   &test_acquire_in_one_then_another_timed   s   
z?TestSemaphoreMultiThread.test_acquire_in_one_then_another_timedc              	      s   ddl m} | |d dd l}|       fdd}g }g }t |}|j| j	|
 |
  ||tjddd_t      |  td	D ]}t   tj qQ| |jd | g | | d
g| | |j| d }d S )Nr   rU   rW   c                     s       }   ttj | S r&   )r   r`   r    r!   r   rd   )ackrL   threadr   r   greenlet_one   s   zeTestSemaphoreMultiThread.test_acquire_in_one_wait_greenlet_wait_thread_gives_up.<locals>.greenlet_oner   rX   T   F)r    rV   r[   r\   rW   r@   r   spawnr^   rS   r]   r   rd   r_   rc   r   rG   ra   r"   r
   rf   dead)r   rV   rW   ry   rF   rK   gletrj   r   rw   r   6test_acquire_in_one_wait_greenlet_wait_thread_gives_up   s6   

zOTestSemaphoreMultiThread.test_acquire_in_one_wait_greenlet_wait_thread_gives_upc                 C   s   |  |j| d S r&   )rr   rI   )r   rL   r   r   r   assertOneHasNoHub   s   z*TestSemaphoreMultiThread.assertOneHasNoHubz&Flaky there; can't reproduce elsewherer   Nc           	   	      s,  ddl m} ddlm |d dd l}ddlm 	 
 dddgd f	d	d
}|j|dd}d|_|j|dd}d|_|  |  |d |d | sk| rt}|d |d |krdn| sk| skd d g d S )Nr   rU   rA   rW   )r!   i'  Tc              
      s8  rt   zzItD ]B}s n=j  } s|sJ |r#  || < s4  | |f rC|d dkrCt tj q|d dkrNtj qW n" t	yr } zdd l
}|  t|| < d }W Y d }~nd }~ww W  }|d ur|  |jdd d S d S  }|d ur|  |jdd w w )NrZ   r   d   TrC   )r    get_hubra   r   r   rr   r!   r   rp   	Exception	traceback	print_excstrrG   rH   )ixirK   exr   rI   	acquire_argscount
create_hubrB   native_sleepresultsrunr   rL   r   r   do_it  sL   


z<TestSemaphoreMultiThread.test_dueling_threads.<locals>.do_it)r   )rY   args)r?   r?      F)r    rV   rR   rB   r[   r\   rW   timer!   r@   r   r^   r_   r`   rG   is_alivelistr"   )	r   r   r   rV   rW   r   t1t2curr   r   r   test_dueling_threads   s8   
"



	z-TestSemaphoreMultiThread.test_dueling_threadsc                 C   s   |  d d S )N)T   r   r+   r   r   r   test_dueling_threads_timeout<  r.   z5TestSemaphoreMultiThread.test_dueling_threads_timeoutc                 C   rl   )NT)r   r   r+   r   r   r   test_dueling_threads_with_hub?  rn   z6TestSemaphoreMultiThread.test_dueling_threads_with_hub)TF)r   N)r/   r0   r1   r=   r@   rS   rb   rk   rm   ru   r~   r   r9   skipOnPyPyOnWindowsr   r   r   r   r   r   r   r;   G   s     
>0Jr;   c                   @      e Zd Zdd ZdS )TestBoundedSemaphoreMultiThreadc                 C   r<   r&   r   r+   r   r   r   r=   I  r>   z/TestBoundedSemaphoreMultiThread._getTargetClassN)r/   r0   r1   r=   r   r   r   r   r   G      r   zNeeds C extensionc                   @   r   )TestCExtc                 C   s   |  tjd d S )Nzgevent._gevent_c_semaphore)r"   r   r0   r+   r   r   r   test_c_extensionO  s   zTestCExt.test_c_extensionN)r/   r0   r1   r   r   r   r   r   r   L  s    r   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )SwitchWithFixedHashc                 C   s   |j | _ || _d S r&   )switchhashcode)r   greenletr   r   r   r   r,   [  s   
zSwitchWithFixedHash.__init__c                 C      t r&   AssertionErrorr+   r   r   r   __hash___  r>   zSwitchWithFixedHash.__hash__c                 C   r   r&   r   )r   otherr   r   r   __eq__b  r>   zSwitchWithFixedHash.__eq__c                 O   s   | j |i |S r&   )r   r   r   kwargsr   r   r   __call__e  rn   zSwitchWithFixedHash.__call__c                 C   s
   t | jS r&   )r   r   r+   r   r   r   __repr__h  s   
zSwitchWithFixedHash.__repr__N)r/   r0   r1   r,   r   r   r   r   r   r   r   r   r   T  s    r   c                   @   s   e Zd ZdZdd ZdS )FirstGrZ   c                 O   s,   t jj| g|R i | t| | j| _d S r&   )r    Greenletr,   r   r   r   r   r   r   r   r,   p  s   zFirstG.__init__N)r/   r0   r1   r   r,   r   r   r   r   r   k  s    r   c                   @   s   e Zd ZdZdS )LastG   N)r/   r0   r1   r   r   r   r   r   r   u  s    r   c                 C   s   |    |d d S )NT)r   r   )rL   should_quitr   r   r   acquire_then_exitz  s   r   c                 C   s*   |rd S |    tt| |}|  d S r&   )r   r   r{   release_then_spawnrG   rL   r   gr   r   r   acquire_then_spawn  s
   r   c                 C   s*   |    |rd S tt| |}|  d S r&   )r   r   r{   r   rG   r   r   r   r   r     s
   r   c                   @   r   )TestSemaphoreFairc                 C   s   t  }g }tt||}tt||}tt||}| tjj	 t
|||g W d    n1 s4w   Y  | |j| | |j| | |j| |  |  |  |  t  d S r&   )r   r   r{   r   r   r   assertRaisesr    
exceptionsLoopExitjoinallrf   r|   r[   r   killrc   )r   rL   r   keep_going1keep_going2exitingr   r   r   test_fair_or_hangs  s    z$TestSemaphoreFair.test_fair_or_hangsN)r/   r0   r1   r   r   r   r   r   r     r   r   __main__)
__future__r   r   r'   r    gevent.exceptionsgevent.lockr   r   gevent.testingtestingr9   r   TestCaser	   r;   r   skipOnPurePythonr   r3   r   r   r   r   r   r   r   r   r/   mainr   r   r   r   <module>   s4   3  
