summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio-stream/tests/stream_timeout.rs
blob: 2338f833587b9081b35476fafd4d0b5b7f68fe9a (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#![cfg(all(feature = "time", feature = "sync", feature = "io-util"))]

use tokio::time::{self, sleep, Duration};
use tokio_stream::{self, StreamExt};
use tokio_test::*;

use futures::stream;

async fn maybe_sleep(idx: i32) -> i32 {
    if idx % 2 == 0 {
        sleep(ms(200)).await;
    }
    idx
}

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

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

    // Items 2 and 4 time out. If we run the stream until it completes,
    // we end up with the following items:
    //
    // [Ok(1), Err(Elapsed), Ok(2), Ok(3), Err(Elapsed), Ok(4)]

    let stream = stream::iter(1..=4).then(maybe_sleep).timeout(ms(100));
    let mut stream = task::spawn(stream);

    // First item completes immediately
    assert_ready_eq!(stream.poll_next(), Some(Ok(1)));

    // Second item is delayed 200ms, times out after 100ms
    assert_pending!(stream.poll_next());

    time::advance(ms(150)).await;
    let v = assert_ready!(stream.poll_next());
    assert!(v.unwrap().is_err());

    assert_pending!(stream.poll_next());

    time::advance(ms(100)).await;
    assert_ready_eq!(stream.poll_next(), Some(Ok(2)));

    // Third item is ready immediately
    assert_ready_eq!(stream.poll_next(), Some(Ok(3)));

    // Fourth item is delayed 200ms, times out after 100ms
    assert_pending!(stream.poll_next());

    time::advance(ms(60)).await;
    assert_pending!(stream.poll_next()); // nothing ready yet

    time::advance(ms(60)).await;
    let v = assert_ready!(stream.poll_next());
    assert!(v.unwrap().is_err()); // timeout!

    time::advance(ms(120)).await;
    assert_ready_eq!(stream.poll_next(), Some(Ok(4)));

    // Done.
    assert_ready_eq!(stream.poll_next(), None);
}

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

    let stream = stream::iter(1..=3).then(maybe_sleep).timeout(ms(50));
    let mut stream = task::spawn(stream);

    // First item completes immediately
    assert_ready_eq!(stream.poll_next(), Some(Ok(1)));

    // Second item is delayed 200ms, times out after 50ms. Only one `Elapsed`
    // error is returned.
    assert_pending!(stream.poll_next());
    //
    time::advance(ms(51)).await;
    let v = assert_ready!(stream.poll_next());
    assert!(v.unwrap().is_err()); // timeout!

    // deadline elapses again, but no error is returned
    time::advance(ms(50)).await;
    assert_pending!(stream.poll_next());

    time::advance(ms(100)).await;
    assert_ready_eq!(stream.poll_next(), Some(Ok(2)));
    assert_ready_eq!(stream.poll_next(), Some(Ok(3)));

    // Done
    assert_ready_eq!(stream.poll_next(), None);
}

#[tokio::test]
async fn no_timeouts() {
    let stream = stream::iter(vec![1, 3, 5])
        .then(maybe_sleep)
        .timeout(ms(100));

    let mut stream = task::spawn(stream);

    assert_ready_eq!(stream.poll_next(), Some(Ok(1)));
    assert_ready_eq!(stream.poll_next(), Some(Ok(3)));
    assert_ready_eq!(stream.poll_next(), Some(Ok(5)));
    assert_ready_eq!(stream.poll_next(), None);
}