Skip to content

Commit

Permalink
fixed retry event attempt counter being higher by 1 and added more sp…
Browse files Browse the repository at this point in the history
…ecific retrying tests
  • Loading branch information
georgebv committed Mar 25, 2024
1 parent 5738b62 commit b1e7c14
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docs/documentation/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ or by checking the type of the event object.
!!! note "Note"

The `attempt` attribute of the `RetryEvent` indicates sequential number
of the retry attempt. The first attempt is `1`, the second is `2`, and so on.
of the retry attempt. The first retry attempt is `1`, the second is `2`, and so on.
Attempt `0` is the initial request and is not a retry.

!!! note "Note"

Expand Down
4 changes: 2 additions & 2 deletions src/aiosalesforce/retries/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ async def send_request_with_retries(
event_bus.publish_event(
RetryEvent(
type="retry",
attempt=self.retry_count["total"] + 1,
attempt=self.retry_count["total"],
request=request,
exception=exc,
)
Expand All @@ -164,7 +164,7 @@ async def send_request_with_retries(
event_bus.publish_event(
RetryEvent(
type="retry",
attempt=self.retry_count["total"] + 1,
attempt=self.retry_count["total"],
request=request,
response=response,
)
Expand Down
121 changes: 112 additions & 9 deletions tests/test_retries.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio

from unittest.mock import AsyncMock, MagicMock, call, patch
from unittest.mock import ANY, AsyncMock, MagicMock, call, patch

import httpx
import pytest
import respx
import time_machine

from aiosalesforce.events import EventBus, RestApiCallConsumptionEvent
from aiosalesforce.events import EventBus, RestApiCallConsumptionEvent, RetryEvent
from aiosalesforce.exceptions import SalesforceError
from aiosalesforce.retries import (
ExceptionRule,
Expand Down Expand Up @@ -263,10 +263,33 @@ async def test_request_retry_on_exception(
assert response.status_code == 200

# Assert event hook was called:
# - 1 for consumption (exception retries don't consume API calls)
# - 2 for retry
assert event_hook.await_count == 4
assert sleep_mock.await_count == 3
event_hook.assert_has_awaits(
[
# 3 retries (exceptions don't consume API calls)
*[
call(
RetryEvent(
type="retry",
attempt=i,
request=ANY,
response=None,
exception=ANY,
)
)
for i in range(1, 4)
],
# 1 consumption on success
call(
RestApiCallConsumptionEvent(
type="rest_api_call_consumption",
response=response,
count=1,
)
),
]
)

async def test_request_retry_on_exception_exceed_limit(
self,
Expand Down Expand Up @@ -312,9 +335,25 @@ async def test_request_retry_on_exception_exceed_limit(
)

# Assert event hook was called:
# - 3 for retry (no consumption because exceptions don't consume API calls)
assert event_hook.await_count == 3
assert sleep_mock.await_count == 3
event_hook.assert_has_awaits(
[
# 3 retries (exceptions don't consume API calls)
*[
call(
RetryEvent(
type="retry",
attempt=i,
request=ANY,
response=None,
exception=ANY,
)
)
for i in range(1, 4)
],
]
)

async def test_request_retry_on_response(
self,
Expand Down Expand Up @@ -356,10 +395,42 @@ async def test_request_retry_on_response(
assert response.status_code == 200

# Assert event hook was called:
# - 3 for consumption
# - 2 for retry
assert event_hook.await_count == 5
assert sleep_mock.await_count == 2
calls = []
# 2 retries with 1 consumption each
for i in range(1, 3):
calls.extend(
[
call(
RestApiCallConsumptionEvent(
type="rest_api_call_consumption",
response=ANY,
count=1,
)
),
call(
RetryEvent(
type="retry",
attempt=i,
request=ANY,
response=ANY,
exception=None,
)
),
]
)
# 1 consumption on final successfull request
calls.append(
call(
RestApiCallConsumptionEvent(
type="rest_api_call_consumption",
response=response,
count=1,
)
)
)
event_hook.assert_has_awaits(calls)

async def test_request_retry_on_response_exceed_limit(
self,
Expand Down Expand Up @@ -403,7 +474,39 @@ async def test_request_retry_on_response_exceed_limit(
assert response.status_code == 504

# Assert event hook was called:
# - 4 for consumption
# - 3 for retry
assert event_hook.await_count == 7
assert sleep_mock.await_count == 3
calls = []
# 3 retries with 1 consumption each
for i in range(1, 4):
calls.extend(
[
call(
RestApiCallConsumptionEvent(
type="rest_api_call_consumption",
response=ANY,
count=1,
)
),
call(
RetryEvent(
type="retry",
attempt=i,
request=ANY,
response=ANY,
exception=None,
)
),
]
)
# 1 consumption on final failed request
calls.append(
call(
RestApiCallConsumptionEvent(
type="rest_api_call_consumption",
response=response,
count=1,
)
)
)
event_hook.assert_has_awaits(calls)

0 comments on commit b1e7c14

Please sign in to comment.