|
1 | 1 | import logging
|
| 2 | +from unittest.mock import Mock, patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from requests import Response |
2 | 6 |
|
3 | 7 | import jira.resilientsession
|
| 8 | +from jira.exceptions import JIRAError |
4 | 9 | from tests.conftest import JiraTestCase
|
5 | 10 |
|
6 | 11 |
|
@@ -53,3 +58,40 @@ def test_logging_with_connection_error(self):
|
53 | 58 | def tearDown(self):
|
54 | 59 | jira.resilientsession.logging.getLogger().removeHandler(self.loggingHandler)
|
55 | 60 | del self.loggingHandler
|
| 61 | + |
| 62 | + |
| 63 | +status_codes_retries_test_data = [ |
| 64 | + (429, 4, 3), |
| 65 | + (401, 1, 0), |
| 66 | + (403, 1, 0), |
| 67 | + (404, 1, 0), |
| 68 | + (502, 1, 0), |
| 69 | + (503, 1, 0), |
| 70 | + (504, 1, 0), |
| 71 | +] |
| 72 | + |
| 73 | + |
| 74 | +@patch("requests.Session.get") |
| 75 | +@patch("time.sleep") |
| 76 | +@pytest.mark.parametrize( |
| 77 | + "status_code,expected_number_of_retries,expected_number_of_sleep_invocations", |
| 78 | + status_codes_retries_test_data, |
| 79 | +) |
| 80 | +def test_status_codes_retries( |
| 81 | + mocked_sleep_method: Mock, |
| 82 | + mocked_get_method: Mock, |
| 83 | + status_code: int, |
| 84 | + expected_number_of_retries: int, |
| 85 | + expected_number_of_sleep_invocations: int, |
| 86 | +): |
| 87 | + mocked_response: Response = Response() |
| 88 | + mocked_response.status_code = status_code |
| 89 | + mocked_response.headers["X-RateLimit-Remaining"] = "1" |
| 90 | + mocked_get_method.return_value = mocked_response |
| 91 | + session: jira.resilientsession.ResilientSession = ( |
| 92 | + jira.resilientsession.ResilientSession() |
| 93 | + ) |
| 94 | + with pytest.raises(JIRAError): |
| 95 | + session.get("mocked_url") |
| 96 | + assert mocked_get_method.call_count == expected_number_of_retries |
| 97 | + assert mocked_sleep_method.call_count == expected_number_of_sleep_invocations |
0 commit comments