
    [e!                        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mZ d dl	Z	d dl
mZmZ d dlmZ e	j        j         d             Ze	j        j         d             Ze	j        j         d             Ze	j        j         d	             Ze	j        j         d
             Ze	j        j         e	j                            dddg          d                         Ze	j        j         d             Ze	j        j         d             ZdS )    N)UserDict)ExecutorFuture)AsyncBufferFunc)
utils_testc                   K   t          i t          j        d          d          5 }d|d<   d|d<   d|d<   t          |j                  h d	k    sJ |j        rJ |j        rJ d
|d<   t          |j                  h dk    sJ |j        rJ |j        sJ t          j        |j                   d {V  t          |j                  h dk    sJ t          |j                  dhk    sJ |	                                 |j        rJ |	                    d           |j        rJ |	                    d           |j        sJ t          j        |j                   d {V  t          |j                  ddhk    sJ t          |j                  ddhk    sJ t          |j        j                  ddgk    sJ |                    dg          }|                                sJ | d {V ddik    sJ t          |j        j                  ddgk    sJ t          j        t           d          5  |                    ddgd          }d d d            n# 1 swxY w Y   t          |j        j                  ddgk    sJ |                    ddgd          }|                                sJ | d {V dd
ik    sJ t          |j        j                  ddgk    sJ t          j        t"                    5  |                    dgd          }d d d            n# 1 swxY w Y   |                    ddg          }|                                rJ ||j        v sJ | d {V dddk    sJ |j        rJ t          |j                  h dk    sJ t          |j                  dhk    sJ 	 d d d            d S # 1 swxY w Y   d S )N{Gz?   n   a   bc>   r   r   r      d>   r   r   r   r   >   r   r   r   
   m)matchraisemissingomitmisspell)r   r   >   r   r   r   )r   r   SlowDictsetfastslowfuturesasynciowaitasync_evict_until_below_targetlistorder	async_getdonepytestraisesKeyError
ValueError)check_thread_leaksbufffuture_s       <lib/python3.11/site-packages/zict/tests/test_async_buffer.pytest_simpler2      s     	R,T22a	8	8	8 5'DS	S	S	49~~00009<S	49~~!5!5!555559|l4<(((((((((49~~000049~~#&&&&++---<++B///<++A...|l4<(((((((((49~~#s++++49~~#s++++ DIO$$c
2222&&{{}}||||||Qx''''DIO$$c
2222 ]83/// 	< 	<Sz7;;A	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<DIO$$c
2222c
F;;{{}}||||||Qx''''DIO$$c
2222]:&& 	: 	:uj99A	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: c
++;;==   %%%%||||||QQ//////<49~~000049~~#&&&&&k5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5' 5's\   HO	7IO	I!	!O	$I!	%BO	L'O	'L+	+O	.L+	/BO		OOc                   K   t          i t          j        d          d          5 }d|d<   d|d<   d|d<   t          |j                  dk    sJ |j        rJ |                    d           t          |j                  dk    sJ t          |j        	                                          dgk    sJ |                    d           t          |j                  dk    sJ t          |j        	                                          dgk    sJ |                    d           t          |j                  dk    sJ t          |j        	                                          ddgk    sJ t          j        |j        t          j        	           d
{V  t          |j                  dk    sJ t          |j        	                                          dgk    sJ t          j        |j                   d
{V  |j        rJ |j        rJ |j        ddik    sJ |j        j        dddk    sJ |                    d           |                    d           |j        rJ |j        rJ 	 d
d
d
           d
S # 1 swxY w Y   d
S )zMUser calls async_evict_until_below_target() while the same is already runningr
   r   r   r   xr   yz)return_whenN)r4   r5   r   )r   r   r   lenr   r!   r$   r%   evictingvaluesr"   r#   FIRST_COMPLETEDr    dataevict_until_below_targetr-   r.   s     r1   test_double_evictr@   H   s      
