| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2020, Abhijeet Kasurde <[email protected]> |
| 3 |
# Copyright: (c) 2020, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
|
| 9 |
from ansible.module_utils.api import rate_limit, retry, retry_with_delays_and_condition |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
class CustomException(Exception): |
| 15 |
pass |
| 16 |
|
| 17 |
|
| 18 |
class CustomBaseException(BaseException): |
| 19 |
pass |
| 20 |
|
| 21 |
|
| 22 |
class TestRateLimit: |
| 23 |
|
| 24 |
def test_ratelimit(self): |
| 25 |
@rate_limit(rate=1, rate_limit=1) |
| 26 |
def login_database(): |
| 27 |
return "success" |
| 28 |
r = login_database() |
| 29 |
|
| 30 |
assert r == 'success' |
| 31 |
|
| 32 |
|
| 33 |
class TestRetry: |
| 34 |
|
| 35 |
def test_no_retry_required(self): |
| 36 |
@retry(retries=4, retry_pause=2) |
| 37 |
def login_database(): |
| 38 |
login_database.counter += 1 |
| 39 |
return 'success' |
| 40 |
|
| 41 |
login_database.counter = 0 |
| 42 |
r = login_database() |
| 43 |
|
| 44 |
assert r == 'success' |
| 45 |
assert login_database.counter == 1 |
| 46 |
|
| 47 |
def test_catch_exception(self): |
| 48 |
|
| 49 |
@retry(retries=1) |
| 50 |
def login_database(): |
| 51 |
return 'success' |
| 52 |
|
| 53 |
with pytest.raises(Exception, match="Retry"): |
| 54 |
login_database() |
| 55 |
|
| 56 |
def test_no_retries(self): |
| 57 |
|
| 58 |
@retry() |
| 59 |
def login_database(): |
| 60 |
assert False, "Should not execute" |
| 61 |
|
| 62 |
login_database() |
| 63 |
|
| 64 |
|
| 65 |
class TestRetryWithDelaysAndCondition: |
| 66 |
|
| 67 |
def test_empty_retry_iterator(self): |
| 68 |
@retry_with_delays_and_condition(backoff_iterator=[]) |
| 69 |
def login_database(): |
| 70 |
login_database.counter += 1 |
| 71 |
|
| 72 |
login_database.counter = 0 |
| 73 |
r = login_database() |
| 74 |
assert login_database.counter == 1 |
| 75 |
|
| 76 |
def test_no_retry_exception(self): |
| 77 |
@retry_with_delays_and_condition( |
| 78 |
backoff_iterator=[1], |
| 79 |
should_retry_error=lambda x: False, |
| 80 |
) |
| 81 |
def login_database(): |
| 82 |
login_database.counter += 1 |
| 83 |
if login_database.counter == 1: |
| 84 |
raise CustomException("Error") |
| 85 |
|
| 86 |
login_database.counter = 0 |
| 87 |
with pytest.raises(CustomException, match="Error"): |
| 88 |
login_database() |
| 89 |
assert login_database.counter == 1 |
| 90 |
|
| 91 |
def test_no_retry_baseexception(self): |
| 92 |
@retry_with_delays_and_condition( |
| 93 |
backoff_iterator=[1], |
| 94 |
should_retry_error=lambda x: True, # Retry all exceptions inheriting from Exception |
| 95 |
) |
| 96 |
def login_database(): |
| 97 |
login_database.counter += 1 |
| 98 |
if login_database.counter == 1: |
| 99 |
# Raise an exception inheriting from BaseException |
| 100 |
raise CustomBaseException("Error") |
| 101 |
|
| 102 |
login_database.counter = 0 |
| 103 |
with pytest.raises(CustomBaseException, match="Error"): |
| 104 |
login_database() |
| 105 |
assert login_database.counter == 1 |
| 106 |
|
| 107 |
def test_retry_exception(self): |
| 108 |
@retry_with_delays_and_condition( |
| 109 |
backoff_iterator=[1], |
| 110 |
should_retry_error=lambda x: isinstance(x, CustomException), |
| 111 |
) |
| 112 |
def login_database(): |
| 113 |
login_database.counter += 1 |
| 114 |
if login_database.counter == 1: |
| 115 |
raise CustomException("Retry") |
| 116 |
return 'success' |
| 117 |
|
| 118 |
login_database.counter = 0 |
| 119 |
assert login_database() == 'success' |
| 120 |
assert login_database.counter == 2 |