1 extern crate env_logger;
2 extern crate futures;
3 extern crate tokio;
4 extern crate tokio_io;
5
6 use tokio::prelude::*;
7 use tokio::timer::*;
8
9 use std::sync::mpsc;
10 use std::time::{Duration, Instant};
11
12 #[test]
timer_with_runtime()13 fn timer_with_runtime() {
14 let _ = env_logger::try_init();
15
16 let when = Instant::now() + Duration::from_millis(100);
17 let (tx, rx) = mpsc::channel();
18
19 tokio::run({
20 Delay::new(when)
21 .map_err(|e| panic!("unexpected error; err={:?}", e))
22 .and_then(move |_| {
23 assert!(Instant::now() >= when);
24 tx.send(()).unwrap();
25 Ok(())
26 })
27 });
28
29 rx.recv().unwrap();
30 }
31
32 #[test]
starving()33 fn starving() {
34 use futures::{task, Async, Poll};
35
36 let _ = env_logger::try_init();
37
38 struct Starve(Delay, u64);
39
40 impl Future for Starve {
41 type Item = u64;
42 type Error = ();
43
44 fn poll(&mut self) -> Poll<Self::Item, ()> {
45 if self.0.poll().unwrap().is_ready() {
46 return Ok(self.1.into());
47 }
48
49 self.1 += 1;
50
51 task::current().notify();
52
53 Ok(Async::NotReady)
54 }
55 }
56
57 let when = Instant::now() + Duration::from_millis(20);
58 let starve = Starve(Delay::new(when), 0);
59
60 let (tx, rx) = mpsc::channel();
61
62 tokio::run({
63 starve.and_then(move |_ticks| {
64 assert!(Instant::now() >= when);
65 tx.send(()).unwrap();
66 Ok(())
67 })
68 });
69
70 rx.recv().unwrap();
71 }
72
73 #[test]
deadline()74 fn deadline() {
75 use futures::future;
76
77 let _ = env_logger::try_init();
78
79 let when = Instant::now() + Duration::from_millis(20);
80 let (tx, rx) = mpsc::channel();
81
82 #[allow(deprecated)]
83 tokio::run({
84 future::empty::<(), ()>().deadline(when).then(move |res| {
85 assert!(res.is_err());
86 tx.send(()).unwrap();
87 Ok(())
88 })
89 });
90
91 rx.recv().unwrap();
92 }
93
94 #[test]
timeout()95 fn timeout() {
96 use futures::future;
97
98 let _ = env_logger::try_init();
99
100 let (tx, rx) = mpsc::channel();
101
102 tokio::run({
103 future::empty::<(), ()>()
104 .timeout(Duration::from_millis(20))
105 .then(move |res| {
106 assert!(res.is_err());
107 tx.send(()).unwrap();
108 Ok(())
109 })
110 });
111
112 rx.recv().unwrap();
113 }
114