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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! Async timers crate
//!
//! This library provides timers that can be easily scheduled and canceled. For example, the tokio's [`tokio::time::Interval`] has no way of stopping the timer.
//! You could have set the interval duration to a very big value, however that is rather a work around. Also, tokio's [`tokio::time::Sleep`] is a one-time use object,
//! meaning it's .await requires to move the object and requires you to recreated it when you need to sleep again.
//!
//! This crate provides [`PeriodicTimer`] and [`OneshotTimer`] that aim to make the use of timers more pleasant.
//! This timers have methods to cancel and restart timers.
use std::task;

use futures::Future;
use tokio::time::{interval, sleep_until, Duration, Instant, Interval};

/// NeverExpire is a future that never unblocks
#[derive(Default, Debug)]
struct NeverExpire {}

impl Future for NeverExpire {
    type Output = Instant;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        task::Poll::Pending
    }
}

/// PeriodicTimer expires on given interval
///
/// PeriodicTimer is an extension and built on top of [`tokio::time::Interval`].
/// It can be in two states: [`PeriodicTimer::Started`] and [`PeriodicTimer::Stopped`].
/// When in [`PeriodicTimer::Started`] state the timer will expire every interval duration but
/// when in [`PeriodicTimer::Stopped`] it won't expire until the timer is started again.
///
/// ```
/// use async_timers::PeriodicTimer;
/// use tokio::time::{Duration, timeout};
///
/// #[tokio::main]
/// async fn main() {
///     let mut timer = PeriodicTimer::started(Duration::from_millis(10));
///
///     timer.tick().await;
///     timer.tick().await;
///     timer.tick().await;
///
///     // approximately 30ms have elapsed.
///
///     let result = timeout(Duration::from_millis(100), timer.tick()).await;
///     assert!(result.is_ok(), "Timeout should not occur since timer is running");
///
///     timer.stop();
///
///     let result = timeout(Duration::from_millis(100), timer.tick()).await;
///     assert!(result.is_err(), "Timeout should occur since timer is stopped");
/// }
/// ```
#[derive(Default, Debug)]
pub enum PeriodicTimer {
    Started(Interval),
    #[default]
    Stopped,
}

impl PeriodicTimer {
    /// Create started timer with the given `period`
    pub fn started(period: Duration) -> Self {
        let mut interval = interval(period);
        interval.reset();
        Self::Started(interval)
    }

    /// Create stopped timer
    pub fn stopped() -> Self {
        Self::Stopped
    }

    /// Start the timer with given `period`
    pub fn start(&mut self, period: Duration) {
        *self = Self::started(period);
    }

    /// Stop the timer
    pub fn stop(&mut self) {
        *self = Self::stopped()
    }

    /// Returns a [`Future`] that will expire based on timer's state
    pub async fn tick(&mut self) -> Instant {
        match self {
            Self::Started(interval) => interval.tick().await,
            Self::Stopped => NeverExpire::default().await,
        }
    }
}

/// OneshotTimer expires once after a given duration
///
/// OneshotTimer is used for tasks that need to be executed once after some delay.
/// OneshotTimer is an extension and built on top of [`tokio::time::Sleep`].
/// In [`OneshotTimer::Scheduled`] state it will expire *once* and transition into
/// [`OneshotTimer::Expired`] state.
///
/// ```
/// use async_timers::OneshotTimer;
/// use tokio::time::{Duration, timeout};
///
/// #[tokio::main]
/// async fn main() {
///     let mut timer = OneshotTimer::scheduled(Duration::from_millis(10));
///
///     timer.tick().await;
///
///     // approximately 10ms have elapsed.
///
///     let result = timeout(Duration::from_millis(100), timer.tick()).await;
///     assert!(result.is_err(), "Timeout should occur since timer is expired");
///
///     timer.schedule(Duration::from_millis(30));
///
///     let result = timeout(Duration::from_millis(100), timer.tick()).await;
///     assert!(result.is_ok(), "Timeout should not occur since timer has been scheduled");
/// }
/// ```
#[derive(Default, Debug)]
pub enum OneshotTimer {
    Scheduled(Instant),
    #[default]
    Expired,
}

impl OneshotTimer {
    /// Create a timer scheduled to be expired after `duration`
    pub fn scheduled(duration: Duration) -> Self {
        Self::Scheduled(Instant::now() + duration)
    }

    /// Create a timer that won't expire
    pub fn expired() -> Self {
        Self::Expired
    }

    /// Schedule a new duration
    pub fn schedule(&mut self, duration: Duration) {
        *self = Self::scheduled(duration);
    }

    /// Cancel the timer
    pub fn cancel(&mut self) {
        *self = Self::expired()
    }

