U
    |h                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZmZm	Z	 d dl
mZmZ d dlmZ ddlmZmZ dd Zejj d	d
 Zejj dd Zeejj dd Zejj dd Zejj dd ZdS )    N)AsyncMemorizedFuncAsyncNotMemorizedFuncMemorizedResultMemoryNotMemorizedResult)np
with_numpy)raises   )corrupt_single_cache_itemmonkeypatch_cached_func_warnc                    sd   t |dd}|| } tdD ]@}tdD ]2}| |I dH }||ksHtt||d ks*tq*qdS )z= Similar to check_identity_lazy_async for coroutine functionsr   locationverbose      Nr
   )r   cacherangeAssertionErrorlen)funcaccumulatorr   memoryi_value r   A/tmp/pip-unpacked-wheel-9qhk7zno/joblib/test/test_memory_async.pycheck_identity_lazy_async   s    
r   c           	         s  t    fdd}t| | jI d H  dD ]}dD ]`}t| jd||d}tj| jdd ||}|d	I d H  |jd
d t }|d	I d H }q2t |d	 kst	|
|d	I d H }||kst	t |d	 ks*t	q*d|_t| jdd}||d	I d H  d S )Nc                    s   t dI d H   d | S N皙?r
   asynciosleepappend)nr   r   r   f   s    
z(test_memory_integration_async.<locals>.f)FT)rN
   )r   r   	mmap_modecompressT)ignore_errorsr
   F)warn__main__r   r   )listr   strpathr   shutilrmtreer   clearr   r   eval
__module__)	tmpdirr'   r+   r*   r   gcurrent_accumulatoroutZevaledr   r&   r   test_memory_integration_async   s,     
r:   c                     sd   t    fdd} td dd}|| }tdD ].}t }|dI d H  t |d ks0tq0d S )Nc                    s   t dI d H   d | S r   r!   )xr&   r   r   ffH   s    
z test_no_memory_async.<locals>.ffr   r      r
   )r/   r   r   r   r   r   )r<   r   ggr   r8   r   r&   r   test_no_memory_asyncD   s    
r?   c           
         s   t | jddd}| dd }td}||I dH }||I dH }t|tjsVt|jdksdtt|tjstt|jdkst~~t	
  t| t||}||I dH }t|dkstd	}	|	|d kstt|tjst|jdkstdS )
z8Check that mmap_mode is respected even at the first callr(   r   )r   r*   r   c                    s   | d S )Nr   r   )ar   r   r   twice\   s    z6test_memory_numpy_check_mmap_mode_async.<locals>.twicer   Nr
   zException while loading results)r   r0   r   r   Zones
isinstanceZmemmapr   modegcZcollectr   r   r   )
r6   Zmonkeypatchr   rA   r@   bcZrecorded_warningsdZexception_msgr   r   r   'test_memory_numpy_check_mmap_mode_asyncU   s*    


rH   c              
      s   d	dd}t t|| jt|t| jdd|td d|fttttfD ]p\}}tdD ]2}|	dI d H }t
||s~t| dks\tq\|  tt |  W 5 Q R X |  qLd S )
Nr
   c                    s   t dI d H  | d | S )Nr    r   )r"   r#   )r;   yr   r   r   r'      s    z%test_call_and_shelve_async.<locals>.fr   r   )r   r      )r
   )zipr   r0   r   r   r   r   r   r   Zcall_and_shelverB   r   getr3   r	   KeyError)r6   r'   r   ZResultr   resultr   r   r   test_call_and_shelve_async}   s.    
 	
rO   c                    s   dd }| j |dgd}i }|d|I d H dks4t|d|I d H dksLt|d|I d H \}}|dksrtdt|tstdd S )	Nc                    s,   t dI d H  || dd || < ||  S )Nr    r   r
   )r"   r#   rL   )r;   counterr   r   r   r<      s    z*test_memorized_func_call_async.<locals>.ffrP   )ignorer   r
   zf has not been called properlyz0Metadata are not returned by MemorizedFunc.call.)r   r   callrB   dict)r   r<   r>   rP   r;   metar   r   r   test_memorized_func_call_async   s    rU   )r"   rD   r1   ZpytestZjoblib.memoryr   r   r   r   r   Zjoblib.test.commonr   r   Zjoblib.testingr	   Ztest_memoryr   r   r   markr:   r?   rH   rO   rU   r   r   r   r   <module>   s&   
(
&
