a
    4ñ»j»
  ã                   @   s2   d Z ddlmZ ddlZddlmZ ddd„ZdS )	aü  
Provides support functionality to retry scenarios a number of times before
their failure is accepted. This functionality can be helpful when you use
behave tests in a unreliable server/network infrastructure.

EXAMPLE:

.. sourcecode:: gherkin

    # -- FILE: features/alice.feature
    # TAG:  Feature or Scenario/ScenarioOutline with @autoretry
    # NOTE: If you tag the feature, all its scenarios are retried.
    @autoretry
    Feature: Use unreliable Server infrastructure

        Scenario: ...


.. sourcecode:: python

    # -- FILE: features/environment.py
    from behave.contrib.scenario_autoretry import patch_scenario_with_autoretry

    def before_feature(context, feature):
        for scenario in feature.scenarios:
            if "autoretry" in scenario.effective_tags:
                patch_scenario_with_autoretry(scenario, max_attempts=2)

.. seealso::
    * https://github.com/behave/behave/pull/328
    * https://github.com/hypothesis/smokey/blob/sauce-reliability/smokey/features/environment.py
é    )Úprint_functionN)ÚScenarioOutlineé   c                    sT   ‡ fdd„}t | tƒr<| }|jD ]} | j}t ||¡| _q n| j}t ||¡| _dS )a¶  Monkey-patches :func:`~behave.model.Scenario.run()` to auto-retry a
    scenario that fails. The scenario is retried a number of times
    before its failure is accepted.

    This is helpful when the test infrastructure (server/network environment)
    is unreliable (which should be a rare case).

    :param scenario:        Scenario or ScenarioOutline to patch.
    :param max_attempts:    How many times the scenario can be run.
    c                    sn   t dˆ d ƒD ]H}| |i |¤Žs@|dkr:d}t| |¡ƒ  dS |ˆ k rtd |¡ƒ qd}t| ˆ ¡ƒ dS )Né   z/AUTO-RETRY SCENARIO PASSED (after {0} attempts)Fz!AUTO-RETRY SCENARIO (attempt {0})z/AUTO-RETRY SCENARIO FAILED (after {0} attempts)T)ÚrangeÚprintÚformat)Úscenario_runÚargsÚkwargsÚattemptÚmessage©Úmax_attempts© úGE:\soccer\ml_env\Lib\site-packages\behave/contrib/scenario_autoretry.pyÚscenario_run_with_retries4   s    z@patch_scenario_with_autoretry.<locals>.scenario_run_with_retriesN)Ú
isinstancer   Z	scenariosÚrunÚ	functoolsÚpartial)Zscenarior   r   Zscenario_outliner	   r   r   r   Úpatch_scenario_with_autoretry)   s    

r   )r   )Ú__doc__Ú
__future__r   r   Zbehave.modelr   r   r   r   r   r   Ú<module>   s   !