B
    u9avq                @   s  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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Zd dlZd dlZd dlZd dlZd dlmZ ejdZejd 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! yd dlm"Z" e"j#Z$W n e%k
r8   dZ$Y nX yd dl&m'Z'm(Z( d	Z)W n e%k
rl   dZ)Y nX yd dl*Z*W n e%k
r   dZ*Y nX d
Z+dd Z,dd Z-dd Z.e!j/Z0dZ1dZ2e2rd\Z3Z4Z5n
d\Z3Z4Z5e6edd Z7ej8dkZ9d dlm:Z: dd Z;ye<dZ=W n   dZ=Y nX ddgZ>yd dl?m@Z@mAZAmBZBmCZC W n& e%k
rr   eDZ@d ZA ZBZCY nX dd ZEG d d! d!eDZFG d"d# d#eDZGd$d% ZHG d&d' d'ZIG d(d) d)eGZJG d*d+ d+ejKZLG d,d- d-eGZMd.d/ ZNd0d1 ZOG d2d3 d3eGZPG d4d5 d5eGZQG d6d7 d7eGZRG d8d9 d9eGZSG d:d; d;eGZTG d<d= d=eDZUd>d? ZVG d@dA dAeDZWG dBdC dCeDZXG dDdE dEeGZYG dFdG dGeGZZG dHdI dIeGZ[G dJdK dKeGZ\ddMdNZ]dOdP Z^dQdR Z_dSdT Z`G dUdV dVeDZaG dWdX dXebZcdYdZ ZdG d[d\ d\eGZed]d^ Zfd_d` ZgG dadb dbeGZhG dcdd ddeGZid delmjZjmkZkmlZl G dfdg dgeDZmdhdi ZnG djdk dkekZoG dldm dmejZpepjqdnemdo epjqdpemdqdr epjqdieneods G dtdu dueGZres Ztdvdw ZuG dxdy dyejZvevjqdweudo G dzd{ d{ejZwewqdw d|ZxG d}d~ d~eGZyG dd deGZze,dZ{G dd deGZ|G dd deGZ}G dd deGZ~G dd deGZe e$dG dd deGZG dd deGZG dd de@ZG dd deGZG dd deGZG dd de jZG dd deGZG dd deGZG dd de jZG dd de jZdd ZG dd de jZdd Zdd Zdd Zdd ZG dd deDZG dd de jZG dd de jZG dd de jZG dd de jZG dd de jZG dd de jZG dd de jZG dd de jZG dd de jZG dd de jZe ej8dkdG ddÄ de jZG ddń de jZG ddǄ de jZG ddɄ deDZG dd˄ deZG dd̈́ deZG ddτ deZddф ZdS )    N)support_multiprocessingzmultiprocessing.synchronize)util)	reductionF)ValuecopyTg      N@c             C   s
   |  dS )Nlatin)encode)s r   +/usr/lib/python3.7/_test_multiprocessing.pyr   F   s    r   c             C   s"   t | tjjr|   |   d S )N)
isinstancemultiprocessingqueuesQueueclosejoin_thread)queuer   r   r   close_queueJ   s    r   c             C   s   t j| td d S )N)timeout)r   r   TIMEOUT)processr   r   r   join_processP   s    r   g?)g=
ףp=?gffffff?gffffff?)g?g?g?ZHAVE_BROKEN_SEM_GETVALUEwin32)waitc             C   s    |d k	r|dk rd }t | g|S )Ng        )r   )Zhandler   r   r   r   wait_for_handlen   s    r   SC_OPEN_MAX   __main__z$test.test_multiprocessing_forkserver)	Structurec_intc_double
c_longlongc           	   C   sR   d} yt d}W n ttfk
r*   dS X |dks<|| kr@dS td|  dS )zACheck that the system supports enough semaphores to run the test.r   SC_SEM_NSEMS_MAXNzHThe OS doesn't support enough semaphores to run the test (required: %d).)ossysconfAttributeError
ValueErrorunittestSkipTest)Z	nsems_minZnsemsr   r   r   check_enough_semaphores   s    r+   c               @   s   e Zd Zdd Zdd ZdS )TimingWrapperc             C   s   || _ d | _d S )N)funcelapsed)selfr-   r   r   r   __init__   s    zTimingWrapper.__init__c             O   s*   t  }z| j||S t  | | _X d S )N)time	monotonicr-   r.   )r/   argskwdstr   r   r   __call__   s    zTimingWrapper.__call__N)__name__
__module____qualname__r0   r6   r   r   r   r   r,      s   r,   c               @   s,   e Zd ZdZdd Zdd Zdd ZeZdS )	BaseTestCase)	processesmanagerthreadsc             C   s   t r| ||d d S )N   )CHECK_TIMINGSassertAlmostEqual)r/   abr   r   r   assertTimingAlmostEqual   s    z$BaseTestCase.assertTimingAlmostEqualc             G   s2   y|| }W n t k
r    Y nX | ||S d S )N)NotImplementedErrorassertEqual)r/   valuer-   r3   resr   r   r   assertReturnsIfImplemented   s
    z'BaseTestCase.assertReturnsIfImplementedc             G   s   t dd S )Nz#shouldn't try to pickle a test case)rD   )r/   r3   r   r   r   
__reduce__   s    zBaseTestCase.__reduce__N)r7   r8   r9   ALLOWED_TYPESrC   rH   rI   __reduce_ex__r   r   r   r   r:      s
   
r:   c             C   s^   y|   S  tk
rX   y| jS  tk
rR   y| jS  tk
rL   tY nX Y nX Y nX d S )N)	get_valuer'   Z_Semaphore__valueZ_valuerD   )r/   r   r   r   rL      s    rL   c               @   s   e Zd Zdd ZdS )DummyCallablec             C   s   t |tst|d d S )N   )r   rM   AssertionErrorput)r/   qcr   r   r   r6      s    zDummyCallable.__call__N)r7   r8   r9   r6   r   r   r   r   rM      s   rM   c               @   s0  e Zd ZdZdd Zdd Zedd Zdd	 Zed
d Z	edd Z
dd Zdd Zdd Zdd Zdd Zedd Zdd Zedd Zdd ZedAd"d#Zd$d% Zd&d' Zd(d) Zed*d+ Zd,d- Zed.d/ Zd0d1 Zei fd2d3Zd4d5 Zd6d7 ZedBd9d:Zd;d< Z d=d> Z!d?d@ Z"d!S )C_TestProcess)r;   r=   c             C   s   | j dkr| d| j  |  }|j}| |  | |j  | |t	 | t
|dk | |jt  | |jd  d S )Nr=   ztest not appropriate for {}r   )TYPEskipTestformatcurrent_processauthkey
assertTrueis_alivedaemonassertIsInstancebyteslenrE   Zidentr%   getpidexitcode)r/   currentrX   r   r   r   test_current   s    
z_TestProcess.test_currentc             C   sz   | j dkr| d| j  | j| jd}| |j|  j | j| jdd}| |j | j| jdd}| 	|j d S )Nr=   ztest not appropriate for {})targetT)rc   r[   F)
rT   rU   rV   Process_testrE   r[   rW   rY   assertFalse)r/   Zproc0Zproc1proc2r   r   r   test_daemon_argument   s    
z!_TestProcess.test_daemon_argumentc             O   sR   |   }|| || ||j | jdkrN|t|j ||j d S )Nr=   )rW   rP   namerT   r]   rX   pid)clsrQ   r3   r4   ra   r   r   r   re      s    


z_TestProcess._testc             C   s  |  d}|  }|ddf}ddd}d}| j| j|||d}d|_|  }| jd	krf| |j|j | |	 d
 | |jd | 
||   | t|  tk | |jd  |  | |jd  | |	 d | ||   | | |dd   | | | | | |j | jd	krX| | |j | | |j |  | |jd | |	 d
 | 
||   t| d S )Nr>         gRQ@)helloZbyeZSomeProcess)rc   r3   kwargsri   Tr=   Fr   )r   Eventrd   re   r[   rW   rT   rE   rX   rZ   assertNotInactive_childrenrY   typelistr`   startassertIngetri   rj   joinr   )r/   rQ   er3   ro   ri   pra   r   r   r   test_process  s>    



z_TestProcess.test_processc             C   s   t d d S )Nd   )r1   sleep)rk   r   r   r   _sleep_some+  s    z_TestProcess._sleep_somec             C   s   t | d S )N)r1   r}   )rk   delayr   r   r   _test_sleep/  s    z_TestProcess._test_sleepc          
      s  | j dkr| d| j  | j| jd d _   |   d | 	 | 
  |  jd  t j}| |dd  | |jd |   d | |dd  | |jd |   d td |  ttd	r> fd
d}ttj|}ztd | | d  W d td ttj| X n| | d  | |jd |   d |  | 
      jS )Nr=   ztest not appropriate for {})rc   Tr   g        r$   r>   alarmc                 s   t d  d S )Nzjoin took too long: %s)RuntimeError)r3   )rz   r   r   handlerQ  s    z+_TestProcess._kill_process.<locals>.handler
   F)rT   rU   rV   rd   r~   r[   ru   rE   rZ   rv   rr   r`   r,   rx   rC   r.   r1   r}   hasattrsignalSIGALRMr   rq   )r/   Zmethrx   r   Zold_handlerr   )rz   r   _kill_process3  s>    




z_TestProcess._kill_processc             C   s,   |  tjj}tjdkr(| |tj  d S )Nnt)	r   r   rd   	terminater%   ri   rE   r   SIGTERM)r/   r`   r   r   r   test_terminatef  s    
z_TestProcess.test_terminatec             C   s,   |  tjj}tjdkr(| |tj  d S )Nr   )	r   r   rd   killr%   ri   rE   r   SIGKILL)r/   r`   r   r   r   	test_killk  s    
z_TestProcess.test_killc             C   sJ   yt  }W n tk
r$   d}Y nX | t|tk | |dk d S )Nr>   )r   	cpu_countrD   rY   rs   int)r/   Zcpusr   r   r   test_cpu_countp  s    
z_TestProcess.test_cpu_countc             C   sp   |  t|  t | jtjtfd}| ||   d|_	|
  | ||   |  | ||   d S )N)rc   r3   T)rE   rs   rr   rt   rd   r1   r}   DELTArq   r[   ru   rv   rx   )r/   rz   r   r   r   test_active_childrenx  s    z!_TestProcess.test_active_childrenc             C   sV   | | t|dk rRx:tdD ].}| j| j|||g fd}|  |  q W d S )Nrl   )rc   r3   )sendr^   rangerd   _test_recursionru   rx   )rk   wconnidirz   r   r   r   r     s    
z_TestProcess._test_recursionc             C   s|   | j dd\}}| |g  tt g }x| rD||  q,W g dgddgddgdgddgddgg}| || d S )NF)duplexr   r>   )	Piper   r1   r}   r   pollappendrecvrE   )r/   rconnr   resultexpectedr   r   r   test_recursion  s    


z_TestProcess.test_recursionc             C   s   | d d S )Ng      $@)r   )rk   eventr   r   r   _test_sentinel  s    z_TestProcess._test_sentinelc          	   C   s   | j dkr| d| j  |  }| j| j|fd}| t |j W d Q R X |	  | 
|j |j}| |t | t|dd |  |  | t|dd d S )Nr=   ztest not appropriate for {})rc   r3   g        )r   r>   )rT   rU   rV   rp   rd   r   assertRaisesr(   sentinelru   
addCleanuprx   r\   r   rf   r   setrY   )r/   r   rz   r   r   r   r   test_sentinel  s    
z_TestProcess.test_sentinelr   Nc             C   s   |d k	r|   t| d S )N)rw   sysexit)rk   rcrQ   r   r   r   _test_close  s    z_TestProcess._test_closec          	   C   s<  | j dkr| d| j  |  }| j| jd|id}d|_|  | |	 d | 
t |  W d Q R X |d  |  | |	 d | |jd |  | 
t |	  W d Q R X | 
t |  W d Q R X | 
t |  W d Q R X |  t|}~t  | | d  t| d S )Nr=   ztest not appropriate for {}rQ   )rc   ro   TFr   )rT   rU   rV   r   rd   r   r[   ru   rE   rZ   r   r(   r   rP   rx   r`   r   weakrefrefgccollectassertIsr   )r/   rQ   rz   wrr   r   r   
test_close  s4    


z_TestProcess.test_closec                sF   j dkr d j  t }|dkr0dnd} fddt|D }x|D ]}|  qPW x|D ]}t| qfW x|D ]} |j	d q|W  fd	dt|D }x|D ]}|  qW t
d
 x|D ]}|  qW x|D ]}t| qW tjdkrBtj g}tjdkr$|tj  x|D ]} |j	| q*W d S )Nr=   ztest not appropriate for {}spawnrN   r|   c                s   g | ]} j  jd dqS ))g{Gz?)rc   r3   )rd   r   ).0r   )r/   r   r   
<listcomp>  s   z4_TestProcess.test_many_processes.<locals>.<listcomp>r   c                s   g | ]} j  jd qS ))rc   )rd   r~   )r   r   )r/   r   r   r     s   gMbP?r   darwin)rT   rU   rV   r   get_start_methodr   ru   r   rE   r`   r1   r}   r   r%   ri   r   r   r   platformr   r   rv   )r/   smNprocsrz   Z	exitcodesr   )r/   r   test_many_processes  s6    











z _TestProcess.test_many_processesc             C   sf   t  }t|}|  }| j|||fd}~|  |  | | d  | |	 d t
| d S )N)rc   r3   rN   )rM   r   r   r   rd   ru   rx   r   rE   rw   r   )r/   rR   r   rQ   rz   r   r   r   test_lose_target_ref  s    
z!_TestProcess.test_lose_target_refc             C   s   | tj  |  d S )N)rP   testr   Zfd_countr   )r/   evtrQ   r   r   r   _test_child_fd_inflation  s    z%_TestProcess._test_child_fd_inflationc          	      s   j dkrdj  t }|dkr<d| d}    fddt|D }x|D ]}|  qpW z0fddt|D }	t
t|d| W d    x|D ]}|  qW t X d S )	Nr=   ztest not appropriate for {}forkrN   c                s    g | ]}j j fd qS ))rc   r3   )rd   r   )r   r   )r   rQ   r/   r   r   r   "  s   z8_TestProcess.test_child_fd_inflation.<locals>.<listcomp>c                s   g | ]}   qS r   )rw   )r   r   )rQ   r   r   r   (  s    r>   )rT   rU   rV   r   r   rp   r   r   ru   rE   r^   r   rx   r   )r/   r   r   r   rz   Z	fd_countsr   )r   rQ   r/   r   test_child_fd_inflation  s&    


z$_TestProcess.test_child_fd_inflationc                s>    fdd} fdd}t j|d  t j|dd  d S )Nc                  s   t d    d S )Ng      ?)r1   r}   r   r   )r   r   r   func13  s    
z2_TestProcess._test_wait_for_threads.<locals>.func1c                  s   t d    d S )N   )r1   r}   clearr   )r   r   r   func27  s    
z2_TestProcess._test_wait_for_threads.<locals>.func2)rc   T)rc   r[   )	threadingThreadru   )r/   r   r   r   r   )r   r   _test_wait_for_threads1  s    z#_TestProcess._test_wait_for_threadsc             C   sX   | j dkr| d| j  |  }| j| j|fd}|  |  | |	  d S )Nr=   ztest not appropriate for {})rc   r3   )
rT   rU   rV   rp   rd   r   ru   rx   rY   is_set)r/   r   procr   r   r   test_wait_for_threads>  s    
z"_TestProcess.test_wait_for_threadsc             C   sX   xJ|  D ]>\}}|dkr,t }|  n|dks8td }tt|d  q
W |  d S )Nr   remove)itemsioStringIOr   rO   setattrr   r   )r/   r   Zbreak_std_streamsstream_nameactionstreamr   r   r   _test_error_on_stdio_flushJ  s    
z'_TestProcess._test_error_on_stdio_flushc             C   s   t  d g}|d   xdD ]}x||D ]t}tt|}tt|| zJ|  }| j| j|fd}|	  |
  | |  | |jd W d tt|| X q(W qW d S )Nr   )stdoutstderr)rc   r3   )r   r   r   getattrr   r   rp   rd   r   ru   rx   rY   r   rE   r`   )r/   Zstreamsr   r   
old_streamr   r   r   r   r   test_error_on_stdio_flush_1V  s    



z(_TestProcess.test_error_on_stdio_flush_1c             C   s   xdD ]|}xvdD ]n}t t|}zP|  }| j| j|||ifd}|  |  | |  | 	|j
d W d tt|| X qW qW d S )N)r   r   )r   r   )rc   r3   r   )r   r   rp   rd   r   ru   rx   rY   r   rE   r`   r   )r/   r   r   r   r   r   r   r   r   test_error_on_stdio_flush_2i  s    


z(_TestProcess.test_error_on_stdio_flush_2        c             C   s   t | |  d S )N)r1   r}   r   )r/   r   r   r   r   r   _sleep_and_set_eventz  s    
z!_TestProcess._sleep_and_set_eventc       
      C   s  | j dkr| d| j  t }|dkr<| d| ddlm} |  d}|  }| j	| j
