File: test_client.py

package info (click to toggle)
python-aiohttp-retry 2.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: python: 973; makefile: 6
file content (477 lines) | stat: -rw-r--r-- 15,454 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
from types import SimpleNamespace
from typing import Optional, Tuple

import pytest
from aiohttp import (
    BasicAuth,
    ClientResponse,
    ClientResponseError,
    ClientSession,
    TraceConfig,
    TraceRequestStartParams,
    hdrs,
)
from yarl import URL

from aiohttp_retry import ExponentialRetry, ListRetry, RetryClient
from aiohttp_retry.client import RequestParams
from aiohttp_retry.retry_options import RetryOptionsBase
from tests.app import App


async def get_retry_client_and_test_app_for_test(
    aiohttp_client,
    raise_for_status: bool = False,
    retry_options: Optional[RetryOptionsBase] = None,
) -> Tuple[RetryClient, App]:
    test_app = App()
    app = test_app.web_app()
    client = await aiohttp_client(app, raise_for_status=raise_for_status)

    retry_client = RetryClient(client_session=client, retry_options=retry_options)
    return retry_client, test_app


async def test_hello(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.get('/ping') as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 1

    await retry_client.close()


async def test_hello_by_request(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.request(method=hdrs.METH_GET, url='/ping') as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 1

    await retry_client.close()


async def test_hello_with_context(aiohttp_client):
    test_app = App()
    app = test_app.web_app()
    client = await aiohttp_client(app)
    async with RetryClient() as retry_client:
        retry_client._client = client
        async with retry_client.get('/ping') as response:
            text = await response.text()
            assert response.status == 200
            assert text == 'Ok!'

            assert test_app.counter == 1


async def test_internal_error(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    retry_options = ExponentialRetry(attempts=5)
    async with retry_client.get('/internal_error', retry_options) as response:
        assert response.status == 500
        assert test_app.counter == 5

    await retry_client.close()


async def test_not_found_error(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    retry_options = ExponentialRetry(attempts=5, statuses={404})
    async with retry_client.get('/not_found_error', retry_options) as response:
        assert response.status == 404
        assert test_app.counter == 5

    await retry_client.close()


async def test_sometimes_error(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    retry_options = ExponentialRetry(attempts=5)
    async with retry_client.get('/sometimes_error', retry_options) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 3

    await retry_client.close()


async def test_sometimes_error_with_raise_for_status(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client, raise_for_status=True)
    retry_options = ExponentialRetry(attempts=5, exceptions={ClientResponseError})
    async with retry_client.get('/sometimes_error', retry_options) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 3

    await retry_client.close()


async def test_override_options(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(
        aiohttp_client,
        retry_options=ExponentialRetry(attempts=1)
    )
    retry_options = ExponentialRetry(attempts=5)
    async with retry_client.get('/sometimes_error', retry_options) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 3

    await retry_client.close()


async def test_hello_awaitable(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    response = await retry_client.get('/ping')
    text = await response.text()
    assert response.status == 200
    assert text == 'Ok!'

    assert test_app.counter == 1

    await retry_client.close()


async def test_add_trace_request_ctx(aiohttp_client):
    actual_request_contexts = []

    async def on_request_start(
        _: ClientSession,
        trace_config_ctx: SimpleNamespace,
        __: TraceRequestStartParams,
    ) -> None:
        actual_request_contexts.append(trace_config_ctx)

    test_app = App()

    trace_config = TraceConfig()
    trace_config.on_request_start.append(on_request_start)  # type: ignore

    retry_client = RetryClient()
    retry_client._client = await aiohttp_client(
        test_app.web_app(),
        trace_configs=[trace_config]
    )

    async with retry_client.get('/sometimes_error', trace_request_ctx={'foo': 'bar'}):
        assert test_app.counter == 3

    assert actual_request_contexts == [
        SimpleNamespace(
            trace_request_ctx={
                'foo': 'bar',
                'current_attempt': i + 1,
            },
        )
        for i in range(3)
    ]


@pytest.mark.parametrize("attempts", [2, 3])
async def test_change_urls_in_request(aiohttp_client, attempts):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(
        aiohttp_client,
        retry_options=ExponentialRetry(attempts=attempts)
    )
    async with retry_client.get(url=['/internal_error', '/ping']) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 2

    await retry_client.close()


@pytest.mark.parametrize("attempts", [2, 3])
async def test_change_urls_as_tuple_in_request(aiohttp_client, attempts):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(
        aiohttp_client,
        retry_options=ExponentialRetry(attempts=attempts)
    )
    async with retry_client.get(url=('/internal_error', '/ping')) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 2

    await retry_client.close()


@pytest.mark.parametrize("url", [{"/ping", "/internal_error"}, []])
async def test_pass_bad_urls(aiohttp_client, url):
    retry_client, _ = await get_retry_client_and_test_app_for_test(aiohttp_client)
    with pytest.raises(ValueError):
        async with retry_client.get(url=url):
            pass

    await retry_client.close()


@pytest.mark.parametrize("url, method", [
    ("/options_handler", 'options'),
    ("/head_handler", 'head'),
    ("/post_handler", 'post'),
    ("/put_handler", 'put'),
    ("/patch_handler", 'patch'),
    ("/delete_handler", 'delete'),
])
async def test_methods(aiohttp_client, url, method):
    retry_client, _ = await get_retry_client_and_test_app_for_test(aiohttp_client)
    method_func = getattr(retry_client, method)
    async with method_func(url) as response:
        assert response.method.lower() == method

    await retry_client.close()


async def test_not_found_error_with_retry_client_raise_for_status(aiohttp_client):
    test_app = App()
    app = test_app.web_app

    client = await aiohttp_client(app)
    retry_client = RetryClient(raise_for_status=True)
    retry_client._client = client

    retry_options = ExponentialRetry(attempts=5, statuses={404})
    override_response = retry_client.get('/not_found_error', retry_options, raise_for_status=False)
    assert not override_response._raise_for_status
    response = retry_client.get('/not_found_error', retry_options)
    assert response._raise_for_status

    try:
        async with response:
            pass
    except ClientResponseError as exc:
        assert exc.status == 404
        assert test_app.counter == 5
    else:
        raise AssertionError('Expected ClientResponseError not raised')

    await retry_client.close()
    await client.close()


async def test_request(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.request(hdrs.METH_GET, '/ping') as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 1

    await retry_client.close()


async def test_url_as_yarl(aiohttp_client):
    """https://github.com/inyutin/aiohttp_retry/issues/41"""
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.get(URL('/ping')) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 1

    await retry_client.close()


async def test_change_client_retry_options(aiohttp_client):
    retry_options = ExponentialRetry(attempts=5)
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client, retry_options=retry_options)

    # first time with 5 attempts is okay
    async with retry_client.get('/sometimes_error') as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 3

    test_app.counter = 0
    retry_client.retry_options.attempts = 2

    # second time with 5 attempts is error
    async with retry_client.get('/sometimes_error') as response:
        text = await response.text()
        assert response.status == 500
        assert test_app.counter == 2

    await retry_client.close()


async def test_not_retry_server_errors(aiohttp_client):
    retry_options = ExponentialRetry(retry_all_server_errors=False)
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.get('/internal_error', retry_options) as response:
        assert response.status == 500
        assert test_app.counter == 1

    await retry_client.close()


async def test_list_retry_works_for_multiple_attempts(aiohttp_client):
    retry_options = ListRetry(timeouts=[0]*3)
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)

    async with retry_client.get('/internal_error', retry_options) as response:
        assert response.status == 500
        assert test_app.counter == 3

    await retry_client.close()


async def test_implicit_client(aiohttp_client):
    # check that if client not passed that it created implicitly
    test_app = App()

    retry_client = RetryClient()
    assert retry_client._client is not None

    retry_client._client = await aiohttp_client(test_app.web_app())
    async with retry_client.get('/ping') as response:
        assert response.status == 200

    await retry_client.close()


async def test_evaluate_response_callback(aiohttp_client):
    async def evaluate_response(response: ClientResponse) -> bool:
        try:
            await response.json()
        except:
            return False
        return True

    retry_options = ExponentialRetry(attempts=5, evaluate_response_callback=evaluate_response)
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client, retry_options=retry_options)

    async with retry_client.get('/sometimes_json') as response:
        body = await response.json()
        assert response.status == 200
        assert body == {'status': 'Ok!'}

        assert test_app.counter == 3


async def test_multiply_urls_by_requests(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.requests(
        params_list=[
            RequestParams(
                method='GET',
                url='/internal_error',
            ),
            RequestParams(
                method='GET',
                url='/ping',
            ),
        ]
    ) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 2

    await retry_client.close()


async def test_multiply_methods_by_requests(aiohttp_client):
    retry_options = ExponentialRetry(statuses={405})  # method not allowed
    retry_client, _ = await get_retry_client_and_test_app_for_test(aiohttp_client, retry_options=retry_options)
    async with retry_client.requests(
        params_list=[
            RequestParams(
                method='POST',
                url='/ping',
            ),
            RequestParams(
                method='GET',
                url='/ping',
            ),
        ]
    ) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

    await retry_client.close()


async def test_change_headers(aiohttp_client):
    retry_options = ExponentialRetry(statuses={406})
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client, retry_options=retry_options)
    async with retry_client.requests(
        params_list=[
            RequestParams(
                method='GET',
                url='/check_headers',
            ),
            RequestParams(
                method='GET',
                url='/check_headers',
                headers={'correct_headers': 'True'},
            ),
        ]
    ) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 2

    await retry_client.close()


async def test_additional_params(aiohttp_client):
    # https://github.com/inyutin/aiohttp_retry/issues/79
    auth = BasicAuth("username", "password")
    retry_client, _ = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.request(hdrs.METH_GET, '/with_auth', auth=auth) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

    await retry_client.close()


async def test_request_headers(aiohttp_client):
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)
    async with retry_client.get(url='/check_headers', headers={'correct_headers': 'True'}) as response:
        text = await response.text()
        assert response.status == 200
        assert text == 'Ok!'

        assert test_app.counter == 1

    await retry_client.close()


async def test_list_retry_all_failed(aiohttp_client):
    # there was a specific bug
    async def evaluate_response(response: ClientResponse) -> bool:
        return False

    retry_options = ListRetry(timeouts=[1]*3, statuses={403}, evaluate_response_callback=evaluate_response)
    retry_client, test_app = await get_retry_client_and_test_app_for_test(aiohttp_client)

    async with retry_client.get('/with_auth', retry_options=retry_options) as response:
        assert response.status == 403
        assert test_app.counter == 3

    await retry_client.close()