o
    tPf`7                     @   s  d dl mZ d dl mZ d dl mZ d dlZd dlZd dlmZmZ d dl	m
Z d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ ee 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G dd deZG dd deZG dd dejZ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jZ G d!d" d"e Z!G d#d$ d$e Z"G d%d& d&ejZ#G d'd( d(e#Z$G d)d* d*e#Z%G d+d, d,e#Z&G d-d. d.ejZ'[[e(d/kre)  dS dS )0    )absolute_import)print_function)divisionN)EventAsyncResult)xrange)AbstractGenericGetTestCase)AbstractGenericWaitTestCase)
SMALL_TICK)SMALL_TICK_MAX_ADJc                   @      e Zd Zdd Zdd ZdS )TestEventWaitc                 C      t  j|d d S Ntimeout)r   waitselfr    r   Y/var/www/html/humari/django-venv/lib/python3.10/site-packages/gevent/tests/test__event.pyr         zTestEventWait.waitc                 C   s   t t  d S N)strr   r   r   r   r   
test_cover      zTestEventWait.test_coverN)__name__
__module____qualname__r   r   r   r   r   r   r          r   c                   @   r   )TestGeventWaitOnEventc                 C      t jt g|d d S r   )geventr   r   r   r   r   r   r          zTestGeventWaitOnEvent.waitc                    s4   t    fdd fdd}t|  d S )Nc                      s       d S r   setr   )eventr   r   setter*      z:TestGeventWaitOnEvent.test_set_during_wait.<locals>.setterc                     sv   t }   }|    |         t j fdd}    | d S )Ng{Gz?r   )	r#   spawnr   
assertTruereadyjoinclearassertFalseassertNotIn)sresor'   r   r(   r   r   waiter-   s   

z:TestGeventWaitOnEvent.test_set_during_wait.<locals>.waiter)r   r#   r*   r-   )r   r5   r   r4   r   test_set_during_wait#   s   z*TestGeventWaitOnEvent.test_set_during_waitN)r   r   r   r   r6   r   r   r   r   r!      r    r!   c                   @      e Zd Zdd ZdS )TestAsyncResultWaitc                 C   r   r   )r   r   r   r   r   r   r   G   r   zTestAsyncResultWait.waitNr   r   r   r   r   r   r   r   r8   E       r8   c                   @   r7   )TestWaitAsyncResultc                 C   r"   r   )r#   r   r   r   r   r   r   r   M   r$   zTestWaitAsyncResult.waitNr9   r   r   r   r   r;   K   r:   r;   c                   @   r7   )TestAsyncResultGetc                 C   r   r   )r   getr   r   r   r   r   S   r   zTestAsyncResultGet.waitNr9   r   r   r   r   r<   Q   r:   r<   c                   @   s   e Zd ZdS )MyExceptionN)r   r   r   r   r   r   r   r>   V   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 )TestAsyncResultc                 C   s6   t  }| t|jd  |d  |d  t| d S r   )r   assertRaises	TypeErrorrawlinkunlinkr   r   arr   r   r   	test_link[   s
   

zTestAsyncResult.test_linkc                    sp   g t    jd  jd   fdd}t| t } | td d|fg d S )Nr   c                     sF    t}    W d    n1 sw   Y  d| jf d S )Ncaught)r@   r>   r=   append	exception)excelogr   r   r   r5   h   s   
z,TestAsyncResult.test_set_exc.<locals>.waiterr   rG   )	r   assertEqualexc_inforI   r#   r*   r>   set_exceptionsleep)r   r5   objr   rK   r   test_set_excb   s   


zTestAsyncResult.test_set_excc              	   C   s   t  }td}tt|jd}| |j tj	d|) | 
t}|  W d    n1 s1w   Y  | ||j W d    d S 1 sHw   Y  d S )Ninterruptedzhello event1r   )r   r>   r#   spawn_laterDELAYr&   _close_on_teardownkillTimeout	start_newr@   r=   assertIsrI   )r   event1	timer_excgrJ   r   r   r   test_setr   s   
"zTestAsyncResult.test_setc                 C   s6   t  }t }tjt|j|d}| ||d|f  d S )Ntimeout_valuez1Nobody sent anything to event2 yet it received %r)r   objectr#   with_timeoutrV   r=   r[   )r   event2Xresultr   r   r   test_set_with_timeout   s   z%TestAsyncResult.test_set_with_timeoutc                 C   s.   t  }| jtj|jdd | tj|j d S )NF)block)r   r@   r#   rY   r=   
get_nowaitrD   r   r   r   test_nonblocking_get   s   z$TestAsyncResult.test_nonblocking_getN)r   r   r   rF   rS   r_   rg   rj   r   r   r   r   r?   Y   s    	r?   c                   @   st   e Zd Zdd Zdd ZdZdd Zejdd	d
Z	dd Z
dd Zdd Zdd Zejdd Zejdd ZdS )TestAsyncResultCrossThreadc                 C      t  S r   r   r   r   r   r   _makeOne      z#TestAsyncResultCrossThread._makeOnec                 C   s   | d d S Nz	from mainr%   r   oner   r   r   _setOne   r   z"TestAsyncResultCrossThread._setOne<   c                 C   s6   t jrdd l}|jd d dkr| d d S d S d S )Nr      )   ru   ru   z;PyPy bug: https://foss.heptapod.net/pypy/pypy/-/issues/3381)	greentestPYPYsyspypy_version_infoskipTest)r   ry   r   r   r   _check_pypy_switch   s   z-TestAsyncResultCrossThread._check_pypy_switchFc              
      s   |    ddlm ddlm  s|   | G  fddd}| }|  z|j  | |j	 |j
td  W |td  n|td  w | |j d S )Nr   Threadr   c                       s,   e Zd Z fddZfddZdS )z@TestAsyncResultCrossThread.test_cross_thread_use.<locals>.Threadc                    4    |  d| _  | _  | _ | _d| _d S NTz<never set>__init__daemonrunning_eventfinished_eventrn   async_resultrf   r   NativeEventNativeThreadtestr   r   r         


zITestAsyncResultCrossThread.test_cross_thread_use.<locals>.Thread.__init__c                    s|   t    fdd}t|}fdd}r#t|}|  ~n|     |  ~j  t jdd d S )Nc                      s&      s td     rd S d S )N   )is_setr   rV   r   )g_eventr   r   spin   s   zRTestAsyncResultCrossThread.test_cross_thread_use.<locals>.Thread.run.<locals>.spinc                      s2    j   r jj _d S  j  _d S r   )r   r&   r   r   BG_WAIT_DELAYrf   r   )r   r   
timed_waitr   r   work   s   
zRTestAsyncResultCrossThread.test_cross_thread_use.<locals>.Thread.run.<locals>.workTdestroy_loop)r   r#   r*   r-   r&   r   get_hubdestroy)r   r   gletr   worker)r   r   
wait_in_bg)r   r   r   run   s   


zDTestAsyncResultCrossThread.test_cross_thread_use.<locals>.Thread.runNr   r   r   r   r   r   r   r   r   r   r   r   r   r~      s    	r~         )assertNotMonkeyPatched	threadingr~   r   r|   startr   r   rs   r   r   rV   r-   _check_resultrf   )r   r   r   r~   threadr   r   r   test_cross_thread_use   s   	+
 z0TestAsyncResultCrossThread.test_cross_thread_usec                 C   s   |  |d d S rp   )rN   r   rf   r   r   r   r      s   z(TestAsyncResultCrossThread._check_resultc                 C      | j ddd d S )NFTr   r   r   r   r   r   r   test_cross_thread_use_bg   r   z3TestAsyncResultCrossThread.test_cross_thread_use_bgc                 C   r   )NTFr   r   r   r   r   r   test_cross_thread_use_timed   r   z6TestAsyncResultCrossThread.test_cross_thread_use_timedc                 C   s   | j ddd d S )NTr   r   r   r   r   r   test_cross_thread_use_timed_bg   r   z9TestAsyncResultCrossThread.test_cross_thread_use_timed_bgc              
      s   |    ddlm ddlm  |   | G  fddd}| }d }zt|j}|j	| j
}W |td  |d urF|t n|td  |d urX|t w w | | d S )Nr   r}   r   c                       s(   e Zd Z fddZfddZdS )zJTestAsyncResultCrossThread.test_cross_thread_use_set_in_bg.<locals>.Threadc                    r   r   r   r   r   r   r   r      r   zSTestAsyncResultCrossThread.test_cross_thread_use_set_in_bg.<locals>.Thread.__init__c                    s4   | j    | j | j  t jdd d S )NTr   )r   r&   rs   r   r   r#   r   r   r   )r   r   r   r     s   

zNTestAsyncResultCrossThread.test_cross_thread_use_set_in_bg.<locals>.Thread.runNr   r   r   r   r   r~      s    	r~   r   )r   r   r~   r   r|   r#   r*   r   r   r   r   r-   rV   r   )r   r~   r   r   rf   r   r   r   test_cross_thread_use_set_in_bg   s(   
z:TestAsyncResultCrossThread.test_cross_thread_use_set_in_bgc                 C   s   |    d S r   )r   r   r   r   r    test_cross_thread_use_set_in_bg2  s   z;TestAsyncResultCrossThread.test_cross_thread_use_set_in_bg2N)FF)r   r   r   rn   rs   r   r|   rw   ignores_leakcheckr   r   r   r   r   r   r   r   r   r   r   rk      s    
F
#rk   c                   @   $   e Zd Zdd Zdd Zdd ZdS )TestEventCrossThreadc                 C   rl   r   r   r   r   r   r   rn   !  ro   zTestEventCrossThread._makeOnec                 C   s   |   d S r   r%   rq   r   r   r   rs   $  r)   zTestEventCrossThread._setOnec                 C   s   |  | d S r   )r+   r   r   r   r   r   '  r   z"TestEventCrossThread._check_resultN)r   r   r   rn   rs   r   r   r   r   r   r     s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )TestAsyncResultAsLinkTargetFc                 C   s   t dd }t t t }}}|| || || | | d | | d t }t j	t
|j|d}| || d S )Nc                   S   s   dS )N   r   r   r   r   r   <lambda>/  s    z6TestAsyncResultAsLinkTarget.test_set.<locals>.<lambda>r   r`   )r#   r*   r   link
link_valuelink_exceptionrN   r=   rb   rc   rV   r[   )r   r^   s1s2s3re   rf   r   r   r   r_   .  s   


