|
17 | 17 | from unittest import mock
|
18 | 18 |
|
19 | 19 | import pytest
|
| 20 | +from solnlib.splunk_rest_client import MAX_REQUEST_RETRIES |
20 | 21 |
|
| 22 | +from requests.exceptions import ConnectionError |
21 | 23 | from solnlib import splunk_rest_client
|
| 24 | +from solnlib.splunk_rest_client import SplunkRestClient |
22 | 25 |
|
23 | 26 |
|
24 | 27 | @mock.patch.dict(os.environ, {"SPLUNK_HOME": "/opt/splunk"}, clear=True)
|
@@ -80,3 +83,29 @@ def test_init_with_invalid_port():
|
80 | 83 | host="localhost",
|
81 | 84 | port=99999,
|
82 | 85 | )
|
| 86 | + |
| 87 | + |
| 88 | +@mock.patch.dict(os.environ, {"SPLUNK_HOME": "/opt/splunk"}, clear=True) |
| 89 | +@mock.patch("solnlib.splunk_rest_client.get_splunkd_access_info") |
| 90 | +@mock.patch("http.client.HTTPResponse") |
| 91 | +@mock.patch("urllib3.HTTPConnectionPool._make_request") |
| 92 | +def test_request_retry(http_conn_pool, http_resp, mock_get_splunkd_access_info): |
| 93 | + mock_get_splunkd_access_info.return_value = "https", "localhost", 8089 |
| 94 | + session_key = "123" |
| 95 | + context = {"pool_connections": 5} |
| 96 | + rest_client = SplunkRestClient("msg_name_1", session_key, "_", **context) |
| 97 | + |
| 98 | + mock_resp = http_resp() |
| 99 | + mock_resp.status = 200 |
| 100 | + mock_resp.reason = "TEST OK" |
| 101 | + |
| 102 | + side_effects = [ConnectionError(), ConnectionError(), ConnectionError(), mock_resp] |
| 103 | + http_conn_pool.side_effect = side_effects |
| 104 | + res = rest_client.get("test") |
| 105 | + assert http_conn_pool.call_count == len(side_effects) |
| 106 | + assert res.reason == mock_resp.reason |
| 107 | + |
| 108 | + side_effects = [ConnectionError()] * (MAX_REQUEST_RETRIES + 1) + [mock_resp] |
| 109 | + http_conn_pool.side_effect = side_effects |
| 110 | + with pytest.raises(ConnectionError): |
| 111 | + rest_client.get("test") |
0 commit comments