1 extern crate futures;
2 extern crate tokio;
3 extern crate tokio_io;
4 extern crate env_logger;
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, Poll, Async};
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
64 .and_then(move |_ticks| {
65 assert!(Instant::now() >= when);
66 tx.send(()).unwrap();
67 Ok(())
68 })
69 });
70
71 rx.recv().unwrap();
72 }
73
74 #[test]
deadline()75 fn deadline() {
76 use futures::future;
77
78 let _ = env_logger::try_init();
79
80 let when = Instant::now() + Duration::from_millis(20);
81 let (tx, rx) = mpsc::channel();
82
83 #[allow(deprecated)]
84 tokio::run({
85 future::empty::<(), ()>()
86 .deadline(when)
87 .then(move |res| {
88 assert!(res.is_err());
89 tx.send(()).unwrap();
90 Ok(())
91 })
92 });
93
94 rx.recv().unwrap();
95 }
96
97 #[test]
timeout()98 fn timeout() {
99 use futures::future;
100
101 let _ = env_logger::try_init();
102
103 let (tx, rx) = mpsc::channel();
104
105 tokio::run({
106 future::empty::<(), ()>()
107 .timeout(Duration::from_millis(20))
108 .then(move |res| {
109 assert!(res.is_err());
110 tx.send(()).unwrap();
111 Ok(())
112 })
113 });
114
115 rx.recv().unwrap();
116 }
117