File: test_retry_behavior.py

package info (click to toggle)
python-globus-sdk 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,172 kB
  • sloc: python: 35,227; sh: 44; makefile: 35
file content (294 lines) | stat: -rw-r--r-- 10,506 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import pytest
import requests

import globus_sdk
from globus_sdk.testing import RegisteredResponse
from globus_sdk.transport import RequestCallerInfo, RetryConfig


@pytest.mark.parametrize("error_status", [500, 429, 502, 503, 504])
def test_retry_on_transient_error(client, mocksleep, error_status):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=error_status, body="Uh-oh!"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    # no sign of an error in the client
    res = client.get("/bar")
    assert res.http_status == 200
    assert res["baz"] == 1

    # there was a sleep (retry was triggered)
    mocksleep.assert_called_once()


def test_retry_disabled_via_tune(client, mocksleep):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    # the error is seen by the client (automatic retry does not hide it)
    with pytest.raises(globus_sdk.GlobusAPIError) as excinfo:
        with client.retry_config.tune(max_retries=0):
            client.get("/bar")
    assert excinfo.value.http_status == 500

    # there was no sleep (retry was not triggered)
    mocksleep.assert_not_called()


def test_retry_disabled_via_init_param(client_class, mocksleep):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()
    client = client_class(retry_config=RetryConfig(max_retries=0))

    # the error is seen by the client (automatic retry does not hide it)
    with pytest.raises(globus_sdk.GlobusAPIError) as excinfo:
        client.get("/bar")
    assert excinfo.value.http_status == 500

    # there was no sleep (retry was not triggered)
    mocksleep.assert_not_called()


def test_retry_disabled_via_init_param_but_enabled_via_tune(client_class, mocksleep):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()
    client = client_class(retry_config=RetryConfig(max_retries=0))

    # no sign of an error in the client if we "turn it back on"
    with client.retry_config.tune(max_retries=1):
        res = client.get("/bar")
    assert res.http_status == 200
    assert res["baz"] == 1

    # there was a sleep (retry was triggered)
    mocksleep.assert_called_once()