||fd}|  |j}t|| t|d  |  }| j	| j
|fd}	|	  |	  | |  | |	jd |  | |  | |jd	 d S )
Nr=   ztest not appropriate for {}
forkserverr   )_forkserverg      ?)rc   r3   g       @)r      )rT   rU   rV   r   r   Zmultiprocessing.forkserverr   ensure_runningrp   rd   r   ru   Z_forkserver_pidr%   r   r1   r}   rx   rY   r   rE   r`   rv   )
r/   signumr   r   r   r   r   rj   Zevt2rg   r   r   r   check_forkserver_death  s.    
z#_TestProcess.check_forkserver_deathc             C   s   |  tj d S )N)r   r   SIGINT)r/   r   r   r   test_forkserver_sigint  s    z#_TestProcess.test_forkserver_sigintc             C   s   t jdkr| tj d S )Nr   )r%   ri   r   r   r   )r/   r   r   r   test_forkserver_sigkill  s    
z$_TestProcess.test_forkserver_sigkill)r   N)r   )#r7   r8   r9   rJ   rb   rh   classmethodre   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rS      sD   
(3 #&rS   c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_UpperCaserc             C   s    t j|  t  \| _| _d S )N)r   rd   r0   r   
child_connparent_conn)r/   r   r   r   r0     s    z_UpperCaser.__init__c             C   s@   | j   x&t| jjd D ]}| j|  qW | j  d S )N)r   r   iterr   r   r   upper)r/   r
   r   r   r   run  s    
z_UpperCaser.runc             C   s&   t |tkst| j| | j S )N)rs   strrO   r   r   r   )r/   r
   r   r   r   submit  s    z_UpperCaser.submitc             C   s$   | j d  | j   | j  d S )N)r   r   r   r   )r/   r   r   r   stop  s    
z_UpperCaser.stopN)r7   r8   r9   r0   r   r   r   r   r   r   r   r     s   r   c               @   s@   e Zd ZdZdd Zdd Zedd Zedd	 Zd
d Z	dS )_TestSubclassingProcess)r;   c             C   sL   t  }d|_|  | |dd | |dd |  |  d S )NTrn   ZHELLOZworldZWORLD)r   r[   ru   rE   r   r   rx   )r/   Z
uppercaserr   r   r   test_subclassing  s    z(_TestSubclassingProcess.test_subclassingc          	   C   s   | j dkr| d| j  tjj}| tjj| | j| j	|fd}|
  |  t|d2}| }| d| | d| | d| W d Q R X d S )Nr=   ztest not appropriate for {})rc   r3   rZeroDivisionErrorztest_multiprocessing.pyz1/0 # MARKER)rT   rU   rV   r   r   TESTFNr   unlinkrd   _test_stderr_flushru   rx   openreadrv   )r/   testfnr   ferrr   r   r   test_stderr_flush  s    
z)_TestSubclassingProcess.test_stderr_flushc             C   s6   t |t jt jB t jB }t|dddt_dd  d S )NwF)closefdr>   r   )r%   r   O_WRONLYO_CREATO_EXCLr   r   )rk   r   fdr   r   r   r     s    z*_TestSubclassingProcess._test_stderr_flushc             C   s8   t |t jt jB t jB }t|dddt_t| d S )Nr  F)r  )r%   r   r  r  r  r   r   r   )rk   reasonr   r  r   r   r   _test_sys_exit  s    z&_TestSubclassingProcess._test_sys_exitc          
   C   s  | j dkr| d| j  tjj}| tjj| xdddgdfD ]x}| j| j	||fd}d|_
|  t| | |jd t|d	}| }W d Q R X | | t| t| qDW xBd
D ]:}| jtj|fd}d|_
|  t| | |j| qW d S )Nr=   ztest not appropriate for {}r>   rl      zignore this)rc   r3   Tr   )TF   )rT   rU   rV   r   r   r   r   r   rd   r  r[   ru   r   rE   r`   r   r   rstripr   r%   r   r   )r/   r   r  rz   r   Zcontentr   r   r   test_sys_exit  s,    


z%_TestSubclassingProcess.test_sys_exitN)
r7   r8   r9   rJ   r   r   r   r   r  r  r   r   r   r   r     s   	r   c             C   s"   t | dr|  S |  dkS d S )Nemptyr   )r   r  qsize)rQ   r   r   r   queue_empty  s    
r  c             C   s"   t | dr|  S |  |kS d S )Nfull)r   r  r  )rQ   maxsizer   r   r   
queue_full  s    
r  c               @   s   e Zd Zedd Zdd Zedd Zdd Zed	d
 Zdd Z	dd Z
edd Zdd Zdd Zdd Zdd Zdd ZdS )
_TestQueuec             C   s.   |   xtdD ]}|  qW |  d S )N   )r   r   rw   r   )rk   r   child_can_startparent_can_continuer   r   r   r   	_test_put'  s    z_TestQueue._test_putc             C   s  d}| j |d}|  }|  }| j| j|||fd}d|_|  | t|d | t||d |	d |	dd |	ddd  |	d	d |	d
dd  |
d tt | t|d | t||d t|j	}t|j
}| tj|dd | |jd | tj|ddd  | |jd | tj|d | |jd | tj|ddt | |jt | tj|ddt | |jd | jtj|ddtd | |jt |  |  | t|d | t||d |  t| d S )Nr  )r  )rc   r3   TFr>   rl   r	     rN      r   )r   )r   rp   rd   r  r[   ru   rE   r  r  rP   
put_nowaitr1   r}   r   r,   r   pyqueueZFullrC   r.   TIMEOUT1TIMEOUT2TIMEOUT3r   r   rx   r   )r/   ZMAXSIZEr   r  r  r   rP   r  r   r   r   test_put.  sP    




z_TestQueue.test_putc             C   s<   |   |d |d |d |d |  d S )Nrl   r	  r  rN   )r   rP   r   )rk   r   r  r  r   r   r   	_test_geti  s    



z_TestQueue._test_getc             C   s  |   }|  }|  }| j| j|||fd}d|_|  | t|d |  |	  t
t | t|d | |dd d | |dd | |jddd | | d	 | t|d t|j}t|j}| tj|d | |jd
 | tj|dd  | |jd
 | tj| | |jd
 | tj|dt | |jt | tj|dt | |jd
 | jtj|td | |jt |  t| d S )N)rc   r3   TFrl   r	  r>   )r   r  rN   r   )r   rp   rd   r   r[   ru   rE   r  r   r   r1   r}   r   rw   
get_nowaitr,   r   r  EmptyrC   r.   r  r  r  rx   r   )r/   r   r  r  r   rw   r!  r   r   r   test_gets  sD    


z_TestQueue.test_getc             C   s"   xt ddD ]}|| qW d S )Nr   r   )r   rP   )rk   r   r   r   r   r   
_test_fork  s    z_TestQueue._test_forkc             C   s   |   }xtdD ]}|| qW tt | j| j|fd}d|_|	  x tdD ]}| 
| | qXW | tj|jd |  t| d S )Nr   )rc   r3   Tr   F)r   r   rP   r1   r}   r   rd   r$  r[   ru   rE   rw   r   r  r"  rx   r   )r/   r   r   rz   r   r   r   	test_fork  s    
z_TestQueue.test_forkc             C   s   |   }y| | d W n tk
r:   | d Y nX |d | | d |d | | d |  | | d |  | | d t| d S )Nr   zqsize method not implementedr>   rN   rl   )r   rE   r  rD   rU   rP   rw   r   )r/   rQ   r   r   r   
test_qsize  s    

z_TestQueue.test_qsizec             C   s,   x&t |jd D ]}tt |  qW d S )N)r   rw   r1   r}   r   Z	task_done)rk   rQ   objr   r   r   _test_task_done  s    
z_TestQueue._test_task_donec                s       fddtdD }x|D ]}d|_|  q&W xtdD ]} | qFW    x|D ]} d  qfW x|D ]}|  q~W t  d S )Nc                s   g | ]}j j fd qS ))rc   r3   )rd   r(  )r   r   )r   r/   r   r   r     s   z-_TestQueue.test_task_done.<locals>.<listcomp>r  Tr   )JoinableQueuer   r[   ru   rP   rx   r   )r/   workersrz   r   r   )r   r/   r   test_task_done  s    


z_TestQueue.test_task_donec          
   C   s   t j x d}t|d d}|d W d Q R X t jt 4 yt| W n  t	j
k
rr   | d Y nX W d Q R X W d Q R X d S )NZimported_by_an_imported_modulez.pyr  zif 1:
                    import multiprocessing

                    q = multiprocessing.Queue()
                    q.put('knock knock')
                    q.get(timeout=3)
                    q.close()
                    del q
                z?Probable regression on import lock contention; see Issue #22853)r   r   Ztemp_cwdr   writeZDirsOnSysPathr%   getcwd
__import__r  r"  fail)r/   Zmodule_namer   r   r   r   test_no_import_lock_contention  s    z)_TestQueue.test_no_import_lock_contentionc             C   sH   t  }t }| tj|jdd t | }| |d t	| d S )NTg?g?)
r   r   r1   r2   r   r  r"  rw   assertGreaterEqualr   )r/   rQ   ru   deltar   r   r   test_timeout  s    z_TestQueue.test_timeoutc          	   C   s  | j dkr| d| j  G dd dt}tj > |  }||  |d | 	|j
dd t| W d Q R X tj z | jdd	}||  |d y| | d W n tk
r   Y nX | 	|j
dd | 	|  t| W d Q R X d S )
Nr;   ztest not appropriate for {}c               @   s   e Zd Zdd ZdS )zF_TestQueue.test_queue_feeder_donot_stop_onexc.<locals>.NotSerializablec             S   s   t d S )N)r'   )r/   r   r   r   rI     s    zQ_TestQueue.test_queue_feeder_donot_stop_onexc.<locals>.NotSerializable.__reduce__N)r7   r8   r9   rI   r   r   r   r   NotSerializable  s   r4  Tg      ?)r   r>   )r  )rT   rU   rV   objectr   r   captured_stderrr   rP   rY   rw   r   rE   r  rD   r  )r/   r4  rQ   r   r   r   "test_queue_feeder_donot_stop_onexc  s(    


z-_TestQueue.test_queue_feeder_donot_stop_onexcc          	      s   | j dkr| d| j  G dd dt G  fdddtjj}  }tj	 : |t
 d}|| |d | |jd	d
 W d Q R X | |j | |j d S )Nr;   ztest not appropriate for {}c               @   s    e Zd ZdZdd Zdd ZdS )zK_TestQueue.test_queue_feeder_on_queue_feeder_error.<locals>.NotSerializablezMock unserializable objectc             S   s   d| _ d| _d S )NF)reduce_was_called on_queue_feeder_error_was_called)r/   r   r   r   r0   B  s    zT_TestQueue.test_queue_feeder_on_queue_feeder_error.<locals>.NotSerializable.__init__c             S   s   d| _ td S )NT)r8  r'   )r/   r   r   r   rI   F  s    zV_TestQueue.test_queue_feeder_on_queue_feeder_error.<locals>.NotSerializable.__reduce__N)r7   r8   r9   __doc__r0   rI   r   r   r   r   r4  @  s   r4  c                   s    e Zd ZdZe fddZdS )zE_TestQueue.test_queue_feeder_on_queue_feeder_error.<locals>.SafeQueuez1Queue with overloaded _on_queue_feeder_error hookc                s   t | trt | rd|_d S )NT)r   r'   r9  )ry   r'  )r4  r   r   _on_queue_feeder_errorL  s    

z\_TestQueue.test_queue_feeder_on_queue_feeder_error.<locals>.SafeQueue._on_queue_feeder_errorN)r7   r8   r9   r:  staticmethodr;  r   )r4  r   r   	SafeQueueJ  s   r=  )ctxTg      ?)r   )rT   rU   rV   r5  r   r   r   r   r   r6  get_contextrP   rY   rw   r8  r9  )r/   r=  Znot_serializable_objrQ   r   )r4  r   'test_queue_feeder_on_queue_feeder_error:  s    



z2_TestQueue.test_queue_feeder_on_queue_feeder_errorN)r7   r8   r9   r   r  r  r   r#  r$  r%  r&  r(  r+  r0  r3  r7  r@  r   r   r   r   r  $  s   ;
5"r  c               @   s$   e Zd Zdd Zdd Zdd ZdS )	_TestLockc             C   sR   |   }| | d | |dd | | d  | ttjf|j d S )NTF)LockrE   acquirereleaser   r(   r   ZThreadError)r/   lockr   r   r   	test_locke  s
    z_TestLock.test_lockc             C   s~   |   }| | d | | d | | d | | d  | | d  | | d  | ttf|j d S )NT)RLockrE   rC  rD  r   rO   r   )r/   rE  r   r   r   
test_rlockl  s    z_TestLock.test_rlockc          	   C   s   |    W d Q R X d S )N)rB  )r/   r   r   r   test_lock_contextv  s    
z_TestLock.test_lock_contextN)r7   r8   r9   rF  rH  rI  r   r   r   r   rA  c  s   
rA  c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
_TestSemaphorec             C   s   |  dt| | | d |  dt| | | d |  dt| | |dd |  dt| | | d  |  dt| | | d  |  dt| d S )Nrl   Tr>   r   F)rH   rL   rE   rC  rD  )r/   semr   r   r   _test_semaphore}  s    z_TestSemaphore._test_semaphorec             C   sT   |  d}| | | | d  | dt| | | d  | dt| d S )Nrl   r	  r  )	SemaphorerL  rE   rD  rH   rL   )r/   rK  r   r   r   test_semaphore  s    

z_TestSemaphore.test_semaphorec             C   s   |  d}| | d S )Nrl   )BoundedSemaphorerL  )r/   rK  r   r   r   test_bounded_semaphore  s    
z%_TestSemaphore.test_bounded_semaphorec             C   s   | j dkr| d| j  | d}t|j}| |dd | |jd | |dd d | |jd | |dt	d | |jd | |dt
d | |jt
 | |tdd | |jt d S )Nr;   ztest not appropriate for {}r   Fg        T)r   )rT   rU   rV   rM  r,   rC  rE   rC   r.   r  r  r  )r/   rK  rC  r   r   r   r3    s    


z_TestSemaphore.test_timeoutN)r7   r8   r9   rL  rN  rP  r3  r   r   r   r   rJ  {  s   rJ  c               @   s   e Zd ZedddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
edd Zeeddd Zedd Zeeddd Zedd Zdd ZdS )_TestConditionNc             C   s.   |   |  || |  |  d S )N)rC  rD  r   )rk   condsleepingwokenr   r   r   r   r     s
    
z_TestCondition.fc          	   C   s^   xBt dD ]6}y| |krP W n tk
r4   P Y nX tt q
W tt | || d S )Nr   )r   rD   r1   r}   r   rH   )r/   r-   rF   r   r   r   r   assertReachesEventually  s    

z&_TestCondition.assertReachesEventuallyc             C   sZ   | j dkrVy6|j |j  }| |d | |j d W n tk
rT   Y nX d S )Nr;   r   )rT   Z_sleeping_countrL   Z_woken_countrE   Z_wait_semaphorerD   )r/   rR  Zsleepersr   r   r   check_invariant  s    
z_TestCondition.check_invariantc             C   s  |   }| d}| d}| j| j|||fd}d|_|  | |j tj	| j|||fd}d|_|  | |j |
  |
  tt | dt| |
  |  |  tt | dt| |
  |  |  tt | dt| | | |  d S )Nr   )rc   r3   Tr>   rl   )	ConditionrM  rd   r   r[   ru   r   rx   r   r   rC  r1   r}   r   rH   rL   notifyrD  rV  )r/   rR  rS  rT  rz   r   r   r   test_notify  s6    





z_TestCondition.test_notifyc                s  |   }| d}| d xttdD ]h}| j| j|| tfd}d|_|  | |j	 t
j| j|| tfd}d|_|  | |j	 q&W xtdD ]}|  qW xtdD ]}   qW | dt  | | xptdD ]d}| j| j|| fd}d|_|  | |j	 t
j| j|| fd}d|_|  | |j	 qW xtdD ]}|  qZW tt | dt  |  |  |  |  fddd | | d S )Nr   r	  )rc   r3   Tr  c                  s   t  S )N)rL   r   )rT  r   r   <lambda>7      z0_TestCondition.test_notify_all.<locals>.<lambda>)rW  rM  r   rd   r   r  r[   ru   r   rx   r   r   rC  rH   rL   rV  r1   r}   r   Z
notify_allrD  rU  )r/   rR  rS  r   rz   r5   r   )rT  r   test_notify_all  sL    



z_TestCondition.test_notify_allc                sX  |   }| d}| d xptdD ]d}| j| j|| fd}d|_|  | |j t	j
| j|| fd}d|_|  | |j q&W xtdD ]}|  qW tt | dt  |  |jdd |  |  fdd	d |  |jd
d |  |  fdd	d |  |jdd |  | dt  | | d S )Nr   r	  )rc   r3   Tr  rl   )nc                  s   t  S )N)rL   r   )rT  r   r   rZ  [  r[  z._TestCondition.test_notify_n.<locals>.<lambda>r  c                  s   t  S )N)rL   r   )rT  r   r   rZ  b  r[  )rW  rM  r   rd   r   r[   ru   r   rx   r   r   rC  r1   r}   r   rH   rL   rX  rD  rU  rV  )r/   rR  rS  r   rz   r5   r   )rT  r   test_notify_n<  s:    


z_TestCondition.test_notify_nc             C   sH   |   }t|j}|  |t}|  | |d | |jt d S )NF)	rW  r,   r   rC  r  rD  rE   rC   r.   )r/   rR  r   rG   r   r   r   r3  n  s    
z_TestCondition.test_timeoutc          	      sL   |> d _ |  | fdd}|r4 j dkr>td W d Q R X d S )Nr   c                  s
    j dkS )Nr  )rF   r   )stater   r   rZ  |  r[  z0_TestCondition._test_waitfor_f.<locals>.<lambda>r  r>   )rF   rX  wait_forr   r   )rk   rR  r_  r   r   )r_  r   _test_waitfor_fw  s    z_TestCondition._test_waitfor_fzneeds sharedctypesc          
      s   |   }| dd | j| j| fd}d|_|  |0 | fdd}| | |  j	d W d Q R X x@t
