o
    %ifX                     @  sL  d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlZd dl	m
Z
 d dl	mZ d dlZd dlZd dlZd d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ G dd deZG dd dZG dd deZ G dd dZ!d'd d!Z"d(d%d&Z#dS ))    )annotations)auto)Enum)Empty)QueueN)Any)Sequence)Producer)
WorkerInfo)EachScheduling)LoadFileScheduling)LoadGroupScheduling)LoadScheduling)LoadScopeScheduling)
Scheduling)WorkStealingScheduling)NodeManager)WorkerControllerc                   @  s   e Zd ZdZdS )Interruptedz"signals an immediate interruption.N)__name__
__module____qualname____doc__ r   r   G/var/www/html/corbot_env/lib/python3.10/site-packages/xdist/dsession.pyr      s    r   c                   @  s\  e Zd ZU dZded< dedd	ZedfddZdgddZe	j
dddhddZe	j
diddZe	j
dfddZe	j
dddjddZe	j
dfd d!Zdid"d#Zdkd(d)Zdld*d+Zdmd-d.Zdnd1d2Ze	j
dod5d6Zdpd9d:Zdqd>d?Zdqd@dAZdrdDdEZdsdJdKZdtdNdOZdudQdRZdvdWdXZdwdYdZZdud[d\Zdxd]d^Z did_d`Z!dydbdcZ"ddS )z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.
    z
bool | str
shouldstopconfigpytest.ConfigreturnNonec                 C  s   || _ td|jjd| _d | _d | _d| _d| _|	d| _
t | _d | _i | _t | _d| _t| j | _d | _|jd| _| jrQt|| _|j| jd d S d S )Ndsession)enabledFr   maxfailterminalreporterterminaldistreporter)r   r	   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__0   s&   
zDSession.__init__boolc                 C  s   t | jo| j S )zReturn 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.
        )r@   r+   r2   r>   r   r   r   session_finishedE   s   zDSession.session_finishedlinestrc                 C  s,   | j r| jjjdkr| j | d S d S d S )Nr   )r9   r   r&   verbose
write_liner>   rC   r   r   r   report_lineN   s   zDSession.report_lineT)trylastsessionpytest.Sessionc                 C  s4   t | j| _| jj| jjd}| j| || _dS )zCreates 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.
        )puteventN)	r   r   r)   setup_nodesr.   putr2   updater/   )r>   rJ   nodesr   r   r   pytest_sessionstartR   s   
zDSession.pytest_sessionstartc                 C  s&   t | dd}|dur|  d| _dS )zShutdown all nodes.r)   N)getattrteardown_nodesr/   )r>   nmr   r   r   pytest_sessionfinish^   s   
zDSession.pytest_sessionfinishc                 C  s   dS )NTr   rA   r   r   r   pytest_collectionf   s   zDSession.pytest_collectionr(   r	   Scheduling | Nonec                 C  sz   | d}|dkrt||S |dkrt||S |dkr t||S |dkr)t||S |dkr2t||S |dkr;t||S d S )Ndisteachload	loadscopeloadfile	loadgroup	worksteal)r-   r   r   r   r   r   r   )r>   r   r(   rX   r   r   r   pytest_xdist_make_schedulerk   s   






z$DSession.pytest_xdist_make_schedulerc                 C  sl   | j jj| j | jd| _| jd usJ d| _d }| js0|   | jr-|   t	t
| j}| jr|r4|dS )N)r   r(   FT)r   hookr_   r(   r*   r   rB   	loop_oncetriggershutdownr   rD   )r>   pending_exceptionr   r   r   pytest_runtestloop   s   zDSession.pytest_runtestloopc                 C  s   	 | j s|   tdz	| jjdd}W n	 ty   Y q w |\}}|s)J |d| }t| |}| d|| |di | | jdusGJ | jj	rQ|   dS dS )	z-Process one callback from one of the workers.   z(Unexpectedly no active workers availableg       @)timeoutworker_zcalling methodNr   )
r2   rb   RuntimeErrorr.   getr   rR   r(   r*   tests_finished)r>   	eventcallcallnamekwargsmethodcallr   r   r   ra      s(   
zDSession.loop_oncenoder   
workerinfor
   c                 C  sb   ||_ |jj|j d< |jj|j d< | jjj|d | jr"|  dS | j	dus)J | 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)rp   N)rq   gatewayrr   rs   r   r`   pytest_testnodereadyr+   shutdownr*   add_node)r>   rp   rq   r   r   r   worker_workerready   s   
