summaryrefslogtreecommitdiffstats
path: root/tokio/tests/time_interval.rs
blob: c884ca8e9f5b5dc7d699b7dd8eba8dc948ddc3a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#![warn(rust_2018_idioms)]

use tokio::time::{self, Duration, Instant, Interval};
use tokio_test::{assert_pending, assert_ready_eq, task};

#[tokio::test]
#[should_panic]
async fn interval_zero_duration() {
    let _ = Interval::new(Instant::now(), ms(0));
}

#[tokio::test]
async fn usage() {
    time::pause();

    let start = Instant::now();

    // TODO: Skip this
    time::advance(ms(1)).await;

    let mut int = task::spawn(Interval::new(start, ms(300)));

    assert_ready_eq!(int.poll_next(), Some(start));
    assert_pending!(int.poll_next());

    time::advance(ms(100)).await;
    assert_pending!(int.poll_next());

    time::advance(ms(200)).await;
    assert_ready_eq!(int.poll_next(), Some(start + ms(300)));
    assert_pending!(int.poll_next());

    time::advance(ms(400)).await;
    assert_ready_eq!(int.poll_next(), Some(start + ms(600)));
    assert_pending!(int.poll_next());

    time::advance(ms(500)).await;
    assert_ready_eq!(int.poll_next(), Some(start + ms(900)));
    assert_ready_eq!(int.poll_next(), Some(start + ms(1200)));
    assert_pending!(int.poll_next());
}

fn ms(n: u64) -> Duration {
    Duration::from_millis(n)
}