Skip to content

Commit 3365552

Browse files
committed
fix: make retry examples deterministic using seeded random
1 parent 402a348 commit 3365552

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

examples/src/step_with_retry.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from random import random
1+
import random
22
from typing import Any
33

44
from aws_durable_execution_sdk_python.config import StepConfig
@@ -18,15 +18,20 @@
1818
def unreliable_operation(
1919
_step_context: StepContext,
2020
) -> str:
21+
# Use seeded random for deterministic behavior
22+
# With seed 42, this will fail twice then succeed
2123
failure_threshold = 0.5
22-
if random() > failure_threshold: # noqa: S311
24+
if random.random() > failure_threshold: # noqa: S311
2325
msg = "Random error occurred"
2426
raise RuntimeError(msg)
2527
return "Operation succeeded"
2628

2729

2830
@durable_execution
2931
def handler(_event: Any, context: DurableContext) -> str:
32+
# Seed random for deterministic behavior
33+
random.seed(42)
34+
3035
retry_config = RetryStrategyConfig(
3136
max_attempts=3,
3237
retryable_error_types=[RuntimeError],

examples/src/steps_with_retry.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
"""Example demonstrating multiple steps with retry logic."""
22

3-
from random import random
3+
import random
44
from typing import Any
55

66
from aws_durable_execution_sdk_python.config import StepConfig
7-
from aws_durable_execution_sdk_python.context import DurableContext
7+
from aws_durable_execution_sdk_python.context import DurableContext, StepContext
88
from aws_durable_execution_sdk_python.execution import durable_execution
99
from aws_durable_execution_sdk_python.retries import (
1010
RetryStrategyConfig,
1111
create_retry_strategy,
1212
)
1313

1414

15-
def simulated_get_item(name: str) -> dict[str, Any] | None:
16-
"""Simulate getting an item that may fail randomly."""
15+
def simulated_get_item(_step_context: StepContext, name: str) -> dict[str, Any] | None:
16+
"""Simulate getting an item with deterministic seeded random behavior."""
17+
# Use seeded random for deterministic behavior
1718
# Fail 50% of the time
18-
if random() < 0.5: # noqa: S311
19+
if random.random() < 0.5: # noqa: S311
1920
msg = "Random failure"
2021
raise RuntimeError(msg)
2122

2223
# Simulate finding item after some attempts
23-
if random() > 0.3: # noqa: S311
24+
if random.random() > 0.3: # noqa: S311
2425
return {"id": name, "data": "item data"}
2526

2627
return None
@@ -29,6 +30,9 @@ def simulated_get_item(name: str) -> dict[str, Any] | None:
2930
@durable_execution
3031
def handler(event: Any, context: DurableContext) -> dict[str, Any]:
3132
"""Handler demonstrating polling with retry logic."""
33+
# Seed random for deterministic behavior
34+
random.seed(42)
35+
3236
name = event.get("name", "test-item")
3337

3438
# Retry configuration for steps
@@ -49,7 +53,7 @@ def handler(event: Any, context: DurableContext) -> dict[str, Any]:
4953

5054
# Try to get the item with retry
5155
get_response = context.step(
52-
lambda _, n=name: simulated_get_item(n),
56+
lambda _, n=name: simulated_get_item(_, n),
5357
name=f"get_item_poll_{poll_count}",
5458
config=step_config,
5559
)

0 commit comments

Comments
 (0)