test_retry.py 2.34 KB
Newer Older
bailuo's avatar
readme  
bailuo committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import httpx
import pytest
import time

from itertools import product
from nixtla.nixtla_client import (
    ApiError,
)
from nixtla_tests.helpers.checks import check_retry_behavior


def raise_api_error_with_text(*args, **kwargs):
    raise ApiError(
        status_code=503,
        body="""
        <html><head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8">
        <title>503 Server Error</title>
        </head>
        <body text=#000000 bgcolor=#ffffff>
        <h1>Error: Server Error</h1>
        <h2>The service you requested is not available at this time.<p>Service error -27.</h2>
        <h2></h2>
        </body></html>
        """,
    )


def raise_api_error_with_json(*args, **kwargs):
    raise ApiError(
        status_code=422,
        body=dict(detail="Please use numbers"),
    )


def raise_read_timeout_error(*args, **kwargs):
    sleep_seconds = 5
    print(f"raising ReadTimeout error after {sleep_seconds} seconds")
    time.sleep(sleep_seconds)
    raise httpx.ReadTimeout("Timed out")


def raise_http_error(*args, **kwargs):
    print("raising HTTP error")
    raise ApiError(status_code=503, body="HTTP error")


@pytest.mark.parametrize(
    "side_effect,side_effect_exception,should_retry",
    [
        (raise_api_error_with_text, ApiError, True),
        (raise_api_error_with_json, ApiError, False),
    ],
)
def test_retry_behavior(
    air_passengers_df, side_effect, side_effect_exception, should_retry
):
    check_retry_behavior(
        df=air_passengers_df,
        side_effect=side_effect,
        side_effect_exception=side_effect_exception,
        should_retry=should_retry,
    )


combs = [
    (2, 5, 30),
    (10, 1, 5),
]
side_effect_settings = [
    (raise_read_timeout_error, httpx.ReadTimeout),
    (raise_http_error, ApiError),
]


@pytest.mark.parametrize(
    "retry_settings,side_effect_settings", product(combs, side_effect_settings)
)
def test_retry_behavior_set2(air_passengers_df, retry_settings, side_effect_settings):
    max_retries, retry_interval, max_wait_time = retry_settings
    side_effect, side_effect_exception = side_effect_settings
    check_retry_behavior(
        df=air_passengers_df,
        side_effect=side_effect,
        side_effect_exception=side_effect_exception,
        max_retries=max_retries,
        retry_interval=retry_interval,
        max_wait_time=max_wait_time,
    )