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<'_>

source

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.

source

pub fn duration(&self) -> Duration

Duration in the call to AsyncSleep::sleep

Trait Implementations§

source§

impl AsRef<Duration> for CapturedSleep<'_>

source§

fn as_ref(&self) -> &Duration

Converts this type into a shared reference of the (usually inferred) input type.

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> 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, 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.