B
    |?XcB  ã               @   s„   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlmZmZ G dd„ deƒZG dd	„ d	ƒZG d
d„ dƒZdd„ ZdS )é    N)ÚProducer)ÚNodeManager)ÚEachSchedulingÚLoadSchedulingÚLoadScopeSchedulingÚLoadFileSchedulingÚLoadGroupScheduling)ÚEmptyÚQueuec               @   s   e Zd ZdZdS )ÚInterruptedz"signals an immediate interruption.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   ú-lib/python3.7/site-packages/xdist/dsession.pyr      s   r   c               @   s  e Zd ZdZdd„ Zedd„ ƒZdd„ Zej	dd	d
d„ ƒZ
ej	dd„ ƒZej	dd„ ƒZej	dd	dd„ ƒZej	dd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zej	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d0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Z d8d9„ Z!d:S );ÚDSessiona   A pytest plugin which runs a distributed test session

    At the beginning of the test session this creates a NodeManager
    instance which creates and starts all nodes.  Nodes then emit
    events processed in the pytest_runtestloop hook using the worker_*
    methods.

    Once a node is started it will automatically start running the
    pytest mainloop with some custom hooks.  This means a node
    automatically starts collecting tests.  Once tests are collected
    it will wait for instructions.
    c             C   s¢   || _ td|jjd| _d | _d | _d| _d| _| 	d¡| _
tƒ | _d | _i | _tƒ | _d| _t| j ƒ| _d | _|j d¡| _| jržt|ƒ| _|j | jd¡ d S )NZdsession)ZenabledFr   ÚmaxfailÚterminalreporterZterminaldistreporter)Úconfigr   ÚoptionÚdebugÚlogÚnodemanagerÚschedÚshuttingdownÚcountfailuresÚgetvaluer   r
   ÚqueueÚ_sessionÚ_failed_collection_errorsÚsetÚ_active_nodesÚ_failed_nodes_countÚget_default_max_worker_restartÚ_max_worker_restartÚ_summary_reportÚpluginmanagerÚ	getpluginÚterminalÚTerminalDistReporterÚtrdistÚregister)Úselfr   r   r   r   Ú__init__#   s$    
zDSession.__init__c             C   s   t | jo| j ƒS )z¾Return True if the distributed session has finished

        This means all nodes have executed all test items.  This is
        used by pytest_runtestloop to break out of its loop.
        )Úboolr   r"   )r-   r   r   r   Úsession_finished8   s    zDSession.session_finishedc             C   s$   | j r | jjjdkr | j  |¡ d S )Nr   )r)   r   r   ÚverboseÚ
write_line)r-   Úliner   r   r   Úreport_lineA   s    zDSession.report_lineT)Ztrylastc             C   s4   t | jƒ| _| jj| jjd}| j |¡ || _dS )zµCreates and starts the nodes.

        The nodes are setup to put their events onto self.queue.  As
        soon as nodes start they will emit the worker_workerready event.
        )ZputeventN)	r   r   r   Zsetup_nodesr   Úputr"   Úupdater   )r-   ÚsessionÚnodesr   r   r   Úpytest_sessionstartE   s    zDSession.pytest_sessionstartc             C   s&   t | ddƒ}|dk	r| ¡  d| _dS )zShutdown all nodes.r   N)ÚgetattrZteardown_nodesr   )r-   r7   Znmr   r   r   Úpytest_sessionfinishQ   s    zDSession.pytest_sessionfinishc             C   s   dS )NTr   )r-   r   r   r   Úpytest_collectionY   s    zDSession.pytest_collectionc             C   s(   |  d¡}tttttdœ}|| ||ƒS )NÚdist)ZeachÚloadZ	loadscopeZloadfileZ	loadgroup)r   r   r   r   r   r   )r-   r   r   r=   Z