R,T22a	8	8	8 #!DS	S	S	49~~""""<++A...4<  A%%%%DM((**++s2222 	++A...4<  A%%%%DM((**++s2222 	++A...4<  A%%%%DM((**++1v5555l4<W5LMMMMMMMMMM4<  A%%%%DM((**++s2222l4<(((((((((<=   yS!H$$$$y~qq!1!11111 	%%a(((++B///<=    G#! #! #! #! #! #! #! #! #! #! #! #! #! #! #! #! #! #!s   IJ  JJc                 *  K   t          i t          j        d          d          }|                    d t	          d          D                        |j        rJ t          |j                  dk    sJ |                    d           |j	        s!t          j        d           d {V  |j	        !|j        sJ |j        sJ |                                 t          j        |j                   d {V  |j        rJ |j        sJ |j	        sJ d S )Nr
   d   r   c                     i | ]}||S  rD   .0is     r1   
<dictcomp>z+test_close_during_evict.<locals>.<dictcomp>t   s    ***!A***    r   )r   r   r   updateranger!   r9   r   r$   r    r"   sleepcloser#   r?   s     r1   test_close_during_evictrN   q   s<     r:.t44<<<DKK**uSzz***+++|ty>>S    ''***i "mD!!!!!!!!! i "9<JJLLL
,t|
$
$$$$$$$$|99rI   c                   K   t          i t          j        d          d          }|j        j                            d t          d          D                        t          |          dk    sJ |j        rJ |	                    t          t          d                              }|j        sJ |j        s!t          j        d           d {V  |j        !|                                 t          j        t          j                  5  | d {V  d d d            n# 1 swxY w Y   t          j        |j                   d {V  |j        rJ |j        sJ |j        sJ d S )Nr
   rB   r   c                     i | ]}||S rD   rD   rE   s     r1   rH   z)test_close_during_get.<locals>.<dictcomp>   s    444A1a444rI   )r   r   r   r    r=   rJ   rK   r9   r   r'   r%   r!   r"   rL   rM   r)   r*   CancelledErrorr#   )r-   r.   r/   s      r1   test_close_during_getrR      s     r:.t44<<<DIN44s444555t99y^^Ds,,--F<i "mD!!!!!!!!! i " 	JJLLL	w-	.	.                