dD ]4}td	 |   j	d
7  _	|  W d Q R X qzW t| | |jd d S )Nr   r$   )rc   r3   Tc                  s
    j dkS )Nr   )rF   r   )r_  r   r   rZ    r[  z-_TestCondition.test_waitfor.<locals>.<lambda>r   r  g{Gz?r>   )rW  r   rd   ra  r[   ru   r`  rY   rE   rF   r   r1   r}   rX  r   r`   )r/   rR  rz   r   r   r   )r_  r   test_waitfor  s     

z_TestCondition.test_waitforc          	      st   |   |^ d}t }|j fdd|d}t | }|sf|d |  k r\|d k rfn nd|_W d Q R X d S )Ng?c                  s
    j dkS )Nr  )rF   r   )r_  r   r   rZ    r[  z8_TestCondition._test_waitfor_timeout_f.<locals>.<lambda>)r   g333333?g      $@T)rD  r1   r2   r`  rF   )rk   rR  r_  successrK  r   dtr   r   )r_  r   _test_waitfor_timeout_f  s    $z&_TestCondition._test_waitfor_timeout_fc          
   C   s   |   }| dd}| dd}| d}| j| j||||fd}d|_|  | |jt	d x@t
dD ]4}td | | jd	7  _|  W d Q R X qlW t| | |j d S )
Nr   r   F)rc   r3   T)r   r	  g{Gz?r>   )rW  r   rM  rd   re  r[   ru   rY   rC  r   r   r1   r}   rF   rX  r   )r/   rR  r_  rc  rK  rz   r   r   r   r   test_waitfor_timeout  s     

z#_TestCondition.test_waitfor_timeoutc          	   C   s<   | |   W d Q R X td |d k	r8t|tj d S )Nr>   )rX  r1   r}   r%   r   r   r   )rk   rR   rj   r   r   r   _test_wait_result  s
    
z _TestCondition._test_wait_resultc          	   C   s   t | trtjdkrt }nd }|  }|r | |d | |d | j	| j
||fd}|  | |d |d k	r| t|jd |  W d Q R X d S )Nr   r   g?)rc   r3   <   )r   ProcessesMixinr   r   r%   r_   rW  rf   r   rd   rg  ru   rY   r   KeyboardInterruptrx   )r/   rj   rR   rz   r   r   r   test_wait_result  s    
z_TestCondition.test_wait_result)N)r7   r8   r9   r   r   rU  rV  rY  r\  r^  r3  ra  r)   
skipUnlessHAS_SHAREDCTYPESrb  re  rf  rg  rk  r   r   r   r   rQ    s   ->2		rQ  c               @   s    e Zd Zedd Zdd ZdS )
_TestEventc             C   s   t t |  d S )N)r1   r}   r  r   )rk   r   r   r   r   _test_event  s    
z_TestEvent._test_eventc             C   s   |   }t|j}| | d | |dd | |jd | |td | |jt |  | | d | | d | |jd | |td | |jd |	  | j
| j|fd}d|_|  | | d |  d S )NFg        T)rc   r3   )rp   r,   r   rE   r   rC   r.   r  r   r   rd   ro  r[   ru   rx   )r/   r   r   rz   r   r   r   
test_event  s&    
z_TestEvent.test_eventN)r7   r8   r9   r   ro  rp  r   r   r   r   rn    s   rn  c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )
_DummyListc             C   s6   t jtd}t  }| ||f d| jd< d S )Nr   r   )r   heapBufferWrapperstructcalcsizerB  __setstate__
_lengthbuf)r/   wrapperrE  r   r   r   r0     s    z_DummyList.__init__c             C   s"   |\| _ | _| j  d| _d S )Nr   )_wrapper_lockZcreate_memoryviewcastrw  )r/   r_  r   r   r   rv    s    z_DummyList.__setstate__c             C   s   | j | jfS )N)ry  rz  )r/   r   r   r   __getstate__  s    z_DummyList.__getstate__c          	   C   s(   | j  | jd  d7  < W d Q R X d S )Nr   r>   )rz  rw  )r/   _r   r   r   r     s    z_DummyList.appendc          	   C   s   | j  | jd S Q R X d S )Nr   )rz  rw  )r/   r   r   r   __len__  s    z_DummyList.__len__N)r7   r8   r9   r0   rv  r|  r   r~  r   r   r   r   rq    s
   rq  c               C   s   t d d S )Ng{Gz?)r1   r}   r   r   r   r   _wait#  s    r  c               @   sB   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dS )Bunchz
    A bunch of threads.
    Fc       
      C   s   || _ || _|| _| | _| | _| | _|s>| j  g }x6t	|D ]*}|j
| jd}d|_|  || qLW dd }	t| |	|| _dS )z
        Construct a bunch of `n` threads running the same function `f`.
        If `wait_before_exit` is True, the threads won't terminate until
        do_finish() is called.
        )rc   Tc             S   s   x| D ]}|   qW d S )N)rx   )r=   rz   r   r   r   finalizeB  s    
z Bunch.__init__.<locals>.finalizeN)r   r3   r]  	DummyListstartedfinishedrp   	_can_exitr   r   rd   taskr[   ru   r   r   r  
_finalizer)
r/   	namespacer   r3   r]  Zwait_before_exitr=   r   rz   r  r   r   r   r0   ,  s     



zBunch.__init__c          	   C   sR   t  }| j| z| j| j  W d | j| | jd | j	 sLt
X d S )N   )r%   r_   r  r   r   r3   r  r  r   r   rO   )r/   rj   r   r   r   r  H  s    z
Bunch.taskc             C   s    xt | j| jk rt  qW d S )N)r^   r  r]  r  )r/   r   r   r   wait_for_startedR  s    zBunch.wait_for_startedc             C   s    xt | j| jk rt  qW d S )N)r^   r  r]  r  )r/   r   r   r   wait_for_finishedV  s    zBunch.wait_for_finishedc             C   s   | j   d S )N)r  r   )r/   r   r   r   	do_finishZ  s    zBunch.do_finishc             C   s   |    d S )N)r  )r/   r   r   r   r   ]  s    zBunch.closeN)F)
r7   r8   r9   r:  r0   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S )
AppendTruec             C   s
   || _ d S )N)r'  )r/   r'  r   r   r   r0   b  s    zAppendTrue.__init__c             C   s   | j d d S )NT)r'  r   )r/   r   r   r   r6   d  s    zAppendTrue.__call__N)r7   r8   r9   r0   r6   r   r   r   r   r  a  s   r  c               @   s   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	e
dd Zd6ddZdd Ze
dd Zdd Ze
dd Zdd Ze
dd Zdd Ze
dd  Zd!d" Ze
d#d$ Zd%d& Ze
d'd( Zd)d* Ze
d+d, Zd-d. Zd/d0 Ze
d1d2 Zd3d4 Zd5S )7_TestBarrierz$
    Tests for Barrier objects.
    rN   g      >@c             C   s   | j | j| jd| _d S )N)r   )Barrierr   defaultTimeoutbarrier)r/   r   r   r   setUpo  s    z_TestBarrier.setUpc             C   s   | j   d | _ d S )N)r  abort)r/   r   r   r   tearDownr  s    
z_TestBarrier.tearDownc             C   s,   | j dkrg S | j dkr"| j S t S d S )Nr=   r<   )rT   r<   rt   rq  )r/   r   r   r   r  v  s
    


z_TestBarrier.DummyListc             C   s8   t | ||| jd }z||  |  W d |  X d S )Nr>   )r  r   r  r   )r/   r   r3   rB   r   r   r   run_threads~  s
    z_TestBarrier.run_threadsc             C   s   |j }|| jkstxpt|D ]d}|d d t|d || ksHt|  |d d t|d |d | kszt|  qW y|jdkstW n tk
r   Y nX |j	rtd S )Nr   Tr>   )
partiesr   rO   r   r   r^   r   	n_waitingrD   broken)rk   r  resultsr]  mr   r   r   r   	multipass  s    z_TestBarrier.multipassr>   c             C   s*   |   |   g}| | j| j||f dS )z;
        Test that a barrier is passed in lockstep
        N)r  r  r  r  )r/   passesr  r   r   r   test_barrier  s    z_TestBarrier.test_barrierc             C   s
   |  dS )zC
        Test that a barrier works for 10 consecutive runs
        r   )r  )r/   r   r   r   test_barrier_10  s    z_TestBarrier.test_barrier_10c             C   s   |  }|| d S )N)r   rP   )rk   r  r   rG   r   r   r   _test_wait_return_f  s    z _TestBarrier._test_wait_return_fc                sR   |    | | j| j f  fddt| jD }| |dd t  dS )z9
        test the return value from barrier.wait
        c                s   g | ]}   qS r   )rw   )r   r   )r   r   r   r     s    z1_TestBarrier.test_wait_return.<locals>.<listcomp>r   r>   N)	r   r  r  r  r   r   rE   countr   )r/   r  r   )r   r   test_wait_return  s
    z_TestBarrier.test_wait_returnc             C   s   |   t|dkrtd S )Nr>   )r   r^   r   )rk   r  r  r   r   r   _test_action_f  s    z_TestBarrier._test_action_fc             C   sB   |   }| j| jt|d}| | j||f | t|d dS )z,
        Test the 'action' callback
        )r   r>   N)r  r  r   r  r  r  rE   r^   )r/   r  r  r   r   r   test_action  s    z_TestBarrier.test_actionc             C   sp   y0|  }|| jd krt|   |d W n: tjk
rP   |d Y n tk
rj   |  Y nX d S )Nrl   T)r   r   r   r   r   BrokenBarrierErrorr  )rk   r  results1results2r   r   r   r   _test_abort_f  s    z_TestBarrier._test_abort_fc             C   s^   |   }|   }| | j| j||f | t|d | t|| jd  | | jj dS )zK
        Test that an abort will put the barrier in a broken state
        r   r>   N)	r  r  r  r  rE   r^   r   rY   r  )r/   r  r  r   r   r   
test_abort  s    z_TestBarrier.test_abortc             C   s   |  }|| jd kr@x|j| jd k r4td qW |  n8y|   |d W n  tjk
rv   |d Y nX |   |d d S )Nrl   r>   gMbP?T)	r   r   r  r1   r}   resetr   r   r  )rk   r  r  r  results3r   r   r   r   _test_reset_f  s    
z_TestBarrier._test_reset_fc             C   sl   |   }|   }|   }| | j| j|||f | t|d | t|| jd  | t|| j dS )zL
        Test that a 'reset' on a barrier frees the waiting threads
        r   r>   N)r  r  r  r  rE   r^   r   )r/   r  r  r  r   r   r   
test_reset  s    z_TestBarrier.test_resetc             C   s   y0|  }|| jd krt|   |d W n: tjk
rP   |d Y n tk
rj   |  Y nX |  | jd kr|  |   |   |d d S )Nrl   T)r   r   r   r   r   r  r  r  )rk   r  barrier2r  r  r  r   r   r   r   _test_abort_and_reset_f  s    z$_TestBarrier._test_abort_and_reset_fc             C   sz   |   }|   }|   }| | j}| | j| j||||f | t|d | t|| jd  | t|| j dS )zF
        Test that a barrier can be reset after being broken.
        r   r>   N)r  r  r   r  r  r  rE   r^   )r/   r  r  r  r  r   r   r   test_abort_and_reset  s    z!_TestBarrier.test_abort_and_resetc             C   sT   |  }|| jd kr td y| d W n  tjk
rN   |d Y nX d S )Nrl   g      ?g      ?T)r   r   r1   r}   r   r  r   )rk   r  r  r   r   r   r   _test_timeout_f  s    
z_TestBarrier._test_timeout_fc             C   s4   |   }| | j| j|f | t|| jj dS )z$
        Test wait(timeout)
        N)r  r  r  r  rE   r^   r  )r/   r  r   r   r   r3  *  s    z_TestBarrier.test_timeoutc             C   sV   | | j}|| jd kr$td y|   W n  tjk
rP   |d Y nX d S )Nrl   g      ?T)r   r  r   r1   r}   r   r  r   )rk   r  r  r   r   r   r   _test_default_timeout_f2  s    
z$_TestBarrier._test_default_timeout_fc             C   s@   | j | jdd}|  }| | j||f | t||j dS )z4
        Test the barrier's default timeout
        g      ?)r   N)r  r   r  r  r  rE   r^   r  )r/   r  r  r   r   r   test_default_timeout=  s    z!_TestBarrier.test_default_timeoutc             C   s   |  d}|  |  d S )Nr>   )r  r   )r/   rB   r   r   r   test_single_threadF  s    
z_TestBarrier.test_single_threadc          
   C   s8   x2t |D ]&}|  | || W d Q R X q
W d S )N)r   r   r   )rk   r  r  connrE  r   r   r   r   _test_thousand_fK  s    z_TestBarrier._test_thousand_fc             C   s   | j dkr| d| j  d}|  }| d\}}x@t| jD ]2}| j| j| j	|||fd}|
  | |j qBW x4t|D ](}x"t| jD ]}| | | qW qW d S )Nr<   ztest not appropriate for {}i  F)rc   r3   )rT   rU   rV   rB  r   r   r   rd   r  r  ru   r   rx   rE   r   )r/   r  rE  r  r   jrz   r   r   r   r   test_thousandR  s    
z_TestBarrier.test_thousandN)r>   )r7   r8   r9   r:  r   r  r  r  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r3  r  r  r  r  r  r   r   r   r   r  h  s6   

		r  c               @   sZ   e Zd ZdZdddddededfgZd	d
 Zedd ZdddZ	dd Z
dd ZdS )
_TestValue)r;   )r   i  i^  )dg      @g      )hi   )rQ   l        l        rR   xyc             C   s   t s| d d S )Nz%requires multiprocessing.sharedctypes)rm  rU   )r/   r   r   r   r  r  s    z_TestValue.setUpc             C   s(   x"t || jD ]\}}|d |_qW d S )Nrl   )zipcodes_valuesrF   )rk   valuessvcvr   r   r   re   v  s    z_TestValue._testFc                s   |r fdd j D }n fdd j D }x*t| j D ]\}} |j|d  q<W  j j|fd}d|_|  |  x*t| j D ]\}} |j|d  qW d S )Nc                s   g | ]\}}}  ||qS r   )RawValue)r   coderF   r}  )r/   r   r   r   ~  s   z)_TestValue.test_value.<locals>.<listcomp>c                s   g | ]\}}}  ||qS r   )r   )r   r  rF   r}  )r/   r   r   r     s   r>   )rc   r3   Trl   )	r  r  rE   rF   rd   re   r[   ru   rx   )r/   rawr  r  r  r   r   )r/   r   
test_value|  s    


z_TestValue.test_valuec             C   s   | j dd d S )NT)r  )r  )r/   r   r   r   test_rawvalue  s    z_TestValue.test_rawvaluec             C   s   |  dd}| }| }| j ddd d}| }| }|  }| j dd|d}| }	| }
| ||	 | j dddd}| t|d | t|d | jt| j dddd | 	dd}| t|d | t|d d S )Nr   rN   )rE  Fget_lockget_objZnavalue)
r   r  r  rB  rE   rf   r   r   r'   r  )r/   Zval1lock1obj1Zval2lock2obj2rE  Zval3lock3obj3arr4arr5r   r   r   test_getobj_getlock  s$    z_TestValue.test_getobj_getlockN)F)r7   r8   r9   rJ   r   r  r  r   re   r  r  r  r   r   r   r   r  f  s   
r  c               @   s~   e Zd ZdZedd ZeedkddddZ	eedkdd	d
 Z
eedkddd Zeedkddd ZdS )
_TestArray)r;   c             C   s4   x.t dt|D ]}||  ||d  7  < qW d S )Nr>   )r   r^   )rk   seqr   r   r   r   r     s    z_TestArray.fNzrequires _ctypesFc          
   C   s  ddddddddd	d
g
}|r*|  d|}n| d|}| t|t| | |d |d  | t|dd t|dd  tdddddg |dd< |dd< | t|d d  | | | | j| j|fd}d|_|	  |
  | t|d d  | d S )Ni  ir  i  i5        i$  i  i  i?  r   r	  rl   r  r>   r  r
  )rc   r3   T)RawArrayArrayrE   r^   rt   arrayr   rd   r[   ru   rx   )r/   r  r  arrrz   r   r   r   
test_array  s    $(
z_TestArray.test_arrayc             C   sv   d}xlt dD ]`}| d|}| t|| | t|dg|  t d|d d < | t|tt d ~qW d S )Nr   r	  r   r   )r   r  rE   r^   rt   )r/   sizer}  r  r   r   r   test_array_from_size  s    z_TestArray.test_array_from_sizec             C   s   | j dd d S )NT)r  )r  )r/   r   r   r   test_rawarray  s    z_TestArray.test_rawarrayc             C   s
  |  dttd}| }| }| j dttdd d}| }| }|  }| j dttd|d}| }	| }
| ||	 | j dtddd}| t|d | t|d | j	t
| j dtddd | dtd}| t|d | t|d d S )Nr   r   )rE  Fr  r  Znotalock)r  rt   r   r  r  rB  rE   rf   r   r   r'   r  )r/   Zarr1r  r  Zarr2r  r  rE  Zarr3r  r  r  r  r   r   r   test_getobj_getlock_obj  s&    z"_TestArray.test_getobj_getlock_obj)F)r7   r8   r9   rJ   r   r   r)   skipIfr    r  r  r  r  r   r   r   r   r    s   r  c               @   sH   e Zd 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 )_TestContainers)r<   c             C   s  |  t td}| |d d  t td |   }| |d d  g  |t td | |d d  t td | |d d | |dd dddg |d9 }| |d d  ddddddddddg
 | |ddg ddddddddddddg | |d d  t td ||g}|  |}| d	d
 |D ddddddddddg
