mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
chore: apply rustfmt to all crates (#917)
This commit is contained in:
@@ -6,24 +6,21 @@ extern crate rand;
|
||||
|
||||
use tokio_threadpool::*;
|
||||
|
||||
use futures::*;
|
||||
use futures::future::{lazy, poll_fn};
|
||||
use futures::*;
|
||||
use rand::*;
|
||||
|
||||
use std::sync::*;
|
||||
use std::sync::atomic::*;
|
||||
use std::sync::atomic::Ordering::*;
|
||||
use std::time::Duration;
|
||||
use std::sync::atomic::*;
|
||||
use std::sync::*;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn basic() {
|
||||
let _ = ::env_logger::try_init();
|
||||
|
||||
let pool = Builder::new()
|
||||
.pool_size(1)
|
||||
.max_blocking(1)
|
||||
.build();
|
||||
let pool = Builder::new().pool_size(1).max_blocking(1).build();
|
||||
|
||||
let (tx1, rx1) = mpsc::channel();
|
||||
let (tx2, rx2) = mpsc::channel();
|
||||
@@ -32,7 +29,8 @@ fn basic() {
|
||||
let res = blocking(|| {
|
||||
let v = rx1.recv().unwrap();
|
||||
tx2.send(v).unwrap();
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert!(res.is_ready());
|
||||
Ok(().into())
|
||||
@@ -50,10 +48,7 @@ fn basic() {
|
||||
fn notify_task_on_capacity() {
|
||||
const BLOCKING: usize = 10;
|
||||
|
||||
let pool = Builder::new()
|
||||
.pool_size(1)
|
||||
.max_blocking(1)
|
||||
.build();
|
||||
let pool = Builder::new().pool_size(1).max_blocking(1).build();
|
||||
|
||||
let rem = Arc::new(AtomicUsize::new(BLOCKING));
|
||||
let (tx, rx) = mpsc::channel();
|
||||
@@ -71,7 +66,8 @@ fn notify_task_on_capacity() {
|
||||
if prev == 1 {
|
||||
tx.send(()).unwrap();
|
||||
}
|
||||
}).map_err(|e| panic!("blocking err {:?}", e))
|
||||
})
|
||||
.map_err(|e| panic!("blocking err {:?}", e))
|
||||
})
|
||||
}));
|
||||
}
|
||||
@@ -83,17 +79,14 @@ fn notify_task_on_capacity() {
|
||||
|
||||
#[test]
|
||||
fn capacity_is_use_it_or_lose_it() {
|
||||
use futures::*;
|
||||
use futures::Async::*;
|
||||
use futures::sync::oneshot;
|
||||
use futures::task::Task;
|
||||
use futures::Async::*;
|
||||
use futures::*;
|
||||
|
||||
// TODO: Run w/ bigger pool size
|
||||
|
||||
let pool = Builder::new()
|
||||
.pool_size(1)
|
||||
.max_blocking(1)
|
||||
.build();
|
||||
let pool = Builder::new().pool_size(1).max_blocking(1).build();
|
||||
|
||||
let (tx1, rx1) = mpsc::channel();
|
||||
let (tx2, rx2) = oneshot::channel();
|
||||
@@ -105,24 +98,24 @@ fn capacity_is_use_it_or_lose_it() {
|
||||
poll_fn(move || {
|
||||
blocking(|| {
|
||||
rx1.recv().unwrap();
|
||||
}).map_err(|_| panic!())
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
})
|
||||
}));
|
||||
|
||||
pool.spawn(lazy(move || {
|
||||
rx2
|
||||
.map_err(|_| panic!())
|
||||
.and_then(|task: Task| {
|
||||
poll_fn(move || {
|
||||
blocking(|| {
|
||||
// Notify the other task
|
||||
task.notify();
|
||||
rx2.map_err(|_| panic!()).and_then(|task: Task| {
|
||||
poll_fn(move || {
|
||||
blocking(|| {
|
||||
// Notify the other task
|
||||
task.notify();
|
||||
|
||||
// Block until woken
|
||||
rx3.recv().unwrap();
|
||||
}).map_err(|_| panic!())
|
||||
// Block until woken
|
||||
rx3.recv().unwrap();
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
})
|
||||
})
|
||||
}));
|
||||
|
||||
// Spawn a future that will try to block, get notified, then not actually
|
||||
@@ -136,8 +129,7 @@ fn capacity_is_use_it_or_lose_it() {
|
||||
0 => {
|
||||
i = 1;
|
||||
|
||||
let res = blocking(|| unreachable!())
|
||||
.map_err(|_| panic!());
|
||||
let res = blocking(|| unreachable!()).map_err(|_| panic!());
|
||||
|
||||
assert!(res.unwrap().is_not_ready());
|
||||
|
||||
@@ -157,8 +149,7 @@ fn capacity_is_use_it_or_lose_it() {
|
||||
return Ok(NotReady);
|
||||
}
|
||||
2 => {
|
||||
let res = blocking(|| unreachable!())
|
||||
.map_err(|_| panic!());
|
||||
let res = blocking(|| unreachable!()).map_err(|_| panic!());
|
||||
|
||||
assert!(res.unwrap().is_not_ready());
|
||||
|
||||
@@ -177,10 +168,7 @@ fn capacity_is_use_it_or_lose_it() {
|
||||
|
||||
#[test]
|
||||
fn blocking_thread_does_not_take_over_shutdown_worker_thread() {
|
||||
let pool = Builder::new()
|
||||
.pool_size(2)
|
||||
.max_blocking(1)
|
||||
.build();
|
||||
let pool = Builder::new().pool_size(2).max_blocking(1).build();
|
||||
|
||||
let (enter_tx, enter_rx) = mpsc::channel();
|
||||
let (exit_tx, exit_rx) = mpsc::channel();
|
||||
@@ -197,7 +185,8 @@ fn blocking_thread_does_not_take_over_shutdown_worker_thread() {
|
||||
enter_tx.send(()).unwrap();
|
||||
exit_rx.recv().unwrap();
|
||||
exited.store(true, Relaxed);
|
||||
}).map_err(|_| panic!())
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
})
|
||||
}));
|
||||
}
|
||||
@@ -208,13 +197,9 @@ fn blocking_thread_does_not_take_over_shutdown_worker_thread() {
|
||||
// Spawn another task that attempts to block
|
||||
pool.spawn(lazy(move || {
|
||||
poll_fn(move || {
|
||||
let res = blocking(|| {
|
||||
let res = blocking(|| {}).unwrap();
|
||||
|
||||
}).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
res.is_ready(),
|
||||
exited.load(Relaxed));
|
||||
assert_eq!(res.is_ready(), exited.load(Relaxed));
|
||||
|
||||
try_tx.send(res.is_ready()).unwrap();
|
||||
|
||||
@@ -242,10 +227,7 @@ fn blocking_one_time_gets_capacity_for_multiple_blocks() {
|
||||
const BLOCKING: usize = 2;
|
||||
|
||||
for _ in 0..ITER {
|
||||
let pool = Builder::new()
|
||||
.pool_size(4)
|
||||
.max_blocking(1)
|
||||
.build();
|
||||
let pool = Builder::new().pool_size(4).max_blocking(1).build();
|
||||
|
||||
let rem = Arc::new(AtomicUsize::new(BLOCKING));
|
||||
let (tx, rx) = mpsc::channel();
|
||||
@@ -259,7 +241,8 @@ fn blocking_one_time_gets_capacity_for_multiple_blocks() {
|
||||
// First block
|
||||
let res = blocking(|| {
|
||||
thread::sleep(Duration::from_millis(100));
|
||||
}).map_err(|e| panic!("blocking err {:?}", e));
|
||||
})
|
||||
.map_err(|e| panic!("blocking err {:?}", e));
|
||||
|
||||
try_ready!(res);
|
||||
|
||||
@@ -302,8 +285,12 @@ fn shutdown() {
|
||||
Builder::new()
|
||||
.pool_size(1)
|
||||
.max_blocking(BLOCKING)
|
||||
.after_start(move || { num_inc.fetch_add(1, Relaxed); })
|
||||
.before_stop(move || { num_dec.fetch_add(1, Relaxed); })
|
||||
.after_start(move || {
|
||||
num_inc.fetch_add(1, Relaxed);
|
||||
})
|
||||
.before_stop(move || {
|
||||
num_dec.fetch_add(1, Relaxed);
|
||||
})
|
||||
.build()
|
||||
};
|
||||
|
||||
@@ -317,7 +304,8 @@ fn shutdown() {
|
||||
let res = blocking(|| {
|
||||
barrier.wait();
|
||||
Ok::<_, ()>(())
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
tx.send(()).unwrap();
|
||||
|
||||
@@ -394,7 +382,8 @@ fn hammer() {
|
||||
}
|
||||
|
||||
cnt_block.fetch_add(1, Relaxed);
|
||||
}).map_err(|_| panic!())
|
||||
})
|
||||
.map_err(|_| panic!())
|
||||
})
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -3,16 +3,16 @@ extern crate tokio_threadpool;
|
||||
|
||||
use tokio_threadpool::*;
|
||||
|
||||
use futures::{Future, Stream, Sink, Poll};
|
||||
use futures::{Future, Poll, Sink, Stream};
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn hammer() {
|
||||
use futures::future;
|
||||
use futures::sync::{oneshot, mpsc};
|
||||
use futures::sync::{mpsc, oneshot};
|
||||
|
||||
const N: usize = 1000;
|
||||
const ITER: usize = 20;
|
||||
@@ -37,7 +37,7 @@ fn hammer() {
|
||||
}
|
||||
}
|
||||
|
||||
for _ in 0.. ITER {
|
||||
for _ in 0..ITER {
|
||||
let pool = Builder::new()
|
||||
// .pool_size(30)
|
||||
.build();
|
||||
@@ -61,14 +61,13 @@ fn hammer() {
|
||||
rx2
|
||||
})
|
||||
.map_err(|e| panic!("e={:?}", e))
|
||||
.and_then(|_| {
|
||||
Ok(())
|
||||
});
|
||||
.and_then(|_| Ok(()));
|
||||
|
||||
pool.spawn(Counted {
|
||||
inner: task,
|
||||
cnt: c1.clone(),
|
||||
}).unwrap();
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
});
|
||||
@@ -85,17 +84,12 @@ fn hammer() {
|
||||
listen_tx.send(tx).unwrap();
|
||||
|
||||
pool.spawn({
|
||||
let task = rx
|
||||
.map_err(|e| panic!("rx err={:?}", e))
|
||||
.and_then(|tx| {
|
||||
tx.send(()).unwrap();
|
||||
Ok(())
|
||||
});
|
||||
let task = rx.map_err(|e| panic!("rx err={:?}", e)).and_then(|tx| {
|
||||
tx.send(()).unwrap();
|
||||
Ok(())
|
||||
});
|
||||
|
||||
Counted {
|
||||
inner: task,
|
||||
cnt,
|
||||
}
|
||||
Counted { inner: task, cnt }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
extern crate tokio_threadpool;
|
||||
extern crate tokio_executor;
|
||||
extern crate futures;
|
||||
extern crate env_logger;
|
||||
extern crate futures;
|
||||
extern crate tokio_executor;
|
||||
extern crate tokio_threadpool;
|
||||
|
||||
use tokio_executor::park::{Park, Unpark};
|
||||
use tokio_threadpool::*;
|
||||
use tokio_threadpool::park::{DefaultPark, DefaultUnpark};
|
||||
use tokio_threadpool::*;
|
||||
|
||||
use futures::{Poll, Sink, Stream, Async, Future};
|
||||
use futures::future::lazy;
|
||||
use futures::{Async, Future, Poll, Sink, Stream};
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::sync::atomic::*;
|
||||
use std::sync::atomic::Ordering::Relaxed;
|
||||
use std::sync::atomic::*;
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::time::Duration;
|
||||
|
||||
thread_local!(static FOO: Cell<u32> = Cell::new(0));
|
||||
@@ -56,7 +56,8 @@ fn natural_shutdown_simple_futures() {
|
||||
|
||||
t.send("one").unwrap();
|
||||
Ok(())
|
||||
})).unwrap();
|
||||
}))
|
||||
.unwrap();
|
||||
rx
|
||||
};
|
||||
|
||||
@@ -68,7 +69,8 @@ fn natural_shutdown_simple_futures() {
|
||||
|
||||
t.send("two").unwrap();
|
||||
Ok(())
|
||||
})).unwrap();
|
||||
}))
|
||||
.unwrap();
|
||||
rx
|
||||
};
|
||||
|
||||
@@ -223,7 +225,8 @@ fn many_oneshot_futures() {
|
||||
tx.spawn(lazy(move || {
|
||||
cnt.fetch_add(1, Relaxed);
|
||||
Ok(())
|
||||
})).unwrap();
|
||||
}))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// Wait for the pool to shutdown
|
||||
@@ -257,15 +260,17 @@ fn many_multishot_futures() {
|
||||
for _ in 0..CHAIN {
|
||||
let (next_tx, next_rx) = mpsc::channel(10);
|
||||
|
||||
let rx = chain_rx
|
||||
.map_err(|e| panic!("{:?}", e));
|
||||
let rx = chain_rx.map_err(|e| panic!("{:?}", e));
|
||||
|
||||
// Forward all the messages
|
||||
pool_tx.spawn(next_tx
|
||||
.send_all(rx)
|
||||
.map(|_| ())
|
||||
.map_err(|e| panic!("{:?}", e))
|
||||
).unwrap();
|
||||
pool_tx
|
||||
.spawn(
|
||||
next_tx
|
||||
.send_all(rx)
|
||||
.map(|_| ())
|
||||
.map_err(|e| panic!("{:?}", e)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
chain_rx = next_rx;
|
||||
}
|
||||
@@ -321,7 +326,8 @@ fn global_executor_is_configured() {
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
})).unwrap();
|
||||
}))
|
||||
.unwrap();
|
||||
|
||||
signal_rx.recv().unwrap();
|
||||
|
||||
@@ -339,17 +345,12 @@ fn busy_threadpool_is_not_idle() {
|
||||
use futures::sync::oneshot;
|
||||
|
||||
// let pool = ThreadPool::new();
|
||||
let pool = Builder::new()
|
||||
.pool_size(4)
|
||||
.max_blocking(2)
|
||||
.build();
|
||||
let pool = Builder::new().pool_size(4).max_blocking(2).build();
|
||||
let tx = pool.sender().clone();
|
||||
|
||||
let (term_tx, term_rx) = oneshot::channel();
|
||||
|
||||
tx.spawn(term_rx.then(|_| {
|
||||
Ok(())
|
||||
})).unwrap();
|
||||
tx.spawn(term_rx.then(|_| Ok(()))).unwrap();
|
||||
|
||||
let mut idle = pool.shutdown_on_idle();
|
||||
|
||||
@@ -426,7 +427,7 @@ fn multi_threadpool() {
|
||||
|
||||
#[test]
|
||||
fn eagerly_drops_futures() {
|
||||
use futures::future::{Future, lazy, empty};
|
||||
use futures::future::{empty, lazy, Future};
|
||||
use futures::task;
|
||||
use std::sync::mpsc;
|
||||
|
||||
@@ -486,12 +487,10 @@ fn eagerly_drops_futures() {
|
||||
let notify_on_drop = NotifyOnDrop(drop_tx);
|
||||
|
||||
let pool = tokio_threadpool::Builder::new()
|
||||
.custom_park(move |_| {
|
||||
MyPark {
|
||||
inner: DefaultPark::new(),
|
||||
park_tx: park_tx.clone(),
|
||||
unpark_tx: unpark_tx.clone(),
|
||||
}
|
||||
.custom_park(move |_| MyPark {
|
||||
inner: DefaultPark::new(),
|
||||
park_tx: park_tx.clone(),
|
||||
unpark_tx: unpark_tx.clone(),
|
||||
})
|
||||
.build();
|
||||
|
||||
@@ -506,7 +505,9 @@ fn eagerly_drops_futures() {
|
||||
// `notify_on_drop` handle.
|
||||
empty::<(), ()>().then(move |_| {
|
||||
// This code path should never be reached.
|
||||
if true { panic!() }
|
||||
if true {
|
||||
panic!()
|
||||
}
|
||||
|
||||
// Explicitly drop `notify_on_drop` here, this is mostly to ensure
|
||||
// that the `notify_on_drop` handle gets moved into the task. It
|
||||
|
||||
Reference in New Issue
Block a user