B
    18\|                 @   s  d dl Zejd ejd 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Zd dlZd dlZ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 d dlmZmZmZmZmZmZmZ d d	lmZ d d
l m!Z! eddfddZ"e"edZ#e"edZ$e"edZ%e"edZ&e"ee' dZ(e"eddZ)da*dd Z+dd Z,dd Z-dd Z.dd Z/dfddZ0G dd de1Z2G d d! d!Z3d"d# Z4G d$d% d%ej5Z6G d&d' d'Z7G d(d) d)e7Z8G d*d+ d+e7Z9G d,d- d-e7Z:G d.d/ d/e7Z;e6fe8e9e;e:ffd0d1Z<G d2d3 d3e7Z=G d4d5 d5e7Z>e<e= e<e> G d6d7 d7Z?G d8d9 d9e8e?e6Z@G d:d; d;e?ZAe<eAe9e;e:fd< G d=d> d>ZBG d?d@ d@e8eBe6ZCe<eBe9e;e:fd< G dAdB dBZDe<eD G dCdD dDZEG dEdF dFe8eEe6ZFG dGdH dHeEZGe<eGe9e;e:fd< dIdJ ZHdgdKdLZIdMdN ZJdOdP ZKdQdR ZLG dSdT dTe1ZMG dUdV dVe1ZNG dWdX dXe1ZOG dYdZ dZe1ZPG d[d\ d\e1ZQG d]d^ d^e1ZRG d_d` d`ZSe<eSe9e;e:fd< G dadb dbe6ZTejjUdcdd ZVeWdekreV  dS )h    N_multiprocessingzmultiprocessing.synchronize)assert_python_ok)QueueHandler)PicklingError)futures)PENDINGRUNNING	CANCELLEDCANCELLED_AND_NOTIFIEDFINISHEDFutureBrokenExecutor)BrokenProcessPool)get_contextc             C   s   t  }| |_||_||_|S )N)r   _state
_exceptionZ_result)state	exceptionresultf r   `/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_concurrent_futures.pycreate_future   s
    r   )r   )r   r   *   )r   r   uninitializedc             C   s   | | S )Nr   )xyr   r   r   mul1   s    r   c             C   s   t |  tdd S )Nzthis is an exception)timesleep	Exception)tr   r   r   sleep_and_raise4   s    
r"   c             C   s    t |  t| tj  d S )N)r   r   printsysstdoutflush)r!   msgr   r   r   sleep_and_print8   s    
r(   c             C   s   | a d S )N)INITIALIZER_STATUS)r   r   r   r   init=   s    r*   c               C   s   t S )N)r)   r   r   r   r   get_init_statusA   s    r+   c             C   sF   | d k	r0t d}|t|  |d d|_td tdd S )Nzconcurrent.futuresCRITICALFg?zerror in initializer)	logging	getLogger
addHandlerr   setLevel	propagater   r   
ValueError)	log_queueloggerr   r   r   	init_failD   s    


r5   c               @   s   e Zd Zdd ZdS )MyObjectc             C   s   d S )Nr   )selfr   r   r   	my_methodO   s    zMyObject.my_methodN)__name__
__module____qualname__r8   r   r   r   r   r6   N   s   r6   c               @   s   e Zd Zdd Zdd ZdS )EventfulGCObjc             C   s   t | }| | _d S )N)r   ManagerEventevent)r7   ctxZmgrr   r   r   __init__T   s    zEventfulGCObj.__init__c             C   s   | j   d S )N)r?   set)r7   r   r   r   __del__X   s    zEventfulGCObj.__del__N)r9   r:   r;   rA   rC   r   r   r   r   r<   S   s   r<   c             C   s   t  S )N)r6   )_r   r   r   make_dummy_object\   s    rE   c               @   s   e Zd Zdd Zdd ZdS )BaseTestCasec             C   s   t j | _d S )N)testsupportthreading_setup_thread_key)r7   r   r   r   setUpa   s    zBaseTestCase.setUpc             C   s   t j  t jj| j  d S )N)rG   rH   reap_childrenthreading_cleanuprJ   )r7   r   r   r   tearDownd   s    
zBaseTestCase.tearDownN)r9   r:   r;   rK   rN   r   r   r   r   rF   `   s   rF   c                   s@   e Zd ZdZi Z fddZ fddZdd Zdd	 Z  Z	S )
ExecutorMixin   c                sf   t    t | _t| dr@| jf | j|  d| j	| _
n| jf d| ji| j	| _
|   d S )Nr@   )max_workers
mp_contextrQ   )superrK   r   	monotonict1hasattrexecutor_typeworker_countr   executor_kwargsexecutor_prime_executor)r7   )	__class__r   r   rK   m   s    



zExecutorMixin.setUpc                sV   | j jdd d | _ t | j }tjjr:td| dd | 	|dd t
   d S )NT)waitz%.2fs )endi,  z+synchronization issue: test lasted too long)rZ   shutdownr   rT   rU   rG   rH   verboser#   
