2017-12-12 15:19:39 -06:00
|
|
|
extern crate tokio;
|
|
|
|
|
|
|
|
|
|
use std::thread;
|
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
|
|
|
|
|
use tokio::reactor::Reactor;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn works() {
|
|
|
|
|
let mut r = Reactor::new().unwrap();
|
|
|
|
|
r.handle().wakeup();
|
2017-12-20 00:15:30 +02:00
|
|
|
r.turn(None).unwrap();
|
2017-12-12 15:19:39 -06:00
|
|
|
|
|
|
|
|
let now = Instant::now();
|
|
|
|
|
let mut n = 0;
|
|
|
|
|
while now.elapsed() < Duration::from_millis(10) {
|
|
|
|
|
n += 1;
|
2017-12-20 00:15:30 +02:00
|
|
|
r.turn(Some(Duration::from_millis(10))).unwrap();
|
2017-12-12 15:19:39 -06:00
|
|
|
}
|
|
|
|
|
assert!(n < 5);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn wakes() {
|
|
|
|
|
const N: usize = 1_000;
|
|
|
|
|
|
|
|
|
|
let mut r = Reactor::new().unwrap();
|
|
|
|
|
let handle = r.handle();
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
let t = thread::spawn(move || {
|
|
|
|
|
for _ in 0..N {
|
|
|
|
|
rx.recv().unwrap();
|
|
|
|
|
handle.wakeup();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for _ in 0..N {
|
|
|
|
|
tx.send(()).unwrap();
|
2017-12-20 00:15:30 +02:00
|
|
|
r.turn(None).unwrap();
|
2017-12-12 15:19:39 -06:00
|
|
|
}
|
|
|
|
|
t.join().unwrap();
|
|
|
|
|
}
|