ddddddddddg
g |  |g}|d | |d d d  dddddddddddg d S )Nr   rN   rl   r	  r  r   r>   r  c             S   s   g | ]}|d d  qS )Nr   )r   Zelementr   r   r   r     s    z-_TestContainers.test_list.<locals>.<listcomp>r  r
  	   rn   )rt   r   rE   extendr   )r/   rA   rB   r  ry   r   r   r   r   	test_list  s(    (,
2
z_TestContainers.test_listc             C   sf   |  t td}t|}| t |t td | t |g  t|}d|d< | t|d d S )Nr   r|   r   )rt   r   r   rE   next)r/   rA   itr   r   r   test_list_iter"  s    z_TestContainers.test_list_iterc                s     fddtdD } dd |D dddggd  d|d d	<  |d d d  dddg x0tddD ]"} || d d  dddg qtW  |d  d  t|d d x(tdddD ]} t|| d qW ~   }|| ~d S )
Nc                s   g | ]}  td qS )r	  )rt   r   )r   Z_i)r/   r   r   r   -  s    z;_TestContainers.test_list_proxy_in_list.<locals>.<listcomp>r	  c             S   s   g | ]}|d d  qS )Nr   )r   innerr   r   r   r   .  s    r   r>   rl   7   r$   )rt   r   rE   popr^   r   )r/   rA   r   rB   r   )r/   r   test_list_proxy_in_list,  s    ""
z'_TestContainers.test_list_proxy_in_listc             C   s   |   }ttdd}x|D ]}t|||< qW | | t dd |D  | t| | | t| dd |D  | t|	 dd |D  d S )NA   F   c             s   s   | ]}|t |fV  qd S )N)chr)r   r   r   r   r   	<genexpr>E  s    z,_TestContainers.test_dict.<locals>.<genexpr>c             S   s   g | ]}t |qS r   )r  )r   r   r   r   r   r   G  s    z-_TestContainers.test_dict.<locals>.<listcomp>c             S   s   g | ]}|t |fqS r   )r  )r   r   r   r   r   r   H  s    )
dictrt   r   r  rE   r   sortedkeysr  r   )r/   r  indicesr   r   r   r   	test_dict@  s    
z_TestContainers.test_dictc             C   sz   |   }ttdd}x|D ]}t|||< qW t|}| t|| | t|g  t|}|  | tt	| d S )Nr  r  )
r  rt   r   r  r   rE   r   r   r   r  )r/   r  r  r   r  r   r   r   test_dict_iterJ  s    
z_TestContainers.test_dict_iterc             C   s  | j ddd}| j ddd}| j ||d}| |d d | |d	 d d d
|d	 d< | |d d
 | |d	 d d
 d|d	 d< | |d d | |d	 d d ~~| |d d d d|d	 d< | |d	 d d |d }|d	 }d|d< | |d d | |d	 d d |  | t|d | |d d | |d d | ||g}d|d d< | |d d | |d d d ~~| |d d d | ddg|g}| |d t | |d d d d d S )Nrl   r  )ferretshamstersr   r	  )waterfeed)petssuppliesr  r  rN   Zblanketsr  r  r     r   r  r>   ZmarmotsX   c   r$   r  )r  rE   r   r^   rt   r\   )r/   r  r  r  lZouterr   r   r   test_dict_proxy_nestedW  sF    z&_TestContainers.test_dict_proxy_nestedc             C   sh   |   }d|_d|_d|_| |j|jfd |`| t|d | t|d | t|d  d S )NBobBuilderZhidden)r  r  zNamespace(name='Bob')ri   job)	Namespaceri   r  Z_hiddenrE   r   rY   r   )r/   r]  r   r   r   test_namespace  s    z_TestContainers.test_namespaceN)r7   r8   r9   rJ   r  r  r  r  r  r   r  r   r   r   r   r    s   

.r          c             C   s   t | | |  S )N)r1   r}   )r  r   r   r   r   sqr  s    
r  c             C   s   | | S )Nr   )r  r  r   r   r   mul  s    r  c             C   s   t |  tdd d S )Nr  i   )r1   r}   r(   )r   r   r   r   raise_large_valuerror  s    
r	  c             C   s   | S )Nr   )r  r   r   r   identity  s    r
  c               @   s    e Zd ZdZdd Zdd ZdS )CountedObjectr   c             C   s   |  j d7  _ t| S )Nr>   )n_instancesr5  __new__)rk   r   r   r   r    s    zCountedObject.__new__c             C   s   t |  jd8  _d S )Nr>   )rs   r  )r/   r   r   r   __del__  s    zCountedObject.__del__N)r7   r8   r9   r  r  r  r   r   r   r   r    s   r  c               @   s   e Zd ZdS )SayWhenErrorN)r7   r8   r9   r   r   r   r   r    s    r  c             c   s<   |dkrt dx&t| D ]}||kr.t d|V  qW d S )Nr$   zSomebody said when)r  r   )ZtotalZwhenr   r   r   r   exception_throwing_generator  s    r  c                   s   e Zd Ze fddZe f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%d& Zd'd( Zd)d* Zed+d, Zd-d. Zed/d0 Zd1d2 Zd3d4 Zd5d6 Z  ZS )7	_TestPoolc                s   t    | d| _d S )Nr  )super
setUpClassPoolpool)rk   )	__class__r   r   r    s    
z_TestPool.setUpClassc                s(   | j   | j   d | _ t   d S )N)r  r   rx   r  tearDownClass)rk   )r  r   r   r    s    

z_TestPool.tearDownClassc             C   s@   | j j}| |tdtd | |tddditdd d S )N)rN   rN   r   r  r	  )r  )r  applyrE   r  )r/   Zpapplyr   r   r   
test_apply  s    z_TestPool.test_applyc          	   C   sh   | j j}| |tttdtttttd | |tttdddtttttd d S )Nr   r|   r   )	chunksize)r  maprE   r  rt   r   )r/   Zpmapr   r   r   test_map  s    ,z_TestPool.test_mapc             C   s   | j j}tttdtddd}| |t|ttt| tttdtddd}| |t|ddttt| d S )Nr   r  r$   r|   r  r   )r  )r  starmaprt   r  r   rE   r  	itertools)r/   Zpsmaptuplesr   r   r   test_starmap  s    z_TestPool.test_starmapc             C   sD   t ttdtddd}| | jt| t t	t| d S )Nr|   r  r$   )
rt   r  r   rE   r  Zstarmap_asyncr  rw   r  r  )r/   r  r   r   r   test_starmap_async  s    z_TestPool.test_starmap_asyncc          	   C   s8   |  | jtttd tttttd d S )Nr   )rE   r  	map_asyncr  rt   r   rw   r  )r/   r   r   r   test_map_async  s    z_TestPool.test_map_asyncc             C   s   | j dkr| j ng }| jjtdg|j|jd  | dt	| | dg|d  | jjtdg|j|jd  | dt	| | 
|d t d S )Nr<   1)callbackerror_callbackr>   r   rA   rl   )rT   r<   rt   r  r"  r   r   r   rE   r^   r\   r(   )r/   	call_argsr   r   r   test_map_async_callbacks  s    z"_TestPool.test_map_async_callbacksc          	   C   s\   | j dkr| d| j  G dd dt}| t | jt| gd  W d Q R X d S )Nr=   ztest not appropriate for {}c               @   s   e Zd Zdd ZdS )z*_TestPool.test_map_unplicklable.<locals>.Ac             S   s   t dd S )Nzcannot pickle)r   )r/   r   r   r   rI     s    z5_TestPool.test_map_unplicklable.<locals>.A.__reduce__N)r7   r8   r9   rI   r   r   r   r   A  s   r)  r   )	rT   rU   rV   r5  r   r   r  r  r  )r/   r)  r   r   r   test_map_unplicklable  s
    
z_TestPool.test_map_unplicklablec             C   sD   y| j jtg ddjtd W n  tjk
r>   | d Y nX d S )Nr>   )r  )r   z2pool.map_async with chunksize stalled on null list)r  r"  r  rw   r  r   TimeoutErrorr/  )r/   r   r   r   test_map_chunksize  s    z_TestPool.test_map_chunksizec          	   C   s  | j dkr| d| j  | t | jttddd W d Q R X | t | jttddd W d Q R X | t | jttddd W d Q R X G dd d}| t | jt| d W d Q R X | t | jt| d W d Q R X d S )	Nr<   ztest not appropriate for {}r>   r$   r   r	  c               @   s$   e Zd Zdd Zdd Zdd ZdS )zE_TestPool.test_map_handle_iterable_exception.<locals>.SpecialIterablec             S   s   | S )Nr   )r/   r   r   r   __iter__	  s    zN_TestPool.test_map_handle_iterable_exception.<locals>.SpecialIterable.__iter__c             S   s   t d S )N)r  )r/   r   r   r   __next__	  s    zN_TestPool.test_map_handle_iterable_exception.<locals>.SpecialIterable.__next__c             S   s   dS )Nr>   r   )r/   r   r   r   r~  	  s    zM_TestPool.test_map_handle_iterable_exception.<locals>.SpecialIterable.__len__N)r7   r8   r9   r-  r.  r~  r   r   r   r   SpecialIterable	  s   r/  )	rT   rU   rV   r   r  r  r  r  r  )r/   r/  r   r   r   "test_map_handle_iterable_exception  s    
   z,_TestPool.test_map_handle_iterable_exceptionc             C   s<   | j tdtf}t|j}| | d | |jt d S )Nr  1   )	r  apply_asyncr  r  r,   rw   rE   rC   r.   )r/   rG   rw   r   r   r   
test_async	  s    
z_TestPool.test_asyncc             C   sD   | j tdtd f}t|j}| jtj|td | 	|j
t d S )Nr  g      ?)r   )r  r2  r  r  r,   rw   r   r   r+  rC   r.   )r/   rG   rw   r   r   r   test_async_timeout	  s    
z_TestPool.test_async_timeoutc          	   C   s   | j tttd}| t|tttttd | j tttd}x$tdD ]}| t|||  qXW | t	|j
 | j jtttddd}x$tdD ]}| t|||  qW | t	|j
 d S )Nr   i  r|   )r  )r  imapr  rt   r   rE   r  r  r   StopIterationr.  )r/   r  r   r   r   r   	test_imap%	  s    "z_TestPool.test_imapc             C   sH  | j dkr| d| j  | jttddd}| t|j	 | jttddd}| t|j	 | jttddd}x$t
dD ]}| t|||  qW | t|j	 | jttddd	}x$t
d
D ]}| t|||  qW | t|j	 | jttddd}x&t
dD ]}| t|||  qW | t|j	 d S )Nr<   ztest not appropriate for {}r>   r$   r   r	  r   r  rl   r  r  )rT   rU   rV   r  r5  r  r  r   r  r.  r   rE   r  )r/   r  r   r   r   r   #test_imap_handle_iterable_exception3	  s$    
z-_TestPool.test_imap_handle_iterable_exceptionc          	   C   sx   | j tttd}| t|tttttd | j jtttddd}| t|tttttd d S )Nr   i  r|   )r  )r  imap_unorderedr  rt   r   rE   r  r  )r/   r  r   r   r   test_imap_unorderedM	  s    "z_TestPool.test_imap_unorderedc          	   C   sN  | j dkr| d| j  | jttddd}| t|j	 | jttddd}| t|j	 | jttddd}t
ttt
td}| t6 x.tdD ]"}t|}| || || qW W d Q R X | jttddd	}t
ttt
td}| t8 x0tdD ]$}t|}| || || qW W d Q R X d S )
Nr<   ztest not appropriate for {}r>   r$   r   r	  r   r  rl   )rT   rU   rV   r  r9  r  r  r   r  r.  rt   r  r   r  rv   r   )r/   r  Zexpected_valuesr   rF   r   r   r   -test_imap_unordered_handle_iterable_exceptionT	  s8    
z7_TestPool.test_imap_unordered_handle_iterable_exceptionc             C   st   | j dkrtnt}| || jd | || jd | j dkrp| d}z| dt|j W d |  |	  X d S )Nr<   r$   r   r	  )
rT   RemoteErrorr(   r   r  rE   r^   _poolr   rx   )r/   Zexpected_errorrz   r   r   r   test_make_poolx	  s    

z_TestPool.test_make_poolc             C   sP   | j jtjdd tdD dd}| j   t| j j}|  | |j	d d S )Nc             S   s   g | ]}d qS )g?r   )r   r   r   r   r   r   	  s    z,_TestPool.test_terminate.<locals>.<listcomp>i'  r>   )r  g       @)
r  r"  r1   r}   r   r   r,   rx   
assertLessr.   )r/   r   rx   r   r   r   r   	  s    
z_TestPool.test_terminatec             C   sz   |  d}| |tg g  | t|tg g  | t|tg g  | |tg  g  |	  |
  d S )Nr>   )r  rE   r  r  rt   r5  r9  r"  rw   r   rx   )r/   rz   r   r   r   test_empty_iterable	  s    
z_TestPool.test_empty_iterablec          	   C   st   | j dkrpttd}dd |D }| d"}|t|}| | | W d Q R X |  | 	t
|jt| d S )Nr;   r   c             S   s   g | ]}t |qS r   )r  )r   r   r   r   r   r   	  s    z*_TestPool.test_context.<locals>.<listcomp>rl   )rT   rt   r   r  r"  r  rE   rw   rx   r   r(   )r/   Lr   rz   r   r   r   r   test_context	  s    
z_TestPool.test_contextc             C   s   t dd S )N{   )r   )rk   r   r   r   _test_traceback	  s    z_TestPool._test_tracebackc             C   s  | j dkr| dH}y|| j W n& tk
rN } z|}W d d }~X Y nX | d W d Q R X |  | 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p}y|ttddd W n( tk
rP } z|}W d d }~X Y nX | d | t|t | |jd  W d Q R X |  d S )Nr;   r>   zexpected RuntimeError)rC  z&raise RuntimeError(123) # some commentr$   zexpected SayWhenError)rT   r  r  rD  	Exceptionr/  rx   r   rs   r   rE   r3   	__cause__r   r  ZRemoteTracebackrv   tbr   r   r6  r   
excepthookexc_infogetvaluer  r  r  r  )r/   rz   ry   exccausef1r   r   r   test_traceback	  s:    

z_TestPool.test_tracebackc             C   s   t dd S )Nfoo)r   )rk   r   r   r   _test_wrapped_exception	  s    z!_TestPool._test_wrapped_exceptionc          
   C   sD   |  d(}| t || j W d Q R X W d Q R X |  d S )Nr>   )r  r   r   r  rP  rx   )r/   rz   r   r   r   test_wrapped_exception	  s     z _TestPool.test_wrapped_exceptionc             C   s~   t  }| tN | d8}z|tddg W d t d |  |	  X W d Q R X W d Q R X | 
t  | d d S )Nrl   r   r>   g      ?g?)r1   r2   r   r(   r  r  r	  r}   r   rx   assertGreater)r/   Zt_startrz   r   r   r   test_map_no_failfast	  s    
z_TestPool.test_map_no_failfastc             C   sh   dd t dD }dd |D }| jt| ~tt | tdd |D d h | t	j
d d S )Nc             S   s   g | ]
}t  qS r   )r  )r   r   r   r   r   r   	  s    z4_TestPool.test_release_task_refs.<locals>.<listcomp>r   c             S   s   g | ]}t |qS r   )r   r   )r   or   r   r   r   	  s    c             s   s   | ]}| V  qd S )Nr   )r   r   r   r   r   r  	  s    z3_TestPool.test_release_task_refs.<locals>.<genexpr>r   )r   r  r  r
  r1   r}   r   rE   r   r  r  )r/   ZobjsZrefsr   r   r   test_release_task_refs	  s    
z _TestPool.test_release_task_refs) r7   r8   r9   r   r  r  r  r  r   r!  r#  r(  r*  r,  r0  r3  r4  r7  r8  r:  r;  r>  r   r@  rB  rD  rN  rP  rQ  rS  rU  __classcell__r   r   )r  r   r    s6   	
$

&r  c               C   s   t dd S )Nkey)KeyErrorr   r   r   r   raising	  s    rY  c               C   s   dd S )Nc               S   s   dS )N*   r   r   r   r   r   rZ  
  r[  z%unpickleable_result.<locals>.<lambda>r   r   r   r   r   unpickleable_result
  s    r[  c               @   s    e Zd ZdZdd Zdd ZdS )_TestPoolWorkerErrors)r;   c                sj   t d}d g  fdd}|jt|d}| t|j |  d  |  d t |	  |
  d S )Nrl   c                s   |  d< d S )Nr   r   )rK  )
scratchpadr   r   errback
  s    z@_TestPoolWorkerErrors.test_async_error_callback.<locals>.errback)r&  r   )r   r  r2  rY  r   rX  rw   rY   r\   r   rx   )r/   rz   r^  rG   r   )r]  r   test_async_error_callback
  s    
z/_TestPoolWorkerErrors.test_async_error_callbackc                s   ddl m} td}xxtdD ]l}d g  fdd}|jt|d}| ||j  d }| 	| | 
 d | | |j | |j q W |  |  d S )Nr   )MaybeEncodingErrorrl   r   c                s   |  d< d S )Nr   r   )rK  )r]  r   r   r^  
  s    z?_TestPoolWorkerErrors.test_unpickleable_result.<locals>.errback)r&  )multiprocessing.poolr`  r   r  r   r2  r[  r   rw   rY   r\   ZassertIsNotNonerK  rF   r   rx   )r/   r`  rz   Z	iterationr^  rG   wrappedr   )r]  r   test_unpickleable_result
  s    

z._TestPoolWorkerErrors.test_unpickleable_resultN)r7   r8   r9   rJ   r_  rc  r   r   r   r   r\  
  s   r\  c               @   s    e Zd ZdZdd Zdd ZdS )_TestPoolWorkerLifetime)r;   c       	      C   s  t jddd}| dt|j dd |jD }g }x$tdD ]}||t|f q>W x(t	|D ]\}}| |
 t| qdW |  d}x.|rtdd	 |jD s|d
8 }tt qW dd |jD }| d | | d | | t|t| |  |  d S )Nr	  r   )maxtasksperchildc             S   s   g | ]
}|j qS r   )rj   )r   r  r   r   r   r   3
  s    zE_TestPoolWorkerLifetime.test_pool_worker_lifetime.<locals>.<listcomp>r|   2   c             s   s   | ]}|  V  qd S )N)rZ   )r   r  r   r   r   r  A
  s    zD_TestPoolWorkerLifetime.test_pool_worker_lifetime.<locals>.<genexpr>r>   c             S   s   g | ]
}|j qS r   )rj   )r   r  r   r   r   r   D
  s    )r   r  rE   r^   r=  r   r   r2  r  	enumeraterw   Z_repopulate_poolallr1   r}   r   rq   ZassertNotEqualr  r   rx   )	r/   rz   Zorigworkerpidsr  r   r  rG   Z	countdownZfinalworkerpidsr   r   r   test_pool_worker_lifetime0
  s&    z1_TestPoolWorkerLifetime.test_pool_worker_lifetimec             C   sx   t jddd}g }x&tdD ]}||t|df qW |  |  x(t|D ]\}}| 	|
 t| qTW d S )Nr	  r>   )re  r  g333333?)r   r  r   r   r2  r  r   rx   rg  rE   rw   )r/   rz   r  r   r  rG   r   r   r   %test_pool_worker_lifetime_early_closeM
  s    z=_TestPoolWorkerLifetime.test_pool_worker_lifetime_early_closeN)r7   r8   r9   rJ   ri  rj  r   r   r   r   rd  -
  s   rd  )BaseManager	BaseProxyr<  c               @   s$   e Zd Zdd Zdd Zdd ZdS )FooBarc             C   s   dS )Nzf()r   )r/   r   r   r   r   a
  s    zFooBar.fc             C   s   t d S )N)r(   )r/   r   r   r   gc
  s    zFooBar.gc             C   s   dS )Nz_h()r   )r/   r   r   r   _he
  s    z	FooBar._hN)r7   r8   r9   r   rn  ro  r   r   r   r   rm  `
  s   rm  c              c   s    xt dD ]} | |  V  q