assertLessrS   rN   )r7   dt)r\   r   r   rN   |   s    zExecutorMixin.tearDownc             C   s
   t | jS )N)r   r@   )r7   r   r   r   r      s    zExecutorMixin.get_contextc                s2    fddt  jD }x|D ]}|  qW d S )Nc                s   g | ]} j tjd qS )g?)rZ   submitr   r   ).0rD   )r7   r   r   
<listcomp>   s   z1ExecutorMixin._prime_executor.<locals>.<listcomp>)rangerX   r   )r7   r   r   r   )r7   r   r[      s    

zExecutorMixin._prime_executor)
r9   r:   r;   rX   rY   rK   rN   r   r[   __classcell__r   r   )r\   r   rO   i   s   rO   c               @   s   e Zd ZejZdS )ThreadPoolMixinN)r9   r:   r;   r   ThreadPoolExecutorrW   r   r   r   r   ri      s   ri   c                   s&   e Zd ZejZdZ fddZ  ZS )ProcessPoolForkMixinforkc                s   t jdkr| d t  S )Nwin32zrequire unix system)r$   platformskipTestrS   r   )r7   )r\   r   r   r      s    

z ProcessPoolForkMixin.get_context)	r9   r:   r;   r   ProcessPoolExecutorrW   r@   r   rh   r   r   )r\   r   rk      s   rk   c               @   s   e Zd ZejZdZdS )ProcessPoolSpawnMixinspawnN)r9   r:   r;   r   rp   rW   r@   r   r   r   r   rq      s   rq   c                   s&   e Zd ZejZdZ fddZ  ZS )ProcessPoolForkserverMixin
forkserverc                s   t jdkr| d t  S )Nrm   zrequire unix system)r$   rn   ro   rS   r   )r7   )r\   r   r   r      s    

z&ProcessPoolForkserverMixin.get_context)	r9   r:   r;   r   rp   rW   r@   r   rh   r   r   )r\   r   rs      s   rs   c             C   sT   dd }xF|D ]>}d||j || j f }t|| f|f | i }|t |< qW d S )Nc             S   s4   |  dr| d d S |  dr,| d d S | S d S )N)ZMixinZTestsZTest)endswith)namer   r   r   strip_mixin   s
    

z*create_executor_tests.<locals>.strip_mixinz%s%sTest)r9   typeglobals)Zmixinbasesexecutor_mixinsry   Zexerx   clsr   r   r   create_executor_tests   s    
r   c                   s(   e Zd ZdZ fddZdd Z  ZS )InitializerMixin   c                s    da ttdd| _t   d S )Nr   )initialized)initializerinitargs)r)   dictr*   rY   rS   rK   )r7   )r\   r   r   rK      s    
zInitializerMixin.setUpc                s:    fddt  jD }x|D ]} | d qW d S )Nc                s   g | ]} j tqS r   )rZ   rd   r+   )re   rD   )r7   r   r   rf      s   z5InitializerMixin.test_initializer.<locals>.<listcomp>r   )rg   rX   assertEqualr   )r7   r   r   r   )r7   r   test_initializer   s    

z!InitializerMixin.test_initializer)r9   r:   r;   rX   rK   r   rh   r   r   )r\   r   r      s   r   c                   s>   e Zd ZdZ fddZdd Zdd Zejdd	 Z	  Z
S )
FailingInitializerMixinr   c                sZ   t | dr4|  | _| j | _tt| jfd| _nd | _d | _ttd| _t 	  d S )Nr@   )r   r   )r   )
rV   r   rR   Queuer3   r   r5   rY   rS   rK   )r7   )r\   r   r   rK      s    

zFailingInitializerMixin.setUpc          
   C   s   |  d y| jt}W n tk
r0   Y n X | t |  W d Q R X t }x0| jj	st | dkr|| 
d td qZW | t | jt W d Q R X W d Q R X d S )Nz ValueError: error in initializerrP   zexecutor not broken after 5 s.g{Gz?)_assert_loggedrZ   rd   r+   r   assertRaisesr   r   rT   Z_brokenfailr   )r7   futurerU   r   r   r   r      s    

