durable-lambda-testing
MockDurableContext and assertion helpers for testing durable Lambda handlers without AWS credentials.
Overview
No AWS credentials needed. durable-lambda-testing provides MockDurableContext and a suite of assertion helpers so you can test durable Lambda handlers entirely in-memory, without any AWS configuration or network calls.
This crate is part of the durable-rust SDK. It is battle-tested -- the SDK's own test suites (28 end-to-end tests, cross-approach parity tests, and Python-Rust compliance tests) all use MockDurableContext exclusively.
Features
MockDurableContextbuilder for creating pre-loaded durable contexts with step results, errors, waits, callbacks, and invokes- Two testing modes:
- Replay testing -- pre-load results, verify closures are NOT executed, assert no checkpoints
- Execute testing -- empty context, verify closures ARE executed, assert operation sequence
- 5 assertion helpers for verifying checkpoint calls and operation sequences
- Deterministic operation IDs using the same blake2b algorithm as the production engine
- Batch mode testing support via
build_with_batch_counter() - Zero external dependencies beyond
durable-lambda-core
Getting Started
Add to your Cargo.toml:
[]
= "0.1"
= { = "1", = ["full"] }
= "1"
Import everything via the prelude:
use *;
The prelude re-exports MockDurableContext, all assertion helpers, DurableContext, DurableError, StepOptions, ExecutionMode, checkpoint recorders, and operation recorders.
MockDurableContext Builder
MockDurableContext uses a builder pattern to pre-load completed operations. When you call .build(), it returns a tuple of (DurableContext, CheckpointRecorder, OperationRecorder).
Builder Methods
new
// Pre-load a successful step result (JSON string)
.with_step_result
// Pre-load a failed step (error type + JSON error data)
.with_step_error
// Pre-load a completed wait
.with_wait
// Pre-load a completed callback (callback_id + JSON result)
.with_callback
// Pre-load a completed invoke (JSON result)
.with_invoke
// Build and get context + recorders
.build
.await;
How It Works
- With pre-loaded operations: The context starts in Replaying mode. Step closures are NOT executed -- cached results are returned instead.
- Without pre-loaded operations: The context starts in Executing mode. Step closures are executed and their results are recorded.
Operation IDs are generated deterministically using blake2b, matching the core engine. The nth with_step_result() call corresponds to the nth ctx.step() call in your handler.
Testing Patterns
Replay Testing (verify cached results)
Pre-load results and verify that your handler correctly processes replayed data without re-executing closures:
use *;
async
Execute Testing (verify new execution)
Create an empty context and verify that closures execute and produce the expected operation sequence:
use *;
async
Error Replay Testing
Verify that your handler correctly processes replayed errors:
use *;
async
Mixed Replay + Execute Testing
Pre-load some operations and let the handler execute past the replay boundary:
use *;
async
Assertion Helpers
assert_no_checkpoints(calls)
Verify that no checkpoint API calls were made. Use in replay tests to confirm pure replay behavior.
assert_no_checkpoints.await;
assert_checkpoint_count(calls, n)
Verify the exact number of checkpoint API calls.
assert_checkpoint_count.await;
assert_operations(ops, expected)
Verify the exact operation sequence using "type:name" format strings.
assert_operations.await;
assert_operation_names(ops, expected)
Verify operation names only, ignoring operation types.
assert_operation_names.await;
assert_operation_count(ops, n)
Verify the total number of recorded operations.
assert_operation_count.await;
Batch Mode Testing
For testing batch checkpoint behavior, use build_with_batch_counter():
use *;
async
API Reference
Types
| Type | Description |
|---|---|
MockDurableContext |
Builder for creating mock contexts with pre-loaded results |
CheckpointRecorder |
Arc<Mutex<Vec<CheckpointCall>>> -- records checkpoint API calls |
OperationRecorder |
Arc<Mutex<Vec<OperationRecord>>> -- records executed operations |
BatchCallCounter |
Arc<Mutex<usize>> -- counts batch checkpoint calls |
CheckpointCall |
Details of a single checkpoint API call |
OperationRecord |
Details of a single executed operation |
Re-exported from durable-lambda-core
| Type | Description |
|---|---|
DurableContext |
The context type your handler receives |
DurableError |
SDK infrastructure error type |
StepOptions |
Step configuration (retries, backoff, timeout) |
ExecutionMode |
Replaying or Executing |
Full API documentation: docs.rs/durable-lambda-testing
License
Licensed under either of MIT or Apache-2.0 at your option.