Remove timers from Tokio.

In accordance with tokio-rs/tokio-rfcs#3, timers are being extracted
from Tokio and moved to a separate crate (probably futures-timer).

This PR removes timers from the code base.
This commit is contained in:
Carl Lerche
2017-10-30 18:16:00 -07:00
committed by Alex Crichton
parent b23a997cb8
commit 697851210c
19 changed files with 6 additions and 2242 deletions
-38
View File
@@ -1,38 +0,0 @@
extern crate env_logger;
extern crate futures;
extern crate tokio;
use std::time::{Instant, Duration};
use futures::stream::{Stream};
use tokio::reactor::{Core, Interval};
macro_rules! t {
($e:expr) => (match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
})
}
#[test]
fn single() {
drop(env_logger::init());
let mut l = t!(Core::new());
let dur = Duration::from_millis(10);
let start = Instant::now();
let interval = t!(Interval::new(dur, &l.handle()));
t!(l.run(interval.take(1).collect()));
assert!(start.elapsed() >= dur);
}
#[test]
fn two_times() {
drop(env_logger::init());
let mut l = t!(Core::new());
let dur = Duration::from_millis(10);
let start = Instant::now();
let interval = t!(Interval::new(dur, &l.handle()));
let result = t!(l.run(interval.take(2).collect()));
assert!(start.elapsed() >= dur*2);
assert_eq!(result, vec![(), ()]);
}
-147
View File
@@ -1,147 +0,0 @@
extern crate tokio;
extern crate env_logger;
extern crate futures;
use std::any::Any;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use futures::{Future, Poll};
use futures::future;
use futures::sync::oneshot;
use tokio::reactor::{Core, Timeout};
#[test]
fn simple() {
drop(env_logger::init());
let mut lp = Core::new().unwrap();
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
lp.handle().spawn(future::lazy(|| {
tx1.send(1).unwrap();
Ok(())
}));
lp.remote().spawn(|_| {
future::lazy(|| {
tx2.send(2).unwrap();
Ok(())
})
});
assert_eq!(lp.run(rx1.join(rx2)).unwrap(), (1, 2));
}
#[test]
fn simple_core_poll() {
drop(env_logger::init());
let mut lp = Core::new().unwrap();
let (tx, rx) = mpsc::channel();
let (tx1, tx2) = (tx.clone(), tx.clone());
lp.turn(Some(Duration::new(0, 0)));
lp.handle().spawn(future::lazy(move || {
tx1.send(1).unwrap();
Ok(())
}));
lp.turn(Some(Duration::new(0, 0)));
lp.handle().spawn(future::lazy(move || {
tx2.send(2).unwrap();
Ok(())
}));
assert_eq!(rx.try_recv().unwrap(), 1);
assert!(rx.try_recv().is_err());
lp.turn(Some(Duration::new(0, 0)));
assert_eq!(rx.try_recv().unwrap(), 2);
}
#[test]
fn spawn_in_poll() {
drop(env_logger::init());
let mut lp = Core::new().unwrap();
let (tx1, rx1) = oneshot::channel();
let (tx2, rx2) = oneshot::channel();
let remote = lp.remote();
lp.handle().spawn(future::lazy(move || {
tx1.send(1).unwrap();
remote.spawn(|_| {
future::lazy(|| {
tx2.send(2).unwrap();
Ok(())
})
});
Ok(())
}));
assert_eq!(lp.run(rx1.join(rx2)).unwrap(), (1, 2));
}
#[test]
fn drop_timeout_in_spawn() {
drop(env_logger::init());
let mut lp = Core::new().unwrap();
let (tx, rx) = oneshot::channel();
let remote = lp.remote();
thread::spawn(move || {
remote.spawn(|handle| {
drop(Timeout::new(Duration::new(1, 0), handle));
tx.send(()).unwrap();
Ok(())
});
});
lp.run(rx).unwrap();
}
#[test]
fn spawn_in_drop() {
drop(env_logger::init());
let mut lp = Core::new().unwrap();
let (tx, rx) = oneshot::channel();
let remote = lp.remote();
struct OnDrop<F: FnMut()>(F);
impl<F: FnMut()> Drop for OnDrop<F> {
fn drop(&mut self) {
(self.0)();
}
}
struct MyFuture {
_data: Box<Any>,
}
impl Future for MyFuture {
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<(), ()> {
Ok(().into())
}
}
thread::spawn(move || {
let mut tx = Some(tx);
remote.spawn(|handle| {
let handle = handle.clone();
MyFuture {
_data: Box::new(OnDrop(move || {
let mut tx = tx.take();
handle.spawn_fn(move || {
tx.take().unwrap().send(()).unwrap();
Ok(())
});
})),
}
});
});
lp.run(rx).unwrap();
}
-37
View File
@@ -1,37 +0,0 @@
extern crate env_logger;
extern crate futures;
extern crate tokio;
use std::time::{Instant, Duration};
use tokio::reactor::{Core, Timeout};
macro_rules! t {
($e:expr) => (match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
})
}
#[test]
fn smoke() {
drop(env_logger::init());
let mut l = t!(Core::new());
let dur = Duration::from_millis(10);
let start = Instant::now();
let timeout = t!(Timeout::new(dur, &l.handle()));
t!(l.run(timeout));
assert!(start.elapsed() >= (dur / 2));
}
#[test]
fn two() {
drop(env_logger::init());
let mut l = t!(Core::new());
let dur = Duration::from_millis(10);
let timeout = t!(Timeout::new(dur, &l.handle()));
t!(l.run(timeout));
let timeout = t!(Timeout::new(dur, &l.handle()));
t!(l.run(timeout));
}