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
impl TimeControl
Sourcepub async fn enable() -> Result<(), TestError>
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();
}Sourcepub async fn disable() -> Result<(), TestError>
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());
}Sourcepub fn is_enabled() -> bool
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());
}Sourcepub async fn advance(duration: Duration)
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();
}Sourcepub async fn advance_secs(seconds: u64)
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();
}Sourcepub async fn advance_millis(millis: u64)
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();
}Sourcepub async fn reset() -> Result<(), TestError>
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
impl Clone for TimeControl
Source§fn clone(&self) -> TimeControl
fn clone(&self) -> TimeControl
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TimeControl
impl Debug for TimeControl
impl Copy for TimeControl
Auto Trait Implementations§
impl Freeze for TimeControl
impl RefUnwindSafe for TimeControl
impl Send for TimeControl
impl Sync for TimeControl
impl Unpin for TimeControl
impl UnsafeUnpin for TimeControl
impl UnwindSafe for TimeControl
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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