Skip to main content

TimeControl

Struct TimeControl 

Source
pub struct TimeControl;
Expand description

Utilities for controlling time during durable execution tests.

TimeControl provides methods to pause, resume, and advance time using Tokio’s time manipulation features. This enables tests to skip wait operations instantly without blocking.

§Thread Safety

Time control is global to the Tokio runtime. Only one test should use time control at a time, or tests should be run sequentially when using time manipulation.

§Examples

use durable_execution_sdk_testing::TimeControl;
use std::time::Duration;

#[tokio::test]
async fn test_workflow_with_waits() {
    // Enable time skipping for fast test execution
    TimeControl::enable().await.unwrap();

    // Run your workflow - wait operations complete instantly
    // ...

    // Optionally advance time by a specific duration
    TimeControl::advance(Duration::from_secs(60)).await;

    // Restore normal time behavior
    TimeControl::disable().await.unwrap();
}

Implementations§

Source§

impl TimeControl

Source

pub async fn enable() -> Result<(), TestError>

Enables time skipping by pausing Tokio’s internal clock.

When time is paused, tokio::time::sleep and other time-based operations will not block. Instead, time advances only when explicitly advanced or when there are no other tasks to run.

§Errors

Returns Ok(()) on success. This operation is idempotent - calling it multiple times has no additional effect.

§Examples
use durable_execution_sdk_testing::TimeControl;

#[tokio::test]
async fn test_enable_time_control() {
    TimeControl::enable().await.unwrap();
    assert!(TimeControl::is_enabled());
    TimeControl::disable().await.unwrap();
}
Source

pub async fn disable() -> Result<(), TestError>

Disables time skipping by resuming Tokio’s internal clock.

After calling this method, time-based operations will use real time again.

§Errors

Returns Ok(()) on success. This operation is idempotent - calling it multiple times has no additional effect.

§Examples
use durable_execution_sdk_testing::TimeControl;

#[tokio::test]
async fn test_disable_time_control() {
    TimeControl::enable().await.unwrap();
    TimeControl::disable().await.unwrap();
    assert!(!TimeControl::is_enabled());
}
Source

pub fn is_enabled() -> bool

Returns whether time control is currently enabled.

§Examples
use durable_execution_sdk_testing::TimeControl;

#[tokio::test]
async fn test_is_enabled() {
    assert!(!TimeControl::is_enabled());
    TimeControl::enable().await.unwrap();
    assert!(TimeControl::is_enabled());
    TimeControl::disable().await.unwrap();
    assert!(!TimeControl::is_enabled());
}
Source

pub async fn advance(duration: Duration)

Advances the paused clock by the specified duration.

This method instantly advances Tokio’s internal clock by the given duration, causing any pending timers that would expire within that duration to fire.

§Arguments
  • duration - The amount of time to advance the clock by
§Panics

This method will panic if time control is not enabled (i.e., if enable() has not been called or disable() has been called).

§Examples
use durable_execution_sdk_testing::TimeControl;
use std::time::Duration;

#[tokio::test]
async fn test_advance_time() {
    TimeControl::enable().await.unwrap();

    // Advance time by 1 hour instantly
    TimeControl::advance(Duration::from_secs(3600)).await;

    TimeControl::disable().await.unwrap();
}
Source

pub async fn advance_secs(seconds: u64)

Advances the paused clock by the specified number of seconds.

This is a convenience method equivalent to advance(Duration::from_secs(seconds)).

§Arguments
  • seconds - The number of seconds to advance the clock by
§Examples
use durable_execution_sdk_testing::TimeControl;

#[tokio::test]
async fn test_advance_seconds() {
    TimeControl::enable().await.unwrap();

    // Advance time by 60 seconds
    TimeControl::advance_secs(60).await;

    TimeControl::disable().await.unwrap();
}
Source

pub async fn advance_millis(millis: u64)

Advances the paused clock by the specified number of milliseconds.

This is a convenience method equivalent to advance(Duration::from_millis(millis)).

§Arguments
  • millis - The number of milliseconds to advance the clock by
§Examples
use durable_execution_sdk_testing::TimeControl;

#[tokio::test]
async fn test_advance_millis() {
    TimeControl::enable().await.unwrap();

    // Advance time by 500 milliseconds
    TimeControl::advance_millis(500).await;

    TimeControl::disable().await.unwrap();
}
Source

pub async fn reset() -> Result<(), TestError>

Resets time control state.

This method disables time control if it’s enabled and resets the internal state. It’s useful for cleanup between tests.

§Examples
use durable_execution_sdk_testing::TimeControl;

#[tokio::test]
async fn test_reset() {
    TimeControl::enable().await.unwrap();
    TimeControl::reset().await.unwrap();
    assert!(!TimeControl::is_enabled());
}

Trait Implementations§

Source§

impl Clone for TimeControl

Source§

fn clone(&self) -> TimeControl

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TimeControl

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for TimeControl

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> AsOut<T> for T
where T: Copy,

Source§

fn as_out(&mut self) -> Out<'_, T>

Returns an out reference to self.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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