Skip to main content

MockDurableContext

Struct MockDurableContext 

Source
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

Source

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;
Source

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);
Source

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");
Source

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;
Source

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 ID
  • result_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;
Source

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;
Source

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 with ctx.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;
Source

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§

Source§

impl Default for MockDurableContext

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more