
    YfU-                        d Z ddlmZ ddlmZmZmZmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZ ddlmZmZ ddlmZ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#m"Z"  e	d          Z$dede%de%de%dee%         ddfdZ&dede%de%de%dee%         ddfdZ'dede%de%de%ddf
dZ(e G d d                       Z) G d! d"e          Z*dS )#zZ
Test reporter forwarding test results over trial distributed AMP commands.

@since: 12.3
    )TracebackType)CallableListOptionalSequenceTypeTypeVar)TestCase)Factorydefine)Literal)DeferredmaybeDeferred)AMPMAX_VALUE_LENGTH)Failure)qual)managercommands)
TestResult   )TrialFailure   )chunkstreamTamptestName
errorClasserrorframesreturnNc                   K   t          | t          |                    d          t                               d{V }t          | d |D                        d{V }|                     t
          j        ||||           d{V  dS )a/  
    Send an error to the worker manager over an AMP connection.

    First the pieces which can be large are streamed over the connection.
    Then, L{managercommands.AddError} is called with the rest of the
    information and the stream IDs.

    :param amp: The connection to use.
    :param testName: The name (or ID) of the test the error relates to.
    :param errorClass: The fully qualified name of the error type.
    :param error: The string representation of the error.
    :param frames: The lines of the traceback associated with the error.
    utf-8Nc              3   @   K   | ]}|                     d           V  dS r#   Nencode.0frames     T/var/www/html/env/lib/python3.11/site-packages/twisted/trial/_dist/workerreporter.py	<genexpr>zaddError.<locals>.<genexpr>1   .      'R'R%W(=(='R'R'R'R'R'R    )r   r   errorStreamIdframesStreamId)r   r   r'   r   
callRemoter   AddError)r   r   r   r   r    r/   r0   s          r+   addErrorr3      s      " !eELL,A,ACS&T&TUUUUUUUUM!#'R'R6'R'R'RSSSSSSSSN
.. #%            r.   fail	failClassc                   K   t          | t          |                    d          t                               d{V }t          | d |D                        d{V }|                     t
          j        ||||           d{V  dS )a[  
    Like L{addError} but for failures.

    :param amp: See L{addError}
    :param testName: See L{addError}
    :param failClass: The fully qualified name of the exception associated
        with the failure.
    :param fail: The string representation of the failure.
    :param frames: The lines of the traceback associated with the error.
    r#   Nc              3   @   K   | ]}|                     d           V  dS r%   r&   r(   s     r+   r,   zaddFailure.<locals>.<genexpr>J   r-   r.   )r   r5   failStreamIdr0   )r   r   r'   r   r1   r   
AddFailure)r   r   r4   r5   r    r8   r0   s          r+   
addFailurer:   <   s        U4;;w+?+?AQ%R%RSSSSSSSSL!#'R'R6'R'R'RSSSSSSSSN
.."!%            r.   todoc                    K   t          | t          |                    d          t                               d{V }|                     t
          j        |||           d{V  dS )z
    Like L{addError} but for expected failures.

    :param amp: See L{addError}
    :param testName: See L{addError}
    :param error: The string representation of the expected failure.
    :param todo: The string description of the expectation.
    r#   N)r   r/   r;   )r   r   r'   r   r1   r   AddExpectedFailure)r   r   r   r;   r/   s        r+   addExpectedFailurer>   U   s       !eELL,A,ACS&T&TUUUUUUUUM
..*#	            r.   c            	           e Zd ZU dZded<    ee          Zee	e
                  ed<   dee	e
                  fdZdee         ded	eded
         fdZde	e
         ddfdZdS )ReportingResultsa  
    A mutable container for the result of sending test results back to the
    parent process.

    Since it is possible for these sends to fail asynchronously but the
    L{TestResult} protocol is not well suited for asynchronous result
    reporting, results are collected on an instance of this class and when the
    runner believes the test is otherwise complete, it can collect the results
    and do something with any errors.

    :ivar _reporter: The L{WorkerReporter} this object is associated with.
        This is the object doing the result reporting.

    :ivar _results: A list of L{Deferred} instances representing the results
        of reporting operations.  This is expected to grow over the course of
        the test run and then be inspected by the runner once the test is
        over.  The public interface to this list is via the context manager
        interface.
    WorkerReporter	_reporter_resultsr!   c                     | j         S )a  
        Begin a new reportable context in which results can be collected.

        :return: A sequence which will contain the L{Deferred} instances
            representing the results of all test result reporting that happens
            while the context manager is active.  The sequence is extended as
            the test runs so its value should not be consumed until the test
            is over.
        )rC   selfs    r+   	__enter__zReportingResults.__enter__   s     }r.   excTypeexcValueexcTracebackFc                     d| j         _        dS )z-
        End the reportable context.
        NF)rB   
