generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(testing-sdk): add dual-mode integration testing infrastructure #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """Example demonstrating nested child contexts (blocks).""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python.context import ( | ||
| DurableContext, | ||
| durable_with_child_context, | ||
| ) | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
|
|
||
|
|
||
| @durable_with_child_context | ||
| def nested_block(ctx: DurableContext) -> str: | ||
| """Nested block with its own child context.""" | ||
| # Wait in the nested block | ||
| ctx.wait(seconds=1) | ||
| return "nested block result" | ||
|
|
||
|
|
||
| @durable_with_child_context | ||
| def parent_block(ctx: DurableContext) -> dict[str, str]: | ||
| """Parent block with nested operations.""" | ||
| # Nested step | ||
| nested_result: str = ctx.step( | ||
| lambda _: "nested step result", | ||
| name="nested_step", | ||
| ) | ||
|
|
||
| # Nested block with its own child context | ||
| nested_block_result: str = ctx.run_in_child_context(nested_block()) | ||
|
|
||
| return { | ||
| "nestedStep": nested_result, | ||
| "nestedBlock": nested_block_result, | ||
| } | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(_event: Any, context: DurableContext) -> dict[str, str]: | ||
| """Handler demonstrating nested child contexts.""" | ||
| # Run parent block which contains nested operations | ||
| result: dict[str, str] = context.run_in_child_context( | ||
| parent_block(), name="parent_block" | ||
| ) | ||
|
|
||
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Example demonstrating logger usage in DurableContext.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python.context import ( | ||
| DurableContext, | ||
| durable_with_child_context, | ||
| ) | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
|
|
||
|
|
||
| @durable_with_child_context | ||
| def child_workflow(ctx: DurableContext) -> str: | ||
| """Child workflow with its own logging context.""" | ||
| # Child context logger has step_id populated with child context ID | ||
| ctx.logger.info("Running in child context") | ||
|
|
||
| # Step in child context has nested step ID | ||
| child_result: str = ctx.step( | ||
| lambda _: "child-processed", | ||
| name="child_step", | ||
| ) | ||
|
|
||
| ctx.logger.info("Child workflow completed", extra={"result": child_result}) | ||
|
|
||
| return child_result | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(event: Any, context: DurableContext) -> str: | ||
| """Handler demonstrating logger usage.""" | ||
| # Top-level context logger: no step_id field | ||
| context.logger.info("Starting workflow", extra={"eventId": event.get("id")}) | ||
|
|
||
| # Logger in steps - gets enriched with step ID and attempt number | ||
| result1: str = context.step( | ||
| lambda _: "processed", | ||
| name="process_data", | ||
| ) | ||
|
|
||
| context.logger.info("Step 1 completed", extra={"result": result1}) | ||
|
|
||
| # Child contexts inherit the parent's logger and have their own step ID | ||
| result2: str = context.run_in_child_context(child_workflow(), name="child_workflow") | ||
|
|
||
| context.logger.info( | ||
| "Workflow completed", extra={"result1": result1, "result2": result2} | ||
| ) | ||
|
|
||
| return f"{result1}-{result2}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| """Example demonstrating parallel-like operations for concurrent execution.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python.context import DurableContext | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(_event: Any, context: DurableContext) -> str: | ||
| # Execute multiple operations in parallel | ||
| def handler(_event: Any, context: DurableContext) -> list[str]: | ||
| # Execute multiple operations | ||
| task1 = context.step(lambda _: "Task 1 complete", name="task1") | ||
| task2 = context.step(lambda _: "Task 2 complete", name="task2") | ||
| task3 = context.step(lambda _: "Task 3 complete", name="task3") | ||
|
|
||
| # All tasks execute concurrently and results are collected | ||
| return f"Results: {task1}, {task2}, {task3}" | ||
| return [task1, task2, task3] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """Example demonstrating multiple steps with retry logic.""" | ||
|
|
||
| from random import random | ||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python.config import StepConfig | ||
| from aws_durable_execution_sdk_python.context import DurableContext | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
| from aws_durable_execution_sdk_python.retries import ( | ||
| RetryStrategyConfig, | ||
| create_retry_strategy, | ||
| ) | ||
|
|
||
|
|
||
| def simulated_get_item(name: str) -> dict[str, Any] | None: | ||
| """Simulate getting an item that may fail randomly.""" | ||
| # Fail 50% of the time | ||
| if random() < 0.5: # noqa: S311 | ||
| msg = "Random failure" | ||
| raise RuntimeError(msg) | ||
|
|
||
| # Simulate finding item after some attempts | ||
| if random() > 0.3: # noqa: S311 | ||
| return {"id": name, "data": "item data"} | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(event: Any, context: DurableContext) -> dict[str, Any]: | ||
| """Handler demonstrating polling with retry logic.""" | ||
| name = event.get("name", "test-item") | ||
|
|
||
| # Retry configuration for steps | ||
| retry_config = RetryStrategyConfig( | ||
| max_attempts=5, | ||
| retryable_error_types=[RuntimeError], | ||
| ) | ||
|
|
||
| step_config = StepConfig(create_retry_strategy(retry_config)) | ||
|
|
||
| item = None | ||
| poll_count = 0 | ||
| max_polls = 5 | ||
|
|
||
| try: | ||
| while poll_count < max_polls: | ||
| poll_count += 1 | ||
|
|
||
| # Try to get the item with retry | ||
| get_response = context.step( | ||
| lambda _, n=name: simulated_get_item(n), | ||
| name=f"get_item_poll_{poll_count}", | ||
| config=step_config, | ||
| ) | ||
|
|
||
| # Did we find the item? | ||
| if get_response: | ||
| item = get_response | ||
| break | ||
|
|
||
| # Wait 1 second until next poll | ||
| context.wait(seconds=1) | ||
|
|
||
| except RuntimeError as e: | ||
| # Retries exhausted | ||
| return {"error": "DDB Retries Exhausted", "message": str(e)} | ||
|
|
||
| if not item: | ||
| return {"error": "Item Not Found"} | ||
|
|
||
| # We found the item! | ||
| return {"success": True, "item": item, "pollsRequired": poll_count} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| """Example demonstrating wait-for-condition pattern.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from aws_durable_execution_sdk_python.context import DurableContext | ||
| from aws_durable_execution_sdk_python.execution import durable_execution | ||
|
|
||
|
|
||
| @durable_execution | ||
| def handler(_event: Any, context: DurableContext) -> int: | ||
| """Handler demonstrating wait-for-condition pattern.""" | ||
| state = 0 | ||
| attempt = 0 | ||
| max_attempts = 5 | ||
|
|
||
| while attempt < max_attempts: | ||
| attempt += 1 | ||
|
|
||
| # Execute step to update state | ||
| state = context.step( | ||
| lambda _, s=state: s + 1, | ||
| name=f"increment_state_{attempt}", | ||
| ) | ||
|
|
||
| # Check condition | ||
| if state >= 3: | ||
| # Condition met, stop | ||
| break | ||
|
|
||
| # Wait before next attempt | ||
| context.wait(seconds=1) | ||
|
|
||
| return state |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we introduce randomness like this, we should seed the random so that we don't get transient failures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So either seeding, or mocking the response and knowing the sequence. I prefer large sequence of randoms tested over multiple seeds.