Skip to content

Commit c69358f

Browse files
author
Andrei Neagu
committed
added cancellation tests
1 parent 1723000 commit c69358f

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

packages/service-library/src/servicelib/long_running_interfaces/_client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from ._errors import (
1212
AlreadyStartedError,
1313
FinishedWithError,
14+
NoMoreRetryAttemptsError,
1415
TimedOutError,
15-
UnexpectedNoMoreRetryAttemptsError,
1616
UnexpectedResultTypeError,
1717
UnexpectedStatusError,
1818
)
@@ -145,8 +145,7 @@ async def _decrease_remaining_attempts_or_raise(
145145
traceback=last_result.error.traceback,
146146
)
147147

148-
# NOTE: this edge case should not happen
149-
raise UnexpectedNoMoreRetryAttemptsError(
148+
raise NoMoreRetryAttemptsError(
150149
unique_id=unique_id,
151150
retry_count=retry_count,
152151
remaining_attempts=remaining_attempts,

packages/service-library/src/servicelib/long_running_interfaces/_errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FinishedWithError(BaseLongRunningError):
1515
msg_template = "unique_id='{unique_id}' finished with error='{error}' message='{message}'\n{traceback}"
1616

1717

18-
class UnexpectedNoMoreRetryAttemptsError(FinishedWithError):
18+
class NoMoreRetryAttemptsError(FinishedWithError):
1919
msg_template = "attempt {remaining_attempts} of {retry_count} for unique_id='{unique_id}' with last_result='{last_result}'"
2020

2121

packages/service-library/src/servicelib/long_running_interfaces/_rpc/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ async def result(self, unique_id: JobUniqueId) -> ResultModel:
104104
traceback=formatted_traceback,
105105
)
106106
)
107+
finally:
108+
await self.remove(unique_id)

packages/service-library/tests/long_running_interfaces/test_workflow.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# pylint:disable=unused-argument
33

44
import asyncio
5-
from asyncio import CancelledError
65
from collections.abc import AsyncIterable
76
from dataclasses import dataclass
87
from datetime import timedelta
@@ -212,7 +211,9 @@ async def _cancel_task_in_server(server: Server) -> None:
212211
task.cancel()
213212

214213

215-
async def test_cancellation_from_server(server: Server, client: Client):
214+
async def test_cancellation_from_server_retires_and_finishes(
215+
server: Server, client: Client
216+
):
216217
async def _to_run() -> None:
217218
result = await client.ensure_result(
218219
"sleep_for_f",
@@ -229,7 +230,29 @@ async def _to_run() -> None:
229230

230231
await _cancel_task_in_server(server)
231232

233+
await task
234+
235+
236+
async def test_cancellation_from_server_fails_if_no_more_retries_available(
237+
server: Server, client: Client
238+
):
239+
async def _to_run() -> None:
240+
result = await client.ensure_result(
241+
"sleep_for_f",
242+
expected_type=type(None),
243+
timeout=timedelta(seconds=10),
244+
duration=2,
245+
retry_count=1,
246+
)
247+
assert result is None
248+
249+
await _assert_tasks_count(server, count=0)
250+
251+
task = asyncio.create_task(_to_run())
252+
await _assert_tasks_count(server, count=1)
253+
254+
await _cancel_task_in_server(server)
255+
232256
with pytest.raises(FinishedWithError) as exec_info:
233257
await task
234-
235-
assert exec_info.value.error == CancelledError
258+
assert exec_info.value.error == asyncio.CancelledError

0 commit comments

Comments
 (0)