B
    u9a+                 @   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mZ d dlmZmZmZmZmZmZmZ d dlmZ d dlmZ dZdZdd Zd	d
 Zdd Zdd ZG dd dZe  ddZ!G dd de"Z#G dd dej$Z%dd Z&G dd dZ'dd Z(dS )    N)support)runtestINTERRUPTEDCHILD_ERRORPROGRESS_MIN_TIMEformat_test_result
TestResult	is_failed)setup_tests)format_durationg      >@c             C   s&   | j tkrdS |jr"t| |r"dS dS )NTF)resultr   Zfailfastr	   )r   ns r    /usr/lib/python3.7/runtest_mp.py	must_stop   s
    
r   c             C   s"   t | \}}tjf |}||fS )N)jsonloadstypesSimpleNamespace)worker_argsns_dict	test_namer   r   r   r   parse_worker_args$   s    r   c             C   sX   t |}|| f}t|}tjft dddd|f}tj|tj	tj	dt
jdktjdS )Nz-uz-mztest.regrtestz--worker-argsTnt)stdoutstderrZuniversal_newlinesZ	close_fdscwd)varsr   dumpssys
executabler   Zargs_from_interpreter_flags
subprocessPopenPIPEosnameZSAVEDCWD)Ztestnamer   r   r   cmdr   r   r   run_test_in_subprocess*   s    

r'   c             C   s<   t |  t| |}t  ttt|dd td d S )NT)flushr   )r
   r   printr   r   listr   exit)r   r   r   r   r   r   run_tests_worker?   s
    
r,   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )MultiprocessIteratorz8A thread-safe iterator over tests for multiprocess mode.c             C   s   t  | _|| _d S )N)	threadingZLocklock
tests_iter)selfr0   r   r   r   __init__P   s    
zMultiprocessIterator.__init__c             C   s   | S )Nr   )r1   r   r   r   __iter__T   s    zMultiprocessIterator.__iter__c          	   C   s*   | j  | jd krtt| jS Q R X d S )N)r/   r0   StopIterationnext)r1   r   r   r   __next__W   s    
zMultiprocessIterator.__next__c          	   C   s   | j  d | _W d Q R X d S )N)r/   r0   )r1   r   r   r   stop]   s    zMultiprocessIterator.stopN)__name__
__module____qualname____doc__r2   r3   r6   r7   r   r   r   r   r-   L   s
   r-   MultiprocessResultzresult stdout stderr error_msgc               @   s   e Zd ZdS )
ExitThreadN)r8   r9   r:   r   r   r   r   r=   e   s   r=   c                   s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )MultiprocessThreadc                s8   t    || _|| _|| _d | _d | _d | _d| _d S )NF)	superr2   pendingoutputr   current_test_name
start_time_popen_killed)r1   r@   rA   r   )	__class__r   r   r2   j   s    
zMultiprocessThread.__init__c             C   s\   dg}| j }|  r|d |r2|d|  | j}|rN|d|j  dd| S )Nr>   Zaliveztest=zpid=z<%s> )rB   is_aliveappendrD   pidjoin)r1   infotestpopenr   r   r   __repr__t   s    
zMultiprocessThread.__repr__c             C   s8   d| _ | j}|d krd S |  |j  |j  d S )NT)rE   rD   killr   closer   )r1   rN   r   r   r   rP      s    
zMultiprocessThread.killc          
   C   sz  zt  | _|| _t|| j| _| j}|n yH| jr@|   t	y|
 \}}W n  tk
rp   | jrjt	 Y nX W n   |   |   Y nX W d Q R X | }W d d | _d | _X | }| }d }|dkrd| }nh|d\}}}| }|sd}nDyt|}t| }W n, tk
rD }	 zd|	 }W d d }	~	X Y nX |d k	rlt  | j }
t|t|
d }t||||S )Nr   zExit code %s
zFailed to parse worker stdoutzFailed to parse worker JSON: %s)time	monotonicrC   rB   r'   r   rD   rE   rP   r=   ZcommunicateOSErrorwaitstriprstrip
rpartitionr   r   r   	Exceptionr   r<   )r1   r   rN   r   r   ZretcodeZerr_msg_r   exc	test_timer   r   r   _runtest   sR    



zMultiprocessThread._runtestc             C   s   x| j syTyt| j}W n tk
r.   P Y nX | |}| jd|f t|j| j	rZP W q t
k
rr   P Y q tk
r   | jdt f P Y qX qW d S )NFT)rE   r5   r@   r4   r^   rA   Zputr   r   r   r=   BaseException	traceback
format_exc)r1   r   	mp_resultr   r   r   run   s    
zMultiprocessThread.run)	r8   r9   r:   r2   rO   rP   r^   rc   __classcell__r   r   )rF   r   r>   i   s
   
