|
1 | | -## My Project |
| 1 | +# aws-durable-functions-sdk-python |
2 | 2 |
|
3 | | -TODO: Fill this README out! |
| 3 | +[](https://pypi.org/project/aws-durable-functions-sdk-python) |
| 4 | +[](https://pypi.org/project/aws-durable-functions-sdk-python) |
4 | 5 |
|
5 | | -Be sure to: |
| 6 | +----- |
6 | 7 |
|
7 | | -* Change the title in this README |
8 | | -* Edit your repository description on GitHub |
| 8 | +## Table of Contents |
9 | 9 |
|
10 | | -## Security |
| 10 | +- [Installation](#installation) |
| 11 | +- [Quick Start](#quick-start) |
| 12 | +- [Architecture](#architecture) |
| 13 | +- [Developer Guide](#developers) |
| 14 | +- [License](#license) |
11 | 15 |
|
12 | | -See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more information. |
| 16 | +## Installation |
13 | 17 |
|
14 | | -## License |
| 18 | +```console |
| 19 | +pip install aws-durable-functions-sdk-python-testing |
| 20 | +``` |
| 21 | + |
| 22 | +## Overview |
| 23 | + |
| 24 | +Use the Durable Functions Python Testing Framework to test your Python Durable Functions locally. |
| 25 | + |
| 26 | +The test framework contains a local runner, so you can run and test your Durable Function locally |
| 27 | +before you deploy it. |
| 28 | + |
| 29 | +## Quick Start |
| 30 | + |
| 31 | +### A Durable Function under test |
| 32 | + |
| 33 | +```python |
| 34 | +from durable_executions_python_language_sdk.context import ( |
| 35 | + DurableContext, |
| 36 | + durable_step, |
| 37 | + durable_with_child_context, |
| 38 | +) |
| 39 | +from durable_executions_python_language_sdk.execution import durable_handler |
| 40 | + |
| 41 | +@durable_step |
| 42 | +def one(a: int, b: int) -> str: |
| 43 | + return f"{a} {b}" |
| 44 | + |
| 45 | +@durable_step |
| 46 | +def two_1(a: int, b: int) -> str: |
| 47 | + return f"{a} {b}" |
| 48 | + |
| 49 | +@durable_step |
| 50 | +def two_2(a: int, b: int) -> str: |
| 51 | + return f"{b} {a}" |
| 52 | + |
| 53 | +@durable_with_child_context |
| 54 | +def two(ctx: DurableContext, a: int, b: int) -> str: |
| 55 | + two_1_result: str = ctx.step(two_1(a, b)) |
| 56 | + two_2_result: str = ctx.step(two_2(a, b)) |
| 57 | + return f"{two_1_result} {two_2_result}" |
| 58 | + |
| 59 | +@durable_step |
| 60 | +def three(a: int, b: int) -> str: |
| 61 | + return f"{a} {b}" |
| 62 | + |
| 63 | +@durable_handler |
| 64 | +def function_under_test(event: Any, context: DurableContext) -> list[str]: |
| 65 | + results: list[str] = [] |
| 66 | + |
| 67 | + result_one: str = context.step(one(1, 2)) |
| 68 | + results.append(result_one) |
| 69 | + |
| 70 | + context.wait(seconds=1) |
| 71 | + |
| 72 | + result_two: str = context.run_in_child_context(two(3, 4)) |
| 73 | + results.append(result_two) |
| 74 | + |
| 75 | + result_three: str = context.step(three(5, 6)) |
| 76 | + results.append(result_three) |
| 77 | + |
| 78 | + return results |
| 79 | +``` |
| 80 | + |
| 81 | +### Your test code |
| 82 | + |
| 83 | +```python |
| 84 | +from aws_durable_functions_sdk_python.execution import InvocationStatus |
| 85 | +from aws_durable_functions_sdk_python_testing.runner import ( |
| 86 | + ContextOperation, |
| 87 | + DurableFunctionTestResult, |
| 88 | + DurableFunctionTestRunner, |
| 89 | + StepOperation, |
| 90 | +) |
| 91 | + |
| 92 | +def test_my_durable_functions(): |
| 93 | + with DurableFunctionTestRunner(handler=function_under_test) as runner: |
| 94 | + result: DurableFunctionTestResult = runner.run(input="input str", timeout=10) |
15 | 95 |
|
16 | | -This project is licensed under the Apache-2.0 License. |
| 96 | + assert result.status is InvocationStatus.SUCCEEDED |
| 97 | + assert result.result == '["1 2", "3 4 4 3", "5 6"]' |
| 98 | + |
| 99 | + one_result: StepOperation = result.get_step("one") |
| 100 | + assert one_result.result == '"1 2"' |
| 101 | + |
| 102 | + two_result: ContextOperation = result.get_context("two") |
| 103 | + assert two_result.result == '"3 4 4 3"' |
| 104 | + |
| 105 | + three_result: StepOperation = result.get_step("three") |
| 106 | + assert three_result.result == '"5 6"' |
| 107 | +``` |
| 108 | +## Architecture |
| 109 | + |
| 110 | + |
| 111 | +## Event Flow |
| 112 | + |
| 113 | + |
| 114 | +1. **DurableTestRunner** starts execution via **Executor** |
| 115 | +2. **Executor** creates **Execution** and schedules initial invocation |
| 116 | +3. During execution, checkpoints are processed by **CheckpointProcessor** |
| 117 | +4. **Individual Processors** transform operation updates and may trigger events |
| 118 | +5. **ExecutionNotifier** broadcasts events to **Executor** (observer) |
| 119 | +6. **Executor** updates **Execution** state based on events |
| 120 | +7. **Execution** completion triggers final event notifications |
| 121 | +8. **DurableTestRunner** run() blocks until it receives completion event, and then returns `DurableFunctionTestResult`. |
| 122 | + |
| 123 | +## Major Components |
| 124 | + |
| 125 | +### Core Execution Flow |
| 126 | +- **DurableTestRunner** - Main entry point that orchestrates test execution |
| 127 | +- **Executor** - Manages execution lifecycle. Mutates Execution. |
| 128 | +- **Execution** - Represents the state and operations of a single durable execution |
| 129 | + |
| 130 | +### Service Client Integration |
| 131 | +- **InMemoryServiceClient** - Replaces AWS Lambda service client for local testing. Injected into SDK via `DurableExecutionInvocationInputWithClient` |
| 132 | + |
| 133 | +### Checkpoint Processing Pipeline |
| 134 | +- **CheckpointProcessor** - Orchestrates operation transformations and validation |
| 135 | +- **Individual Validators** - Validate operation updates and state transitions |
| 136 | +- **Individual Processors** - Transform operation updates into operations (step, wait, callback, context, execution) |
| 137 | + |
| 138 | +### Execution status changes (Observer Pattern) |
| 139 | +- **ExecutionNotifier** - Notifies observers of execution events |
| 140 | +- **ExecutionObserver** - Interface for receiving execution lifecycle events |
| 141 | +- **Executor** implements `ExecutionObserver` to handle completion events |
| 142 | + |
| 143 | +## Component Relationships |
| 144 | + |
| 145 | +### 1. DurableTestRunner → Executor → Execution |
| 146 | +- **DurableTestRunner** serves as the main API entry point and sets up all components |
| 147 | +- **Executor** manages the execution lifecycle, handling invocations and state transitions |
| 148 | +- **Execution** maintains the state of operations and completion status |
| 149 | + |
| 150 | +### 2. Service Client Injection |
| 151 | +- **DurableTestRunner** creates **InMemoryServiceClient** with **CheckpointProcessor** |
| 152 | +- **InProcessInvoker** injects the service client into SDK via `DurableExecutionInvocationInputWithClient` |
| 153 | +- When durable functions call checkpoint operations, they're intercepted by **InMemoryServiceClient** |
| 154 | +- **InMemoryServiceClient** delegates to **CheckpointProcessor** for local processing |
| 155 | + |
| 156 | +### 3. CheckpointProcessor → Individual Validators → Individual Processors |
| 157 | +- **CheckpointProcessor** orchestrates the checkpoint processing pipeline |
| 158 | +- **Individual Validators** (CheckpointValidator, TransitionsValidator, and operation-specific validators) ensure operation updates are valid |
| 159 | +- **Individual Processors** (StepProcessor, WaitProcessor, etc.) transform `OperationUpdate` into `Operation` |
| 160 | + |
| 161 | +### 4. Observer Pattern Flow |
| 162 | +The observer pattern enables loose coupling between checkpoint processing and execution management: |
| 163 | + |
| 164 | +1. **CheckpointProcessor** processes operation updates |
| 165 | +2. **Individual Processors** detect state changes (completion, failures, timer scheduling) |
| 166 | +3. **ExecutionNotifier** broadcasts events to registered observers |
| 167 | +4. **Executor** (as ExecutionObserver) receives notifications and updates **Execution** state |
| 168 | +5. **Execution** complete_* methods finalize the execution state |
| 169 | + |
| 170 | + |
| 171 | +## Developers |
| 172 | +Please see [CONTRIBUTING.md](CONTRIBUTING.md). It contains the testing guide, sample commands and instructions |
| 173 | +for how to contribute to this package. |
| 174 | + |
| 175 | +tldr; use `hatch` and it will manage virtual envs and dependencies for you, so you don't have to do it manually. |
| 176 | + |
| 177 | +## License |
17 | 178 |
|
| 179 | +This project is licensed under the [Apache-2.0 License](LICENSE). |
0 commit comments