mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-25 00:00:18 +02:00
Start adding a global event loop
This commit starts to add support for a global event loop by adding a `Handle::default` method and implementing it. Currently the support is quite rudimentary and doesn't support features such as shutdown, overriding the return value of `Handle::default`, etc. Those will come as future commits.
This commit is contained in:
committed by
Carl Lerche
parent
23a0e990d2
commit
32f2750c2d
@@ -0,0 +1,37 @@
|
||||
extern crate futures;
|
||||
extern crate tokio;
|
||||
|
||||
use std::thread;
|
||||
|
||||
use futures::prelude::*;
|
||||
use tokio::net::{TcpStream, TcpListener};
|
||||
use tokio::reactor::Handle;
|
||||
|
||||
macro_rules! t {
|
||||
($e:expr) => (match $e {
|
||||
Ok(e) => e,
|
||||
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hammer() {
|
||||
let threads = (0..10).map(|_| {
|
||||
thread::spawn(|| {
|
||||
let handle = Handle::default();
|
||||
let srv = t!(TcpListener::bind(&"127.0.0.1:0".parse().unwrap(), &handle));
|
||||
let addr = t!(srv.local_addr());
|
||||
let mine = TcpStream::connect(&addr, &handle);
|
||||
let theirs = srv.incoming().into_future()
|
||||
.map(|(s, _)| s.unwrap().0)
|
||||
.map_err(|(s, _)| s);
|
||||
let (mine, theirs) = t!(mine.join(theirs).wait());
|
||||
|
||||
assert_eq!(t!(mine.local_addr()), t!(theirs.peer_addr()));
|
||||
assert_eq!(t!(theirs.local_addr()), t!(mine.peer_addr()));
|
||||
})
|
||||
}).collect::<Vec<_>>();
|
||||
for thread in threads {
|
||||
thread.join().unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user