o
    i                     @   s   d dl mZ d dl mZ d dlm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 ejrGd dlmZ G dd deZG dd deZG dd deZG dd deZdS )    )ABC)abstractmethod)defaultdictN)	TAG_FALSE)TAG_TRUE)Test)TestRun)
TestStatus)TestTag)SessionManagerc                   @   s   e Zd ZdddZededefdd	Zededefd
dZedede	fddZ
ededdfddZedefddZdS )RetryHandlersession_managerr   returnNc                 C   s
   || _ d S Nr   selfr    r   [/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/testing/internal/retry_handlers.py__init__      
zRetryHandler.__init__testc                 C      dS )a  
        Return whether this retry policy should be applied to the given test.

        This is called before any test runs have happened, and should consider test properties (such as whether it's
        new), as well as per-session retry limits (accessible via `self.session_manager`).

        For each test, the test plugin will try each retry handler in the session's retry handlers list, and use the
        first one for which `should_apply()` returns True. The `should_apply()` check can assume that the retry feature
        is enabled for the current session (otherwise the retry handler would not be in the session's retry handlers
        list).
        Nr   r   r   r   r   r   should_apply       zRetryHandler.should_applyc                 C   r   )z
        Return whether one more test run should be performed for the given test.

        This should consider the status of previous runs, as well as number of attempts and per-session retry limits.
        Nr   r   r   r   r   should_retry%   r   zRetryHandler.should_retryc                 C   r   )a  
        Return the final status to assign to the test, and set the final test run tags on the passed test.

        Final status and tags are calculated together because they typically depend on the same data (count of
        passed/failed/skipped test runs).
        Nr   r   r   r   r   get_final_status-   r   zRetryHandler.get_final_statustest_runc                 C   r   )zE
        Set the tags to be added to a given retry test run.
        Nr   r   r   r   r   r   set_tags_for_test_run6   r   z"RetryHandler.set_tags_for_test_runc                 C   r   )zD
        Return a human-readable name of the retry handler.
        Nr   r   r   r   r   get_pretty_name<   r   zRetryHandler.get_pretty_namer   r   r   N)__name__
__module____qualname__r   r   r   boolr   r   r	   r   r   r    strr"   r   r   r   r   r      s    
r   c                       st   e Zd Zd fddZdefddZd	edefd
dZd	edefddZ	d	ede
fddZdeddfddZ  ZS )AutoTestRetriesHandlerr   r   r   Nc                    s6   t  j|d ttdd| _ttdd| _d S )Nr   'DD_CIVISIBILITY_TOTAL_FLAKY_RETRY_COUNT1000!DD_CIVISIBILITY_FLAKY_RETRY_COUNT5)superr   intosgetenvmax_tests_to_retry_per_sessionmax_retries_per_testr   	__class__r   r   r   D   s   zAutoTestRetriesHandler.__init__c                 C   r   )NzAuto Test Retriesr   r!   r   r   r   r"   I      z&AutoTestRetriesHandler.get_pretty_namer   c                 C   s
   | j dkS Nr   )r2   r   r   r   r   r   L   r   z#AutoTestRetriesHandler.should_applyc                 C   s(   t |jd }|j tjko|| jk S N   )len	test_runslast_test_run
get_statusr	   FAILr3   r   r   retries_so_farr   r   r   r   O   s   z#AutoTestRetriesHandler.should_retryc                 C   s   |  j d8  _ |j S r8   )r2   r<   r=   r   r   r   r   r   S   s   
z'AutoTestRetriesHandler.get_final_statusr   c                 C   (   |j dkrd S |tjttjdi d S )Nr   auto_test_retryattempt_numberset_tagsr
   IS_RETRYr   RETRY_REASONr   r   r   r   r    W      