zDSession.worker_workerreadyc                 C  s   | j jj|dd |jd dkr| d| _| |d dS |jd }|jd }||fD ]}|r8| js6|| _ nq,| jdus@J || jjv rT| j|}|rTJ ||f| j	
| dS )	zEmitted 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rp   error
exitstatus   z received keyboard-interruptzkeyboard-interrupt
shouldfailr   )r   r`   pytest_testnodedownworkeroutputr   worker_errordownr*   rP   remove_noder2   remove)r>   rp   r}   r   shouldx	crashitemr   r   r   worker_workerfinished   s$   

zDSession.worker_workerfinishedformatted_errorc                 C  sN   | j | zJ | ty&   tj }| }| jjj	||d Y dS w )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.
        F)excreprexcinfoN)
r2   r   AssertionErrorpytestExceptionInfofrom_currentgetreprr   r`   pytest_internalerror)r>   rp   r   r   r   r   r   r   worker_internal_error   s   

zDSession.worker_internal_errorrz   object | Nonec                 C  s   | j jj||d | jdusJ z| j|}W n	 ty!   Y n	w |r*| || |  jd7  _| jduo;| j| jk}|ra| jdkrLd|j	j
 d}nd| j }|| _| d|  |   n| d	|j	j
  d
| _| | | j| dS )z1Emitted by the WorkerController when a node dies.ry   Nre   r   worker z' crashed and worker restarting disabledz!maximum crashed workers reached: 
z
replacing crashed worker %sF)r   r`   r~   r*   r   KeyErrorhandle_crashitemr3   r5   rt   rr   r6   rH   rb   r+   _clone_noder2   r   )r>   rp   rz   r   maximum_reachedmsgr   r   r   r      s0   




zDSession.worker_errordownr$   r   c                 C  s4   | j jjdkr| jr|dd| j  d S d S d S )Nr   =zxdist: )r   r&   rE   r6   	write_sep)r>   r$   r   r   r   pytest_terminal_summary  s   z DSession.pytest_terminal_summaryidsSequence[str]c                 C  s   | j rdS | jjj||d | jdusJ t|| j_| jdus"J | j|| | j	r:| j
j|jjtjt|d | jjrj| j	rc| jjsc| j
  | j	d | jjjdkrc| j	d| jjj  | j  dS 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)rp   r   tests_collected r   zscheduling tests via )r+   r   r`   %pytest_xdist_node_collection_finishedr/   lentestscollectedr*   add_node_collectionr9   r;   	setstatusrt   rs   WorkerStatusCollectionDonecollection_is_completedhas_pendingensure_show_statusrF   r&   rE   	__class__r   schedule)r>   rp   r   r   r   r   worker_collectionfinish  s*   
