summaryrefslogtreecommitdiffstats
path: root/tokio-timer/tests/timeout.rs
blob: 5dada019de1a44caac30b25842b0229f5d53a251 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#![deny(warnings, rust_2018_idioms)]
#![cfg(feature = "broken")]

mod support;
use crate::support::*;

use futures::sync::{mpsc, oneshot};
use futures::{future, Future, Stream};
use tokio_timer::*;

#[test]
fn simultaneous_deadline_future_completion() {
    mocked(|_, time| {
        // Create a future that is immediately ready
        let fut = future::ok::<_, ()>(());

        // Wrap it with a deadline
        let mut fut = Timeout::new_at(fut, time.now());

        // Ready!
        assert_ready!(fut);
    });
}

#[test]
fn completed_future_past_deadline() {
    mocked(|_, time| {
        // Create a future that is immediately ready
        let fut = future::ok::<_, ()>(());

        // Wrap it with a deadline
        let mut fut = Timeout::new_at(fut, time.now() - ms(1000));

        // Ready!
        assert_ready!(fut);
    });
}

#[test]
fn future_and_deadline_in_future() {
    mocked(|timer, time| {
        // Not yet complete
        let (tx, rx) = oneshot::channel();

        // Wrap it with a deadline
        let mut fut = Timeout::new_at(rx, time.now() + ms(100));

        // Ready!
        assert_not_ready!(fut);

        // Turn the timer, it runs for the elapsed time
        advance(timer, ms(90));

        assert_not_ready!(fut);

        // Complete the future
        tx.send(()).unwrap();

        assert_ready!(fut);
    });
}

#[test]
fn future_and_timeout_in_future() {
    mocked(|timer, _time| {
        // Not yet complete
        let (tx, rx) = oneshot::channel();

        // Wrap it with a deadline
        let mut fut = Timeout::new(rx, ms(100));

        // Ready!
        assert_not_ready!(fut);

        // Turn the timer, it runs for the elapsed time
        advance(timer, ms(90));

        assert_not_ready!(fut);

        // Complete the future
        tx.send(()).unwrap();

        assert_ready!(fut);
    });
}

#[test]
fn deadline_now_elapses() {
    mocked(|_, time| {
        let fut = future::empty::<(), ()>();

        // Wrap it with a deadline
        let mut fut = Timeout::new_at(fut, time.now());

        assert_elapsed!(fut);
    });
}

#[test]
fn deadline_future_elapses() {
    mocked(|timer, time| {
        let fut = future::empty::<(), ()>();

        // Wrap it with a deadline
        let mut fut = Timeout::new_at(fut, time.now() + ms(300));

        assert_not_ready!(fut);

        advance(timer, ms(300));

        assert_elapsed!(fut);
    });
}

#[test]
fn future_errors_first() {
    mocked(|_, time| {
        let fut = future::err::<(), ()>(());

        // Wrap it with a deadline
        let mut fut = Timeout::new_at(fut, time.now() + ms(100));

        // Ready!
        assert!(fut.poll().unwrap_err().is_inner());
    });
}

#[test]
fn stream_and_timeout_in_future() {
    mocked(|timer, _time| {
        // Not yet complete
        let (tx, rx) = mpsc::unbounded();

        // Wrap it with a deadline
        let mut stream = Timeout::new(rx, ms(100));

        // Not ready
        assert_not_ready!(stream);

        // Turn the timer, it runs for the elapsed time
        advance(timer, ms(90));

        assert_not_ready!(stream);

        // Complete the future
        tx.unbounded_send(()).unwrap();

        let item = assert_ready!(stream);
        assert!(item.is_some());
    });
}

#[test]
fn idle_stream_timesout_periodically() {
    mocked(|timer, _time| {
        // Not yet complete
        let (_tx, rx) = mpsc::unbounded::<()>();

        // Wrap it with a deadline
        let mut stream = Timeout::new(rx, ms(100));

        // Not ready
        assert_not_ready!(stream);

        // Turn the timer, it runs for the elapsed time
        advance(timer, ms(100));

        assert_elapsed!(stream);
        // Stream's timeout should reset
        assert_not_ready!(stream);

        // Turn the timer, it runs for the elapsed time
        advance(timer, ms(100));
        assert_elapsed!(stream);
    });
}