    /// Returns a [`Future`] that will expire based on timer's state
    pub async fn tick(&mut self) {
        match self {
            Self::Scheduled(instant) => {
                sleep_until(*instant).await;
                *self = Self::expired();
            }
            Self::Expired => {
                NeverExpire::default().await;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_periodic_timer() {
        let mut timer1 = PeriodicTimer::stopped();
        let mut timer2 = PeriodicTimer::started(Duration::from_secs(2));

        let mut timer1_expired = false;
        let mut timer2_expired = false;

        tokio::select! {
            _ = timer1.tick() => {
                timer1_expired = true;
            }
            _ = timer2.tick() => {
                timer2_expired = true;
            }
        }

        assert!(!timer1_expired, "timer1 should not have expired");
        assert!(timer2_expired, "timer1 should have expired");

        timer1.start(Duration::from_secs(1));
        timer2.stop();

        timer1_expired = false;
        timer2_expired = false;

        tokio::select! {
            _ = timer1.tick() => {
                timer1_expired = true;
            }
            _ = timer2.tick() => {
                timer2_expired = true;
            }
        }

        assert!(timer1_expired, "timer1 should have expired");
        assert!(!timer2_expired, "timer2 should not have expired");
    }

    #[tokio::test]
    async fn test_oneshot_timer() {
        let mut timer1 = OneshotTimer::expired();
        let mut timer2 = OneshotTimer::scheduled(Duration::from_secs(2));

        let mut timer1_expired = false;
        let mut timer2_expired = false;

        tokio::select! {
            _ = timer1.tick() => {
                timer1_expired = true;
            }
            _ = timer2.tick() => {
                timer2_expired = true;
            }
        }

        assert!(!timer1_expired, "timer1 should not have expired");
        assert!(timer2_expired, "timer1 should have expired");

        timer1.schedule(Duration::from_secs(1));

        timer1_expired = false;
        timer2_expired = false;

        tokio::select! {
            _ = timer1.tick() => {
                timer1_expired = true;
            }
            _ = timer2.tick() => {
                timer2_expired = true;
            }
        }

        assert!(timer1_expired, "timer1 should have expired");
        assert!(!timer2_expired, "timer2 should not have expired");

        timer1.schedule(Duration::from_secs(1));
        timer2.schedule(Duration::from_secs(2));

        timer1.cancel();

        timer1_expired = false;
        timer2_expired = false;

        tokio::select! {
            _ = timer1.tick() => {
                timer1_expired = true;
            }
            _ = timer2.tick() => {
                timer2_expired = true;
            }
        }

        assert!(!timer1_expired, "timer1 should not have expired");
        assert!(timer2_expired, "timer2 should have expired");
    }

    #[tokio::test]
    async fn test_oneshot_state() {
        let mut timer1 = OneshotTimer::scheduled(Duration::from_secs(1));
        let result = tokio::time::timeout(Duration::from_millis(1500), timer1.tick()).await;
        assert!(result.is_ok(), "Should not timeout");

        let mut timer1 = OneshotTimer::scheduled(Duration::from_secs(5));
        let mut timer2 = OneshotTimer::scheduled(Duration::from_secs(2));

        tokio::select! {
            _ = timer1.tick() => {}
            _ = timer2.tick() => {}
        }

        match timer1 {
            OneshotTimer::Scheduled(_) => {}
            OneshotTimer::Expired => assert!(false, "Should be in scheduled state"),
        }

        let result = tokio::time::timeout(Duration::from_millis(3500), timer1.tick()).await;
        assert!(result.is_ok(), "Should not timeout");

        match timer1 {
            OneshotTimer::Scheduled(_) => assert!(false, "Timer should be in expired state"),
            OneshotTimer::Expired => {}
        }
    }

    #[tokio::test]
    async fn test_my_task() {
        struct MyTask {
            period: PeriodicTimer,
        }

        impl MyTask {
            fn new() -> Self {
                Self {
                    period: PeriodicTimer::started(Duration::from_secs(1)),
                }
            }

            fn do_work(&mut self) {
                println!("here");
            }
        }

        let mut task = MyTask::new();
        let mut sleep = OneshotTimer::scheduled(Duration::from_secs(3));

        let result = tokio::time::timeout(Duration::from_secs(10), async move {
            for _ in 0..3 {
                tokio::select! {
                    _ = task.period.tick() => {
                        task.do_work();
                        task.period.stop();
                    }
                    _ = sleep.tick() => {
                        println!("sleep");
                        task.period.start(Duration::from_secs(1));
                    }
                }
            }
        })
        .await;

        assert!(result.is_ok(), "Should not timeout");
    }
}