Files
tokio/tokio-executor/tests/threadpool_blocking.rs
T

413 lines
10 KiB
Rust
Raw Normal View History

#![warn(rust_2018_idioms)]
2018-04-15 12:29:22 -07:00
use tokio_executor::threadpool::*;
2019-06-27 22:30:56 -07:00
use tokio_test::*;
use futures_core::ready;
2019-08-07 16:26:44 -04:00
use futures_util::future::poll_fn;
2018-04-15 12:29:22 -07:00
use rand::*;
use std::sync::atomic::Ordering::*;
2019-02-21 11:56:15 -08:00
use std::sync::atomic::*;
use std::sync::*;
2019-06-27 22:30:56 -07:00
use std::task::{Poll, Waker};
2018-04-15 12:29:22 -07:00
use std::thread;
2019-02-21 11:56:15 -08:00
use std::time::Duration;
2019-06-27 22:30:56 -07:00
2018-04-15 12:29:22 -07:00
#[test]
fn basic() {
2019-02-21 11:56:15 -08:00
let pool = Builder::new().pool_size(1).max_blocking(1).build();
2018-04-15 12:29:22 -07:00
let (tx1, rx1) = mpsc::channel();
let (tx2, rx2) = mpsc::channel();
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
2018-04-15 12:29:22 -07:00
let res = blocking(|| {
let v = rx1.recv().unwrap();
tx2.send(v).unwrap();
2019-06-27 22:30:56 -07:00
});
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
assert_ready!(res).unwrap();
});
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
2018-04-15 12:29:22 -07:00
tx1.send(()).unwrap();
2019-06-27 22:30:56 -07:00
});
2018-04-15 12:29:22 -07:00
rx2.recv().unwrap();
}
#[test]
fn other_executors_can_run_inside_blocking() {
let pool = Builder::new().pool_size(1).max_blocking(1).build();
let (tx, rx) = mpsc::channel();
pool.spawn(async move {
let res = blocking(|| {
let _e = tokio_executor::enter().expect("nested blocking enter");
tx.send(()).unwrap();
});
assert_ready!(res).unwrap();
});
rx.recv().unwrap();
}
2018-04-15 12:29:22 -07:00
#[test]
fn notify_task_on_capacity() {
const BLOCKING: usize = 10;
2019-02-21 11:56:15 -08:00
let pool = Builder::new().pool_size(1).max_blocking(1).build();
2018-04-15 12:29:22 -07:00
let rem = Arc::new(AtomicUsize::new(BLOCKING));
let (tx, rx) = mpsc::channel();
for _ in 0..BLOCKING {
let rem = rem.clone();
let tx = tx.clone();
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
poll_fn(move |_| {
2018-04-15 12:29:22 -07:00
blocking(|| {
thread::sleep(Duration::from_millis(100));
2019-06-27 22:30:56 -07:00
let prev = rem.fetch_sub(1, SeqCst);
2018-04-15 12:29:22 -07:00
if prev == 1 {
tx.send(()).unwrap();
}
2019-02-21 11:56:15 -08:00
})
.map_err(|e| panic!("blocking err {:?}", e))
2018-04-15 12:29:22 -07:00
})
2019-06-27 22:30:56 -07:00
.await
.unwrap()
});
2018-04-15 12:29:22 -07:00
}
rx.recv().unwrap();
2019-06-27 22:30:56 -07:00
assert_eq!(0, rem.load(SeqCst));
2018-04-15 12:29:22 -07:00
}
#[test]
fn capacity_is_use_it_or_lose_it() {
2019-06-27 22:30:56 -07:00
use tokio_sync::oneshot;
2018-04-15 12:29:22 -07:00
// TODO: Run w/ bigger pool size
2019-02-21 11:56:15 -08:00
let pool = Builder::new().pool_size(1).max_blocking(1).build();
2018-04-15 12:29:22 -07:00
let (tx1, rx1) = mpsc::channel();
let (tx2, rx2) = oneshot::channel();
let (tx3, rx3) = mpsc::channel();
let (tx4, rx4) = mpsc::channel();
// First, fill the blocking capacity
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
poll_fn(move |_| {
2018-04-15 12:29:22 -07:00
blocking(|| {
rx1.recv().unwrap();
2019-02-21 11:56:15 -08:00
})
2018-04-15 12:29:22 -07:00
})
2019-06-27 22:30:56 -07:00
.await
.unwrap()
});
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
let task: Waker = rx2.await.unwrap();
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
poll_fn(move |_| {
blocking(|| {
// Notify the other task
task.wake_by_ref();
// Block until woken
rx3.recv().unwrap();
2018-04-15 12:29:22 -07:00
})
2019-02-21 11:56:15 -08:00
})
2019-06-27 22:30:56 -07:00
.await
.unwrap();
});
2018-04-15 12:29:22 -07:00
// Spawn a future that will try to block, get notified, then not actually
// use the blocking
let mut i = 0;
let mut tx2 = Some(tx2);
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
poll_fn(move |cx| {
2018-04-15 12:29:22 -07:00
match i {
0 => {
i = 1;
2019-02-21 11:56:15 -08:00
let res = blocking(|| unreachable!()).map_err(|_| panic!());
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
assert_pending!(res);
2018-04-15 12:29:22 -07:00
// Unblock the first blocker
tx1.send(()).unwrap();
2019-06-27 22:30:56 -07:00
return Poll::Pending;
2018-04-15 12:29:22 -07:00
}
1 => {
i = 2;
// Skip blocking, and notify the second task that it should
// start blocking
2019-06-27 22:30:56 -07:00
let me = cx.waker().clone();
2018-04-15 12:29:22 -07:00
tx2.take().unwrap().send(me).unwrap();
2019-06-27 22:30:56 -07:00
return Poll::Pending;
2018-04-15 12:29:22 -07:00
}
2 => {
2019-02-21 11:56:15 -08:00
let res = blocking(|| unreachable!()).map_err(|_| panic!());
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
assert_pending!(res);
2018-04-15 12:29:22 -07:00
// Unblock the first blocker
tx3.send(()).unwrap();
tx4.send(()).unwrap();
2019-06-27 22:30:56 -07:00
Poll::Ready(())
2018-04-15 12:29:22 -07:00
}
_ => unreachable!(),
}
})
2019-06-27 22:30:56 -07:00
.await
});
2018-04-15 12:29:22 -07:00
rx4.recv().unwrap();
}
#[test]
fn blocking_thread_does_not_take_over_shutdown_worker_thread() {
2019-02-21 11:56:15 -08:00
let pool = Builder::new().pool_size(2).max_blocking(1).build();
2018-04-15 12:29:22 -07:00
let (enter_tx, enter_rx) = mpsc::channel();
let (exit_tx, exit_rx) = mpsc::channel();
let (try_tx, try_rx) = mpsc::channel();
let exited = Arc::new(AtomicBool::new(false));
{
let exited = exited.clone();
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
poll_fn(move |_| {
2018-04-15 12:29:22 -07:00
blocking(|| {
enter_tx.send(()).unwrap();
exit_rx.recv().unwrap();
2019-06-27 22:30:56 -07:00
exited.store(true, SeqCst);
2019-02-21 11:56:15 -08:00
})
2018-04-15 12:29:22 -07:00
})
2019-06-27 22:30:56 -07:00
.await
.unwrap()
});
2018-04-15 12:29:22 -07:00
}
// Wait for the task to block
let _ = enter_rx.recv().unwrap();
// Spawn another task that attempts to block
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
poll_fn(move |_| {
let res = blocking(|| {});
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
assert_eq!(res.is_ready(), exited.load(SeqCst));
2018-04-15 12:29:22 -07:00
try_tx.send(res.is_ready()).unwrap();
2019-06-27 22:30:56 -07:00
res.map(|_| ())
2018-04-15 12:29:22 -07:00
})
2019-06-27 22:30:56 -07:00
.await
});
2018-04-15 12:29:22 -07:00
// Wait for the second task to try to block (and not be ready).
let res = try_rx.recv().unwrap();
assert!(!res);
// Unblock the first task
exit_tx.send(()).unwrap();
// Wait for the second task to successfully block.
let res = try_rx.recv().unwrap();
assert!(res);
drop(pool);
}
#[test]
2018-05-08 14:44:17 -04:00
fn blocking_one_time_gets_capacity_for_multiple_blocks() {
2018-04-15 12:29:22 -07:00
const ITER: usize = 1;
const BLOCKING: usize = 2;
for _ in 0..ITER {
2019-02-21 11:56:15 -08:00
let pool = Builder::new().pool_size(4).max_blocking(1).build();
2018-04-15 12:29:22 -07:00
let rem = Arc::new(AtomicUsize::new(BLOCKING));
let (tx, rx) = mpsc::channel();
for _ in 0..BLOCKING {
let rem = rem.clone();
let tx = tx.clone();
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
poll_fn(move |_| {
2018-04-15 12:29:22 -07:00
// First block
let res = blocking(|| {
thread::sleep(Duration::from_millis(100));
2019-06-27 22:30:56 -07:00
});
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
ready!(res).unwrap();
2018-04-15 12:29:22 -07:00
let res = blocking(|| {
thread::sleep(Duration::from_millis(100));
2019-06-27 22:30:56 -07:00
let prev = rem.fetch_sub(1, SeqCst);
2018-04-15 12:29:22 -07:00
if prev == 1 {
tx.send(()).unwrap();
}
});
2019-06-27 22:30:56 -07:00
assert!(res.is_ready());
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
Poll::Ready(())
2018-04-15 12:29:22 -07:00
})
2019-06-27 22:30:56 -07:00
.await
});
2018-04-15 12:29:22 -07:00
}
rx.recv().unwrap();
2019-06-27 22:30:56 -07:00
assert_eq!(0, rem.load(SeqCst));
2018-04-15 12:29:22 -07:00
}
}
#[test]
fn shutdown() {
const ITER: usize = 1_000;
const BLOCKING: usize = 10;
for _ in 0..ITER {
let num_inc = Arc::new(AtomicUsize::new(0));
let num_dec = Arc::new(AtomicUsize::new(0));
let (tx, rx) = mpsc::channel();
let pool = {
let num_inc = num_inc.clone();
let num_dec = num_dec.clone();
Builder::new()
.pool_size(1)
.max_blocking(BLOCKING)
2019-02-21 11:56:15 -08:00
.after_start(move || {
2019-06-27 22:30:56 -07:00
num_inc.fetch_add(1, SeqCst);
2019-02-21 11:56:15 -08:00
})
.before_stop(move || {
2019-06-27 22:30:56 -07:00
num_dec.fetch_add(1, SeqCst);
2019-02-21 11:56:15 -08:00
})
2018-04-15 12:29:22 -07:00
.build()
};
let barrier = Arc::new(Barrier::new(BLOCKING));
for _ in 0..BLOCKING {
let barrier = barrier.clone();
let tx = tx.clone();
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
2018-04-15 12:29:22 -07:00
let res = blocking(|| {
barrier.wait();
Ok::<_, ()>(())
2019-06-27 22:30:56 -07:00
});
2018-04-15 12:29:22 -07:00
tx.send(()).unwrap();
assert!(res.is_ready());
2019-06-27 22:30:56 -07:00
});
2018-04-15 12:29:22 -07:00
}
for _ in 0..BLOCKING {
rx.recv().unwrap();
}
// Shutdown
drop(pool);
2019-06-27 22:30:56 -07:00
assert_eq!(11, num_inc.load(SeqCst));
assert_eq!(11, num_dec.load(SeqCst));
2018-04-15 12:29:22 -07:00
}
}
#[derive(Debug, Copy, Clone)]
enum Sleep {
Skip,
Yield,
Rand,
Fixed(Duration),
}
#[test]
fn hammer() {
use self::Sleep::*;
const ITER: usize = 5;
let combos = [
(2, 4, 1_000, Skip),
(2, 4, 1_000, Yield),
(2, 4, 100, Rand),
(2, 4, 100, Fixed(Duration::from_millis(3))),
(2, 4, 100, Fixed(Duration::from_millis(12))),
];
for &(size, max_blocking, n, sleep) in &combos {
for _ in 0..ITER {
let pool = Builder::new()
.pool_size(size)
.max_blocking(max_blocking)
.build();
let cnt_task = Arc::new(AtomicUsize::new(0));
let cnt_block = Arc::new(AtomicUsize::new(0));
for _ in 0..n {
let cnt_task = cnt_task.clone();
let cnt_block = cnt_block.clone();
2019-06-27 22:30:56 -07:00
pool.spawn(async move {
cnt_task.fetch_add(1, SeqCst);
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
poll_fn(move |_| {
2018-04-15 12:29:22 -07:00
blocking(|| {
match sleep {
Skip => {}
Yield => {
thread::yield_now();
}
Rand => {
let ms = thread_rng().gen_range(3, 12);
thread::sleep(Duration::from_millis(ms));
}
Fixed(dur) => {
thread::sleep(dur);
}
}
2019-06-27 22:30:56 -07:00
cnt_block.fetch_add(1, SeqCst);
2019-02-21 11:56:15 -08:00
})
.map_err(|_| panic!())
2018-04-15 12:29:22 -07:00
})
2019-06-27 22:30:56 -07:00
.await
.unwrap()
});
2018-04-15 12:29:22 -07:00
}
// Wait for the work to complete
2019-06-27 22:30:56 -07:00
pool.shutdown_on_idle().wait();
2018-04-15 12:29:22 -07:00
2019-06-27 22:30:56 -07:00
assert_eq!(n, cnt_task.load(SeqCst));
assert_eq!(n, cnt_block.load(SeqCst));
2018-04-15 12:29:22 -07:00
}
}
}