;r>   c             C   sR   g }xH| D ]@}|j }|sq
t |j }|tkr
d|t|f }|| q
W |S )Nz%s (%s))rB   rS   rT   rC   r   r   rI   )workersrunningworkerrB   dttextr   r   r   get_running   s    
rj   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )MultiprocessRunnerc             C   sR   || _ |j| _t | _t| j j| _| jjd k	rB| jjd | _	nd | _	d | _
d S )Ng      ?)regrtestr   queueZQueuerA   r-   Ztestsr@   timeouttest_timeoutre   )r1   rl   r   r   r   r2      s    
zMultiprocessRunner.__init__c                sJ    fddt  jjD  _tdt j  x jD ]}|  q6W d S )Nc                s   g | ]}t  j j jqS r   )r>   r@   rA   r   ).0r[   )r1   r   r   
<listcomp>   s   z4MultiprocessRunner.start_workers.<locals>.<listcomp>z.Run tests in parallel using %s child processes)ranger   Zuse_mpre   r)   lenstart)r1   rg   r   )r1   r   start_workers   s    
z MultiprocessRunner.start_workersc             C   s   t  }x| jD ]}|  qW xZ| jD ]P}xJ|d | sBP t  | }td||f  |tkr.td|  P q.W q(W d S )Ng      ?z(Wait for regrtest worker %r for %.1f secz.Warning -- failed to join a regrtest worker %s)rS   rT   re   rP   rK   rH   r)   JOIN_TIMEOUT)r1   rC   rg   rh   r   r   r   wait_workers   s    
zMultiprocessRunner.wait_workersc             C   s   t dd | jD s:y| jjddS  tjk
r8   d S X xz| jd k	rVtj| jdd t	t
t}y| jj|dS  tjk
r   Y nX t| j}|r<| jjs<tdd| dd	 q<W d S )
Nc             s   s   | ]}|  V  qd S )N)rH   )rp   rg   r   r   r   	<genexpr>  s    z1MultiprocessRunner._get_result.<locals>.<genexpr>r   )rn   T)r+   zrunning: %sz, )r(   )anyre   rA   getrm   ZEmptyro   faulthandlerZdump_traceback_latermaxPROGRESS_UPDATEr   rj   r   pgor)   rK   )r1   rn   rf   r   r   r   _get_result  s     


zMultiprocessRunner._get_resultc             C   s   |j }t|}|jd k	r(|d|j 7 }n$|jtkrL| jjsL|dt|j 7 }t| j	}|rt| jjst|dd
| 7 }| j| j| d S )Nz (%s)z -- running: %sz, )r   r   Z	error_msgr]   r   r   r~   r   rj   re   rK   rl   Zdisplay_progress
test_index)r1   rb   r   ri   rf   r   r   r   display_result$  s    

z!MultiprocessRunner.display_resultc             C   s   |d r*|d }t d| tjdd dS |  jd7  _|d }| j|j | | |jrlt |jdd |jr| j	j
st |jtjdd t|j| j	rdS dS )Nr      zregrtest worker thread failed: T)filer(   )r(   F)r)   r   r   r   rl   Zaccumulate_resultr   r   r   r   r~   r   )r1   itemra   rb   r   r   r   _process_result2  s     

z"MultiprocessRunner._process_resultc             C   s   |    d| _zTy,x&|  }|d kr&P | |}|rP qW W n" tk
r^   t  d| j_Y nX W d | jd k	rvt	
  X | j  |   d S )Nr   T)ru   r   r   r   KeyboardInterruptr)   rl   Zinterruptedro   r{   Zcancel_dump_traceback_laterr@   r7   rw   )r1   r   r7   r   r   r   	run_testsI  s"    




zMultiprocessRunner.run_testsN)
r8   r9   r:   r2   ru   rw   r   r   r   r   r   r   r   r   rk      s   rk   c             C   s   t |   d S )N)rk   r   )rl   r   r   r   run_tests_multiprocessb  s    r   ))collectionsr{   r   r$   rm   r!   r   r.   rS   r`   r   rM   r   Ztest.libregrtest.runtestr   r   r   r   r   r   r	   Ztest.libregrtest.setupr
   Ztest.libregrtest.utilsr   r}   rv   r   r   r'   r,   r-   
namedtupler<   rZ   r=   ZThreadr>   rj   rk   r   r   r   r   r   <module>   s8   $rz