|
1 | | -import os |
| 1 | +import json |
2 | 2 | import unittest |
3 | 3 | import warnings |
4 | 4 | from datetime import datetime, timedelta |
| 5 | +from unittest.mock import Mock, patch |
5 | 6 | from uuid import UUID, uuid4 |
6 | 7 |
|
| 8 | +import httpretty |
| 9 | +import pytest |
| 10 | + |
7 | 11 | from sypht.client import SyphtClient |
8 | 12 |
|
9 | 13 |
|
@@ -93,5 +97,75 @@ def test_reauthentication(self): |
93 | 97 | self.assertFalse(self.sypht_client._is_token_expired()) |
94 | 98 |
|
95 | 99 |
|
| 100 | +class RetryTest(unittest.TestCase): |
| 101 | + """Test the global retry logic works as we expect it to.""" |
| 102 | + |
| 103 | + @patch.object(SyphtClient, "_authenticate_v2", return_value=("access_token", 100)) |
| 104 | + @patch.object(SyphtClient, "_authenticate_v1", return_value=("access_token2", 100)) |
| 105 | + @httpretty.activate(verbose=True, allow_net_connect=False) |
| 106 | + def test_it_should_retry_n_times(self, auth_v1: Mock, auth_v2: Mock): |
| 107 | + # arrange |
| 108 | + self.count = 0 |
| 109 | + |
| 110 | + def get_annotations(request, uri, response_headers): |
| 111 | + self.count += 1 |
| 112 | + # 1 req + 3 retries = 4 |
| 113 | + if self.count == 4: |
| 114 | + return [200, response_headers, json.dumps({"annotations": []})] |
| 115 | + return [502, response_headers, json.dumps({})] |
| 116 | + |
| 117 | + httpretty.register_uri( |
| 118 | + httpretty.GET, |
| 119 | + "https://api.sypht.com/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
| 120 | + body=get_annotations, |
| 121 | + ) |
| 122 | + |
| 123 | + sypht_client = SyphtClient(base_endpoint="https://api.sypht.com") |
| 124 | + |
| 125 | + # act / assert |
| 126 | + response = sypht_client.get_annotations( |
| 127 | + from_date=datetime( |
| 128 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 129 | + ).strftime("%Y-%m-%d"), |
| 130 | + to_date=datetime( |
| 131 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 132 | + ).strftime("%Y-%m-%d"), |
| 133 | + ) |
| 134 | + |
| 135 | + assert response == {"annotations": []} |
| 136 | + |
| 137 | + @patch.object(SyphtClient, "_authenticate_v2", return_value=("access_token", 100)) |
| 138 | + @patch.object(SyphtClient, "_authenticate_v1", return_value=("access_token2", 100)) |
| 139 | + @httpretty.activate(verbose=True, allow_net_connect=False) |
| 140 | + def test_retry_should_eventually_fail_for_50x(self, auth_v1: Mock, auth_v2: Mock): |
| 141 | + # arrange |
| 142 | + self.count = 0 |
| 143 | + |
| 144 | + def get_annotations(request, uri, response_headers): |
| 145 | + self.count += 1 |
| 146 | + return [502, response_headers, json.dumps({})] |
| 147 | + |
| 148 | + httpretty.register_uri( |
| 149 | + httpretty.GET, |
| 150 | + "https://api.sypht.com/app/annotations?offset=0&fromDate=2021-01-01&toDate=2021-01-01", |
| 151 | + body=get_annotations, |
| 152 | + ) |
| 153 | + |
| 154 | + sypht_client = SyphtClient(base_endpoint="https://api.sypht.com") |
| 155 | + |
| 156 | + # act / assert |
| 157 | + with self.assertRaisesRegex(Exception, ".") as e: |
| 158 | + sypht_client.get_annotations( |
| 159 | + from_date=datetime( |
| 160 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 161 | + ).strftime("%Y-%m-%d"), |
| 162 | + to_date=datetime( |
| 163 | + year=2021, month=1, day=1, hour=0, minute=0, second=0 |
| 164 | + ).strftime("%Y-%m-%d"), |
| 165 | + ) |
| 166 | + |
| 167 | + assert self.count == 4, "should be 1 req + 3 retries" |
| 168 | + |
| 169 | + |
96 | 170 | if __name__ == "__main__": |
97 | 171 | unittest.main() |
0 commit comments