Struct aws_smithy_async::test_util::CapturedSleep
source · pub struct CapturedSleep<'a>(/* private fields */);
Available on crate feature
test-util
only.Expand description
Guard returned from SleepGate::expect_sleep
§Examples
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, UNIX_EPOCH};
use aws_smithy_async::rt::sleep::AsyncSleep;
use aws_smithy_async::test_util::controlled_time_and_sleep;
let (time, sleep, mut gate) = controlled_time_and_sleep(UNIX_EPOCH);
let progress = Arc::new(AtomicUsize::new(0));
let task_progress = progress.clone();
let task = tokio::spawn(async move {
let progress = task_progress;
progress.store(1, Ordering::Release);
sleep.sleep(Duration::from_secs(1)).await;
progress.store(2, Ordering::Release);
sleep.sleep(Duration::from_secs(2)).await;
});
while progress.load(Ordering::Acquire) != 1 {}
let guard = gate.expect_sleep().await;
assert_eq!(guard.duration(), Duration::from_secs(1));
assert_eq!(progress.load(Ordering::Acquire), 1);
guard.allow_progress();
let guard = gate.expect_sleep().await;
assert_eq!(progress.load(Ordering::Acquire), 2);
assert_eq!(task.is_finished(), false);
guard.allow_progress();
task.await.expect("successful completion");
Implementations§
source§impl CapturedSleep<'_>
impl CapturedSleep<'_>
sourcepub fn allow_progress(self)
pub fn allow_progress(self)
Allow the calling code to advance past the call to AsyncSleep::sleep
In order to facilitate testing with no flakiness, the future returned by the call to sleep
will not resolve until CapturedSleep
is dropped or this method is called.
use std::time::Duration;
use aws_smithy_async::rt::sleep::AsyncSleep;
async fn do_something(sleep: &dyn AsyncSleep) {
println!("before sleep");
sleep.sleep(Duration::from_secs(1)).await;
println!("after sleep");
}
To be specific, when do_something
is called, the code will advance to sleep.sleep
.
When SleepGate::expect_sleep
is called, the 1 second sleep will be captured, but after sleep
WILL NOT be printed, until allow_progress
is called.
sourcepub fn duration(&self) -> Duration
pub fn duration(&self) -> Duration
Duration in the call to AsyncSleep::sleep
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for CapturedSleep<'a>
impl<'a> !RefUnwindSafe for CapturedSleep<'a>
impl<'a> Send for CapturedSleep<'a>
impl<'a> Sync for CapturedSleep<'a>
impl<'a> Unpin for CapturedSleep<'a>
impl<'a> !UnwindSafe for CapturedSleep<'a>
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
Mutably borrows from an owned value. Read more