W d S )Nr   )r   )r   r   r   r   bazh
  s    rp  c               @   s    e Zd ZdZdd Zdd ZdS )IteratorProxy)r.  c             C   s   | S )Nr   )r/   r   r   r   r-  n
  s    zIteratorProxy.__iter__c             C   s
   |  dS )Nr.  )_callmethod)r/   r   r   r   r.  p
  s    zIteratorProxy.__next__N)r7   r8   r9   Z	_exposed_r-  r.  r   r   r   r   rq  l
  s   rq  c               @   s   e Zd ZdS )	MyManagerN)r7   r8   r9   r   r   r   r   rs  s
  s   rs  Foo)callableBar)r   ro  )ru  Zexposed)ru  Z	proxytypec               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )_TestMyManager)r<   c             C   s4   t  }|  | | |  | |jjd d S )Nr   )rs  ru   commonshutdownrE   _processr`   )r/   r<   r   r   r   test_mymanager
  s
    
z_TestMyManager.test_mymanagerc          	   C   s8   t  }| | W d Q R X | |jjdtj f d S )Nr   )rs  rx  rv   rz  r`   r   r   )r/   r<   r   r   r   test_mymanager_context
  s    z%_TestMyManager.test_mymanager_contextc          	   C   s<   t  }|  | | | W d Q R X | |jjd d S )Nr   )rs  ru   rx  rE   rz  r`   )r/   r<   r   r   r   !test_mymanager_context_prestarted
  s
    z0_TestMyManager.test_mymanager_context_prestartedc                s  |  |  | }fdddD } fdddD }| |ddg | |ddg |  d | tj | dd | t	jd |   d |  
 d	 |  dd |  dd	 | t|d
d tdD  d S )Nc                s   g | ]}t  |r|qS r   )r   )r   ri   )rO  r   r   r   
  s    z)_TestMyManager.common.<locals>.<listcomp>)r   rn  ro  c                s   g | ]}t  |r|qS r   )r   )r   ri   )barr   r   r   
  s    r   rn  ro  zf()z_h()c             S   s   g | ]}|| qS r   r   )r   r   r   r   r   r   
  s    r   )rt  rv  rp  rE   r   r   r(   rn  rr  r<  ro  rt   r   )r/   r<   rp  Zfoo_methodsZbar_methodsr   )r~  rO  r   rx  
  s     z_TestMyManager.commonN)r7   r8   r9   rJ   r{  r|  r}  rx  r   r   r   r   rw  {
  s
   rw  c               C   s   t S )N)_queuer   r   r   r   	get_queue
  s    r  c               @   s   e Zd ZdZdS )QueueManagerz$manager class used by server processN)r7   r8   r9   r:  r   r   r   r   r  
  s   r  c               @   s   e Zd ZdZdS )QueueManager2z@manager class which specifies the same interface as QueueManagerN)r7   r8   r9   r:  r   r   r   r   r  
  s   r  Z	xmlrpclibc               @   sB   e Zd ZdZdddddddgZedd Zed	d
 Zdd ZdS )_TestRemoteManager)r<   zhello worldNTg      @u   hallå världenu   привіт світs   hall vrldenc             C   s2   t ||td}|  | }|t| j d S )N)addressrX   
serializer)r  
SERIALIZERconnectr  rP   tupler  )rk   r  rX   r<   r   r   r   r   _putter
  s
    z_TestRemoteManager._putterc             C   s   t d}ttjjdf|td}|  | |j	 | j
| j|j|fd}d|_|  t|j|td}|  | }| | | j | t|jtj ~d S )N    r   )r  rX   r  )rc   r3   T)r%   urandomr  r   r   HOSTr  ru   r   ry  rd   r  r  r[   r  r  r  rE   rw   r   r   rE  rP   r1   r}   )r/   rX   r<   rz   Zmanager2r   r   r   r   test_remote
  s    
z_TestRemoteManager.test_remote)	r7   r8   r9   rJ   r  r   r   r  r  r   r   r   r   r  
  s   
r  c               @   s    e Zd Zedd Zdd ZdS )_TestManagerRestartc             C   s,   t ||td}|  | }|d d S )N)r  rX   r  zhello world)r  r  r  r  rP   )rk   r  rX   r<   r   r   r   r   r  
  s
    z_TestManagerRestart._putterc          
   C   s(  t d}ttjjdf|td}zd| }|j}|j	
  |  | j| j|j|fd}|  |  | }| | d ~W d t|dr|  X t||td}y|  | |j W nb tk
r" } zB|jtjkr td t||td}t|dr| |j W d d }~X Y nX d S )Nr  r   )r  rX   r  )rc   r3   zhello worldry  g      ?)r%   r  r  r   r   r  r  Z
get_serverr  Zlistenerr   ru   rd   r  rx   r  rE   rw   r   ry  r   OSErrorerrnoZ
EADDRINUSEr1   r}   )r/   rX   r<   Zsrvraddrrz   r   ry   r   r   r   test_rapid_restart
  s:    




z&_TestManagerRestart.test_rapid_restartN)r7   r8   r9   r   r  r  r   r   r   r   r  
  s   r   c               @   s   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	edd Z
ed$ddZeeddd Zeedeejdkdeedkdeeedddd Zedd Zeedeejdkddd  Zd!d" Zd#S )%_TestConnection)r;   r=   c             C   s,   xt |jtD ]}|| qW |  d S )N)r   
recv_bytesSENTINEL
send_bytesr   )rk   r  msgr   r   r   _echo-  s    z_TestConnection._echoc          
   C   sL  |   \}}| j| j|fd}d|_|  ddd g}td}|d }tdttd}| j	d	krx| 
t| t | 
||d  | 
| | | 
||d  | 
| | | j	d	kr tdd
gd }t|d
gdt|   }	| 
||d  | 
||t||j  | 
t||	 tdd
gd }d
gd t| d
gdt|   }	| 
||d  | 
||d|j t||j  | 
t||	 ttd}| 
||d  y||}
W n6 tjk
r } z| 
|j|f W d d }~X Y nX | d|
  t|j}| 
| d | |jd
 | 
|dd | |jd
 | 
|td | |jt |d  t d | 
|td | |jd
 | 
| d  tdd }|| | 
| | |t! |"  | j	d	kr@| 
|j#d | 
|j$d | %t&|j | %t&|j |'  d S )N)rc   r3   Tr>   g      @zhello worldr   r   r  r;   r   r	  r  z(                                        zexpected BufferTooShort, got %sFr$   g?Xi   )(r   rd   r  r[   ru   r   r  rt   r   rT   rE   rs   filenor   r   r   r  r  r^   Zrecv_bytes_intoitemsize	bytearrayr   ZBufferTooShortr3   r/  r,   r   rC   r.   r  r1   r}   r  r   readablewritabler   EOFErrorrx   )r/   r  r   rz   r  r  Zlongmsgr  bufferr   rG   ry   r   Zreally_big_msgr   r   r   test_connection3  sr    

$"




z_TestConnection.test_connectionc             C   s   | j dd\}}| |dd  | | d | jdkr| |jd | |jd | |jd | |jd | t|jd | t|j | t|j	 d S )NF)r   r>   r;   Trl   )
r   rE   r   r   rT   r  r  r   r  r   )r/   readerwriterr   r   r   test_duplex_false  s    
z!_TestConnection.test_duplex_falsec             C   st   |   \}}| j| j|fd}d|_|  |  td}|| | |	 | |t
 |  |  d S )N)rc   r3   Trn   )r   rd   r  r[   ru   r   r   r  rE   r  r  rx   )r/   r  r   rz   r  r   r   r   test_spawn_close  s    

z _TestConnection.test_spawn_closec             C   s:  | j dkr| d| j  td}|  \}}|| | | | ||d | | |dd   ||dd | | |dd  ||d | | td	 ||dd
 | | td	 | t	|j|d | t	|j|dd | t	|j|dd | t	|j|d | t	|j|dd d S )Nr;   ztest not appropriate for {}ZabcdefghijklmnopqrstuvwxyzrN   r  r
        r  r         r>   r$   r  )
rT   rU   rV   r   r   r  rE   r  r   r(   )r/   r  rA   rB   r   r   r   test_sendbytes  s&    

z_TestConnection.test_sendbytesc          
   C   sL   yt | W n4 tk
rB } z|jtjkr0dS  W d d }~X Y nX dS d S )NFT)r%   fstatr  r  EBADF)rk   r  ry   r   r   r   _is_fd_assigned  s    z_TestConnection._is_fd_assignedFc             C   sh   |r2x,t ddD ]}| |st| | qW t|}trNt|tj	}t
|| t| d S )Nr   r   )r   r  r%   dup2r  r   recv_handlemsvcrtZopen_osfhandler  r,  r   )rk   r  dataZcreate_dummy_fdsr   r  r   r   r   _writefd  s    

z_TestConnection._writefdz$test needs multiprocessing.reductionc          	   C   s   | j dkr| d | jdd\}}| j| j|dfd}d|_|  | tj	j
tj	j ttj	jd,}| }trt|}t|||j W d Q R X |  ttj	jd}| | d W d Q R X d S )	Nr;   zonly makes sense with processesT)r   s   foo)rc   r3   wbrb)rT   rU   r   rd   r  r[   ru   r   r   r   r   r   r   r  r  Zget_osfhandler   send_handlerj   rx   rE   r   )r/   r  r   rz   r   r  r   r   r   test_fd_transfer  s    


z _TestConnection.test_fd_transferr   z*test semantics don't make sense on Windowsr   z)largest assignable fd number is too smallr  ztest needs os.dup2()c          
   C   s  | j dkr| d | jdd\}}| j| j|ddfd}d|_|  | tj	j
tj	j ttj	jdf}| }x(tdtD ]}| |sP qW | d	 t|| zt|||j W d t| X W d Q R X |  ttj	jd
}| | d W d Q R X d S )Nr;   zonly makes sense with processesT)r   s   bar)rc   r3   r  r   z2could not find an unassigned large file descriptorr  )rT   rU   r   rd   r  r[   ru   r   r   r   r   r   r   r  r   MAXFDr  r/  r%   r  r   r  rj   r   rx   rE   r   )r/   r  r   rz   r   r  Znewfdr   r   r   test_large_fd_transfer  s(    	



z&_TestConnection.test_large_fd_transferc             C   s   t | d d S )N    )r%   r,  r  )r/   r  r   r   r   _send_data_without_fd  s    z%_TestConnection._send_data_without_fdzdoesn't make sense on Windowsc             C   s`   | j dkr| d | jdd\}}| j| j|fd}d|_|  | tt	j
| |  d S )Nr;   zonly makes sense with processesT)r   )rc   r3   )rT   rU   r   rd   r  r[   ru   r   r   r   r  rx   )r/   r  r   rz   r   r   r   test_missing_fd_transfer  s    

z(_TestConnection.test_missing_fd_transferc          
   C   s   |   \}}|R |B |d | | d | jdkrT| |j | |j W d Q R X W d Q R X | jdkr| |j | |j | t	|j | t	|j d S )Ni  r;   )
r   r   rE   r   rT   rf   closedrY   r   r  )r/   rA   rB   r   r   r   rB  "  s    

 
z_TestConnection.test_contextN)F)r7   r8   r9   rJ   r   r  r  r  r  r  r  r  r)   rl  HAS_REDUCTIONr  r  r   r   r  r   r%   r  r  r  rB  r   r   r   r   r  )  s*   O 


r  c               @   s    e Zd ZdZdd Zdd ZdS )_TestListener)r;   c             C   sF   x@| j jD ]4}| j j|d}| |j | t| j j|j| q
W d S )N)family)
connectionfamiliesListenerr   r   r   r  r  )r/   r  r  r   r   r   test_multiple_bind6  s
    z _TestListener.test_multiple_bindc             C   sz   | j  N}| j |j4}|  }|d | | d W d Q R X W d Q R X W d Q R X | jdkrv| 	t
|j d S )Ni  r;   )r  r  Clientr  acceptr   rE   r   rT   r   r  )r/   r  rR   r  r   r   r   rB  =  s    

.
z_TestListener.test_contextN)r7   r8   r9   rJ   r  rB  r   r   r   r   r  2  s   r  c               @   s4   e Zd ZdZedd Zdd Zdd Zdd	 Zd
S )_TestListenerClient)r;   r=   c             C   s"   | j |}|d |  d S )Nrn   )r  r  r   r   )rk   r  r  r   r   r   re   K  s    
z_TestListenerClient._testc             C   sn   xh| j jD ]\}| j j|d}| j| j|jfd}d|_|  | }| 	|
 d |  |  q
W d S )N)r  )rc   r3   Trn   )r  r  r  rd   re   r  r[   ru   r  rE   r   rx   r   )r/   r  r  rz   r  r   r   r   test_listener_clientQ  s    z(_TestListenerClient.test_listener_clientc             C   sj   | j  }| j| j|jfd}d|_|  td |	 }| 
| d |  |  |  d S )N)rc   r3   Tr>   rn   )r  r  rd   re   r  r[   ru   r1   r}   r  rE   r   r   rx   )r/   r  rz   r  r   r   r   test_issue14725\  s    

z#_TestListenerClient.test_issue14725c             C   sl   xf| j jD ]Z}| j j|d}| j |j}| }|d | |d |	  |	  |	  q
W d S )N)r  s   hellor>   )
r  r  r  r  r  r  r  rY   r   r   )r/   famr  rR   rA   r   r   r   test_issue16955l  s    
z#_TestListenerClient.test_issue16955N)	r7   r8   r9   rJ   r   re   r  r  r  r   r   r   r   r  G  s
   r  c               @   sT   e Zd ZdZdd Zedd Zdd Zedd	 Zd
d Z	edd Z
dd ZdS )	_TestPoll)r;   r=   c             C   sJ   |   \}}| | d |d | | d | | d d S )NFr[  T)r   rE   r   r  )r/   rA   rB   r   r   r   test_empty_string{  s
    
z_TestPoll.test_empty_stringc             C   s.   x |D ]}t d || qW |  d S )Ng?)r1   r}   r  r   )rk   r  stringsr
   r   r   r   _child_strings  s    

z_TestPoll._child_stringsc             C   sx   d}|   \}}| j| j||fd}|  x>|D ]6}xtdD ]}|dr@P q@W | }| || q2W |  d S )N)s   hellor[     a   br[  s   byer[  s   lop)rc   r3      g{Gz?)	r   rd   r  ru   r   r   r  rE   rx   )r/   r  rA   rB   rz   r
   r   r  r   r   r   test_strings  s    

z_TestPoll.test_stringsc             C   s   | d d S )NrN   )r   )rk   r   r   r   r   _child_boundaries  s    z_TestPoll._child_boundariesc             C   sv   |  d\}}| j| j|fd}|  td ddg}x|D ]}|| q@W |  |  | 	|
 | d S )NF)rc   r3   rl   s   firsts   second)r   rd   r  ru   r1   r}   r  r   rx   rv   r  )r/   r   r  rz   rA  r'  r   r   r   test_boundaries  s    

z_TestPoll.test_boundariesc             C   s"   | d | d | d d S )Nr  r  s   cd)r  )rk   rB   r   r   r   _child_dont_merge  s    