z(FailingInitializerMixin.test_initializerc             C   s   d S )Nr   )r7   r   r   r   r[      s    z'FailingInitializerMixin._prime_executorc          	   #   s   | j d k	rLd V  g }yx|| j    qW W qp tjk
rH   Y qpX n$| dd}d V  W d Q R X |j}| t	 fdd|D | d S )Nzconcurrent.futuresr,   c             3   s   | ]} |kV  qd S )Nr   )re   line)r'   r   r   	<genexpr>  s    z9FailingInitializerMixin._assert_logged.<locals>.<genexpr>)
r3   append
get_nowait
getMessagequeueEmpty
assertLogsoutput
assertTrueany)r7   r'   r   cmr   )r'   r   r      s    
z&FailingInitializerMixin._assert_logged)r9   r:   r;   rX   rK   r   r[   
contextlibcontextmanagerr   rh   r   r   )r\   r   r      s
   r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ExecutorShutdownTestc             C   s$   | j   | t| j jtdd d S )Nr   rP   )rZ   r`   r   RuntimeErrorrd   pow)r7   r   r   r   test_run_after_shutdown  s    
z,ExecutorShutdownTest.test_run_after_shutdownc             C   sD   t ddj| jjt| ddd\}}}| | | | d d S )Nz-caK  if 1:
            from concurrent.futures import {executor_type}
            from time import sleep
            from test.test_concurrent_futures import sleep_and_print
            if __name__ == "__main__":
                context = '{context}'
                if context == "":
                    t = {executor_type}(5)
                else:
                    from multiprocessing import get_context
                    context = get_context(context)
                    t = {executor_type}(5, mp_context=context)
                t.submit(sleep_and_print, 1.0, "apple")
            r@    )rW   contexts   apple)r   formatrW   r9   getattrassertFalser   strip)r7   rcouterrr   r   r   test_interpreter_shutdown  s
    

z.ExecutorShutdownTest.test_interpreter_shutdownc             C   sJ   t ddj| jjt| ddd\}}}| d|  | | d d S )Nz-ca  if 1:
            import atexit
            @atexit.register
            def run_last():
                try:
                    t.submit(id, None)
                except RuntimeError:
                    print("runtime-error")
                    raise
            from concurrent.futures import {executor_type}
            if __name__ == "__main__":
                context = '{context}'
                if not context:
                    t = {executor_type}(5)
                else:
                    from multiprocessing import get_context
                    context = get_context(context)
                    t = {executor_type}(5, mp_context=context)
                    t.submit(id, 42).result()
            r@   r   )rW   r   z)RuntimeError: cannot schedule new futuress   runtime-error)	r   r   rW   r9   r   assertIndecoder   r   )r7   r   r   r   r   r   r   &test_submit_after_interpreter_shutdown2  s
    
z;ExecutorShutdownTest.test_submit_after_interpreter_shutdownc                s:    fddt dD } j  x|D ]}|  q&W d S )Nc                s   g | ]} j tjd qS )g?)rZ   rd   r   r   )re   rD   )r7   r   r   rf   O  s    z=ExecutorShutdownTest.test_hang_issue12364.<locals>.<listcomp>2   )rg   rZ   r`   r   )r7   fsr   r   )r7   r   test_hang_issue12364N  s    