z,AutoTestRetriesHandler.set_tags_for_test_runr#   )r$   r%   r&   r   r(   r"   r   r'   r   r   r	   r   r   r    __classcell__r   r   r4   r   r)   C   s    r)   c                   @   sx   e Zd ZdZdefddZdedefddZdede	fdd	Z
dedefd
dZdedefddZdeddfddZdS )EarlyFlakeDetectionHandler,  r   c                 C   r   )NzEarly Flake Detectionr   r!   r   r   r   r"   f   r6   z*EarlyFlakeDetectionHandler.get_pretty_namer   c                 C   s   |  o|  S r   )is_newhas_parametersr   r   r   r   r   i   s   z'EarlyFlakeDetectionHandler.should_applyc                 C   sT   | j jj}|jd  }|dkr|jS |dkr|jS |dkr!|jS |dkr(|jS dS )Nr      
      rK   )	r   settingsearly_flake_detectionr;   seconds_so_farslow_test_retries_5sslow_test_retries_10sslow_test_retries_30sslow_test_retries_5m)r   r   efd_settingsinitial_attempt_secondsr   r   r   _target_number_of_retrieso   s   
z4EarlyFlakeDetectionHandler._target_number_of_retriesc                 C   s<   |  | jkr|d dS | |}t|jd }||k S )NslowFr9   )rS   EFD_ABORT_TEST_SECONDS&set_early_flake_detection_abort_reasonrZ   r:   r;   )r   r   target_number_of_retriesr@   r   r   r   r   }   s   

z'EarlyFlakeDetectionHandler.should_retryc                 C   s|   t dd }d}|jD ]}||   d7  < |d7 }q|tj dkr1|tj |kr.|  tjS |tj dkr;tjS tjS )Nc                   S   r   r7   r   r   r   r   r   <lambda>   r   z=EarlyFlakeDetectionHandler.get_final_status.<locals>.<lambda>r   r9   )r   r;   r=   r	   PASSmark_flaky_runr>   SKIP)r   r   status_countstotal_countr   r   r   r   r      s   

z+EarlyFlakeDetectionHandler.get_final_statusr   Nc                 C   rA   )Nr   rR   rC   r   r   r   r   r       rH   z0EarlyFlakeDetectionHandler.set_tags_for_test_run)r$   r%   r&   r\   r(   r"   r   r'   r   r/   rZ   r   r	   r   r   r    r   r   r   r   rJ   c   s    	rJ   c                   @   sb   e Zd ZdefddZdedefddZdedefddZdede	fd	d
Z
deddfddZdS )AttemptToFixHandlerr   c                 C   r   )NzAttempt to Fixr   r!   r   r   r   r"      r6   z#AttemptToFixHandler.get_pretty_namer   c                 C   s   |  S r   )is_attempt_to_fixr   r   r   r   r      s   z AttemptToFixHandler.should_applyc                 C   s   t |jd }|| jjjjk S r8   )r:   r;   r   rQ   test_managementattempt_to_fix_retriesr?   r   r   r   r      s   z AttemptToFixHandler.should_retryc                 C   s   t jti}tdd }d}|jD ]}||   d7  < |d7 }q|tj dkr,tj}n|tj dkr7tj}ntj	}|tj |krGt
|t j< n|tj |krSt
|t j< |j| |S )Nc                   S   r   r7   r   r   r   r   r   r_      r   z6AttemptToFixHandler.get_final_status.<locals>.<lambda>r   r9   )r
   ATTEMPT_TO_FIX_PASSEDr   r   r;   r=   r	   r`   r>   rb   r   HAS_FAILED_ALL_RETRIESr<   rE   )r   r   
final_tagsrc   rd   r   final_statusr   r   r   r      s$   


z$AttemptToFixHandler.get_final_statusr   Nc                 C   rA   )Nr   attempt_to_fixrC   r   r   r   r   r       rH   z)AttemptToFixHandler.set_tags_for_test_run)r$   r%   r&   r(   r"   r   r'   r   r   r	   r   r   r    r   r   r   r   re      s    re   )abcr   r   collectionsr   r0   typingt"ddtrace.testing.internal.constantsr   r   "ddtrace.testing.internal.test_datar   r   r	   r
   TYPE_CHECKING(ddtrace.testing.internal.session_managerr   r   r)   rJ   re   r   r   r   r   <module>   s"    0 A