Module aws_smithy_async::test_util::tick_advance_sleep

source ·
Available on crate feature test-util only.
Expand description

Test time/sleep implementations that work by manually advancing time with a tick()

§Examples

Spawning a task that creates new sleep tasks and waits for them sequentially, and advancing passed all of them with a single call to tick().

use std::time::{Duration, SystemTime};
use aws_smithy_async::test_util::tick_advance_sleep::tick_advance_time_and_sleep;
use aws_smithy_async::time::TimeSource;
use aws_smithy_async::rt::sleep::AsyncSleep;

// Create the test time/sleep implementations.
// They will start at SystemTime::UNIX_EPOCH.
let (time, sleep) = tick_advance_time_and_sleep();

// Spawn the task that sequentially sleeps
let task = tokio::spawn(async move {
    sleep.sleep(Duration::from_secs(1)).await;
    sleep.sleep(Duration::from_secs(2)).await;
    sleep.sleep(Duration::from_secs(3)).await;
});
// Verify that task hasn't done anything yet since we haven't called `tick`
tokio::task::yield_now().await;
assert!(!task.is_finished());
assert_eq!(SystemTime::UNIX_EPOCH, time.now());

// Tick 6 seconds, which is long enough to go passed all the sequential sleeps
time.tick(Duration::from_secs(6)).await;
assert_eq!(SystemTime::UNIX_EPOCH + Duration::from_secs(6), time.now());

// Verify the task joins, indicating all the sleeps are done
task.await.unwrap();

Structs§

Functions§