pub struct MockDurableContext { /* private fields */ }Expand description
Builder for creating a DurableContext with pre-loaded step results.
MockDurableContext generates a DurableContext in Replaying mode
by pre-loading completed operations. When the handler calls ctx.step(),
the pre-loaded results are returned without executing the closure.
Operation IDs are generated deterministically using the same blake2b
algorithm as the core engine, ensuring the nth with_step_result call
corresponds to the nth ctx.step() call.
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, calls, _ops) = MockDurableContext::new()
.with_step_result("validate", r#"{"valid": true}"#)
.with_step_result("charge", r#"100"#)
.build()
.await;
// Steps replay cached results — closures are NOT executed
let result: Result<serde_json::Value, String> = ctx.step("validate", || async {
panic!("not executed during replay");
}).await.unwrap();
assert_eq!(result.unwrap(), serde_json::json!({"valid": true}));
// Verify no checkpoints were made (pure replay)
assert_no_checkpoints(&calls).await;Implementations§
Source§impl MockDurableContext
impl MockDurableContext
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new MockDurableContext builder.
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, calls, _ops) = MockDurableContext::new()
.with_step_result("my_step", r#""hello""#)
.build()
.await;Sourcepub fn with_step_result(self, _name: &str, result_json: &str) -> Self
pub fn with_step_result(self, _name: &str, result_json: &str) -> Self
Add a successful step result to the mock history.
The result_json is a JSON string representing the step’s return value.
It will be returned by ctx.step() during replay without executing
the closure.
§Arguments
_name— Step name (for documentation; the operation ID is position-based)result_json— JSON string of the step result (e.g.,r#"{"valid": true}"#)
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, _, _ops) = MockDurableContext::new()
.with_step_result("validate", r#"42"#)
.build()
.await;
let result: Result<i32, String> = ctx.step("validate", || async {
panic!("not executed");
}).await.unwrap();
assert_eq!(result.unwrap(), 42);Sourcepub fn with_step_error(
self,
_name: &str,
error_type: &str,
error_json: &str,
) -> Self
pub fn with_step_error( self, _name: &str, error_type: &str, error_json: &str, ) -> Self
Add a failed step result to the mock history.
The step will replay as a typed error. The error_type is the type
name and error_json is the serialized error data.
§Arguments
_name— Step name (for documentation; the operation ID is position-based)error_type— The error type name (e.g.,"my_crate::MyError")error_json— JSON string of the error data
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, _, _ops) = MockDurableContext::new()
.with_step_error("charge", "PaymentError", r#""insufficient_funds""#)
.build()
.await;
let result: Result<i32, String> = ctx.step("charge", || async {
panic!("not executed");
}).await.unwrap();
assert_eq!(result.unwrap_err(), "insufficient_funds");Sourcepub fn with_wait(self, _name: &str) -> Self
pub fn with_wait(self, _name: &str) -> Self
Add a completed wait to the mock history.
Simulates a wait that has already completed (SUCCEEDED). During replay,
ctx.wait() will return Ok(()) immediately.
§Arguments
_name— Wait name (for documentation; the operation ID is position-based)
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, _, _ops) = MockDurableContext::new()
.with_step_result("validate", r#"42"#)
.with_wait("cooldown")
.with_step_result("charge", r#"100"#)
.build()
.await;Sourcepub fn with_callback(
self,
_name: &str,
callback_id: &str,
result_json: &str,
) -> Self
pub fn with_callback( self, _name: &str, callback_id: &str, result_json: &str, ) -> Self
Add a completed callback to the mock history.
Simulates a callback that has been signaled with success. During replay,
ctx.create_callback() will return a CallbackHandle with the given
callback_id, and ctx.callback_result() will return the deserialized
result.
§Arguments
_name— Callback name (for documentation; the operation ID is position-based)callback_id— The server-generated callback IDresult_json— JSON string of the callback result
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, _, _ops) = MockDurableContext::new()
.with_callback("approval", "cb-123", r#""approved""#)
.build()
.await;Sourcepub fn with_invoke(self, _name: &str, result_json: &str) -> Self
pub fn with_invoke(self, _name: &str, result_json: &str) -> Self
Add a completed invoke to the mock history.
Simulates a chained invoke that has completed successfully. During replay,
ctx.invoke() will return the deserialized result.
§Arguments
_name— Invoke name (for documentation; the operation ID is position-based)result_json— JSON string of the invoke result
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, _, _ops) = MockDurableContext::new()
.with_invoke("call_processor", r#"{"status":"ok"}"#)
.build()
.await;Sourcepub async fn build(
self,
) -> (DurableContext, CheckpointRecorder, OperationRecorder)
pub async fn build( self, ) -> (DurableContext, CheckpointRecorder, OperationRecorder)
Build the mock context, returning a DurableContext and checkpoint call recorder.
The returned DurableContext starts in Replaying mode if any
operations were pre-loaded, or Executing mode if none were added.
§Returns
A tuple of:
DurableContext— ready for use withctx.step(...)etc.Arc<Mutex<Vec<CheckpointCall>>>— inspect checkpoint calls after test
§Errors
Returns [DurableError] if context construction fails (should not happen
with mock data).
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, calls, _ops) = MockDurableContext::new()
.with_step_result("step1", r#"true"#)
.build()
.await;Sourcepub async fn build_with_batch_counter(
self,
) -> (DurableContext, CheckpointRecorder, OperationRecorder, BatchCallCounter)
pub async fn build_with_batch_counter( self, ) -> (DurableContext, CheckpointRecorder, OperationRecorder, BatchCallCounter)
Build mock context and also return the batch checkpoint call counter.
Use this when testing batch mode — the BatchCallCounter lets you
assert how many times batch_checkpoint() was called.
§Examples
use durable_lambda_testing::prelude::*;
let (mut ctx, calls, _ops, batch_counter) = MockDurableContext::new()
.build_with_batch_counter()
.await;
ctx.enable_batch_mode();
let _: Result<i32, String> = ctx.step("s1", || async { Ok(1) }).await.unwrap();
ctx.flush_batch().await.unwrap();
assert_eq!(*batch_counter.lock().await, 1);Trait Implementations§
Auto Trait Implementations§
impl Freeze for MockDurableContext
impl RefUnwindSafe for MockDurableContext
impl Send for MockDurableContext
impl Sync for MockDurableContext
impl Unpin for MockDurableContext
impl UnsafeUnpin for MockDurableContext
impl UnwindSafe for MockDurableContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more