z)ExecutorShutdownTest.test_hang_issue12364N)r9   r:   r;   r   r   r   r   r   r   r   r   r     s   r   c               @   s<   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )ThreadPoolShutdownTestc             C   s   d S )Nr   )r7   r   r   r   r[   V  s    z&ThreadPoolShutdownTest._prime_executorc             C   sl   | j tdd | j tdd | j tdd | t| j jd | j   x| j jD ]}|  qXW d S )N   r               )rZ   rd   r   r   len_threadsr`   join)r7   r!   r   r   r   test_threads_terminateY  s    
z-ThreadPoolShutdownTest.test_threads_terminatec             C   sj   t jdd<}|}| t|ttddddddddddddg
 W d Q R X x|jD ]}|  qVW d S )	NrP   )rQ   ru      r   r      r   )	r   rj   r   listmapabsrg   r   r   )r7   erZ   r!   r   r   r   test_context_manager_shutdownb  s    $z4ThreadPoolShutdownTest.test_context_manager_shutdownc             C   s@   t jdd}|ttdd |j}~x|D ]}|  q,W d S )NrP   )rQ   ru   )r   rj   r   r   rg   r   r   )r7   rZ   threadsr!   r   r   r   test_del_shutdownk  s    
z(ThreadPoolShutdownTest.test_del_shutdownc             C   sP   t jddd}|ttdd |j}~x"|D ]}| |jd |  q.W d S )NrP   ZSpecialPool)rQ   Zthread_name_prefixru   z^SpecialPool_[0-4]$)	r   rj   r   r   rg   r   assertRegexrx   r   )r7   rZ   r   r!   r   r   r   test_thread_names_assignedt  s    

z1ThreadPoolShutdownTest.test_thread_names_assignedc             C   sN   t jdd}|ttdd |j}~x"|D ]}| |jd |  q,W d S )NrP   )rQ   ru   zThreadPoolExecutor-\d+_[0-4]$)	r   rj   r   r   rg   r   r   rx   r   )r7   rZ   r   r!   r   r   r   test_thread_names_default  s    
z0ThreadPoolShutdownTest.test_thread_names_defaultN)	r9   r:   r;   r[   r   r   r   r   r   r   r   r   r   r   U  s   			r   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
ProcessPoolShutdownTestc             C   s   d S )Nr   )r7   r   r   r   r[     s    z'ProcessPoolShutdownTest._prime_executorc             C   st   | j tdd | j tdd | j tdd | t| j jd | j j}| j   x| D ]}|  q`W d S )Nr   r   r   r   r   r   rP   )	rZ   rd   r   r   r   
_processesr`   valuesr   )r7   	processespr   r   r   test_processes_terminate  s    
z0ProcessPoolShutdownTest.test_processes_terminatec             C   sn   t jdd>}|j}| t|ttddddddddddddg
 W d Q R X x| D ]}|	  qZW d S )	NrP   )rQ   ru   r   r   r   r   r   )
r   rp   r   r   r   r   r   rg   r   r   )r7   r   r   r   r   r   r   r     s    $z5ProcessPoolShutdownTest.test_context_manager_shutdownc             C   sj   t jdd}t|ttdd |j}|j}|j}|j}~|	  x|
 D ]}|	  qNW |  d S )NrP   )rQ   ru   )r   rp   r   r   r   rg   Z_queue_management_threadr   Z_call_queuer   r   join_thread)r7   rZ   Zqueue_management_threadr   Z
call_queuer   r   r   r   r     s    z)ProcessPoolShutdownTest.test_del_shutdownN)r9   r:   r;   r[   r   r   r   r   r   r   r   r     s   	r   )r}   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )	WaitTestsc             C   sd   | j tdd}| j tjd}tjt||gtjd\}}| 	t
|g| | 	t
t|g| d S )Nr   r   g      ?)return_when)rZ   rd   r   r   r   r   r]   CANCELLED_FUTUREFIRST_COMPLETEDr   rB   )r7   future1future2doneZnot_doner   r   r   test_first_completed  s    zWaitTests.test_first_completedc             C   sT   | j tjd}tjtt|gtjd\}}| 	t
ttg| | 	t
|g| d S )Ng      ?)r   )rZ   rd   r   r   r   r]   CANCELLED_AND_NOTIFIED_FUTURESUCCESSFUL_FUTUREr   r   rB   )r7   r   finishedpendingr   r   r   +test_first_completed_some_already_completed  s    
z5WaitTests.test_first_completed_some_already_completedc             C   sr   | j tdd}| j td}| j tjd}tj|||gtjd\}}| 	t
||g| | 	t
|g| d S )Nr   r   g      ?r   )r   )rZ   rd   r   r"   r   r   r   r]   FIRST_EXCEPTIONr   rB   )r7   r   r   Zfuture3r   r   r   r   r   test_first_exception  s    zWaitTests.test_first_exceptionc             C   sl   | j tdd}| j tjd}tjttt	||gtj
d\}}| ttt	|g| | tt|g| d S )Nr   r   g      ?)r   )rZ   rd   divmodr   r   r   r]   r   r   r   r   r   rB   )r7   r   r   r   r   r   r   r   *test_first_exception_some_already_complete  s    z4WaitTests.test_first_exception_some_already_completec             C   sP   | j tjd}tjt|gtjd\}}| t	tg| | t	|g| d S )Nr   )r   )
rZ   rd   r   r   r   r]   EXCEPTION_FUTUREr   r   rB   )r7   r   r   r   r   r   r   'test_first_exception_one_already_failed  s    z1WaitTests.test_first_exception_one_already_failedc             C   sj   | j tdd}| j tdd}tjttt||gtj	d\}}| 
tttt||g| | 
t | d S )Nr   r   r   )r   )rZ   rd   r   r   r   r]   r   r   r   ALL_COMPLETEDr   rB   )r7   r   r   r   r   r   r   r   test_all_completed  s    zWaitTests.test_all_completedc             C   sn   | j tdd}| j tjd}tjttt	||gdtj
d\}}| tttt	|g| | t|g| d S )Nr   r   rP   )timeoutr   )rZ   rd   r   r   r   r   r]   r   r   r   r   r   rB   )r7   r   r   r   r   r   r   r   test_timeout  s    zWaitTests.test_timeoutN)
r9   r:   r;   r   r   r   r   r   r   r   r   r   r   r   r     s   
r   c               @   s   e Zd Zdd ZdS )ThreadPoolWaitTestsc          	      sl   t    fddt }td z4fddtdD }   tj|tj	d W d t| X d S )Nc                  s       d S )N)r]   r   )r?   r   r   future_func&  s    z@ThreadPoolWaitTests.test_pending_calls_race.<locals>.future_funcgư>c                s   h | ]}j  qS r   )rZ   rd   )re   i)r   r7   r   r   	<setcomp>+  s    z>ThreadPoolWaitTests.test_pending_calls_race.<locals>.<setcomp>d   )r   )
	threadingr>   r$   getswitchintervalsetswitchintervalrg   rB   r   r]   r   )r7   Zoldswitchintervalr   r   )r?   r   r7   r   test_pending_calls_race"  s    
z+ThreadPoolWaitTests.test_pending_calls_raceN)r9   r:   r;   r   r   r   r   r   r      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 )AsCompletedTestsc             C   sV   | j tdd}| j tdd}ttttt||g}| 	tttt||g| d S )Nr   r   r   r   )
rZ   rd   r   rB   r   as_completedr   r   r   r   )r7   r   r   	completedr   r   r   test_no_timeout:  s    z AsCompletedTests.test_no_timeoutc             C   sv   | j tjd}t }y.x(tjttt	|gddD ]}|
| q0W W n tjk
rZ   Y nX | tttt	g| d S )Nr   r   )r   )rZ   rd   r   r   rB   r   r   r   r   r   addTimeoutErrorr   )r7   r   Zcompleted_futuresr   r   r   r   test_zero_timeoutJ  s     z"AsCompletedTests.test_zero_timeoutc             C   s@   | j tjd}dd tt|dD }| t	|d d S )Nr   c             S   s   g | ]}|qS r   r   )re   r   r   r   r   rf   c  s    z;AsCompletedTests.test_duplicate_futures.<locals>.<listcomp>r   r   )
rZ   rd   r   r   r   r   	itertoolsrepeatr   r   )r7   r   r   r   r   r   test_duplicate_futures]  s    z'AsCompletedTests.test_duplicate_futuresc          	   C   s   dd t dD }|ttd |ttdd | tj@ x8tj|ddD ]&}|	| t
|}~| |  qRW W d Q R X |d d	 xFt|D ]8}|	| t
|}~| |  |r|d d	 qW d S )
Nc             S   s   g | ]
}t  qS r   )r   )re   rD   r   r   r   rf   j  s    zGAsCompletedTests.test_free_reference_yielded_future.<locals>.<listcomp>   )r   r   )r   r   r   )r   rG   )rg   r   r   r
   r   r   r   r   r   removeweakrefrefassertIsNone
set_result)r7   futures_listr   wrr   r   r   "test_free_reference_yielded_futureg  s"    



z3AsCompletedTests.test_free_reference_yielded_futurec          	   C   sL   t tttg}| tj}ttj|dd W d Q R X | 	t
|jd d S )Nr   )r   z2 (of 4) futures unfinished)r   PENDING_FUTURERUNNING_FUTUREr   r   r   r   r   r   r   strr   )r7   r   r   r   r   r   "test_correct_timeout_exception_msg~  s
    z3AsCompletedTests.test_correct_timeout_exception_msgN)r9   r:   r;   r   r   r   r   r  r   r   r   r   r   8  s
   
r   c               @   s\   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
jdd Zdd Zdd ZdS )ExecutorTestc             C   s$   | j tdd}| d|  d S )Nr   r      )rZ   rd   r   r   r   )r7   r   r   r   r   test_submit  s    zExecutorTest.test_submitc             C   s&   | j jtddd}| d|  d S )Nr   r   )r      )rZ   rd   r   r   r   )r7   r   r   r   r   test_submit_keyword  s    z ExecutorTest.test_submit_keywordc          	   C   sx   |  t| jttdtdttttdtd |  t| jjttdtdddttttdtd d S )N
   r   )	chunksize)r   r   rZ   r   r   rg   )r7   r   r   r   test_map  s    zExecutorTest.test_mapc             C   sR   | j tddddgddddg}| | d | | d | t|j d S )Nr   r   r   r   rP   )r   r   )rZ   r   r   r   __next__r   ZeroDivisionError)r7   r   r   r   r   test_map_exception  s     zExecutorTest.test_map_exceptionc             C   sl   g }y2x,| j jtjdddgddD ]}|| q"W W n tjk
rL   Y nX | d | d d g| d S )Nr   r   rP   )r   zexpected TimeoutError)	rZ   r   r   r   r   r   r   r   r   )r7   resultsr   r   r   r   test_map_timeout  s    
zExecutorTest.test_map_timeoutc             C   s(   | j tdg| jd   | j   d S )Nr   r   )rZ   r   r  rX   r`   )r7   r   r   r   test_shutdown_race_issue12456  s    z*ExecutorTest.test_shutdown_race_issue12456c                sN   t  }t  t| fdd}| j|j ~ jdd}| 	|d d S )Nc                s      S )N)rB   )obj)my_object_collectedr   r   <lambda>      z7ExecutorTest.test_no_stale_references.<locals>.<lambda>g      @)r   z-Stale reference not collected within timeout.)
r6   r   r>   r   r   rZ   rd   r8   r]   r   )r7   Z	my_objectZmy_object_callbackZ	collectedr   )r  r   test_no_stale_references  s    z%ExecutorTest.test_no_stale_referencesc          
   C   s6   x0dD ](}|  td | j|d W d Q R X qW d S )N)r   z"max_workers must be greater than 0)rQ   )assertRaisesRegexr2   rW   )r7   numberr   r   r   test_max_workers_negative  s    