z_TestPoll._child_dont_mergec             C   s   |   \}}| |dd | |dd | j| j|fd}|  | | d | |dd | |dd | | d | |dd | |dd | |dd | | d	 |  d S )
Ng        Fg?)rc   r3   r  g      ?Tr  s   cd)r   rE   r   rd   r  ru   r  rx   )r/   rA   rB   rz   r   r   r   test_dont_merge  s    z_TestPoll.test_dont_mergeN)r7   r8   r9   rJ   r  r   r  r  r  r  r  r  r   r   r   r   r  w  s   r  z$test needs multiprocessing.reductionc               @   sP   e Zd ZdZedd Zedd Zedd Zdd	 Zed
d Z	dd Z
dS )_TestPicklingConnections)r;   c             C   s   ddl m} |jtd d S )Nr   )resource_sharer)r   )r   r  r   r   )rk   r  r   r   r   r    s    z&_TestPicklingConnections.tearDownClassc             C   s   xH|D ]@}| j j|d}||j | }|| |  |  qW t }|tj	j
df |  ||  | \}}|| |  |  |  d S )N)r  r   )r  r  r   r  r  r   socketbindr   r   r  listengetsocknamer   )rk   r  r  r  r  new_connr  r   r   r   	_listener  s     


z"_TestPicklingConnections._listenerc             C   s|   x:t |jd D ]*\}}| j|}||  |  qW | \}}t }|| |	|  |  |  d S )N)
r   r   r  r  r   r   r   r  r  sendall)rk   r  r  r  clientr   r   r   _remote  s    
z _TestPicklingConnections._remotec             C   sf  | j j}|  \}}| j| j||fd}d|_|  |  |  \}}| j| j|fd}d|_|  |  xL|D ]D}d| 	d}	|
 }
||
|	f |
 }| |
 |	  qxW |d  td}	|
 }
||
|	f |
 }g }x |
d}|sP || qW d|}| ||	  |  |d  |  |  |  |  d S )N)rc   r3   TzThis connection uses family %sasciiz$This connection uses a normal socketr|   r[  )r  r  r   rd   r  r[   ru   r   r  r	   r   r   rE   r   r   r   rx   )r/   r  ZlconnZlconn0Zlpr   Zrconn0Zrpr  r  r  r  bufr
   r   r   r   test_pickling  sH    




z&_TestPicklingConnections.test_picklingc             C   sD   |  }|d |  |  }|  }||d  |  d S )Nzall is wellrl   )r   r   r   )rk   r  r  r   r  r   r   r   child_access%  s    
z%_TestPicklingConnections.child_accessc             C   s   |   \}}| j| j|fd}d|_|  |  | j dd\}}|| |  | | d |  | j dd\}}|| |  |d |  | | d |	  d S )N)rc   r3   TF)r   zall is wellZfoobarZfoobarfoobar)
r   rd   r  r[   ru   r   r   rE   r   rx   )r/   r  r   rz   r   r  r   r   r   test_access1  s"    


z$_TestPicklingConnections.test_accessN)r7   r8   r9   rJ   r   r  r  r  r  r  r  r   r   r   r   r    s   .r  c               @   s    e Zd ZdZdd Zdd ZdS )	_TestHeap)r;   c          	   C   s  d}d}g }xVt |D ]J}ttddd }tj|}|| t||krt	|}||= qW tjjj
}g }d}	|j  | |jj xJt|j D ]8}
x2|
D ]*\}}}||j||||| df qW qW x@|jD ]6\}}}||j||||| df |	|| 7 }	qW |  xlt t|d D ]X}|| d d \}}}||d  d d \}}}| ||kr|dkp||k q6W d S )	Ni  rf  r   r>   i  Zfreeoccupiedr	  )r   r   randomZlognormvariater   rr  rs  r   r^   Z	randrangeZ_heaprz  rC  r   rD  rt   Z_len_to_seqr  Z_arenasindexZ_allocated_blockssortrY   )r/   Z
iterationsZ	maxblocksZblocksr   r  rB   rr  rh  r  rA  Zarenaru   r   ZnarenaZnstartZnstopr   r   r   	test_heapV  s:    




z_TestHeap.test_heapc             C   sz   t  st   | t j t  }| jt jf|  t d x4tdD ](}tj	
d}tj	
d}||_||_qJW d S )Nr   i  r>   )r   Z	isenabledZenabler   Zdisableget_thresholdset_thresholdr   r   rr  rs  Zbuddy)r/   Z
thresholdsr   rA   rB   r   r   r   test_free_from_gc}  s    
z_TestHeap.test_free_from_gcN)r7   r8   r9   rJ   r  r  r   r   r   r   r  R  s   'r  c               @   s"   e Zd ZdefdefdefgZdS )_Foor  r  zN)r7   r8   r9   r    r!   r"   Z_fields_r   r   r   r   r    s   r  c               @   s>   e Zd ZdZdd Zedd ZdddZd	d
 Zdd Z	dS )_TestSharedCTypes)r;   c             C   s   t s| d d S )Nz%requires multiprocessing.sharedctypes)rm  rU   )r/   r   r   r   r    s    z_TestSharedCTypes.setUpc             C   s~   | j d9  _ | j d9  _ | j d9  _ | jd9  _| jd9  _| j d9  _ x$tt|D ]}||  d9  < qbW d S )Nrl   )rF   r  r  r   r^   )rk   r  r  r  rO  r  stringr   r   r   r   _double  s    z_TestSharedCTypes._doubleFc       
      C   s   t dd|d}t td|d}t td|d}t tdd|d}| jdttd	|d}| jd
d|d}td|_| j	| j
||||||fd}d|_|  |  | |jd | |jd | |jd | |jd | |jd x$td	D ]}	| ||	 |	d  qW | |jtd d S )Nr   r  )rE  gUUUUUU?l        r	  rl   r  r   rR   r   rn   )rc   r3   T   gUUUUUU?l        r  g      @Z
hellohello)r   r!   r"   r  r  rt   r   r   rF   rd   r  r[   ru   rx   rE   r@   r  r  )
r/   rE  r  r  r  rO  r  r  rz   r   r   r   r   test_sharedctypes  s&    
z#_TestSharedCTypes.test_sharedctypesc             C   s   | j dd d S )NT)rE  )r  )r/   r   r   r   test_synchronize  s    z"_TestSharedCTypes.test_synchronizec             C   sT   t ddd}t|}d|_d|_d|_| |jd | |jd | |jd d S )Nrl   g      @l        r   )r  r   r  r  r  rE   r@   )r/   rO  r~  r   r   r   	test_copy  s    z_TestSharedCTypes.test_copyN)F)
r7   r8   r9   rJ   r  r   r  r  r  r  r   r   r   r   r    s   
r  c               @   s<   e Zd ZdZdd Zdd Zedd Zdd	 Zd
d Z	dS )_TestFinalize)r;   c             C   s   t j | _t j  d S )N)r   _finalizer_registryr   registry_backupr   )r/   r   r   r   r    s    z_TestFinalize.setUpc             C   s   |  tj tj| j d S )N)rf   r   r  updater  )r/   r   r   r   r    s    z_TestFinalize.tearDownc             C   s  G dd dt }| }tj||jdd ~| }tj||jdd}|  |  ~| }tj||jdd | }tj||jddd	 | }tj||jd
dd	 | }	tj|	|jddd	 | }
tj|
|jddd	 tjd |jddd	 tjd |jddd	 t  |  td d S )Nc               @   s   e Zd ZdS )z)_TestFinalize._test_finalize.<locals>.FooN)r7   r8   r9   r   r   r   r   rt    s   rt  )rA   )r3   )rB   )rR   )d10r>   )r3   exitpriority)d01r   )d02)d03)ry   i)STOPi)r5  r   Finalizer   Z_exit_functionr   r%   _exit)rk   r  rt  rA   rB   Zclose_brR   r  r   r  r  r   r   r   _test_finalize  s0    z_TestFinalize._test_finalizec          
   C   sh   |   \}}| j| j|fd}d|_|  |  dd t|jdD }| |dddd	d
ddg d S )N)rc   r3   Tc             S   s   g | ]}|qS r   r   )r   r'  r   r   r   r     s    z/_TestFinalize.test_finalize.<locals>.<listcomp>r  rA   rB   r  r  r  r   ry   )	r   rd   r  r[   ru   rx   r   r   rE   )r/   r  r   rz   r   r   r   r   test_finalize  s    z_TestFinalize.test_finalizec          	      s   dd G fdddt  dd fdd} fdd	}t }t }zftd
 tddd tj|dtj|dg}t	j
| td dW d Q R X d k	rW d t| tj|  t  X d S )Nc               S   s   d S )Nr   r   r   r   r   cb  s    z,_TestFinalize.test_thread_safety.<locals>.cbc                   s   e Zd Z fddZdS )z-_TestFinalize.test_thread_safety.<locals>.Fooc                s"   | | _ tj|  tddd d S )Nr>   r|   )r  )r   r   r  r  Zrandint)r/   )r  r   r   r0     s    z6_TestFinalize.test_thread_safety.<locals>.Foo.__init__N)r7   r8   r9   r0   r   )r  r   r   rt    s   rt  Fc           
      sT   xNsNt t d  yt  W q tk
rJ }  z|  W d d } ~ X Y qX qW d S )Ng?)r1   r}   r  r   Z_run_finalizersrE  )ry   )rK  finishr   r   run_finalizers$  s    z8_TestFinalize.test_thread_safety.<locals>.run_finalizersc           
      sf   i } x\s`y$ fddt dD | td< W q tk
r\ } z||   W d d }~X Y qX qW d S )Nc                s   h | ]
}  qS r   r   )r   r   )rt  r   r   	<setcomp>6  s    zL_TestFinalize.test_thread_safety.<locals>.make_finalizers.<locals>.<setcomp>r   rN   )r   r  ZgetrandbitsrE  r   )r  ry   )rt  rK  r	  r   r   make_finalizers/  s    $z9_TestFinalize.test_thread_safety.<locals>.make_finalizersgư>rN   )rc   g      @T)r5  r   getswitchintervalr   r  setswitchintervalr  r   r   r   r   Zstart_threadsr1   r}   r   )r/   r
  r  Zold_intervalZold_thresholdr=   r   )rt  r  rK  r	  r   test_thread_safety  s*    




z _TestFinalize.test_thread_safetyN)
r7   r8   r9   rJ   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S )_TestImportStarc             C   sb   dd l }tjtj}tj|d}| |}dd |D }dd |D }|d |d |S )Nr   z*.pyc             S   s(   g | ] }t jt j|d  d qS )r>   r   )r%   pathsplitextsplit)r   r   r   r   r   r   X  s    z4_TestImportStar.get_module_names.<locals>.<listcomp>c             S   s   g | ]}d | qS )zmultiprocessing.r   )r   r  r   r   r   r   Y  s    zmultiprocessing.__init__r   )	globr%   r  dirnamer   __file__rx   r   r   )r/   r  Zfolderpatternfilesmodulesr   r   r   get_module_namesS  s    


z _TestImportStar.get_module_namesc             C   s   |   }tjdkr2|d |d |d n|d tsJ|d td kr\|d xZ|D ]R}t| tj| }| t	|d| x(|j
D ]}| t	||d||f  qW qbW d S )	Nr   zmultiprocessing.popen_forkz multiprocessing.popen_forkserverz!multiprocessing.popen_spawn_posixz!multiprocessing.popen_spawn_win32zmultiprocessing.sharedctypes__all__z%r does not have attribute %r)r  r   r   r   r  r    r.  r  rY   r   r  )r/   r  ri   modattrr   r   r   test_import^  s$    







z_TestImportStar.test_importN)r7   r8   r9   r  r  r   r   r   r   r  Q  s   r  c               @   s,   e Zd ZdZdd Zedd Zdd ZdS )	_TestLogging)r;   c             C   sD   t  }|tj | |d k	 |d |d |t d S )Nzthis will not be printedznor will this)	r   
get_loggersetLevelr   
SUBWARNINGrY   debuginfo	LOG_LEVEL)r/   loggerr   r   r   test_enable_logging  s    

z _TestLogging.test_enable_loggingc             C   s   t  }||  d S )N)r   r   r   ZgetEffectiveLevel)rk   r  r&  r   r   r   _test_level  s    z_TestLogging._test_levelc       	      C   s   d}d}t  }t }|j}t jdd\}}|| | j| j|fd}|	  | 
||  |  |  |tj || | j| j|fd}|	  | 
||  |  |  || |jtd d S )Nr  %   F)r   )rc   r3   )level)r   r   loggingZ	getLoggerr*  r   r!  rd   r(  ru   rE   r   rx   r   ZNOTSETr%  )	r/   ZLEVEL1ZLEVEL2r&  Zroot_loggerZ
root_levelr  r  rz   r   r   r   
test_level  s*    


z_TestLogging.test_levelN)r7   r8   r9   rJ   r'  r   r(  r,  r   r   r   r   r  |  s   r  c               @   s6   e Zd ZdZedd Zeee	dddd Z
dS )	_TestPollEintr)r;   c             C   s   t d t|tj d S )Ng?)r1   r}   r%   r   r   SIGUSR1)rk   rj   r   r   r   _killer  s    
z_TestPollEintr._killerr.  zrequires SIGUSR1c          
      s   dg  fdd}t  }ttj|}zj| j| j|fd}|  z$| jtjdd}|  |	  W d |	  X | 
 d  | |jd W d ttj| X d S )NFc                 s   d d< d S )NTr   r   )r3   )
got_signalr   r   record  s    z._TestPollEintr.test_poll_eintr.<locals>.record)rc   r3   )rl   r   )r%   r_   r   r.  rd   r/  ru   r1   r}   rx   rY   rE   r`   )r/   r1  rj   Z
oldhandlerZkillerrz   r   )r0  r   test_poll_eintr  s    
z_TestPollEintr.test_poll_eintrN)r7   r8   r9   rJ   r   r/  r)   rl  r   r   r2  r   r   r   r   r-    s   r-  c               @   s    e Zd Zeeddd ZdS )TestInvalidHandlezskipped on Windowsc          	   C   sZ   t jd}z*y|  W n ttfk
r2   Y nX W d d |_X | ttft jjd d S )NiMr$   )r   r  Z
Connectionr   r(   r  Z_handler   )r/   r  r   r   r   test_invalid_handles  s    

z&TestInvalidHandle.test_invalid_handlesN)r7   r8   r9   r)   r  WIN32r4  r   r   r   r   r3    s   r3  c               @   s   e Zd Zdd Zdd ZdS )	OtherTestc             C   s,   G dd dt }| tjtjj| d d S )Nc               @   s   e Zd Zdd Zdd ZdS )zFOtherTest.test_deliver_challenge_auth_failure.<locals>._FakeConnectionc             S   s   dS )Ns   something bogusr   )r/   r  r   r   r   r    s    zQOtherTest.test_deliver_challenge_auth_failure.<locals>._FakeConnection.recv_bytesc             S   s   d S )Nr   )r/   r  r   r   r   r    s    zQOtherTest.test_deliver_challenge_auth_failure.<locals>._FakeConnection.send_bytesN)r7   r8   r9   r  r  r   r   r   r   _FakeConnection  s   r7  s   abc)r5  r   r   AuthenticationErrorr  Zdeliver_challenge)r/   r7  r   r   r   #test_deliver_challenge_auth_failure  s    z-OtherTest.test_deliver_challenge_auth_failurec             C   s,   G dd dt }| tjtjj| d d S )Nc               @   s$   e Zd Zdd Zdd Zdd ZdS )zEOtherTest.test_answer_challenge_auth_failure.<locals>._FakeConnectionc             S   s
   d| _ d S )Nr   )r  )r/   r   r   r   r0     s    zNOtherTest.test_answer_challenge_auth_failure.<locals>._FakeConnection.__init__c             S   s2   |  j d7  _ | j dkr tjjS | j dkr.dS dS )Nr>   rl   s   something bogusr[  )r  r   r  Z	CHALLENGE)r/   r  r   r   r   r    s    

zPOtherTest.test_answer_challenge_auth_failure.<locals>._FakeConnection.recv_bytesc             S   s   d S )Nr   )r/   r  r   r   r   r    s    zPOtherTest.test_answer_challenge_auth_failure.<locals>._FakeConnection.send_bytesN)r7   r8   r9   r0   r  r  r   r   r   r   r7    s   r7  s   abc)r5  r   r   r8  r  Zanswer_challenge)r/   r7  r   r   r   "test_answer_challenge_auth_failure  s    z,OtherTest.test_answer_challenge_auth_failureN)r7   r8   r9   r9  r:  r   r   r   r   r6    s   
r6  c             C   s   |  j d7  _ d S )Nr>   )r   )nsr   r   r   initializer  s    r<  c               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestInitializersc             C   s"   t  | _| j | _d| j_d S )Nr   )r   Managermgrr  r;  r   )r/   r   r   r   r    s    
zTestInitializers.setUpc             C   s   | j   | j   d S )N)r?  ry  rx   )r/   r   r   r   r     s    
zTestInitializers.tearDownc             C   sN   t j }| t|jd |t| jf | | jj	d |
  |  d S )Nr>   )r   managersSyncManagerr   	TypeErrorru   r<  r;  rE   r   ry  rx   )r/   r  r   r   r   test_manager_initializer$  s    
z)TestInitializers.test_manager_initializerc             C   sH   | j ttjdd tdt| jf}|  |  | | jj	d d S )Nr>   )r<  )
r   rB  r   r  r<  r;  r   rx   rE   r   )r/   rz   r   r   r   test_pool_initializer,  s
    z&TestInitializers.test_pool_initializerN)r7   r8   r9   r  r  rC  rD  r   r   r   r   r=    s   r=  c             C   s,   y| j dd}W n tjk
