U
    3dT                     @   s   d dl Z d dlmZ d dlZd dlmZmZmZ d dlm	Z	m
Z
 dd Zdd Zd	d
 Zdd Zejddddgdd Zdd ZdS )    N)ThreadPoolExecutor)
get_config
set_configconfig_context)delayedParallelc                   C   s  t  dddddddddks ttdd t  d	 dks<ttdd& t  dddddddddkshtW 5 Q R X t  d	 dksttdd td d t  d	 dkstW 5 Q R X t  d	 dksttddt t  d	 dksttd d8 t  d	 dksttdd t  d	 dks&tW 5 Q R X t  d	 dksDtW 5 Q R X t  d	 dksbtW 5 Q R X t  dddddddddksttt td W 5 Q R X tt tdd
  W 5 Q R X d S )NFi   TZdiagram   default)assume_finiteZworking_memoryZprint_changed_onlyZdisplayZarray_api_dispatchZpairwise_dist_chunk_sizeZenable_cython_pairwise_distZtransform_outputr
   r
   Zdo_something_else)r   AssertionErrorr   r   pytestraises	TypeError	__enter__ r   r   =/tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/tests/test_config.pytest_config_context
   sd    


r   c                	   C   sp   t  d dkstz2tdd t  d dks2tt W 5 Q R X W n tk
rX   Y nX t  d dksltd S )Nr
   FTr   )r   r   r   
ValueErrorr   r   r   r   test_config_context_exceptionP   s    r   c                	   C   s   t  d dksttd d t  d dks.ttdd t  d dksJttd d t  d dksfttdd t  d dksttt tdd W 5 Q R X d S )Nr
   Fr   Tr   )r   r   r   r   r   r   r   r   r   r   test_set_config[   s    



r   c              
   C   s6   t | d" t| t d W  5 Q R  S Q R X dS )zAReturn the value of assume_finite after waiting `sleep_duration`.r   r
   N)r   timesleepr   )r
   Zsleep_durationr   r   r   set_assume_finitek   s    
r   backendZlokymultiprocessing	threadingc                 C   sP   ddddg}ddddg}t | dddd t||D }|ddddgksLtd	S )
ao  Test that the global config is threadsafe with all joblib backends.
    Two jobs are spawned and sets assume_finite to two different values.
    When the job with a duration 0.1s completes, the assume_finite value
    should be the same as the value passed to the function. In other words,
    it is not influenced by the other job setting assume_finite to True.
    FT皙?皙?   )r   Zn_jobsc                 s   s    | ]\}}t t||V  qd S )N)r   r   ).0r
   Z	sleep_durr   r   r   	<genexpr>}   s   z0test_config_threadsafe_joblib.<locals>.<genexpr>N)r   zipr   )r   assume_finitessleep_durationsitemsr   r   r   test_config_threadsafe_joblibr   s    r'   c               	   C   s^   ddddg} ddddg}t dd}dd |t| |D }W 5 Q R X |ddddgksZtd	S )
zUses threads directly to test that the global config does not change
    between threads. Same test as `test_config_threadsafe_joblib` but with
    `ThreadPoolExecutor`.FTr   r   r    )max_workersc                 S   s   g | ]}|qS r   r   )r!   outputr   r   r   
<listcomp>   s   z*test_config_threadsafe.<locals>.<listcomp>N)r   mapr   r   )r$   r%   er&   r   r   r   test_config_threadsafe   s    r-   )r   concurrent.futuresr   r   Zsklearnr   r   r   Zsklearn.utils.parallelr   r   r   r   r   r   markZparametrizer'   r-   r   r   r   r   <module>   s   F