z&ExecutorTest.test_max_workers_negativec             C   s8   x2| j ttdD ]}t|}~| |  qW d S )Nr	  )rZ   r   rE   rg   r   r   r   )r7   r  r   r   r   r   test_free_reference  s    
z ExecutorTest.test_free_referenceN)r9   r:   r;   r  r  r  r  r  r  rG   rH   cpython_onlyr  r  r  r   r   r   r   r    s   	r  c               @   s   e Zd Zdd Zdd ZdS )ThreadPoolExecutorTestc                sD   g   fdd}| j |td | j jdd |  td dS )zTests verifying issue 11777.c                s     |  d S )N)r   )n)r   r   r   record_finished  s    zRThreadPoolExecutorTest.test_map_submits_without_iteration.<locals>.record_finishedr	  T)r]   N)rZ   r   rg   r`   assertCountEqual)r7   r  r   )r   r   "test_map_submits_without_iteration  s
    z9ThreadPoolExecutorTest.test_map_submits_without_iterationc             C   s&   |   }| |jt pdd  d S )Nr   rP   )rW   r   Z_max_workersos	cpu_count)r7   rZ   r   r   r   test_default_workers  s    z+ThreadPoolExecutorTest.test_default_workersN)r9   r:   r;   r!  r$  r   r   r   r   r    s   
r  c               @   s8   e Zd Zdd Zdd Zedd Zdd Zd	d
 ZdS )ProcessPoolExecutorTestc             C   sd   | j tjdg}tt| j j }|  x|D ]}| 	t
|j q4W | 	t
| j jtdd d S )Nr   r   r   )rZ   rd   r   r   nextiterr   r   	terminater   r   r   r   )r7   r   r   Zfutr   r   r   test_killed_child  s    
z)ProcessPoolExecutorTest.test_killed_childc          	      s    fdd}t tttdtd} t  jjttdtddd|  t  jjttdtddd|  t  jjttdtddd|  t| d S )Nc                  s$   t  jjttdtddd d S )N(   r  )r
  )r   rZ   r   r   rg   r   )r7   r   r   bad_map  s    z;ProcessPoolExecutorTest.test_map_chunksize.<locals>.bad_mapr*  r   )r
  r   )r   r   r   rg   r   rZ   r   r2   )r7   r+  r   r   )r7   r   test_map_chunksize  s    z*ProcessPoolExecutorTest.test_map_chunksizec             C   s   t dd S )N{   )r   )r~   r   r   r   _test_traceback  s    z'ProcessPoolExecutorTest._test_tracebackc          	   C   s   | j | j}| t}|  W d Q R X |j}| t|t	 | 
|jd |j}| t|tjj | d|j tj 2}y|W n" t	k
r   tjt   Y nX W d Q R X | d|  d S )N)r-  z&raise RuntimeError(123) # some comment)rZ   rd   r.  r   r    r   r   assertIsrz   r   r   args	__cause__r   processZ_RemoteTracebackr   tbrG   rH   captured_stderrr$   
excepthookexc_infogetvalue)r7   r   r   exccausef1r   r   r   test_traceback  s     z&ProcessPoolExecutorTest.test_tracebackc             C   s8   t | j}| jt|}|  | |jjdd d S )Nr   )r   )	r<   r@   rZ   rd   idr   r   r?   r]   )r7   r  r   r   r   r   test_ressources_gced_in_workers"  s    
z7ProcessPoolExecutorTest.test_ressources_gced_in_workersN)	r9   r:   r;   r)  r,  classmethodr.  r;  r=  r   r   r   r   r%    s
   r%  c              C   s   dd l } |  t_d S )Nr   )ioStringIOr$   stderr)r?  r   r   r   hide_process_stderr1  s    rB  c             C   s*   | rt |  ddl}|  |  dS )zInduces a segfault.r   N)r   r   faulthandlerdisable_sigsegv)delayrC  r   r   r   _crash6  s
    
rG  c               C   s   t d dS )z#Induces a sys exit with exitcode 1.r   N)r$   exitr   r   r   r   _exit?  s    rI  c             C   s   t   |  dS )z-Function that raises an Exception in process.N)rB  )ZErrr   r   r   _raise_errorD  s    rJ  c             C   s   t   |  S )z(Function that returns a instance of cls.)rB  )r~   r   r   r   _return_instanceJ  s    rK  c               @   s   e Zd ZdZdd ZdS )CrashAtPicklez5Bad object that triggers a segfault at pickling time.c             C   s
   t   d S )N)rG  )r7   r   r   r   
__reduce__R  s    zCrashAtPickle.__reduce__N)r9   r:   r;   __doc__rM  r   r   r   r   rL  P  s   rL  c               @   s   e Zd ZdZdd ZdS )CrashAtUnpicklez7Bad object that triggers a segfault at unpickling time.c             C   s   t dfS )Nr   )rG  )r7   r   r   r   rM  X  s    zCrashAtUnpickle.__reduce__N)r9   r:   r;   rN  rM  r   r   r   r   rO  V  s   rO  c               @   s   e Zd ZdZdd ZdS )ExitAtPicklez9Bad object that triggers a process exit at pickling time.c             C   s
   t   d S )N)rI  )r7   r   r   r   rM  ^  s    zExitAtPickle.__reduce__N)r9   r:   r;   rN  rM  r   r   r   r   rP  \  s   rP  c               @   s   e Zd ZdZdd ZdS )ExitAtUnpicklez;Bad object that triggers a process exit at unpickling time.c             C   s   t dfS )Nr   )rI  )r7   r   r   r   rM  d  s    zExitAtUnpickle.__reduce__N)r9   r:   r;   rN  rM  r   r   r   r   rQ  b  s   rQ  c               @   s   e Zd ZdZdd ZdS )ErrorAtPicklez3Bad object that triggers an error at pickling time.c             C   s   ddl m} |dd S )Nr   )r   zError in pickle)pickler   )r7   r   r   r   r   rM  j  s    zErrorAtPickle.__reduce__N)r9   r:   r;   rN  rM  r   r   r   r   rR  h  s   rR  c               @   s   e Zd ZdZdd ZdS )ErrorAtUnpicklez5Bad object that triggers an error at unpickling time.c             C   s   ddl m} t|ffS )Nr   )UnpicklingError)rS  rU  rJ  )r7   rU  r   r   r   rM  q  s    zErrorAtUnpickle.__reduce__N)r9   r:   r;   rN  rM  r   r   r   r   rT  o  s   rT  c               @   s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )ExecutorDeadlockTest   c             C   s   t | |S )N)r   r   )r~   r   rF  r   r   r   	_sleep_idy  s    
zExecutorDeadlockTest._sleep_idc          	   C   s   dd l }ddlm} |dd$}|j|d |d | }W d Q R X x|j D ]}|  qTW |j	dd t
d| tjd | d	|  d S )
Nr   )TemporaryFilezw+)mode)fileT)r]   z
Traceback:
 zExecutor deadlock:

)rC  tempfilerY  dump_tracebackseekreadr   r   r(  r`   r#   r$   
__stderr__r   )r7   rZ   rC  rY  r   r3  r   r   r   r   _fail_on_deadlock~  s    
z&ExecutorDeadlockTest._fail_on_deadlockc             C   sh  | j jdd tt ftdftt ftdftt ftdftt ftdft	dtdft
dtd	fttftd
fttftdfttftdfttftdfttftdfttftdfg}x|D ]\}}}}| | tj  | jdt| jd}|j|f| }| |< y|j| jd W n" tjk
r4   | | Y nX W d Q R X |jdd W d Q R X W d Q R X qW d S )NT)r]   zerror at task picklezexit at task unpicklezerror at task unpicklezcrash at task unpickler   z%crash during func execution on workerz$exit during func execution on workerz%error during func execution on workerz$crash during result pickle on workerz#exit during result pickle on workerz$error during result pickle on workerz.error during result unpickle in result_handlerz-exit during result unpickle in result_handlerr   )rQ   rR   )r   )rZ   r`   r<  rR  r   rQ  r   rT  rO  rG  rI  
SystemExitrJ  r   rK  rL  rP  subTestrG   rH   r4  rW   r   r@   rd   r   r   TIMEOUTr   r   ra  )r7   Zcrash_casesfuncr0  errorrx   rZ   resr   r   r   
test_crash  sH    