r&   Y nX d S )NF)block)rw   r  r"  )rQ   itemr   r   r   _this_sub_process8  s    rG  c              C   s2   t  } t jt| fd}d|_|  |  d S )N)rc   r3   T)r   r   rd   rG  r[   ru   rx   )r   ZsubProcr   r   r   _test_process>  s
    rH  c             C   s   | |  S )Nr   )r  r   r   r   _afuncE  s    rI  c           
   C   s:   t jdd} | tdddddddg}|   |   d S )	Nr  )r;   r>   rl   r	  rN   r  r  )r   r  r  rI  r   rx   )r  r  r   r   r   pool_in_processH  s    rJ  c               @   s0   e Zd Zdd Zedd Zdd Zdd Zd	S )

_file_likec             C   s   || _ d | _d S )N)	_delegate_pid)r/   Zdelegater   r   r   r0   O  s    z_file_like.__init__c             C   s$   t  }|| jkr|| _g | _| jS )N)r%   r_   rM  _cache)r/   rj   r   r   r   cacheS  s
    
z_file_like.cachec             C   s   | j | d S )N)rO  r   )r/   r  r   r   r   r,  \  s    z_file_like.writec             C   s   | j d| j g | _d S )Nr  )rL  r,  rx   rO  rN  )r/   r   r   r   flush_  s    z_file_like.flushN)r7   r8   r9   r0   propertyrO  r,  rP  r   r   r   r   rK  N  s   	rK  c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestStdinBadfiledescriptorc             C   s    t jtd}|  |  d S )N)rc   )r   rd   rH  ru   rx   )r/   r   r   r   r   test_queue_in_processe  s    z0TestStdinBadfiledescriptor.test_queue_in_processc             C   s    t jtd}|  |  d S )N)rc   )r   rd   rJ  ru   rx   )r/   rz   r   r   r   test_pool_in_processj  s    z/TestStdinBadfiledescriptor.test_pool_in_processc                sJ   t  }t|  d tj fddd}   | dksFtd S )NrO  c                  s      S )N)rP  r   )fliker   r   rZ  s  r[  z:TestStdinBadfiledescriptor.test_flushing.<locals>.<lambda>)rc   )	r   r   rK  r,  r   rd   rP  rJ  rO   )r/   Zsior   r   )rU  r   test_flushingo  s    
z(TestStdinBadfiledescriptor.test_flushingN)r7   r8   r9   rS  rT  rV  r   r   r   r   rR  c  s   rR  c               @   sl   e Zd Zedd ZdddZedd Zddd	Zd
d Zdd Z	dd Z
edd Zdd Zdd ZdS )TestWaitc             C   sF   x8t dD ],}|r$tt d  ||t f q
W |  d S )Nr   g?)r   r1   r}   r  r   r%   r_   r   )rk   r  slowr   r   r   r   _child_test_waitz  s
    zTestWait._child_test_waitFc          	      s  ddl m} g }g  g }xjtdD ]^}tjdd\}}tj| j||fd}d|_|  |	  |
|  
| | |j q"W xX|rxN||D ]B}y| }	W n& tk
r   || |	  Y qX |
|	 qW qW |  t fdd	td
D }
| ||
 d S )Nr   )r   r  F)r   )rc   r3   Tc             3   s"   | ]} D ]}||j fV  q
qd S )N)rj   )r   r   rz   )r   r   r   r    s    z%TestWait.test_wait.<locals>.<genexpr>r   )multiprocessing.connectionr   r   r   r   rd   rY  r[   ru   r   r   r   rx   r   r  r   r  r  rE   )r/   rX  r   readersmessagesr   r   r  rz   r  r   r   )r   r   	test_wait  s0    


zTestWait.test_waitc             C   sZ   t   }|| x:tdD ].}|r6tt d  |d| d qW |  d S )Nr   g?z%s
r  )	r  r  r   r1   r}   r  r  r	   r   )rk   r  rX  r
   r   r   r   r   _child_test_wait_socket  s    
z TestWait._child_test_wait_socketc             C   sZ  ddl m} t }|tjjdf |  | }g }g }i }xHt	dD ]<}t
j| j||fd}	d|	_|	  ||	 | |	j qLW x.t	dD ]"}| \}
}||
 g ||
< qW |  xL|rx@||D ]4}
|
d}|s||
 |
  q||
 | qW qW ddd	 t	d
D d}x$| D ]}| d|| q:W d S )Nr   )r   r  )rc   r3   Tr  r  c             s   s   | ]}d | V  qdS )z%s
Nr   )r   r   r   r   r   r    s    z,TestWait.test_wait_socket.<locals>.<genexpr>r   r  r[  )rZ  r   r  r  r   r   r  r  r  r   r   rd   r^  r[   ru   r   r   rx   r  r   r   r   r	   r  rE   )r/   rX  r   r  r  r[  r   Zdicr   rz   r   r}  r  r   vr   r   r   test_wait_socket  s<    




zTestWait.test_wait_socketc             C   s   |  d d S )NT)r]  )r/   r   r   r   test_wait_slow  s    zTestWait.test_wait_slowc             C   s   |  d d S )NT)r`  )r/   r   r   r   test_wait_socket_slow  s    zTestWait.test_wait_socket_slowc             C   s   ddl m} d}t \}}t }|||g|}t | }| |g  | ||d  | ||d  |	d  t }|||gd}t | }| ||g | |d d S )Nr   )r   rN   rl   g      ?r   g?)
rZ  r   r   r   r1   r2   rE   r?  rR  r   )r/   r   r   rA   rB   ru   rG   r2  r   r   r   test_wait_timeout  s    
zTestWait.test_wait_timeoutc             C   s   |   t| d S )N)rD  r1   r}   )rk   rK  Zperiodr   r   r   signal_and_sleep  s    zTestWait.signal_and_sleepc             C   s  ddl m} d}dd }td}t \}}tj| j||fd}|  | |j	t
 | |jdd t }|||j	|g|d }	t | }
| |	|j	g | |
|d	  | |
|d	  |d  t }|||j	|gd}	t | }
| ||	||j	|g | |
d
 |d  t }|||j	|gd}	t | }
| ||	|||j	|g | |
d
 |  |  d S )Nr   )r   r	  c             S   s   t | dd dS )Nc             S   s   t | S )N)r   )r  r   r   r   rZ    r[  z>TestWait.test_wait_integer.<locals>.<lambda>.<locals>.<lambda>)rW  )r  )r  r   r   r   rZ    r[  z,TestWait.test_wait_integer.<locals>.<lambda>)rc   r3   r   )r   rl   g?)rZ  r   r   rM  r   rd   rd  ru   r\   r   r   rY   rC  r1   r2   rE   r?  rR  r   r   rx   )r/   r   r   Zsorted_rK  rA   rB   rz   ru   rG   r2  r   r   r   test_wait_integer  s<    


zTestWait.test_wait_integerc             C   sf   ddl m} t \}}t }||gdd}t | }| |g  | |d |  |  d S )Nr   )r   r$   )r   r>   )	rZ  r   r   r   r1   r2   rE   r?  r   )r/   r   rA   rB   r5   rG   r   r   r   test_neg_timeout  s    zTestWait.test_neg_timeoutN)F)F)r7   r8   r9   r   rY  r]  r^  r`  ra  rb  rc  rd  re  rf  r   r   r   r   rW  x  s   


%+rW  c               @   s4   e Zd Zeeddd Zeeddd ZdS )TestInvalidFamilyzskipped on Windowsc          	   C   s&   |  t tjd W d Q R X d S )Nz\\.\test)r   r(   r   r  r  )r/   r   r   r   test_invalid_family-  s    z%TestInvalidFamily.test_invalid_familyz skipped on non-Windows platformsc          	   C   s&   |  t tjd W d Q R X d S )Nz/var/test.pipe)r   r(   r   r  r  )r/   r   r   r   test_invalid_family_win322  s    z+TestInvalidFamily.test_invalid_family_win32N)	r7   r8   r9   r)   r  r5  rh  rl  ri  r   r   r   r   rg  +  s   rg  c               @   s,   e Zd Zedd Zedd Zdd ZdS )	TestFlagsc             C   s   | ttj d S )N)r   r  r   flags)rk   r  r   r   r   run_in_grandchild<  s    zTestFlags.run_in_grandchildc             C   sr   dd l }tjdd\}}tj| j|fd}|  | }|  |  |  t	t
j|f}t|| d S )Nr   F)r   )rc   r3   )jsonr   r   rd   rl  ru   r   rx   r   r  r   rk  printdumps)rk   rm  r   r  rz   grandchild_flagsrk  r   r   r   run_in_child@  s    zTestFlags.run_in_childc             C   sP   dd l }dd l}d}|tjdddd|g}||d\}}| || d S )Nr   zJfrom test._test_multiprocessing import TestFlags; TestFlags.run_in_child()z-Ez-Sz-Oz-cr  )rm  
subprocessZcheck_outputr   
executableloadsdecoderE   )r/   rm  rr  progr  Zchild_flagsrp  r   r   r   
test_flagsM  s    zTestFlags.test_flagsN)r7   r8   r9   r   rl  rq  rw  r   r   r   r   rj  ;  s   rj  c               @   s    e Zd Zedd Zdd ZdS )TestTimeoutsc             C   s>   t d |d |  tj|}|d |  d S )Nr>   rC  i  )r1   r}   r   r   r   r  r  )rk   childr  r  r   r   r   _test_timeout\  s    


zTestTimeouts._test_timeoutc          	   C   s   t  }zt d tjdd\}}tjjdd}tj| j||j	fd}|
  |  | | d |  | }| | d |  |  t| W d t | X d S )	Ng?T)r   AF_INET)r  )rc   r3   rC  i  )r  ZgetdefaulttimeoutZsetdefaulttimeoutr   r   r  r  rd   rz  r  ru   r   rE   r   r  r   )r/   Zold_timeoutparentry  r  rz   r  r   r   r   r3  e  s"    
zTestTimeouts.test_timeoutN)r7   r8   r9   r   rz  r3  r   r   r   r   rx  [  s   	rx  c               @   s   e Zd Zdd ZdS )TestNoForkBombc             C   s   t  }tjtjtd}|dkrVtjj	
||\}}}| |d | d| n2tjj	||\}}}| | d | |d d S )Nzmp_fork_bomb.pyr   r[  s   RuntimeErrors   123)r   r   r%   r  rx   r  r  r   r   script_helperZassert_python_failurerE   rv   assert_python_okr  )r/   r   ri   r   outr   r   r   r   test_noforkbomb~  s    zTestNoForkBomb.test_noforkbombN)r7   r8   r9   r  r   r   r   r   r}  }  s   r}  c               @   s    e Zd Zedd Zdd ZdS )TestForkAwareThreadLockc             C   sV   |dkr:t j| j|d |fd}|  |  t| n|ttj	 |  d S )Nr>   )rc   r3   )
r   rd   ry  ru   r   r   r   r^   r   _afterfork_registry)rk   r]  r  rz   r   r   r   ry    s    
zTestForkAwareThreadLock.childc             C   sd   t d\}}t }ttj}t j| jd|fd}|  |	  |
 }t| | || d S )NFrN   )rc   r3   )r   r   r   ZForkAwareThreadLockr^   r  rd   ry  ru   r   r   r   ZassertLessEqual)r/   r   r  r  Zold_sizerz   Znew_sizer   r   r   rF    s    
z!TestForkAwareThreadLock.test_lockN)r7   r8   r9   r   ry  rF  r   r   r   r   r    s   r  c               @   s0   e Zd Zdd Zdd Zedd Zdd Zd	S )
TestCloseFdsc             C   sb   t rt  S t  }g }x |dk r@|| t|}q"W x|D ]}t| qHW |S d S )Nrf  )r5  r  detachr   r%   dupr   )r/   r  Zto_closer  r   r   r   get_high_socket_fd  s    


zTestCloseFds.get_high_socket_fdc             C   s,   t rtjtjtj|d  n
t| d S )N)r  )r5  r  r{  SOCK_STREAMr   r%   )r/   r  r   r   r   r     s    zTestCloseFds.closec          
   C   sZ   yt |t jt j}W n, tk
rB } z|| W d d }~X Y nX |  |d  d S )N)r  Zfromfdr{  r  rE  r   r   )rk   r  r  r
   ry   r   r   r   _test_closefds  s    zTestCloseFds._test_closefdsc          	   C   s   t stdt \}}|  }z8tj| j||fd}|  |	  |
 }t| W d | 	| |	  |	  X t dkr| |d  n.d}| |t | |jtjkp|j|k| d S )Nzrequires fd pickling)rc   r3   r   i6'  )r  r)   r*   r   r   r  rd   r  ru   r   r   r   r   r   r\   r  rY   r  r  Zwinerror)r/   r  r  r  rz   ry   ZWSAENOTSOCKr   r   r   test_closefd  s(    


zTestCloseFds.test_closefdN)r7   r8   r9   r  r   r   r  r  r   r   r   r   r    s   
r  c               @   sf   e Zd ZeejejZedd Z	e
eedddd Zedd Ze
eeddd	d
 ZdS )TestIgnoreEINTRc             C   sF   dd }t  t j| |d | }|| |d| j  d S )Nc             S   s   d S )Nr   )r   framer   r   r   r     s    z-TestIgnoreEINTR._test_ignore.<locals>.handlerready   x)r   r.  r   r   r  CONN_MAX_SIZE)rk   r  r   r  r   r   r   _test_ignore  s    

zTestIgnoreEINTR._test_ignorer.  zrequires SIGUSR1c             C   s   t  \}}zt j| j|fd}d|_|  |  | | d t	
d t|jtj t	
d |d | | d t	
d t|jtj | | d| j  t	
d |  W d |  X d S )N)rc   r3   Tr  g?i  r  )r   r   rd   r  r[   ru   r   rE   r   r1   r}   r%   r   rj   r   r.  r   r  r  rx   )r/   r  r   rz   r   r   r   test_ignore  s&    





zTestIgnoreEINTR.test_ignorec          	   C   sN   dd }t  t j| tj $}||j | }|d W d Q R X d S )Nc             S   s   d S )Nr   )r   r  r   r   r   r     s    z6TestIgnoreEINTR._test_ignore_listener.<locals>.handlerwelcome)r   r.  r   r  r  r   r  r  )rk   r  r   r  rA   r   r   r   _test_ignore_listener  s    z%TestIgnoreEINTR._test_ignore_listenerc             C   s   t  \}}z|t j| j|fd}d|_|  |  | }t	d t
|jtj t	d t j|}| | d |  W d |  X d S )N)rc   r3   Tg?r  )r   r   rd   r  r[   ru   r   r   r1   r}   r%   r   rj   r   r.  r  r  rE   rx   )r/   r  r   rz   r  r  r   r   r   test_ignore_listener  s    


z$TestIgnoreEINTR.test_ignore_listenerN)r7   r8   r9   maxr   ZPIPE_MAX_SIZEZSOCK_MAX_SIZEr  r   r  r)   rl  r   r   r  r  r  r   r   r   r   r    s
   

r  c               @   s@   e Zd Zedd Zdd Zdd Zdd Zd	d
 Zdd Z	dS )TestStartMethodc             C   s   | t  d S )N)r   r   r   )rk   r  r   r   r   _check_context3  s    zTestStartMethod._check_contextc             C   s^   |j dd\}}|j| j|fd}|  |  | }|  |  | ||  d S )NF)r   )rc   r3   )	r   rd   r  ru   r   r   rx   rE   r   )r/   r>  r   r  rz   Zchild_methodr   r   r   check_context7  s    zTestStartMethod.check_contextc          	   C   s   x|dD ]t}yt |}W n tk
r.   wY nX | | | | | | | t|jd | t|jd  | | qW d S )N)r   r   r   r   )	r   r?  r(   rE   r   r   r   set_start_methodr  )r/   methodr>  r   r   r   rB  A  s    
zTestStartMethod.test_contextc          
   C   s   t t d}t  }zxdD ]}yt j|dd W n tk
rJ   wY nX | t  | t  }| | | | t	|j
 | | |jj
 | | t  |d7 }qW W d t j|dd X | |d d S )Nr   )r   r   r   T)forcer>   )r   set_forkserver_preloadPRELOADr   r  r(   rE   r?  rY   rs   r7   lower
startswithrd   r  r1  )r/   r  Z
old_methodr  r>  r   r   r   test_set_getM  s&    


zTestStartMethod.test_set_getc             C   sF   t  }tjdkr"| |dg n | |ddgkp>|dddgk d S )Nr   r   r   r   )r   Zget_all_start_methodsr   r   rE   rY   )r/   methodsr   r   r   test_get_allc  s
    
zTestStartMethod.test_get_allc             C   s   t  dkr| d tjtjtd}tj	j
|\}}}| }| }| dksd|dkr~t| t| | d d S )Nr   z*test only relevant for 'forkserver' methodzmp_preload.pyokr  z(failed spawning forkserver or grandchild)r   r   rU   r%   r  rx   r  r  r   r   r~  r  ru  r  rn  r/  )r/   ri   r   r  r   r   r   r   test_preload_resourcesk  s    
z&TestStartMethod.test_preload_resourcesN)
r7   r8   r9   r   r  r  rB  r  r  r  r   r   r   r   r  2  s   
r  z*test semantics don't make sense on Windowsc               @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestSemaphoreTrackerc          	   C   s   dd l }d}t \}}|jtjdd|||f g|g|jd}t| t|ddd*}|	 
 d	}|	 
 d	}W d Q R X t| |  |  td
 | t}	t| W d Q R X | |	jjtjtjf |j d}