schedulersr   r   r   Úpytest_xdist_make_scheduler^   s    
z$DSession.pytest_xdist_make_schedulerc             C   s`   | j jj| j | jd| _| jd k	s&t‚d| _x.| jsZ|  ¡  | jr.|  	¡  t
t| jƒƒ‚q.W dS )N)r   r   FT)r   Úhookr?   r   r   ÚAssertionErrorÚ
shouldstopr0   Ú	loop_onceÚtriggershutdownr   Ústr)r-   r   r   r   Úpytest_runtestloopj   s    zDSession.pytest_runtestloopc             C   sš   xF| j s|  ¡  tdƒ‚y| jjdd}P W q tk
rB   wY qX qW |\}}|s\t|ƒ‚d| }t| |ƒ}|  d||¡ |f |Ž | j	j
r–|  ¡  dS )z-Process one callback from one of the workers.z(Unexpectedly no active workers availableg       @)ZtimeoutZworker_zcalling methodN)r"   rD   ÚRuntimeErrorr   Úgetr	   rA   r:   r   r   Ztests_finished)r-   Z	eventcallZcallnameÚkwargsÚmethodZcallr   r   r   rC   y   s"    

zDSession.loop_oncec             C   sR   ||_ |jj|j d< |jj|j d< | jjj|d | jrB| ¡  n| j	 
|¡ dS )zEmitted when a node first starts up.

        This adds the node to the scheduler, nodes continue with
        collection without any further input.
        ÚidÚspec)ÚnodeN)Ú
workerinfoÚgatewayrK   rL   r   r@   Úpytest_testnodereadyr   Úshutdownr   Zadd_node)r-   rM   rN   r   r   r   Úworker_workerready’   s    
zDSession.worker_workerreadyc             C   st   | j jj|dd |jd dkr<d |¡| _|  |d¡ dS || jjkrd| j 	|¡}|rdt
||fƒ‚| j |¡ dS )zéEmitted when node executes its pytest_sessionfinish hook.

        Removes the node from the scheduler.

        The node might not be in the scheduler if it had not emitted
        workerready before shutdown was triggered.
        N)rM   ÚerrorZ
exitstatusé   z{} received keyboard-interruptzkeyboard-interrupt)r   r@   Úpytest_testnodedownZworkeroutputÚformatrB   Úworker_errordownr   r8   Úremove_noderA   r"   Úremove)r-   rM   Ú	crashitemr   r   r   Úworker_workerfinished¢   s    zDSession.worker_workerfinishedc             C   sd   | j  |¡ ydst|ƒ‚W nB tk
r^   ddlm} | ¡ }| ¡ }| jjj	||d Y nX dS )a  
        pytest_internalerror() was called on the worker.

        pytest_internalerror() arguments are an excinfo and an excrepr, which can't
        be serialized, so we go with a poor man's solution of raising an exception
        here ourselves using the formatted message.
        Fr   )ÚExceptionInfo)ÚexcreprÚexcinfoN)
r"   rY   rA   Z_pytest._coder\   Zfrom_currentZgetreprr   r@   Zpytest_internalerror)r-   rM   Zformatted_errorr\   r^   r]   r   r   r   Úworker_internal_error´   s    zDSession.worker_internal_errorc             C   sä   | j jj||d y| j |¡}W n tk
r6   Y nX |rH|  ||¡ |  jd7  _| jdk	oj| j| jk}|r²| jdkrŠd 	|j
j¡}n
d| j }|| _|  d| ¡ |  ¡  n"|  d|j
j ¡ d	| _|  |¡ | j |¡ dS )
z1Emitted by the WorkerController when a node dies.)rM   rS   é   Nr   z0worker {} crashed and worker restarting disabledz#maximum crashed workers reached: %dÚ
z
replacing crashed worker %sF)r   r@   rU   r   rX   ÚKeyErrorÚhandle_crashitemr#   r%   rV   rO   rK   r&   r4   rD   r   Ú_clone_noder"   rY   )r-   rM   rS   rZ   Zmaximum_reachedÚmsgr   r   r   rW   Æ   s,    




zDSession.worker_errordownc             C   s,   | j jjdkr(| jr(| dd | j¡¡ d S )Nr   ú=z	xdist: {})r   r   r1   r&   Z	write_seprV   )r-   r   r   r   r   Úpytest_terminal_summaryæ   s    z DSession.pytest_terminal_summaryc             C   s´   | j r
dS | jjj||d t|ƒ| j_| j ||¡ | j	rV| j
 |jjdt|ƒ ¡ | jjr°| j	r¦| jjs¦| j
 ¡  | j	 d¡ | jjjdkr¦| j	 d| jjj ¡ | j ¡  dS )a…  worker has finished test collection.

        This adds the collection for this node to the scheduler.  If
        the scheduler indicates collection is finished (i.e. all
        initial nodes have submitted their collections), then tells the
        scheduler to schedule the collected items.  When initiating
        scheduling the first time it logs which scheduler is in use.
        N)rM   Úidsz[%d]Ú r   zscheduling tests via %s)r   r   r@   Z%pytest_xdist_node_collection_finishedÚlenr   Ztestscollectedr   Zadd_node_collectionr)   r+   Ú	setstatusrO   rL   Zcollection_is_completedZhas_pendingÚensure_show_statusr2   r   r1   Ú	__class__r   Zschedule)r-   rM   rh   r   r   r   Úworker_collectionfinishë   s    	
z DSession.worker_collectionfinishc             C   s   | j jj||d dS )z;Emitted when a node calls the pytest_runtest_logstart hook.)ÚnodeidÚlocationN)r   r@   Zpytest_runtest_logstart)r-   rM   ro   rp   r   r   r   Úworker_logstart  s    zDSession.worker_logstartc             C   s   | j jj||d dS )z<Emitted when a node calls the pytest_runtest_logfinish hook.)ro   rp   N)r   r@   Zpytest_runtest_logfinish)r-   rM   ro   rp   r   r   r   Úworker_logfinish  s    zDSession.worker_logfinishc             C   s$   ||_ | jjj|d |  |¡ dS )z<Emitted when a node calls the pytest_runtest_logreport hook.)ÚreportN)rM   r   r@   Úpytest_runtest_logreportÚ_handlefailures)r-   rM   Úrepr   r   r   Úworker_testreport  s    zDSession.worker_testreportc             C   s   | j  |||¡ dS )zÙ
        Emitted when a node fires the 'runtest_protocol_complete' event,
        signalling that a test has completed the runtestprotocol and should be
        removed from the pending list in the scheduler.
        N)r   Zmark_test_complete)r-   rM   Z
item_indexZdurationr   r   r   Ú worker_runtest_protocol_complete  s    z)DSession.worker_runtest_protocol_completec             C   s   |j r
t‚|  ||¡ dS )zåEmitted when a node calls the pytest_collectreport hook.

        Because we only need the report when there's a failure/skip, as optimization
        we only expect to receive failed/skipped reports from workers (#330).
        N)ZpassedrA   Ú_failed_worker_collectreport)r-   rM   rv   r   r   r   Úworker_collectreport  s    
zDSession.worker_collectreportc             C   s$   t |||d}| jjjj|d dS )zOEmitted when a node calls the pytest_warning_captured hook (deprecated in 6.0).)Úwarning_messageÚwhenÚitem)rI   N)Údictr   r@   Zpytest_warning_capturedÚcall_historic)r-   r{   r|   r}   rI   r   r   r   Úworker_warning_captured&  s    z DSession.worker_warning_capturedc             C   s&   t ||||d}| jjjj|d dS )z;Emitted when a node calls the pytest_warning_recorded hook.)r{   r|   ro   rp   )rI   N)r~   r   r@   Zpytest_warning_recordedr   )r-   r{   r|   ro   rp   rI   r   r   r   Úworker_warning_recorded-  s    z DSession.worker_warning_recordedc             C   s>   |j j}d|_| jj |¡ | j || jj¡}| j	 
|¡ |S )a.  Return new node based on an existing one.

        This is normally for when a node dies, this will copy the spec
        of the existing node and create a new one with a new id.  The
        new node will have been setup so it will start calling the
        "worker_*" hooks and do work soon.
        N)rO   rL   rK   r   ÚgroupZallocate_idZ
setup_noder   r5   r"   Úadd)r-   rM   rL   r   r   r   rd   4  s    zDSession._clone_nodec             C   s6   |j | jkr2d| j|j < | jjj|d |  |¡ d S )NT)rs   )Zlongreprr    r   r@   Zpytest_collectreportru   )r-   rM   rv   r   r   r   ry   C  s    z%DSession._failed_worker_collectreportc             C   s6   |j r2|  jd7  _| jr2| j| jkr2d| j | _d S )Nr`   zstopping after %d failures)Úfailedr   r   rB   )r-   rv   r   r   r   ru   K  s    zDSession._handlefailuresc             C   s.   |   d¡ d| _x| jjD ]}| ¡  qW d S )Nztriggering shutdownT)r   r   r   r8   rQ   )r-   rM   r   r   r   rD   Q  s    
zDSession.triggershutdownc             C   sv   | j j d¡}| d¡d }d |jj|¡}| ||d |fdd|d¡}||_| j j	j
||| jd | j j	j|d	 d S )
NÚrunnerz::r   z&worker {!r} crashed while running {!r}r   r„   z???)rZ   rs   r   )rs   )r   r'   r(   ÚsplitrV   rO   rK   Z
TestReportrM   r@   Zpytest_handlecrashitemr   rt   )r-   ro   Zworkerr…   Úfspathre   rv   r   r   r   rc   W  s    
zDSession.handle_crashitemN)"r   r   r   r   r.   Úpropertyr0   r4   ÚpytestÚhookimplr9   r;   r<   r?   rF   rC   rR   r[   r_   rW   rg   rn   rq   rr   rw   rx   rz   r€   r   rd   ry   ru   rD   rc   r   r   r   r   r      s8   	 	r   c               @   sx   e Zd Zdd„ Zdd„ Zdd„ Zddd	„Zd
d„ Zddd„Ze	j
dd„ ƒZe	j
dd„ ƒZe	j
dd„ ƒZe	j
dd„ ƒZdS )r*   c             C   s8   || _ |j d¡| _i | _d| _t| jd| jjƒ| _d S )Nr   r   Úisatty)	r   r'   r(   ÚtrÚ_statusÚ_lastlenr:   Z	hasmarkupÚ_isatty)r-   r   r   r   r   r.   k  s
    zTerminalDistReporter.__init__c             C   s   | j  |¡ d S )N)rŒ   r2   )r-   re   r   r   r   r2   r  s    zTerminalDistReporter.write_linec             C   s   | j s|  |  ¡ ¡ d S )N)r   r2   Ú	getstatus)r-   r   r   r   rl   u  s    z'TerminalDistReporter.ensure_show_statusTc             C   s(   || j |j< |r$| jr$|  |  ¡ ¡ d S )N)r   rK   r   Úrewriter   )r-   rL   ÚstatusÚshowr   r   r   rk   y  s    