_reporting)rF   rH   rI   rJ   s       r+   __exit__zReportingResults.__exit__   s     %)!ur.   resultNc                 :    | j                             |           dS )zi
        Record a L{Deferred} instance representing one test result reporting
        operation.
        N)rC   append)rF   rN   s     r+   recordzReportingResults.record   s     
 	V$$$$$r.   )__name__
__module____qualname____doc____annotations__r   listrC   r   r   objectr   rG   r   BaseExceptionr   r   rM   rQ    r.   r+   r@   r@   h   s          (  '.wt}}Hd8F#$444
8HV$45 
 
 
 

m$
  
 $	

 

 
 
 
%Xf- %$ % % % % % %r.   r@   c                   *    e Zd ZU dZdZeed<   dZee	         ed<    fdZ
de	fdZd	edefd
Zdedee         fdZdeg ef         ddfdZdeddf fdZdededdfdZded	eddf fdZdededdf fdZ fdZd Zd fd	Zd fd	Zd Z xZS )rA   a  
    Reporter for trial's distributed workers. We send things not through a
    stream, but through an C{AMP} protocol's C{callRemote} method.

    @ivar _DEFAULT_TODO: Default message for expected failures and
        unexpected successes, used only if a C{Todo} is not provided.

    @ivar _reporting: When a "result reporting" context is active, the
        corresponding context manager.  Otherwise, L{None}.
    zTest expected to failampProtocolNrL   c                 V    t                                                       || _        dS )z
        @param ampProtocol: The communication channel with the trial
            distributed manager which collects all test results.
        N)super__init__r\   )rF   r\   	__class__s     r+   r_   zWorkerReporter.__init__   s)    
 	&r.   r!   c                 8    t          |           | _        | j        S )a  
        Get a "result reporting" context manager.

        In a "result reporting" context, asynchronous test result reporting
        methods may be used safely.  Their results (in particular, failures)
        are available from the context manager.
        )r@   rL   rE   s    r+   gatherReportingResultsz%WorkerReporter.gatherReportingResults   s     +400r.   r   c                 v    t          |t                    r#t          |d         |d         |d                   S |S )zX
        Convert a C{sys.exc_info()}-style tuple to a L{Failure}, if necessary.
        r   r   r   )
isinstancetupler   )rF   r   s     r+   _getFailurezWorkerReporter._getFailure   s9     eU## 	958U1XuQx888r.   failurec           	          g }|j         D ]9}|                    |d         |d         t          |d                   g           :|S )z<
        Extract frames from a C{Failure} instance.
        r   r   r   )r    extendstr)rF   rg   r    r*   s       r+   
_getFrameszWorkerReporter._getFrames   sO     ^ 	? 	?E MM58U1Xs58}}=>>>>r.   fc                     | j         )| j                             t          |                     dS t          d          )a  
        Call L{f} if and only if a "result reporting" context is active.

        @param f: A function to call.  Its result is accumulated into the
            result reporting context.  It may return a L{Deferred} or a
            coroutine or synchronously raise an exception or return a result
            value.

        @raise ValueError: If no result reporting context is active.
        Nz9Cannot call command outside of reporting context manager.)rL   rQ   r   
ValueError)rF   rl   s     r+   _callzWorkerReporter._call   sE     ?&O""=#3#344444K  r.   testc                      t                                          |           |                                                      fd           dS )z
        Send a success to the parent process.

        This must be called in context managed by L{gatherReportingResults}.
        c                  P     j                             t          j                  S )N)r   )r\   r1   r   