,t|
$
$$$$$$$$|99s   	D""D&)D&c                 f  	K   t          j        dd          t          j                    t          j                    	t          j                    t          j                    fd}	fd}t	          i t          ||i           d          5 }                    d           d	|d
<   |j        sJ                     d          sJ                     d                                            t          j        |j                   d {V  |j
        j        d
dik    sJ |                    d
g          }	                    d          sJ                     d                                            | d {V d
dik    sJ 	 d d d            d S # 1 swxY w Y   d S )Nvr   )defaultc                                                                           d          sJ |                                 z   S N   timeoutr   r#   get)rT   
block_dumpctxin_dumps    r1   dumpztest_contextvars.<locals>.dump   <    q)))))37799}rI   c                                                                           d          sJ |                                 z   S rW   r[   )rT   
block_loadr^   in_loads    r1   loadztest_contextvars.<locals>.load   ra   rI   g?r      r   r4   rX   rY   i,     i  iA  )contextvars
ContextVar	threadingEventr   r   r   r!   r#   r"   r    r   r'   )
r-   r`   re   r.   futr]   rc   r^   r_   rd   s
        @@@@@r1   test_contextvarsrm      s*     

 a
0
0
0CoGoG""J""J      
      
 
RdD"--	5	5	5 'S	|||A|&&&&&l4<(((((((((y{sBi''''nncU##||A|&&&&&yyyyyyS#J&&&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's   C?F&&F*-F*r   r   r   c                 ,  K    G d dt                     }t          i  |            d          5 }|j                            d t	          d          D                        t          |          dk    sJ |                    t          t	          d                    |          }|j        s!t          j
        d           d{V  |j        !|j        sJ t	          d          D ]}||= |j        rJ |j        rJ |                                rJ |d	k    r:t          j        t                    5  | d{V  ddd           n# 1 swxY w Y   n&| d{V }d
t          |          cxk     rdk     sn J ddd           dS # 1 swxY w Y   dS )zAll required keys exist in slow when you call get_async(); however some are
    deleted by the time the offloaded thread retrieves their values.
    c                        e Zd Z fdZ xZS )3test_race_condition_get_async_delitem.<locals>.Slowc                 v    || v rt          j        d           t                                          |          S )Nr
   )timerL   super__getitem__)selfkey	__class__s     r1   rt   z?test_race_condition_get_async_delitem.<locals>.Slow.__getitem__   s4    d{{
4   77&&s+++rI   __name__
__module____qualname__rt   __classcell__)rw   s   @r1   Slowrp      s8        	, 	, 	, 	, 	, 	, 	, 	, 	,rI   r}   rB   r   c                     i | ]}||S rD   rD   rE   s     r1   rH   z9test_race_condition_get_async_delitem.<locals>.<dictcomp>   s    3331!Q333rI   r   r
   Nr   r   )r   r   r    rJ   rK   r9   r'   r%   r   r"   rL   r(   r)   r*   r+   )r-   r   r}   r.   r/   rG   outs          r1   %test_race_condition_get_async_delitemr      si     , , , , ,x , , , 
R3	'	'	' &4	33c

3334444yyCU3ZZ 0 0'BB) 	&-%%%%%%%%% ) 	&y s 	 	AQ99;;==   gx((                 ,,,,,,Cs3xx%%%%#%%%%%%+& & & & & & & & & & & & & & & & & &s6   DF	5	E
>F	
E	F	E	*F		FFc                    K   t          j        d           G fddt                    } t          i  |             dd          5 }d|d<   d|d<   |                    d	           |j        rJ t          |j                  ddhk    sJ t          j	        |
                    dg          |
                    dg                     d {V }|ddiddigk    sJ 	 d d d            d S # 1 swxY w Y   d S )
Nr   c                   "     e Zd Z fdZ xZS )+test_multiple_offload_threads.<locals>.Slowc                 r                         d           t                                          |          S rW   )r#   rs   rt   )ru   rv   rw   barriers     r1   rt   z7test_multiple_offload_threads.<locals>.Slow.__getitem__   s/    LLL###77&&s+++rI   rx   )rw   r   s   @r1   r}   r      s>        	, 	, 	, 	, 	, 	, 	, 	, 	, 	,rI   r}   rB   )r   nthreadsr   r4   r5   r   )rj   Barrierr   r   r>   r   r   r    r"   gatherr'   )r}   r.   r   r   s      @r1   test_multiple_offload_threadsr      s|     ""G, , , , , , ,x , , ,
 
R3	3	3	3 +tS	S	%%a(((949~~#s++++N4>>3%#8#8$..#:O:OPPPPPPPPQx#q******+ + + + + + + + + + + + + + + + + +s   BC,,C03C0c                    K   d G fddt                     }  |             }t          i i d|          }d|d<   d|d<   |j        j        ddik    sJ |j        ddik    sJ |                    dg           d {V ddik    sJ |j        j        ddik    sJ |j        ddik    sJ dk    sJ |                                 d S )	Nr   c                        e Zd Z fdZd ZdS )*test_external_executor.<locals>.MyExecutorc                h    dz   ||i |}t                      }|                    |           |S )Nr   )r   
set_result)ru   fnargskwargsr   fn_submits         r1   submitz1test_external_executor.<locals>.MyExecutor.submit   s@    MH"d%f%%CALLHrI   c                      t          d          )Nz.AsyncBuffer.close() called executor.shutdown())AssertionError)ru   r   r   s      r1   shutdownz3test_external_executor.<locals>.MyExecutor.shutdown  s     !QRRRrI   N)ry   rz   r{   r   r   )r   s   r1   
MyExecutorr      sB        	 	 	 	 		S 	S 	S 	S 	SrI   r   r   )r   executorr4   r   r5   )r   r   r   r   r    r'   rM   )r   exr.   r   s      @r1   test_external_executorr      s2     H
S 
S 
S 
S 
S 
S 
SX 
S 
S 
S 
Br2R000DDIDI9;3(""""9a    &&&&&&&&3(22229;3(""""9a    q====JJLLLLLrI   )r"   rh   rj   rr   collectionsr   concurrent.futuresr   r   r)   zictr   r   
zict.testsr   markr2   r@   rN   rR   rm   parametrizer   r   r   rD   rI   r1   <module>r      s                         / / / / / / / /  " " " " " " " " ! ! ! ! ! ! 6' 6' 6'r %! %! %!P   &   * ' ' 'B Wf$566 &  & 76  &F + + +&     rI   