z DSession.worker_collectionfinishnodeidlocationtuple[str, int | None, str]c                 C     | j jj||d dS )z;Emitted when a node calls the pytest_runtest_logstart hook.r   r   N)r   r`   pytest_runtest_logstartr>   rp   r   r   r   r   r   worker_logstart4     zDSession.worker_logstartc                 C  r   )z<Emitted when a node calls the pytest_runtest_logfinish hook.r   N)r   r`   pytest_runtest_logfinishr   r   r   r   worker_logfinish=  r   zDSession.worker_logfinishreppytest.TestReportc                 C  s$   ||_ | jjj|d | | dS )z<Emitted when a node calls the pytest_runtest_logreport hook.reportN)rp   r   r`   pytest_runtest_logreport_handlefailuresr>   rp   r   r   r   r   worker_testreportF  s   zDSession.worker_testreport
item_indexintdurationfloatc                 C  s"   | j dusJ | 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*   mark_test_complete)r>   rp   r   r   r   r   r    worker_runtest_protocol_completeL  s   z)DSession.worker_runtest_protocol_completeindicesSequence[int]c                 C  s    | j dusJ | j || dS )aO  
        Emitted when a node fires the 'unscheduled' event, signalling that
        some tests have been removed from the worker's queue and should be
        sent to some worker again.

        This should happen only in response to 'steal' command, so schedulers
        not using 'steal' command don't have to implement it.
        N)r*   remove_pending_tests_from_node)r>   rp   r   r   r   r   worker_unscheduledW  s   zDSession.worker_unscheduled(pytest.CollectReport | pytest.TestReportc                 C  s   |j rJ | || dS )zEmitted 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)passed_failed_worker_collectreportr   r   r   r   worker_collectreporte  s   

zDSession.worker_collectreportwarning_messagewarnings.WarningMessagewhentuple[str, int, str] | Nonec                 C  s&   t ||||d}| jjjj|d dS )z;Emitted when a node calls the pytest_warning_recorded hook.)r   r   r   r   )rm   N)dictr   r`   pytest_warning_recordedcall_historic)r>   r   r   r   r   rm   r   r   r   worker_warning_recordedr  s   z DSession.worker_warning_recordedc                 C  sL   |j j}d|_| jdusJ | 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)rt   rs   rr   r)   groupallocate_id
setup_noder.   rN   r2   add)r>   rp   rs   cloner   r   r   r     s   zDSession._clone_nodec                 C  s:   |j | jvrd| j|j < | jjj|d | | d S d S )NTr   )longreprr0   r   r`   pytest_collectreportr   r   r   r   r   r     s
   z%DSession._failed_worker_collectreportc                 C  sP   |j r |  jd7  _| jr"| j| jkr$| js&d| j d| _d S d S d S d S d S )Nre   zstopping after z	 failures)failedr,   r#   r   )r>   r   r   r   r   r     s   zDSession._handlefailuresc                 C  sB   | j s| d d| _ | jd usJ | jjD ]}|  qd S d S )Nztriggering shutdownT)r+   r(   r*   rP   rv   )r>   rp   r   r   r   rb     s   

zDSession.triggershutdownworkerc                 C  sn   | dd }d|jjd|}tj||d |fi d|dd}||_| jjj||| j	d | jjj
|d	 d S )
Nz::r   r   z crashed while running r   z???)r   r   keywordsoutcomer   r   )r   r   r*   r   )splitrt   rr   r   
TestReportrp   r   r`   pytest_handlecrashitemr*   r   )r>   r   r   fspathr   r   r   r   r   r     s"   zDSession.handle_crashitemNr   r   r   r    )r   r@   )rC   rD   r   r    )rJ   rK   r   r    r   r    )r   r   r(   r	   r   rW   )rp   r   rq   r
   r   r    rp   r   r   r    )rp   r   r   rD   r   r    )rp   r   rz   r   r   r    )r$   r   r   r    )rp   r   r   r   r   r    )rp   r   r   rD   r   r   r   r    )rp   r   r   r   r   r    )rp   r   r   r   r   r   r   r    )rp   r   r   r   r   r    )rp   r   r   r   r   r    )
r   r   r   rD   r   rD   r   r   r   r    )rp   r   r   r   )r   r   r   r    )r   rD   r   r   r   r    )#r   r   r   r   __annotations__r?   propertyrB   rH   r   hookimplrQ   rU   rV   r_   rd   ra   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rb   r   r   r   r   r   r       sJ   
 










"
	
	







r   c                   @  s(   e Zd ZdZe Ze Ze Ze ZdS )r   z1Status of each worker during creation/collection.N)	r   r   r   r   r   CreatedInitializedReadyForCollectionr   r   r   r   r   r     s    
r   c                   @  s   e Zd Zd1ddZd2d	d
Zd3ddZddd4ddZd5ddZd6d7ddZe	j
d8d"d#Ze	j
d9d&d'Ze	j
d:d*d+Ze	j
d;d.d/Zd0S )<r:   r   r   r   r    c                 C  s8   || _ |jd| _i | _d| _t| jd| jj| _d S )Nr$   r   isatty)	r   r7   r8   tr_status_lastlenrR   	hasmarkup_isattyr=   r   r   r   r?     s
   zTerminalDistReporter.__init__r   rD   c                 C  s   | j | d S N)r   rF   )r>   r   r   r   r   rF     s   zTerminalDistReporter.write_linec                 C  s   | j s| |   d S d S r   )r   rF   	getstatusrA   r   r   r   r     s   z'TerminalDistReporter.ensure_show_statusT)showrs   execnet.XSpecstatusr   r   r   r   r@   c                C  s4   ||f| j |j< |r| jr| |   d S d S d S r   )r   rr   r   rewriter   )r>   rs   r   r   r   r   r   r   r     s   