AddSuccess)rF   r   s   r+   <lambda>z+WorkerReporter.addSuccess.<locals>.<lambda>   s'    D$//*X 0   r.   N)r^   
addSuccessidro   )rF   rp   r   r`   s   ` @r+   ru   zWorkerReporter.addSuccess   se     	4   7799

    	
 	
 	
 	
 	
r.   r   errorObjc                    K   |                      |          }|                                }t          |j                  }|                     |          }t          | j        ||||           d{V  dS )a"  
        Attempt to report an error to the parent process.

        Unlike L{addError} this can fail asynchronously.  This version is for
        infrastructure code that can apply its own failure handling.

        @return: A L{Deferred} that fires with the result of the attempt.
        N)rf   getErrorMessager   typerk   r3   r\   )rF   r   rw   rg   errorStrr   r    s          r+   addErrorFalliblezWorkerReporter.addErrorFallible   s       ""8,,**,,',''
))
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r.   c                      t                                          |           |                                                      fd           dS )z6
        Send an error to the parent process.
        c                  0                                    S N)r|   )r   rF   r   s   r+   rt   z)WorkerReporter.addError.<locals>.<lambda>  s    4005AA r.   N)r^   r3   rv   ro   )rF   rp   r   r   r`   s   ` `@r+   r3   zWorkerReporter.addError  sW     	u%%%7799

AAAAAABBBBBr.   r4   c                 Z    t                                          ||           |                                                     |          }|                                t          |j                                       |                                fd           dS )z&
        Send a Failure over.
        c                  4    t          j                   S r   )r:   r\   )r5   failureMessager    rF   r   s   r+   rt   z+WorkerReporter.addFailure.<locals>.<lambda>   s#    J   r.   N)	r^   r:   rv   rf   ry   r   rz   rk   ro   )	rF   rp   r4   rg   r5   r   r    r   r`   s	   `   @@@@r+   r:   zWorkerReporter.addFailure  s     	4&&&7799""4(( 0022&&	))

       	
 	
 	
 	
 	
r.   c                      t                                          |           t                    |                                                      fd           dS )z#
        Send a skip over.
        c                  R    j                             t          j                   S )N)r   reason)r\   r1   r   AddSkip)r   rF   r   s   r+   rt   z(WorkerReporter.addSkip.<locals>.<lambda>1  s)    D$//'(6 0   r.   N)r^   addSkiprj   rv   ro   )rF   rp   r   r   r`   s   ` `@r+   r   zWorkerReporter.addSkip)  sv     	f%%%V7799

     	
 	
 	
 	
 	
r.   c                 "    || j         S |j        S )zj
        Get the reason for a C{Todo}.

        If C{todo} is L{None}, return a sensible default.
        )_DEFAULT_TODOr   )rF   r;   s     r+   _getTodoReasonzWorkerReporter._getTodoReason6  s     <%%;r.   c                      t                                          ||           |                                |                                                      fd           dS )z0
        Send an expected failure over.
        c                  Z    t          j                                                 S )N)r   r   r;   )r>   r\   r   )errorMessagerF   r   r;   s   r+   rt   z3WorkerReporter.addExpectedFailure.<locals>.<lambda>I  s4    & !"((..	   r.   N)r^   r>   ry   rv   ro   )rF   rp   r   r;   r   r   r`   s   `  `@@r+   r>   z!WorkerReporter.addExpectedFailureA  s     	""4555,,..7799

      	
 	
 	
 	
 	
r.   c                      t                                          |           |                                                      fd           dS )z2
        Send an unexpected success over.
        c                  x     j                             t          j                                                 S )N)r   r;   )r\   r1   r   AddUnexpectedSuccessr   )rF   r   r;   s   r+   rt   z5WorkerReporter.addUnexpectedSuccess.<locals>.<lambda>X  s9    D$//4!((.. 0   r.   N)r^   addUnexpectedSuccessrv   ro   )rF   rp   r;   r   r`   s   ` `@r+   r   z#WorkerReporter.addUnexpectedSuccessQ  sm     	$$T40007799

     	
 	
 	
 	
 	
r.   c                     dS )z*
        I{Don't} print a summary
        NrZ   rE   s    r+   printSummaryzWorkerReporter.printSummary_  s      r.   r   ) rR   rS   rT   rU   r   r   rV   rL   r   r@   r_   rb   r   r   rf   r   rj   rk   r   r   ro   PyUnitTestCaseru   r|   r3   r:   r   r   r>   r   r   __classcell__)r`   s   @r+   rA   rA      s2        	 	 ,M-1J)*111' ' ' ' '	(8 	 	 	 	 '    	' 	d3i 	 	 	 	xA 4    $
~ 
$ 
 
 
 
 
 

s 
l 
t 
 
 
 
*C^ CL CT C C C C C C
~ 
\ 
d 
 
 
 
 
 
(
 
 
 
 
	 	 	
 
 
 
 
 
 
 
 
 
 
 
      r.   rA   )+rU   typesr   typingr   r   r   r   r   r	   unittestr
   r   attrsr   r   typing_extensionsr   twisted.internet.deferr   r   twisted.protocols.ampr   r   twisted.python.failurer   twisted.python.reflectr   twisted.trial._distr   twisted.trial.reporterr   reporterr   r   r   r   rj   r3   r:   r>   r@   rA   rZ   r.   r+   <module>r      sy           D D D D D D D D D D D D D D D D / / / / / / ! ! ! ! ! ! ! ! % % % % % % : : : : : : : : 7 7 7 7 7 7 7 7 * * * * * * ' ' ' ' ' ' / / / / / / - - - - - - # # # # # # ! ! ! ! ! ! ! !GCLL	),58BFs)	   :	#&36@DS		   2#  S  PT    & 5% 5% 5% 5% 5% 5% 5% 5%pA A A A AZ A A A A Ar.   