Skip to content

Commit 767860c

Browse files
GitHKAndrei Neagu
andauthored
🐛 adds tests for S3TransferError; refactoring flaky CI test; better logging for long running task errors (ITISFoundation#3525)
Co-authored-by: Andrei Neagu <[email protected]>
1 parent 31ac97f commit 767860c

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

packages/service-library/src/servicelib/long_running_tasks/_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def get_task_result_old(self, task_id: TaskId) -> TaskResult:
254254
error = TaskExceptionError(
255255
task_id=task_id, exception=exception, traceback=formatted_traceback
256256
)
257-
logger.warning("%s", f"{error}")
257+
logger.warning("Task %s finished with error: %s", task_id, f"{error}")
258258
return TaskResult(result=None, error=f"{error}")
259259
except asyncio.CancelledError:
260260
error = TaskCancelledError(task_id=task_id)

packages/service-library/tests/fastapi/long_running_tasks/test_long_running_tasks_context_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async def test_task_result_times_out(
122122

123123
url = parse_obj_as(AnyHttpUrl, "http://backgroud.testserver.io")
124124
client = Client(app=bg_task_app, async_client=async_client, base_url=url)
125-
timeout = TASK_SLEEP_INTERVAL / 2
125+
timeout = TASK_SLEEP_INTERVAL / 10
126126
with pytest.raises(TaskClientTimeoutError) as exec_info:
127127
async with periodic_task_result(
128128
client,
Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,81 @@
11
# pylint: disable=redefined-outer-name
22
# pylint: disable=unused-argument
33

4-
from asyncio import BaseEventLoop
4+
from dataclasses import dataclass
5+
from typing import AsyncIterable
56

67
import pytest
7-
from aiohttp import web
8-
from aiohttp.test_utils import TestClient
8+
from aiohttp import ClientResponse, ClientSession
9+
from aioresponses import aioresponses
910
from simcore_sdk.node_ports_common.file_io_utils import (
1011
ExtendedClientResponseError,
12+
_check_for_aws_http_errors,
1113
_raise_for_status,
1214
)
1315

14-
TEST_ERROR = "OPSIE there was an error here"
16+
A_TEST_ROUTE = "http://a-fake-address:1249/test-route"
1517

1618

1719
@pytest.fixture
18-
def test_client(loop: BaseEventLoop, aiohttp_client) -> TestClient:
19-
async def raise_error(request):
20-
return web.Response(body=TEST_ERROR, status=web.HTTPBadRequest.status_code)
20+
async def client_session() -> AsyncIterable[ClientSession]:
21+
async with ClientSession() as session:
22+
yield session
2123

22-
app = web.Application()
23-
app.router.add_get("/", raise_error)
24-
return loop.run_until_complete(aiohttp_client(app))
2524

25+
async def test_raise_for_status(
26+
aioresponses_mocker: aioresponses, client_session: ClientSession
27+
):
28+
aioresponses_mocker.get(
29+
A_TEST_ROUTE, body="OPSIE there was an error here", status=400
30+
)
2631

27-
async def test_raise_for_status(test_client: TestClient):
28-
resp = await test_client.get("/")
29-
with pytest.raises(ExtendedClientResponseError) as exe_info:
30-
await _raise_for_status(resp)
31-
assert TEST_ERROR in f"{exe_info.value}"
32+
async with client_session.get(A_TEST_ROUTE) as resp:
33+
assert isinstance(resp, ClientResponse)
34+
35+
with pytest.raises(ExtendedClientResponseError) as exe_info:
36+
await _raise_for_status(resp)
37+
assert "OPSIE there was an error here" in f"{exe_info.value}"
38+
39+
40+
@dataclass
41+
class _TestParams:
42+
will_retry: bool
43+
status_code: int
44+
body: str = ""
45+
46+
47+
@pytest.mark.parametrize(
48+
"test_params",
49+
[
50+
_TestParams(
51+
will_retry=True,
52+
status_code=400,
53+
body='<?xml version="1.0" encoding="UTF-8"?><Error><Code>RequestTimeout</Code>'
54+
"<Message>Your socket connection to the server was not read from or written to within "
55+
"the timeout period. Idle connections will be closed.</Message>"
56+
"<RequestId>7EE901348D6C6812</RequestId><HostId>"
57+
"FfQE7jdbUt39E6mcQq/"
58+
"ZeNR52ghjv60fccNT4gCE4IranXjsGLG+L6FUyiIxx1tAuXL9xtz2NAY7ZlbzMTm94fhY3TBiCBmf"
59+
"</HostId></Error>",
60+
),
61+
_TestParams(will_retry=True, status_code=500),
62+
_TestParams(will_retry=True, status_code=503),
63+
_TestParams(will_retry=False, status_code=400),
64+
_TestParams(will_retry=False, status_code=200),
65+
_TestParams(will_retry=False, status_code=399),
66+
],
67+
)
68+
async def test_check_for_aws_http_errors(
69+
aioresponses_mocker: aioresponses,
70+
client_session: ClientSession,
71+
test_params: _TestParams,
72+
):
73+
aioresponses_mocker.get(
74+
A_TEST_ROUTE, body=test_params.body, status=test_params.status_code
75+
)
76+
77+
async with client_session.get(A_TEST_ROUTE) as resp:
78+
try:
79+
await _raise_for_status(resp)
80+
except ExtendedClientResponseError as exception:
81+
assert _check_for_aws_http_errors(exception) is test_params.will_retry

0 commit comments

Comments
 (0)