Skip to content

Commit d703a22

Browse files
author
Alex Wang
committed
test(examples): add wait and handler error test examples
- Add testing examples - wait for condition - handler error - Update wait example with Duration change - Update web runner, return error response so that we can test on funciton exception
1 parent 74a4bf2 commit d703a22

File tree

15 files changed

+98
-41
lines changed

15 files changed

+98
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dist/
2929
.kiro/
3030
.idea
3131
.env
32+
.env*
3233

3334
.durable_executions
3435

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ from durable_executions_python_language_sdk.context import (
3838
durable_with_child_context,
3939
)
4040
from durable_executions_python_language_sdk.execution import durable_execution
41+
from aws_durable_execution_sdk_python.config import Duration
42+
4143

4244
@durable_step
4345
def one(a: int, b: int) -> str:
@@ -68,7 +70,7 @@ def function_under_test(event: Any, context: DurableContext) -> list[str]:
6870
result_one: str = context.step(one(1, 2))
6971
results.append(result_one)
7072

71-
context.wait(seconds=1)
73+
context.wait(Duration.from_seconds(1))
7274

7375
result_two: str = context.run_in_child_context(two(3, 4))
7476
results.append(result_two)

examples/examples-catalog.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@
297297
"ExecutionTimeout": 300
298298
},
299299
"path": "./src/parallel/parallel_with_batch_serdes.py"
300+
},
301+
{
302+
"name": "Handler Error",
303+
"description": "Simple function with handler error",
304+
"handler": "handler_error.handler",
305+
"integration": true,
306+
"durableConfig": {
307+
"RetentionPeriodInDays": 7,
308+
"ExecutionTimeout": 300
309+
},
310+
"path": "./src/handler_error/handler_error.py"
300311
}
301312
]
302313
}

examples/src/block_example/block_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77
durable_with_child_context,
88
)
99
from aws_durable_execution_sdk_python.execution import durable_execution
10+
from aws_durable_execution_sdk_python.config import Duration
1011

1112

1213
@durable_with_child_context
1314
def nested_block(ctx: DurableContext) -> str:
1415
"""Nested block with its own child context."""
1516
# Wait in the nested block
16-
ctx.wait(seconds=1)
17+
ctx.wait(Duration.from_seconds(1))
1718
return "nested block result"
1819

1920

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Demonstrates how handler-level errors are captured and structured in results."""
2+
3+
from typing import Any
4+
5+
from aws_durable_execution_sdk_python.context import DurableContext
6+
from aws_durable_execution_sdk_python.execution import durable_execution
7+
8+
9+
def generate_large_string(size_in_kb: int) -> str:
10+
"""Generate a string of approximately the specified size in KB."""
11+
base_string = "B" * 1024 # 1KB string
12+
return base_string * size_in_kb
13+
14+
15+
@durable_execution
16+
def handler(_event: Any, _context: DurableContext) -> None:
17+
"""Handler demonstrating handler-level error capture."""
18+
# Simulate a handler-level error that might occur in real applications
19+
raise Exception("Intentional handler failure")

examples/src/run_in_child_context/run_in_child_context_large_data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
durable_with_child_context,
88
)
99
from aws_durable_execution_sdk_python.execution import durable_execution
10+
from aws_durable_execution_sdk_python.config import Duration
1011

1112

1213
def generate_large_string(size_in_kb: int) -> str:
@@ -55,7 +56,7 @@ def handler(_event: Any, context: DurableContext) -> dict[str, Any]:
5556
)
5657

5758
# Add a wait after runInChildContext to test persistence across invocations
58-
context.wait(seconds=1, name="post-processing-wait")
59+
context.wait(Duration.from_seconds(1), name="post-processing-wait")
5960

6061
# Verify the data is still intact after the wait
6162
data_integrity_check = (

examples/src/step/steps_with_retry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from random import random
44
from typing import Any
55

6-
from aws_durable_execution_sdk_python.config import StepConfig
6+
from aws_durable_execution_sdk_python.config import StepConfig, Duration
77
from aws_durable_execution_sdk_python.context import DurableContext
88
from aws_durable_execution_sdk_python.execution import durable_execution
99
from aws_durable_execution_sdk_python.retries import (
@@ -60,7 +60,7 @@ def handler(event: Any, context: DurableContext) -> dict[str, Any]:
6060
break
6161

6262
# Wait 1 second until next poll
63-
context.wait(seconds=1)
63+
context.wait(Duration.from_seconds(1))
6464

6565
except RuntimeError as e:
6666
# Retries exhausted

examples/src/wait/multiple_wait.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
from aws_durable_execution_sdk_python.context import DurableContext
66
from aws_durable_execution_sdk_python.execution import durable_execution
7+
from aws_durable_execution_sdk_python.config import Duration
78

89

910
@durable_execution
1011
def handler(_event: Any, context: DurableContext) -> dict[str, Any]:
1112
"""Handler demonstrating multiple sequential wait operations."""
12-
context.wait(seconds=5, name="wait-1")
13-
context.wait(seconds=5, name="wait-2")
13+
context.wait(Duration.from_seconds(5), name="wait-1")
14+
context.wait(Duration.from_seconds(5), name="wait-2")
1415

1516
return {
1617
"completedWaits": 2,

examples/src/wait/wait.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from aws_durable_execution_sdk_python.context import DurableContext
44
from aws_durable_execution_sdk_python.execution import durable_execution
5+
from aws_durable_execution_sdk_python.config import Duration
56

67

78
@durable_execution
89
def handler(_event: Any, context: DurableContext) -> str:
9-
context.wait(seconds=5)
10+
context.wait(Duration.from_seconds(5))
1011
return "Wait completed"

examples/src/wait/wait_with_name.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
from aws_durable_execution_sdk_python.context import DurableContext
44
from aws_durable_execution_sdk_python.execution import durable_execution
5+
from aws_durable_execution_sdk_python.config import Duration
56

67

78
@durable_execution
89
def handler(_event: Any, context: DurableContext) -> str:
910
# Wait with explicit name
10-
context.wait(seconds=2, name="custom_wait")
11+
context.wait(Duration.from_seconds(2), name="custom_wait")
1112
return "Wait with name completed"

0 commit comments

Comments
 (0)