zTerminalDistReporter.setstatusc                s4   ˆ j jjdkr,‡ fdd„ˆ jD ƒ}d |¡S dS d S )Nr   c                s"   g | ]}d   |jˆ j|j ¡‘qS )z{} {})rV   rK   r   )Ú.0rL   )r-   r   r   ú
<listcomp>  s    z2TerminalDistReporter.getstatus.<locals>.<listcomp>z / zbringing up nodes...)r   r   r1   Ú_specsÚjoin)r-   Úpartsr   )r-   r   r   ~  s    
zTerminalDistReporter.getstatusFc             C   sN   |dt | jt|ƒ dƒ  }|r0d| _|d7 }n
t|ƒ| _| jj|dd d S )Nú r   ra   T)Zbold)ÚmaxrŽ   rj   rŒ   r‘   )r-   r3   ÚnewlineZpliner   r   r   r‘   ‡  s    

zTerminalDistReporter.rewritec             C   s@   || _ x|D ]}| j|ddd qW | j|ddd |  ¡  d S )NÚIF)r“   T)r–   rk   rl   )r-   ZspecsrL   r   r   r   Úpytest_xdist_setupnodes  s
    
z,TerminalDistReporter.pytest_xdist_setupnodesc             C   sZ   | j jjdkrH| ¡ }d|jd d…  }| jd|j|j||jf dd |  	|j
d¡ d S )Nr   z%s.%s.%sé   z[%s] %s Python %s cwd: %sT)r›   ÚC)r   r   r1   Z_rinfoÚversion_infor‘   rK   ÚplatformÚcwdrk   rL   )r-   rO   ZrinfoÚversionr   r   r   Úpytest_xdist_newgateway˜  s    z,TerminalDistReporter.pytest_xdist_newgatewayc             C   sR   | j jjdkr>|j}d |d |d  dd¡¡}| j|dd |  |jj	d	¡ d S )
Nr   z[{}] Python {}rK   r£   ra   z -- T)r›   Úok)
r   r   r1   rN   rV   Úreplacer‘   rk   rO   rL   )r-   rM   ÚdZinfoliner   r   r   rP   ¤  s    z)TerminalDistReporter.pytest_testnodereadyc             C   s"   |sd S |   d |jj|¡¡ d S )Nz[{}] node down: {})r2   rV   rO   rK   )r-   rM   rS   r   r   r   rU   ®  s    z(TerminalDistReporter.pytest_testnodedownN)T)F)r   r   r   r.   r2   rl   rk   r   r‘   r‰   rŠ   r   r¤   rP   rU   r   r   r   r   r*   j  s   
	
	
r*   c             C   s2   | j j}|dk	rt|ƒ}n| j jr.| j jd }|S )z¾gets the default value of --max-worker-restart option if it is not provided.

    Use a reasonable default to avoid workers from restarting endlessly due to crashing collections (#226).
    Né   )r   ZmaxworkerrestartÚintZnumprocesses)r   Úresultr   r   r   r$   µ  s    
r$   )r‰   Zxdist.remoter   Zxdist.workermanager   Zxdist.schedulerr   r   r   r   r   r   r	   r
   ÚKeyboardInterruptr   r   r*   r$   r   r   r   r   Ú<module>   s   	  WK