def test_retry_on_network_error(client, mocksleep):
    # set the response to be a requests NetworkError -- responses will raise the
    # exception when the call is made
    RegisteredResponse(
        path="https://foo.api.globus.org/bar",
        body=requests.ConnectionError("foo-err"),
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    # no sign of an error in the client
    res = client.get("/bar")
    assert res.http_status == 200
    assert res["baz"] == 1

    # there was a sleep (retry was triggered)
    mocksleep.assert_called_once()


@pytest.mark.parametrize("num_errors,expect_err", [(5, False), (6, True), (7, True)])
def test_retry_limit(client, mocksleep, num_errors, expect_err):
    # N errors followed by a success
    for _i in range(num_errors):
        RegisteredResponse(
            path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!"
        ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    if expect_err:
        with pytest.raises(globus_sdk.GlobusAPIError):
            client.get("/bar")
    else:
        # no sign of an error in the client
        res = client.get("/bar")
        assert res.http_status == 200
        assert res["baz"] == 1

    # default num retries = 5
    assert mocksleep.call_count == min(num_errors, 5)


def test_transport_retry_limit(client, mocksleep):
    # this limit is a safety to protect against a bad policy causing infinite retries
    client.retry_config.max_retries = 2

    for _i in range(3):
        RegisteredResponse(
            path="https://foo.api.globus.org/bar", status=500, body="Uh-oh!"
        ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    with pytest.raises(globus_sdk.GlobusAPIError):
        client.get("/bar")

    assert mocksleep.call_count == 2


def test_bad_max_retries_causes_error(client):
    # this test exploits the fact that we loop to (max_retries + 1) in order
    # to ensure that no requests are ever sent
    # the transport should throw an error in this case, since it doesn't have a response
    # value to return
    client.retry_config.max_retries = -1

    with pytest.raises(ValueError):
        client.get("/bar")


def test_persistent_connection_error(client):
    for _i in range(6):
        RegisteredResponse(
            path="https://foo.api.globus.org/bar",
            body=requests.ConnectionError("foo-err"),
        ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    with pytest.raises(globus_sdk.GlobusConnectionError):
        client.get("/bar")


def test_no_retry_401_no_authorizer(client):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=401, body="Unauthorized"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    # error gets raised in client (no retry)
    with pytest.raises(globus_sdk.GlobusAPIError) as excinfo:
        client.get("/bar")
    assert excinfo.value.http_status == 401


def test_retry_with_authorizer(client):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=401, body="Unauthorized"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    # an authorizer class which does nothing but claims to support handling of
    # unauthorized errors
    dummy_authz_calls = []

    class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer):
        def get_authorization_header(self):
            dummy_authz_calls.append("set_authz")
            return "foo"

        def handle_missing_authorization(self):
            dummy_authz_calls.append("handle_missing")
            return True

    authorizer = DummyAuthorizer()
    client.authorizer = authorizer

    # no sign of an error in the client
    res = client.get("/bar")
    assert res.http_status == 200
    assert res["baz"] == 1

    # ensure that setting authz was called twice (once for each request)
    # and that between the two calls, handle_missing_authorization was called once
    assert dummy_authz_calls == ["set_authz", "handle_missing", "set_authz"]


def test_no_retry_with_authorizer_no_handler(client):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=401, body="Unauthorized"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    # an authorizer class which does nothing and does not claim to handle
    # unauthorized errors
    dummy_authz_calls = []

    class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer):
        def get_authorization_header(self):
            dummy_authz_calls.append("set_authz")
            return "foo"

        def handle_missing_authorization(self):
            dummy_authz_calls.append("handle_missing")
            return False

    authorizer = DummyAuthorizer()
    client.authorizer = authorizer

    # error gets raised in client (no retry)
    with pytest.raises(globus_sdk.GlobusAPIError) as excinfo:
        client.get("/bar")
    assert excinfo.value.http_status == 401

    # only two calls, single setting of authz and a call to ask about handling the error
    assert dummy_authz_calls == ["set_authz", "handle_missing"]


def test_retry_with_authorizer_persistent_401(client):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=401, body="Unauthorized"
    ).add()
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=401, body="Unauthorized"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    # an authorizer class which does nothing but claims to support handling of
    # unauthorized errors
    dummy_authz_calls = []

    class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer):
        def get_authorization_header(self):
            dummy_authz_calls.append("set_authz")
            return "foo"

        def handle_missing_authorization(self):
            dummy_authz_calls.append("handle_missing")
            return True

    authorizer = DummyAuthorizer()
    client.authorizer = authorizer

    # the error gets raised in this case because it persists -- the authorizer only gets
    # one chance to resolve the issue
    with pytest.raises(globus_sdk.GlobusAPIError) as excinfo:
        client.get("/bar")
    assert excinfo.value.http_status == 401

    # ensure that setting authz was called twice (once for each request)
    # and that between the two calls, handle_missing_authorization was called once
    # but the handler should not be called a second time because the 401 repeated
    assert dummy_authz_calls == ["set_authz", "handle_missing", "set_authz"]


def test_transport_caller_info_with_retry(client):
    RegisteredResponse(
        path="https://foo.api.globus.org/bar", status=401, body="Unauthorized"
    ).add()
    RegisteredResponse(path="https://foo.api.globus.org/bar", json={"baz": 1}).add()

    dummy_authz_calls = []

    class DummyAuthorizer(globus_sdk.authorizers.GlobusAuthorizer):
        def get_authorization_header(self):
            dummy_authz_calls.append("set_authz")
            return "foo"

        def handle_missing_authorization(self):
            dummy_authz_calls.append("handle_missing")
            return True

    authorizer = DummyAuthorizer()
    caller_info = RequestCallerInfo(
        retry_config=client.retry_config, authorizer=authorizer
    )

    # Test direct transport usage with caller_info
    response = client.transport.request(
        "GET", "https://foo.api.globus.org/bar", caller_info=caller_info
    )

    assert response.status_code == 200
    # Verify that the authorizer was used for both authorization and retry handling
    assert dummy_authz_calls == ["set_authz", "handle_missing", "set_authz"]