[go: up one dir, main page]

futures-timer 0.2.1

Timeouts and intervals for futures.
Documentation
#![feature(async_await)]

use std::error::Error;
use std::time::{Duration, Instant};

use futures::prelude::*;
use futures_timer::Interval;

#[runtime::test]
async fn single() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
    let dur = Duration::from_millis(10);
    let start = Instant::now();
    let interval = Interval::new(dur);
    interval.take(1).collect::<Vec<()>>().await;
    assert!(start.elapsed() >= dur);
    Ok(())
}

#[runtime::test]
async fn two_times() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
    let dur = Duration::from_millis(10);
    let start = Instant::now();
    let interval = Interval::new(dur);
    let result = interval.take(2).collect::<Vec<()>>().await;
    assert!(start.elapsed() >= dur * 2);
    assert_eq!(result, vec![(), ()]);
    Ok(())
}