z$TestAsyncResultAsLinkTarget.test_setc                 C   s   dd }t |}t t t }}}|| || || | tj|j	 t
 }t jt|j	|d}| || | tj|j	 d S )Nc                   S   s
   t d)N.TestAsyncResultAsLinkTarget.test_set_exception)rw   ExpectedExceptionr   r   r   r   func;  s   
z<TestAsyncResultAsLinkTarget.test_set_exception.<locals>.funcr`   )r#   r*   r   r   r   r   r@   rw   r   r=   rb   rc   rV   r[   )r   r   r^   r   r   r   re   rf   r   r   r   test_set_exception:  s   



r   N)r   r   r   error_fatalr_   r   r   r   r   r   r   +  s    r   c                   @   s   e Zd ZdZdd ZdS )TestEvent_SetThenClearr   c                    sN   t    fddt| jD }td       |D ]}|  qd S )Nc                    s   g | ]}t  jqS r   )r#   r*   r   ).0irL   r   r   
<listcomp>N  s    z/TestEvent_SetThenClear.test.<locals>.<listcomp>gMbP?)r   rangeNr#   rQ   r&   r.   r-   )r   waitersgreenletr   r   r   r   L  s   

zTestEvent_SetThenClear.testN)r   r   r   r   r   r   r   r   r   r   I  s    r   c                   @      e Zd ZdZdS )TestEvent_SetThenClear100d   Nr   r   r   r   r   r   r   r   r   V      r   c                   @   r   )TestEvent_SetThenClear1000i  Nr   r   r   r   r   r   Z  r   r   c                   @   s:   e Zd ZdZdZdZed Zdd Ze	ddd	 Z
dS )
TestWaitr   Nr   g      Y@c                 C   sP   |s|r&t | j |r|   t | j |r |   |s|sd S d S r   )r#   rQ   periodpopr&   )r   eventsasyncsr   r   r   _senderd  s   zTestWait._senderz:Not all results have arrived sometimes due to timer issuesc                 C   s   dd t | jD }dd t | jD }t|t| }t| j||}tj|| | j| jd}| jd u r8|}n	t	|| j| j
 }| jd u rO| | | nt	| j|}| | | |  | |t||t||f d S )Nc                 S      g | ]}t  qS r   r   r   _r   r   r   r   o      z!TestWait.test.<locals>.<listcomp>c                 S   r   r   rm   r   r   r   r   r   p  r   )countr   )r   r   lenr#   r*   r   r   r   r   minr   r+   r,   r/   rX   rN   )r   r   r   max_lensenderresultsexpected_lenr   r   r   r   m  s   

 zTestWait.test)r   r   r   r   r   r   r   r   rw   skipOnAppVeyorr   r   r   r   r   r   ^  s    	r   c                   @   s   e Zd ZdZdS )TestWait_notimeoutN)r   r   r   r   r   r   r   r   r     r   r   c                   @   r   )TestWait_count1r   Nr   r   r   r   r   r   r   r   r     r   r   c                   @   r   )TestWait_count2r   Nr   r   r   r   r   r     r   r   c                   @   r   )TestEventBasicsc                 C   s&   t  }t|}| ||  ~~d S r   )r   weakrefrefr[   )r   rL   rr   r   r   test_weakref  s
   
zTestEventBasics.test_weakrefc                    s   t   g  fdd}t|d t|d t  | d   t j }|	j
d    |d | g d |  |  d S )Nc                    s       |  d S r   )r   rH   )argr'   r   r   r   wait_then_append  s   zCTestEventBasics.test_wait_while_notifying.<locals>.wait_then_appendr   r      ru   )r   r   ru   )r   r#   r*   idlerN   	linkcountr   loopcheckr   rH   r&   stopclose)r   r   r   r   r   r   test_wait_while_notifying  s   z)TestEventBasics.test_wait_while_notifyingc                 C   sF   t  }|  t|g}| ||g t|g}| ||g d S r   )r   r&   r#   r   rN   )r   r'   rf   r   r   r   'test_gevent_wait_twice_when_already_set  s   z7TestEventBasics.test_gevent_wait_twice_when_already_setN)r   r   r   r   r   r   r   r   r   r   r     s    r   __main__)*
__future__r   r   r   r   r#   gevent.eventr   r   gevent.testingtestingrw   gevent.testing.sixr   gevent.testing.timingr   r	   r
   r   rV   r   r!   r8   r;   r<   	Exceptionr>   TestCaser?   rk   r   r   r   r   r   r   r   r   r   r   r   mainr   r   r   r   <module>   sJ    	'6 #0