|
| 1 | +import contextlib |
| 2 | +import logging |
| 3 | +from typing import Iterator |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import httpretty |
| 7 | +import pytest |
| 8 | +import requests |
| 9 | + |
| 10 | +from openeo.utils.http import session_with_retries |
| 11 | + |
| 12 | + |
| 13 | +class TestSessionWithRetries: |
| 14 | + @pytest.fixture(autouse=True) |
| 15 | + def time_sleep(self) -> Iterator[mock.Mock]: |
| 16 | + with mock.patch("time.sleep") as mock_sleep: |
| 17 | + yield mock_sleep |
| 18 | + |
| 19 | + @pytest.fixture(autouse=True) |
| 20 | + def _auto_httpretty_enabled(self): |
| 21 | + """Automatically activate httpretty for all tests in this class.""" |
| 22 | + with httpretty.enabled(allow_net_connect=False): |
| 23 | + yield |
| 24 | + |
| 25 | + def test_default_basic(self, time_sleep): |
| 26 | + responses = [ |
| 27 | + httpretty.Response(status=429, body="Stop it!"), |
| 28 | + httpretty.Response(status=200, body="ok then"), |
| 29 | + ] |
| 30 | + httpretty.register_uri(httpretty.GET, uri="https://example.test/", responses=responses) |
| 31 | + session = session_with_retries() |
| 32 | + resp = session.get("https://example.test/") |
| 33 | + assert resp.status_code == 200 |
| 34 | + assert resp.text == "ok then" |
| 35 | + # Single retry does not trigger a sleep |
| 36 | + assert time_sleep.call_args_list == [] |
| 37 | + |
| 38 | + @pytest.mark.parametrize( |
| 39 | + ["fail_count", "expected_sleeps", "success"], |
| 40 | + [ |
| 41 | + (0, [], True), |
| 42 | + (1, [], True), |
| 43 | + (2, [5], True), |
| 44 | + (3, [5, 10], True), |
| 45 | + (5, [5, 10, 20, 40], True), |
| 46 | + (6, [5, 10, 20, 40], False), |
| 47 | + ], |
| 48 | + ) |
| 49 | + def test_default_multiple_attempts(self, time_sleep, fail_count, expected_sleeps, success): |
| 50 | + responses = [httpretty.Response(status=429, body="Stop it!")] * fail_count |
| 51 | + responses.append(httpretty.Response(status=200, body="ok then")) |
| 52 | + httpretty.register_uri(httpretty.GET, uri="https://example.test/", responses=responses) |
| 53 | + session = session_with_retries() |
| 54 | + |
| 55 | + try: |
| 56 | + result = session.get("https://example.test/") |
| 57 | + except Exception as e: |
| 58 | + result = e |
| 59 | + |
| 60 | + if success: |
| 61 | + assert isinstance(result, requests.Response) |
| 62 | + assert result.status_code == 200 |
| 63 | + assert result.text == "ok then" |
| 64 | + else: |
| 65 | + assert isinstance(result, requests.exceptions.RetryError) |
| 66 | + |
| 67 | + assert time_sleep.call_args_list == [mock.call(s) for s in expected_sleeps] |
| 68 | + |
| 69 | + @pytest.mark.parametrize( |
| 70 | + ["retry_config", "responses", "expected", "expected_sleeps"], |
| 71 | + [ |
| 72 | + ( |
| 73 | + # Fail with 500, which is in status_forcelist -> keep trying |
| 74 | + {"total": 3, "backoff_factor": 1.1, "status_forcelist": [500, 502, 503]}, |
| 75 | + [ |
| 76 | + httpretty.Response(status=500, body="Internal Server Error"), |
| 77 | + httpretty.Response(status=500, body="Internal Server Error"), |
| 78 | + httpretty.Response(status=500, body="Internal Server Error"), |
| 79 | + ], |
| 80 | + requests.exceptions.RetryError, |
| 81 | + [2.2, 4.4], |
| 82 | + ), |
| 83 | + ( |
| 84 | + # Fail with 500, not in status_forcelist -> no retrying |
| 85 | + {"total": 3, "backoff_factor": 1.1, "status_forcelist": [502, 503]}, |
| 86 | + [ |
| 87 | + httpretty.Response(status=500, body="Internal Server Error"), |
| 88 | + httpretty.Response(status=500, body="Internal Server Error"), |
| 89 | + httpretty.Response(status=500, body="Internal Server Error"), |
| 90 | + ], |
| 91 | + (500, "Internal Server Error"), |
| 92 | + [], |
| 93 | + ), |
| 94 | + ( |
| 95 | + # Multiple statuses in status_forcelist, retry until success |
| 96 | + {"total": 3, "backoff_factor": 1.1, "status_forcelist": [500, 502, 503]}, |
| 97 | + [ |
| 98 | + httpretty.Response(status=500, body="Internal Server Error"), |
| 99 | + httpretty.Response(status=502, body="Bad Gateway"), |
| 100 | + httpretty.Response(status=503, body="Service Unavailable"), |
| 101 | + httpretty.Response(status=200, body="Ok then"), |
| 102 | + ], |
| 103 | + (200, "Ok then"), |
| 104 | + [2.2, 4.4], |
| 105 | + ), |
| 106 | + ], |
| 107 | + ) |
| 108 | + def test_custom_retries(self, time_sleep, retry_config, responses, expected, expected_sleeps): |
| 109 | + httpretty.register_uri(httpretty.GET, uri="https://example.test/", responses=responses) |
| 110 | + session = session_with_retries(retries=retry_config) |
| 111 | + |
| 112 | + try: |
| 113 | + result = session.get("https://example.test/") |
| 114 | + except Exception as e: |
| 115 | + result = e |
| 116 | + |
| 117 | + if isinstance(expected, type): |
| 118 | + assert isinstance(result, expected) |
| 119 | + elif isinstance(expected, tuple): |
| 120 | + assert (result.status_code, result.text) == expected |
| 121 | + else: |
| 122 | + raise ValueError(expected) |
| 123 | + |
| 124 | + assert time_sleep.call_args_list == [mock.call(s) for s in expected_sleeps] |
| 125 | + |
| 126 | + return |
| 127 | + |
| 128 | + @pytest.mark.parametrize( |
| 129 | + ["retry_config"], |
| 130 | + [ |
| 131 | + (None,), |
| 132 | + # Retry-after is even honored when 429 is not in status_forcelist |
| 133 | + ({"status_forcelist": []},), |
| 134 | + ], |
| 135 | + ) |
| 136 | + def test_retry_after(self, time_sleep, retry_config): |
| 137 | + """ |
| 138 | + Test that the Retry-After header is respected. |
| 139 | + """ |
| 140 | + responses = [ |
| 141 | + httpretty.Response(status=429, body="Stop it!", adding_headers={"Retry-After": "23"}), |
| 142 | + httpretty.Response(status=200, body="ok then"), |
| 143 | + ] |
| 144 | + httpretty.register_uri(httpretty.GET, uri="https://example.test/", responses=responses) |
| 145 | + session = session_with_retries(retry_config) |
| 146 | + resp = session.get("https://example.test/") |
| 147 | + assert resp.status_code == 200 |
| 148 | + assert resp.text == "ok then" |
| 149 | + assert time_sleep.call_args_list == [mock.call(23)] |
0 commit comments