zExecutorDeadlockTest.test_crashc          
   C   sp   | j jdd | jdt| jdD}|| _ |jtdd}|jdd | t |	  W d Q R X W d Q R X d S )NT)r]   r   )rQ   rR   g?)rF  )
rZ   r`   rW   r   r@   rd   rG  r   r   r   )r7   rZ   r   r   r   r   test_shutdown_deadlock  s    z+ExecutorDeadlockTest.test_shutdown_deadlockN)	r9   r:   r;   rd  r>  rX  ra  rh  ri  r   r   r   r   rV  v  s
   4rV  c               @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#S )$FutureTestsc                s:   d   fdd}t  }|| |d | d  d S )Nc                s   |    d S )N)r   )callback_future)callback_resultr   r   fn  s    z6FutureTests.test_done_callback_with_result.<locals>.fnrP   )r   add_done_callbackr   r   )r7   rm  r   r   )rl  r   test_done_callback_with_result  s    

z*FutureTests.test_done_callback_with_resultc                s@   d   fdd}t  }|| |td | d j d S )Nc                s   |    d S )N)r   )rk  )callback_exceptionr   r   rm    s    z9FutureTests.test_done_callback_with_exception.<locals>.fnrG   )rG   )r   rn  set_exceptionr    r   r0  )r7   rm  r   r   )rp  r   !test_done_callback_with_exception  s    
z-FutureTests.test_done_callback_with_exceptionc                s<   d   fdd}t  }|| | |  |   d S )Nc                s   |    d S )N)	cancelled)rk  )was_cancelledr   r   rm    s    z6FutureTests.test_done_callback_with_cancel.<locals>.fn)r   rn  r   cancel)r7   rm  r   r   )rt  r   test_done_callback_with_cancel  s    
z*FutureTests.test_done_callback_with_cancelc          	      s   t j n}dd fdd} fdd}t }|| || |d |  |   | d|  W d Q R X d S )NFc                s   d t dd S )NTzdoh!)r    )rk  )raising_was_calledr   r   
raising_fn  s    z9FutureTests.test_done_callback_raises.<locals>.raising_fnc                s   d d S )NTr   )rk  )fn_was_calledr   r   rm    s    z1FutureTests.test_done_callback_raises.<locals>.fnrP   zException: doh!)	rG   rH   r4  r   rn  r   r   r   r7  )r7   rA  rx  rm  r   r   )ry  rw  r   test_done_callback_raises  s    




z%FutureTests.test_done_callback_raisesc                s:   d   fdd}t  }|d || | d  d S )Nc                s   |    d S )N)r   )rk  )rl  r   r   rm    s    z=FutureTests.test_done_callback_already_successful.<locals>.fnrP   )r   r   rn  r   )r7   rm  r   r   )rl  r   %test_done_callback_already_successful  s    

z1FutureTests.test_done_callback_already_successfulc                s@   d   fdd}t  }|td || | d j d S )Nc                s   |    d S )N)r   )rk  )rp  r   r   rm    s    z9FutureTests.test_done_callback_already_failed.<locals>.fnrG   )rG   )r   rq  r    rn  r   r0  )r7   rm  r   r   )rp  r   !test_done_callback_already_failed  s    
z-FutureTests.test_done_callback_already_failedc                s<   d   fdd}t  }| |  || |   d S )Nc                s   |    d S )N)rs  )rk  )rt  r   r   rm  (  s    z<FutureTests.test_done_callback_already_cancelled.<locals>.fn)r   r   ru  rn  )r7   rm  r   r   )rt  r   $test_done_callback_already_cancelled&  s    
z0FutureTests.test_done_callback_already_cancelledc             C   sd   |  ttd |  ttd |  ttd |  ttd |  ttd |  ttd d S )Nz%<Future at 0x[0-9a-f]+ state=pending>z%<Future at 0x[0-9a-f]+ state=running>z'<Future at 0x[0-9a-f]+ state=cancelled>z5<Future at 0x[0-9a-f]+ state=finished raised OSError>z3<Future at 0x[0-9a-f]+ state=finished returned int>)r   reprr   r  r   r   r   r   )r7   r   r   r   	test_repr1  s    



zFutureTests.test_reprc             C   s   t td}t td}t td}t td}t tt d}t tdd}| |  | 	|j
t | |  | 	|j
t | |  | 	|j
t | |  | 	|j
t | |  | 	|j
t | |  | 	|j
t d S )N)r   )r   r   rP   )r   r   )r   r   r   r	   r
   r   OSErrorr   ru  r   r   r   )r7   r:  f2Zf3Zf4Zf5Zf6r   r   r   test_cancelB  s$    



zFutureTests.test_cancelc             C   sX   |  t  |  t  | t  | t  |  t  |  t  d S )N)	r   r   rs  r  r   r   r   r   r   )r7   r   r   r   test_cancelled\  s    zFutureTests.test_cancelledc             C   sX   |  t  |  t  | t  | t  | t  | t  d S )N)	r   r   r   r  r   r   r   r   r   )r7   r   r   r   	test_doned  s    zFutureTests.test_donec             C   sX   |  t  | t  |  t  |  t  |  t  |  t  d S )N)	r   r   runningr   r  r   r   r   r   )r7   r   r   r   test_runningl  s    zFutureTests.test_runningc             C   sz   | j tjtjdd | j tjtjdd | j tjtjdd | j tjtjdd | j t	t
jdd | tjddd d S )Nr   )r   r   )r   r   r   r   r   r  CancelledErrorr   r   r  r   r   r   )r7   r   r   r   test_result_with_timeoutt  s    z$FutureTests.test_result_with_timeoutc                sJ    fdd}t td tj|d}|  |  jddd |  d S )Nc                  s   t d  d d S )Nr   r   )r   r   r   r   )r:  r   r   notification  s    
z:FutureTests.test_result_with_success.<locals>.notification)r   )targetrP   )r   r   )r   r   r   Threadstartr   r   r   )r7   r  r!   r   )r:  r   test_result_with_success  s    
z$FutureTests.test_result_with_successc                sJ    fdd}t td tj|d}|  | jtj jdd |	  d S )Nc                  s   t d    d S )Nr   )r   r   ru  r   )r:  r   r   r    s    
z9FutureTests.test_result_with_cancel.<locals>.notification)r   )r  rP   )r   )
r   r   r   r  r  r   r   r  r   r   )r7   r  r!   r   )r:  r   test_result_with_cancel  s    
z#FutureTests.test_result_with_cancelc             C   s   | j tjtjdd | j tjtjdd | j tjtjdd | j tjtjdd | 	t
tjddt | tjddd  d S )Nr   )r   )r   r   r   r   r   r  r  r   r   r   
isinstancer   r  r   r   )r7   r   r   r   test_exception_with_timeout  s    z'FutureTests.test_exception_with_timeoutc                sN    fdd}t td tj|d}|  | t jddt |	  d S )Nc            	      s8   t d  j t _t  _ j  W d Q R X d S )Nr   )r   r   Z
_conditionr   r   r  r   
notify_allr   )r:  r   r   r    s
    
z=FutureTests.test_exception_with_success.<locals>.notification)r   )r  rP   )r   )
r   r   r   r  r  r   r  r   r  r   )r7   r  r!   r   )r:  r   test_exception_with_success  s    
z'FutureTests.test_exception_with_successN)r9   r:   r;   ro  rr  rv  rz  r{  r|  r}  r  r  r  r  r  r  r  r  r  r  r   r   r   r   rj    s"   rj  c               C   s"   zt jt W d t j  X d S )N)rG   rH   run_unittestr9   rL   r   r   r   r   	test_main  s    r  __main__)N)N)XZtest.supportrG   rH   import_moduleZtest.support.script_helperr   r   r   r-   logging.handlersr   r"  r   r$   r   r   unittestr   rS  r   Z
concurrentr   Zconcurrent.futures._baser   r   r	   r
   r   r   r   Zconcurrent.futures.processr   multiprocessingr   r   r   r  r   r   r  r   r   r)   r   r"   r(   r*   r+   r5   objectr6   r<   rE   TestCaserF   rO   ri   rk   rq   rs   r   r   r   r   r   r   r   r   r   r  r  r%  rB  rG  rI  rJ  rK  rL  rO  rP  rQ  rR  rT  rV  rj  reap_threadsr  r9   r   r   r   r   <module>   s   $





		*

;@7)
e
POA

	\
 c
