Skip to content

Commit 47fdd70

Browse files
committed
temp changes
1 parent 21ef781 commit 47fdd70

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

google/cloud/storage/_experimental/asyncio/retry/bidi_stream_retry_manager.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,22 @@ async def execute(self, initial_state: Any, retry_policy):
6969
self._strategy.update_state_from_response(response, state)
7070
return
7171
except Exception as e:
72-
if not retry_policy._predicate(e):
73-
raise
72+
# AsyncRetry may expose either 'on_error' (public) or the private
73+
# '_on_error' depending on google.api_core version. Call whichever
74+
# exists so the retry policy can decide to raise (non-retriable /
75+
# deadline exceeded) or allow a retry.
76+
on_error_callable = getattr(retry_policy, "on_error", None)
77+
if on_error_callable is None:
78+
on_error_callable = getattr(retry_policy, "_on_error", None)
7479

75-
await self._strategy.recover_state_on_failure(e, state)
80+
if on_error_callable is None:
81+
# No hook available on the policy; re-raise the error.
82+
raise
7683

77-
sleep = next(sleep_generator)
78-
if deadline is not None and time.monotonic() + sleep > deadline:
79-
raise exceptions.RetryError(
80-
f"Deadline of {retry_policy._deadline}s exceeded", cause=e
81-
) from e
84+
# Let the retry policy handle the error (may raise RetryError).
85+
await on_error_callable(e)
8286

83-
await asyncio.sleep(sleep)
87+
# If the retry policy did not raise, allow the strategy to recover
88+
# and then sleep per policy before next attempt.
89+
await self._strategy.recover_state_on_failure(e, state)
90+
await retry_policy.sleep()

tests/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import asyncio
2+
3+
# Ensure there's an event loop in the main thread so grpc.aio.create_channel
4+
# and other code that expects a loop at import/initialization time don't fail.
5+
try:
6+
asyncio.get_running_loop()
7+
except RuntimeError:
8+
loop = asyncio.new_event_loop()
9+
asyncio.set_event_loop(loop)

tests/unit/asyncio/retry/test_bidi_stream_retry_manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ async def mock_stream_opener(*args, **kwargs):
5959
strategy=mock_strategy, stream_opener=mock_stream_opener
6060
)
6161
retry_policy = AsyncRetry(predicate=_is_retriable, initial=0.01)
62+
retry_policy.sleep = mock.AsyncMock()
63+
6264
await retry_manager.execute(initial_state={}, retry_policy=retry_policy)
6365

6466
self.assertEqual(attempt_count, 2)

0 commit comments

Comments
 (0)