|j  d}| |
| | |
d|  d S )Nr   aM  if 1:
            import multiprocessing as mp, time, os
            mp.set_start_method("spawn")
            lock1 = mp.Lock()
            lock2 = mp.Lock()
            os.write(%d, lock1._semlock.name.encode("ascii") + b"\n")
            os.write(%d, lock2._semlock.name.encode("ascii") + b"\n")
            time.sleep(10)
        z-Ez-c)Zpass_fdsr   r  T)r  r  g       @zutf-8z9semaphore_tracker: There appear to be 2 leaked semaphoreszsemaphore_tracker: %r: \[Errno)rr  r%   pipePopenr   rs  PIPEr   r   readliner  ru  r   Z
sem_unlinkr   r   r1   r}   r   r  rv   Z	exceptionr  ENOENTZEINVALr   r   ZassertRegex)r/   rr  cmdr   r  rz   r   Zname1Zname2r>  r   r   r   r   r   test_semaphore_tracker|  s.    	




z+TestSemaphoreTracker.test_semaphore_trackerc       	   	   C   s   ddl m} |  |j}t|| td t	d}t
 T}|rZ|| td | }|  |  t|}~t  | |  W d Q R X d S )Nr   )_semaphore_trackerg      ?r   zsemaphore_tracker: process died)Z!multiprocessing.semaphore_trackerr  r   rM  r%   r   r1   r}   r   r?  
contextlib	ExitStackenter_contextZassertWarnsRegexUserWarningrM  rC  rD  r   r   r   r   ZassertIsNone)	r/   r   Z
should_dier  rj   r>  stackrK  r   r   r   r   check_semaphore_tracker_death  s$    



z2TestSemaphoreTracker.check_semaphore_tracker_deathc             C   s   |  tjd d S )NF)r  r   r   )r/   r   r   r   test_semaphore_tracker_sigint  s    z2TestSemaphoreTracker.test_semaphore_tracker_sigintc             C   s   |  tjd d S )NT)r  r   r   )r/   r   r   r   test_semaphore_tracker_sigkill  s    z3TestSemaphoreTracker.test_semaphore_tracker_sigkillN)r7   r8   r9   r  r  r  r  r   r   r   r   r  x  s   %r  c               @   s    e Zd Zedd Zdd ZdS )TestSimpleQueuec             C   s8   |   z ||  ||  W d |  X d S )N)r   rP   r  r   )rk   r   r  r  r   r   r   _test_empty  s
    zTestSimpleQueue._test_emptyc             C   s   t  }t  }t  }t j| j|||fd}d|_|  | |  |	  |
  | |  | | d | | d | |  |  d S )N)rc   r3   TF)r   ZSimpleQueuerp   rd   r  r[   ru   rY   r  r   r   rf   rE   rw   rx   )r/   r   r  r  r   r   r   r   
test_empty  s     zTestSimpleQueue.test_emptyN)r7   r8   r9   r   r  r  r   r   r   r   r    s   
r  c               @   sd  e Zd ZdZejjZdd Zdd Z	e
dd ZeZdd	 Zd
d Ze
dd ZdHddZdd Ze
dd Zdd Ze
dd ZdIddZe
dd ZdJddZe
d d! ZdKd#d$Zd%d& Ze
d'd( Zd)d* Ze
d+d, Zd-d. Ze
d/d0 Zd1d2 Z e
d3d4 Z!d5d6 Z"e
d7d8 Z#d9d: Z$e
d;d< Z%d=d> Z&e
d?d@ Z'dAdB Z(e
dCdD Z)dEdF Z*dGS )LTestSyncManagerTypesa}  Test all the types which can be shared between a parent and a
    child process by using a manager which acts as an intermediary
    between them.

    In the following unit-tests the base type is created in the parent
    process, the @classmethod represents the worker process and the
    shared object is readable and editable between the two.

    # The child.
    @classmethod
    def _test_list(cls, obj):
        assert obj[0] == 5
        assert obj.append(6)

    # The parent.
    def test_list(self):
        o = self.manager.list()
        o.append(5)
        self.run_worker(self._test_list, o)
        assert o[1] == 6
    c             C   s   |   | _| j  d | _d S )N)manager_classr<   ru   r   )r/   r   r   r   r     s    

zTestSyncManagerTypes.setUpc             C   sB   | j d k	r(| j  r(| j   | j   | j  d | _d | _ d S )N)r   rZ   r   rx   r<   ry  )r/   r   r   r   r    s    


zTestSyncManagerTypes.tearDownc             C   s   t   d S )N)r   Zreap_children)rk   r   r   r   r    s    zTestSyncManagerTypes.setUpClassc             C   sz   t | j t }d}x^tt dkrtt| |d9 }t | }|dkrdtj	_
tdt |f tjd P qW d S )Ng{Gz?r>   rl   g      @TzPWarning -- multiprocessing.Manager still has %s active children after %s seconds)file)r   r   r1   r2   r^   r   rr   r}   r   r   environment_alteredrn  r   r   )r/   
start_timer5   rd  r   r   r   wait_proc_exit  s    


z#TestSyncManagerTypes.wait_proc_exitc             C   s@   t j||fd| _d| j_| j  |   | | jjd d S )N)rc   r3   Tr   )r   rd   r   r[   ru   r  rE   r`   )r/   Zworkerr'  r   r   r   
run_worker&  s
    
zTestSyncManagerTypes.run_workerc             C   sd   |  dkst| st| r(t| dks8t| rDt| dksTt| s`td S )Nrl   rN   r  )r  rO   r  r  rw   )rk   r'  r   r   r   _test_queue-  s    z TestSyncManagerTypes._test_queuer   c             C   sN   t | j|d}|d |d | | j| | s>t| rJtd S )Nrl   rN   r  )r   r<   rP   r  r  r  rO   r  )r/   ZqnamerT  r   r   r   
test_queue7  s    

zTestSyncManagerTypes.test_queuec             C   s   |  d d S )Nr)  )r  )r/   r   r   r   test_joinable_queue?  s    z(TestSyncManagerTypes.test_joinable_queuec             C   s*   |  st|  |  |d d S )NgMbP?)r   rO   r   r   )rk   r'  r   r   r   ro  B  s    z TestSyncManagerTypes._test_eventc             C   s:   | j  }|  | | j| | r,t|d d S )NgMbP?)r<   rp   r   r  ro  r   rO   r   )r/   rT  r   r   r   rp  I  s
    
zTestSyncManagerTypes.test_eventc             C   s   |   d S )N)rC  )rk   r'  r   r   r   
_test_lockP  s    zTestSyncManagerTypes._test_lockrB  c             C   s6   t | j| }| | j| |  | t|j d S )N)r   r<   r  r  rD  r   r   )r/   lnamerT  r   r   r   rF  T  s    zTestSyncManagerTypes.test_lockc             C   s   |   |  d S )N)rC  rD  )rk   r'  r   r   r   _test_rlockZ  s    z TestSyncManagerTypes._test_rlockc             C   s    t | j| }| | j| d S )N)r   r<   r  r  )r/   r  rT  r   r   r   rH  _  s    zTestSyncManagerTypes.test_rlockc             C   s   |   d S )N)rC  )rk   r'  r   r   r   rL  c  s    z$TestSyncManagerTypes._test_semaphorerM  c             C   s(   t | j| }| | j| |  d S )N)r   r<   r  rL  rD  )r/   snamerT  r   r   r   rN  g  s    z#TestSyncManagerTypes.test_semaphorec             C   s   | j dd d S )NrO  )r  )rN  )r/   r   r   r   rP  l  s    z+TestSyncManagerTypes.test_bounded_semaphorec             C   s   |   |  d S )N)rC  rD  )rk   r'  r   r   r   _test_conditiono  s    z$TestSyncManagerTypes._test_conditionc             C   s   | j  }| | j| d S )N)r<   rW  r  r  )r/   rT  r   r   r   test_conditiont  s    
z#TestSyncManagerTypes.test_conditionc             C   s   |j dkst|  d S )NrN   )r  rO   r  )rk   r'  r   r   r   _test_barrierx  s    z"TestSyncManagerTypes._test_barrierc             C   s   | j d}| | j| d S )NrN   )r<   r  r  r  )r/   rT  r   r   r   r  }  s    z!TestSyncManagerTypes.test_barrierc          	   C   s   | W d Q R X d S )Nr   )rk   r'  r   r   r   
_test_pool  s    zTestSyncManagerTypes._test_poolc             C   s    | j jdd}| | j| d S )Nr  )r;   )r<   r  r  r  )r/   rT  r   r   r   	test_pool  s    zTestSyncManagerTypes.test_poolc             C   sx   |d dkst |ddks"t |ddks4t |  |  x|D ]}qJW t|dksbt |ddkstt d S )Nr   rN   r>   )rO   r  r  r  reverser^   r  )rk   r'  r  r   r   r   
_test_list  s    
zTestSyncManagerTypes._test_listc             C   s>   | j  }|d | | j| |r*t| t|d d S )NrN   r   )r<   rt   r   r  r  rO   rE   r^   )r/   rT  r   r   r   r    s
    

zTestSyncManagerTypes.test_listc             C   s   t |dkst|d dks t|ddks2tt| dgksHtt| dgks^tt| dgkstt| ddikst| dkstd S )Nr>   rO  rN   )rO  rN   )	r^   rO   rw   rt   r   r  r  r   popitem)rk   r'  r   r   r   
_test_dict  s    zTestSyncManagerTypes._test_dictc             C   s<   | j  }d|d< | | j| |r(t| t|d d S )NrN   rO  r   )r<   r  r  r  rO   rE   r^   )r/   rT  r   r   r   r    s
    
zTestSyncManagerTypes.test_dictc             C   s,   |j dkst| dkst|d d S )Nr>   rl   )rF   rO   rw   r   )rk   r'  r   r   r   _test_value  s    z TestSyncManagerTypes._test_valuec             C   s>   | j dd}| | j| | |jd | | d d S )Nr   r>   rl   )r<   r   r  r  rE   rF   rw   )r/   rT  r   r   r   r    s    zTestSyncManagerTypes.test_valuec             C   sH   |d dkst |d dks t t|dks0t t|ddgksDt d S )Nr   r>   rl   )rO   r^   rt   )rk   r'  r   r   r   _test_array  s    z TestSyncManagerTypes._test_arrayc             C   s$   | j dddg}| | j| d S )Nr   r   r>   )r<   r  r  r  )r/   rT  r   r   r   r    s    zTestSyncManagerTypes.test_arrayc             C   s    |j dkst|jdkstd S )Nr   r>   )r  rO   r  )rk   r'  r   r   r   _test_namespace  s    z$TestSyncManagerTypes._test_namespacec             C   s(   | j  }d|_d|_| | j| d S )Nr   r>   )r<   r  r  r  r  r  )r/   rT  r   r   r   r    s    
z#TestSyncManagerTypes.test_namespaceN)r   )rB  )rB  )rM  )+r7   r8   r9   r:  r   r@  rA  r  r  r  r   r  r  r  r  r  r  r  ro  rp  r  rF  r  rH  rL  rN  rP  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r    sH   




r  c               @   s$   e Zd Zedd Zedd ZdS )	BaseMixinc             C   s   t jj tj f| _d S )N)r   r   	_danglingr   r   dangling)rk   r   r   r   r    s    
zBaseMixin.setUpClassc             C   s   t j  ttjjt| jd  }|rBdt j_t	d| t
jd d }ttjt| jd  }|r|dt j_t	d| t
jd d }d S )Nr   Tz!Warning -- Dangling processes: %s)r  r>   zWarning -- Dangling threads: %s)r   r   
gc_collectr   r   r   r  r  r  rn  r   r   r   )rk   r;   r=   r   r   r   r    s    


zBaseMixin.tearDownClassN)r7   r8   r9   r   r  r  r   r   r   r   r    s   r  c               @   s   e Zd ZdZejZejZeejZeej	Z	eej
Z
eejZeejZeejZeejZeejZeejZeejZeejZeejZeejZeejZeejZeejZeejZdS )ri  r;   N)r7   r8   r9   rT   r   rd   r  r<  rW   rr   r  r   r   r)  rB  rG  rM  rO  rW  rp   r  r   r  r  r  r   r   r   r   ri    s(   















ri  c                   s
  e Zd ZdZejZeedZ	eedZ
eedZeedZeedZeedZeedZeed	Zeed
ZeedZeedZeedZeedZeedZedd Ze fddZe fddZ  ZS )ManagerMixinr<   zmanager.Queuezmanager.JoinableQueuezmanager.Lockzmanager.RLockzmanager.Semaphorezmanager.BoundedSemaphorezmanager.Conditionzmanager.Eventzmanager.Barrierzmanager.Valuezmanager.Arrayzmanager.listzmanager.dictzmanager.Namespacec             O   s   | j j||S )N)r<   r  )rk   r3   r4   r   r   r   r    s    zManagerMixin.Poolc                s   t    t | _d S )N)r  r  r   r>  r<   )rk   )r  r   r   r    s    
zManagerMixin.setUpClassc                s   t  }d}x^tt dkrjt | |d9 }t  | }|dkrdtj_t	dt |f t
jd P qW t  | j dkrdtj_t	d	 t	| j  | j  | j  d | _t   d S )
Ng{Gz?r>   rl   g      @TzPWarning -- multiprocessing.Manager still has %s active children after %s seconds)r  r   z@Warning -- Shared objects which still exist at manager shutdown:)r1   r2   r^   r   rr   r}   r   r   r  rn  r   r   r   r   r<   Z_number_of_objectsZ_debug_infory  rx   r  r  )rk   r  r5   rd  )r  r   r   r  $  s*    



zManagerMixin.tearDownClass)r7   r8   r9   rT   r   rd   rQ  operator
attrgetterr   r)  rB  rG  rM  rO  rW  rp   r  r   r  rt   r  r  r   r  r  r  rV  r   r   )r  r   r  	  s&   r  c               @   s   e Zd ZdZejjZejjZeejj	Z	eejj
Z
eejjZeejjZeejjZeejjZeejjZeejjZeejjZeejjZeejjZeejjZeejjZeejjZeejjZdS )ThreadsMixinr=   N)r7   r8   r9   rT   r   Zdummyrd   r  r<  rW   rr   r  r   r   r)  rB  rG  rM  rO  rW  rp   r  r   r  r   r   r   r   r  G  s$   r  c                sH  | d }t  }dddh}x| D ]\}}t|ts6q"t|tr|tkrJq"t|j|ksbt|jx|jD ]\}d|	  |dd   }||	 d  }	G dd	 d	||	t
j}
| |
_|
_||
_|
| |< qjW q"t|t
jr"G d
d	 d	|t}
| |
_|
_||
_|
| |< q"W d d g d g fdd} fdd}|| d< || d< d S )Nr7   r;   r=   r<   ZWithr>   Mixinc               @   s   e Zd ZdS )z*install_tests_in_module_dict.<locals>.TempN)r7   r8   r9   r   r   r   r   Tempn  s   r  c               @   s   e Zd ZdS )z*install_tests_in_module_dict.<locals>.TempN)r7   r8   r9   r   r   r   r   r  t  s   c                 s   t t t j  t jj  d< tj  d< t jddd< yt j	dd W n" t
k
rv   td Y nX tjdryt  } W n tk
r   tdY nX t  t  t  t d S )	Nr   r>   T)Z
allow_none)r  z start method not supportedZlinuxz1OSError raises on RLock creation, see issue 3111!)r   r  r  r   _cleanupr  r   r   r   r  r(   r)   r*   r   r   r  rG  r  r+   r   Zget_temp_dirr   r!  r%  )rE  )r  old_start_methodstart_methodr   r   setUpModule}  s$    

z1install_tests_in_module_dict.<locals>.setUpModulec                 s   d} t j  tjd dd ttjjt d  }|rZd} dt j_t	d| t
jd d }ttjt d  }|rd} dt j_t	d| t
jd d }| rtd	 tj  t j  d S )
NFr   T)r  z!Warning -- Dangling processes: %s)r  r>   zWarning -- Dangling threads: %sg      ?)r   r   r  r   r  r   r   r  r  rn  r   r   r   r1   r}   r  )Z
need_sleepr;   r=   )r  r  r   r   tearDownModule  s*    




z4install_tests_in_module_dict.<locals>.tearDownModuler  r  )globalsr   r   rs   
issubclassr:   r   rJ   rO   
capitalizer)   TestCaser7   r9   r8   r5  )Zremote_globsr  r8   Zlocal_globsZ	ALL_TYPESri   baseZtype_Znewnamer  r  r  r  r   )r  r  r  r   install_tests_in_module_dict_  s8    


r  )r  )r)   r   r  r  r1   r   r  r   r%   r   r  r   r  r  r  r+  rt  r  r   Ztest.supportr   Ztest.support.script_helperr   import_moduler   r   rZ  r   Zmultiprocessing.dummyZmultiprocessing.heapZmultiprocessing.managersra  Zmultiprocessing.queuesr   r   ZHAVE_SEND_HANDLEr  ImportErrorZmultiprocessing.sharedctypesr   r   rm  r  r   r   r   r   r"  r%  r   r?   r  r  r  r   ZHAVE_GETVALUEr   r5  r   r   r&   r  r  Zctypesr   r    r!   r"   r5  r+   r,   r:   rL   rM   rS   rd   r   r   r  r  r  rA  rJ  rQ  rn  rq  r  r  r  r  r  r  r  r  r  r	  r
  r  r(   r  r  r  rY  r[  r\  rd  rk  rl  r<  rm  rp  rq  rs  registerrw  r   r  r  r  r  r  r  r  r  r  r  r  r  rl  r  r  r  r  r  r  r  r  r-  r3  r6  r<  r=  rG  rH  rI  rJ  rK  rR  rW  rg  rj  rx  r}  r  r  r  r  r  r  r  r  r  ri  r  r  r  r   r   r   r   <module>   sl  








   [N  A6  *39   FS 

	  J(19
04  0R 	E;x+E"! 4 "BCF
I% p>