Skip to content

Commit

Permalink
fix(fetch): increase timeout in requests (#663)
Browse files Browse the repository at this point in the history
Signed-off-by: Dariusz Duda <[email protected]>
  • Loading branch information
dariuszd21 authored Feb 26, 2025
1 parent cf0598f commit 4a17a58
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
33 changes: 24 additions & 9 deletions craft_application/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,26 @@ def stop_service(fetch_process: subprocess.Popen[str]) -> None:
fetch_process.kill()


def create_session(*, strict: bool) -> SessionData:
def create_session(*, strict: bool, timeout: float = 5.0) -> SessionData:
"""Create a new fetch-service session.
:param strict: Whether the created session should be strict.
:param timeout: Maximum time to wait for the response from the fetch-service
:return: a SessionData object containing the session's id and token.
"""
json = {"policy": "strict" if strict else "permissive"}
data = _service_request("post", "session", json=json).json()
data = _service_request("post", "session", json=json, timeout=timeout).json()

return SessionData.unmarshal(data=data)


def teardown_session(session_data: SessionData) -> dict[str, Any]:
def teardown_session(
session_data: SessionData, timeout: float = 10.0
) -> dict[str, Any]:
"""Stop and cleanup a running fetch-service session.
:param session_data: the data of a previously-created session.
:param timeout: Maximum time to wait for the response from the fetch-service
:return: A dict containing the session's report (the contents and format
of this dict are still subject to change).
"""
Expand All @@ -250,17 +254,25 @@ def teardown_session(session_data: SessionData) -> dict[str, Any]:

# Revoke token
_revoke_data = _service_request(
"delete", f"session/{session_id}/token", json={"token": session_token}
"delete",
f"session/{session_id}/token",
json={"token": session_token},
timeout=timeout,
).json()

# Get session report
session_report = _service_request("get", f"session/{session_id}", json={}).json()
session_report = _service_request(
"get",
f"session/{session_id}",
json={},
timeout=timeout,
).json()

# Delete session
_service_request("delete", f"session/{session_id}")
_service_request("delete", f"session/{session_id}", timeout=timeout)

# Delete session resources
_service_request("delete", f"resources/{session_id}")
_service_request("delete", f"resources/{session_id}", timeout=timeout)

return cast(dict[str, Any], session_report)

Expand Down Expand Up @@ -302,7 +314,10 @@ def verify_installed() -> None:


def _service_request(
verb: str, endpoint: str, json: dict[str, Any] | None = None
verb: str,
endpoint: str,
json: dict[str, Any] | None = None,
timeout: float = 10.0,
) -> requests.Response:
headers = {
"Content-type": "application/json",
Expand All @@ -315,7 +330,7 @@ def _service_request(
auth=auth,
headers=headers,
json=json, # Use defaults
timeout=0.1,
timeout=timeout,
)
response.raise_for_status()
except requests.RequestException as err:
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
Changelog
*********

4.x.x (YYYY-MMM-DD)
-------------------

Application
===========

- Increase timeout in fetch-service queries.

4.9.1 (2025-Feb-12)
-------------------

Expand Down
15 changes: 13 additions & 2 deletions tests/unit/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ def test_start_service_not_installed(mocker):
("strict", "expected_policy"), [(True, "strict"), (False, "permissive")]
)
def test_create_session(strict, expected_policy):
create_session_timeout = 5.0
responses.add(
responses.POST,
f"http://localhost:{CONTROL}/session",
json={"id": "my-session-id", "token": "my-session-token"},
status=200,
match=[matchers.json_params_matcher({"policy": expected_policy})],
match=[
matchers.json_params_matcher({"policy": expected_policy}),
matchers.request_kwargs_matcher({"timeout": create_session_timeout}),
],
)

session_data = fetch.create_session(strict=strict)
Expand All @@ -171,30 +175,37 @@ def test_create_session(strict, expected_policy):
@assert_requests
def test_teardown_session():
session_data = fetch.SessionData(id="my-session-id", token="my-session-token")
default_timeout = 10.0

# Call to delete token
responses.delete(
f"http://localhost:{CONTROL}/session/{session_data.session_id}/token",
match=[matchers.json_params_matcher({"token": session_data.token})],
match=[
matchers.json_params_matcher({"token": session_data.token}),
matchers.request_kwargs_matcher({"timeout": default_timeout}),
],
json={},
status=200,
)
# Call to get session report
responses.get(
f"http://localhost:{CONTROL}/session/{session_data.session_id}",
json={},
match=[matchers.request_kwargs_matcher({"timeout": default_timeout})],
status=200,
)
# Call to delete session
responses.delete(
f"http://localhost:{CONTROL}/session/{session_data.session_id}",
json={},
match=[matchers.request_kwargs_matcher({"timeout": default_timeout})],
status=200,
)
# Call to delete session resources
responses.delete(
f"http://localhost:{CONTROL}/resources/{session_data.session_id}",
json={},
match=[matchers.request_kwargs_matcher({"timeout": default_timeout})],
status=200,
)

Expand Down

0 comments on commit 4a17a58

Please sign in to comment.