zTerminalDistReporter.setstatusc                 C  s,   | j jjdkrtt| j }|r|S dS )Nr   zbringing up nodes...)r   r&   rE   get_workers_status_linelistr   valuesrG   r   r   r   r     s
   zTerminalDistReporter.getstatusFrC   newlinec                 C  sN   |dt | jt| d  }|rd| _|d7 }nt|| _| jj|dd d S )N r   r   T)bold)maxr   r   r   r   )r>   rC   r   pliner   r   r   r     s   

zTerminalDistReporter.rewritespecsSequence[execnet.XSpec]c                 C  sD   || _ |D ]}| j|tjddd q| j|tjddd |   d S )Nr   F)r   r   T)_specsr   r   r   r   )r>   r  rs   r   r   r   pytest_xdist_setupnodes  s
   z,TerminalDistReporter.pytest_xdist_setupnodesrt   execnet.Gatewayc              	   C  s~   | j jjdkr3| }|jtjk}|r3dj|jd d  }| jd|j	 d|j
 d| d|j dd	 | j|jtjdd
 d S )Nr   z{}.{}.{}   [z] z Python z cwd: Tr   r   )r   r&   rE   _rinfo
executablesysformatversion_infor   rr   platformcwdr   rs   r   r   )r>   rt   rinfodifferent_interpreterversionr   r   r   pytest_xdist_newgateway  s    z,TerminalDistReporter.pytest_xdist_newgatewayrp   r   c                 C  sp   | j jjdkr+|j}|dtjk}|r+|d dd}| jd|d  d| d	d
 | j	|j
jtjdd d S )Nr   r  r  r   z -- r  rr   z	] Python Tr	  r   )r   r&   rE   rq   ri   r  r  replacer   r   rt   rs   r   r   )r>   rp   dr  r  r   r   r   ru     s   
z)TerminalDistReporter.pytest_testnodereadyrz   objectc                 C  s&   |sd S |  d|jj d|  d S )Nr  z] node down: )rF   rt   rr   )r>   rp   rz   r   r   r   r~   $  s   z(TerminalDistReporter.pytest_testnodedownNr   )r   rD   r   r    r   )
rs   r   r   r   r   r   r   r@   r   r    )r   rD   )F)rC   rD   r   r@   r   r    )r  r  r   r    )rt   r  r   r    r   )rp   r   rz   r  r   r    )r   r   r   r?   rF   r   r   r   r   r   r   r  r  ru   r~   r   r   r   r   r:     s     




	r:   r   r   r   
int | Nonec                 C  s<   | j j}|durt|}|S | j jr| j jd }|S d}|S )zGets 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&   maxworkerrestartr   numprocesses)r   
result_strresultr   r   r   r4   +  s   r4   status_and_items"Sequence[tuple[WorkerStatus, int]]rD   c                 C  s2  dd | D }t |}|dkrdnd}| r<tdd |D r<| d }|\}}|dkr,d	nd
}| d| d| d| dS tj|v rUtdd | D }d| d| d| S tj|v rk|tj}	d|	 d| d| S tj|v r|tj}
d|
 d| d| S tj|v r|tj}d| d| d| S dS )z
    Return the line to display during worker setup/collection based on the
    status of the workers and number of tests collected for each.
    c                 S  s   g | ]\}}|qS r   r   .0scr   r   r   
<listcomp>B  s    z+get_workers_status_line.<locals>.<listcomp>re   r   workersc                 s  s    | ]}|t jkV  qd S r   )r   r   )r!  r"  r   r   r   	<genexpr>E  s    z*get_workers_status_line.<locals>.<genexpr>r   itemitemsr   z []c                 s  s     | ]\}}|d krdV  qdS )r   re   Nr   r   r   r   r   r&  M  s    zcollecting: /zready: zinitialized: z	created: r   )	r   allr   r   sumr   countr   r   )r  statusestotal_workersworkers_nounfirstr   r   
tests_noundonereadyinitializedcreatedr   r   r   r   ;  s*   



r   )r   r   r   r  )r  r  r   rD   )$
__future__r   enumr   r   r.   r   r   r  typingr   r   warningsexecnetr   xdist.remoter	   r
   xdist.schedulerr   r   r   r   r   r   r   xdist.workermanager   r   KeyboardInterruptr   r   r   r:   r4   r   r   r   r   r   <module>   s>       *
T