Remove dead futures2 code. (#538)

The futures 0.2 crate is not intended for widespread usage. Also, the
futures team is exploring the compat shim route.

If futures 0.3 support is added to Tokio 0.1, then a different
integration route will be explored, making the current code unhelpful.
This commit is contained in:
Carl Lerche
2018-08-09 21:56:53 -07:00
committed by GitHub
parent 96b556fbff
commit d91c775f36
31 changed files with 67 additions and 1380 deletions
+38 -160
View File
@@ -3,27 +3,10 @@ extern crate tokio_executor;
extern crate futures;
extern crate env_logger;
#[cfg(feature = "unstable-futures")]
extern crate futures2;
use tokio_threadpool::*;
#[cfg(not(feature = "unstable-futures"))]
use futures::{Poll, Sink, Stream, Async, Future};
#[cfg(not(feature = "unstable-futures"))]
use futures::future::lazy;
#[cfg(feature = "unstable-futures")]
use futures2::prelude::*;
#[cfg(feature = "unstable-futures")]
fn lazy<R, F>(f: F) -> Box<Future<Item = R::Item, Error = R::Error> + Send> where
F: Send + 'static + FnOnce() -> R,
R: Send + 'static + IntoFuture,
R::Future: Send,
{
Box::new(::futures2::future::lazy(|_| f()))
}
use std::cell::Cell;
use std::sync::{mpsc, Arc};
use std::sync::atomic::*;
@@ -32,57 +15,10 @@ use std::time::Duration;
thread_local!(static FOO: Cell<u32> = Cell::new(0));
#[cfg(not(feature = "unstable-futures"))]
fn spawn_pool<F>(pool: &mut Sender, f: F)
where F: Future<Item = (), Error = ()> + Send + 'static
{
pool.spawn(f).unwrap()
}
#[cfg(feature = "unstable-futures")]
fn spawn_pool<F>(pool: &mut Sender, f: F)
where F: Future<Item = (), Error = ()> + Send + 'static
{
futures2::executor::Executor::spawn(
pool,
Box::new(f.map_err(|_| panic!()))
).unwrap()
}
#[cfg(not(feature = "unstable-futures"))]
fn spawn_default<F>(f: F)
where F: Future<Item = (), Error = ()> + Send + 'static
{
tokio_executor::spawn(f)
}
#[cfg(feature = "unstable-futures")]
fn spawn_default<F>(f: F)
where F: Future<Item = (), Error = ()> + Send + 'static
{
tokio_executor::spawn2(Box::new(f.map_err(|_| panic!())))
}
fn ignore_results<F: Future + Send + 'static>(f: F) -> Box<Future<Item = (), Error = ()> + Send> {
Box::new(f.map(|_| ()).map_err(|_| ()))
}
#[cfg(feature = "unstable-futures")]
fn await_shutdown(shutdown: Shutdown) {
futures::Future::wait(shutdown).unwrap()
}
#[cfg(not(feature = "unstable-futures"))]
fn await_shutdown(shutdown: Shutdown) {
shutdown.wait().unwrap()
}
#[cfg(not(feature = "unstable-futures"))]
fn block_on<F: Future>(f: F) -> Result<F::Item, F::Error> {
f.wait()
}
#[cfg(feature = "unstable-futures")]
fn block_on<F: Future>(f: F) -> Result<F::Item, F::Error> {
futures2::executor::block_on(f)
}
#[test]
fn natural_shutdown_simple_futures() {
let _ = ::env_logger::init();
@@ -107,29 +43,29 @@ fn natural_shutdown_simple_futures() {
.build()
};
let mut tx = pool.sender().clone();
let tx = pool.sender().clone();
let a = {
let (t, rx) = mpsc::channel();
spawn_pool(&mut tx, lazy(move || {
tx.spawn(lazy(move || {
// Makes sure this runs on a worker thread
FOO.with(|f| assert_eq!(f.get(), 0));
t.send("one").unwrap();
Ok(())
}));
})).unwrap();
rx
};
let b = {
let (t, rx) = mpsc::channel();
spawn_pool(&mut tx, lazy(move || {
tx.spawn(lazy(move || {
// Makes sure this runs on a worker thread
FOO.with(|f| assert_eq!(f.get(), 0));
t.send("two").unwrap();
Ok(())
}));
})).unwrap();
rx
};
@@ -139,7 +75,7 @@ fn natural_shutdown_simple_futures() {
assert_eq!("two", b.recv().unwrap());
// Wait for the pool to shutdown
await_shutdown(pool.shutdown());
pool.shutdown().wait().unwrap();
// Assert that at least one thread started
let num_inc = num_inc.load(Relaxed);
@@ -163,7 +99,6 @@ fn force_shutdown_drops_futures() {
struct Never(Arc<AtomicUsize>);
#[cfg(not(feature = "unstable-futures"))]
impl Future for Never {
type Item = ();
type Error = ();
@@ -173,16 +108,6 @@ fn force_shutdown_drops_futures() {
}
}
#[cfg(feature = "unstable-futures")]
impl Future for Never {
type Item = ();
type Error = ();
fn poll(&mut self, _: &mut futures2::task::Context) -> Poll<(), ()> {
Ok(Async::Pending)
}
}
impl Drop for Never {
fn drop(&mut self) {
self.0.fetch_add(1, Relaxed);
@@ -201,10 +126,10 @@ fn force_shutdown_drops_futures() {
.build();
let mut tx = pool.sender().clone();
spawn_pool(&mut tx, Never(num_drop.clone()));
tx.spawn(Never(num_drop.clone())).unwrap();
// Wait for the pool to shutdown
await_shutdown(pool.shutdown_now());
pool.shutdown_now().wait().unwrap();
// Assert that only a single thread was spawned.
let a = num_inc.load(Relaxed);
@@ -231,7 +156,6 @@ fn drop_threadpool_drops_futures() {
struct Never(Arc<AtomicUsize>);
#[cfg(not(feature = "unstable-futures"))]
impl Future for Never {
type Item = ();
type Error = ();
@@ -241,16 +165,6 @@ fn drop_threadpool_drops_futures() {
}
}
#[cfg(feature = "unstable-futures")]
impl Future for Never {
type Item = ();
type Error = ();
fn poll(&mut self, _: &mut futures2::task::Context) -> Poll<(), ()> {
Ok(Async::Pending)
}
}
impl Drop for Never {
fn drop(&mut self) {
self.0.fetch_add(1, Relaxed);
@@ -271,7 +185,7 @@ fn drop_threadpool_drops_futures() {
.build();
let mut tx = pool.sender().clone();
spawn_pool(&mut tx, Never(num_drop.clone()));
tx.spawn(Never(num_drop.clone())).unwrap();
// Wait for the pool to shutdown
drop(pool);
@@ -309,13 +223,13 @@ fn thread_shutdown_timeout() {
let _ = t.lock().unwrap().send(());
})
.build();
let mut tx = pool.sender().clone();
let tx = pool.sender().clone();
let t = complete_tx.clone();
spawn_pool(&mut tx, lazy(move || {
tx.spawn(lazy(move || {
t.send(()).unwrap();
Ok(())
}));
})).unwrap();
// The future completes
complete_rx.recv().unwrap();
@@ -324,14 +238,14 @@ fn thread_shutdown_timeout() {
shutdown_rx.recv().unwrap();
// Futures can still be run
spawn_pool(&mut tx, lazy(move || {
tx.spawn(lazy(move || {
complete_tx.send(()).unwrap();
Ok(())
}));
})).unwrap();
complete_rx.recv().unwrap();
await_shutdown(pool.shutdown());
pool.shutdown().wait().unwrap();
}
#[test]
@@ -347,14 +261,14 @@ fn many_oneshot_futures() {
for _ in 0..NUM {
let cnt = cnt.clone();
spawn_pool(&mut tx, lazy(move || {
tx.spawn(lazy(move || {
cnt.fetch_add(1, Relaxed);
Ok(())
}));
})).unwrap();
}
// Wait for the pool to shutdown
await_shutdown(pool.shutdown());
pool.shutdown().wait().unwrap();
let num = cnt.load(Relaxed);
assert_eq!(num, NUM);
@@ -363,12 +277,8 @@ fn many_oneshot_futures() {
#[test]
fn many_multishot_futures() {
#[cfg(not(feature = "unstable-futures"))]
use futures::sync::mpsc;
#[cfg(feature = "unstable-futures")]
use futures2::channel::mpsc;
const CHAIN: usize = 200;
const CYCLES: usize = 5;
const TRACKS: usize = 50;
@@ -392,11 +302,11 @@ fn many_multishot_futures() {
.map_err(|e| panic!("{:?}", e));
// Forward all the messages
spawn_pool(&mut pool_tx, next_tx
pool_tx.spawn(next_tx
.send_all(rx)
.map(|_| ())
.map_err(|e| panic!("{:?}", e))
);
).unwrap();
chain_rx = next_rx;
}
@@ -419,84 +329,73 @@ fn many_multishot_futures() {
Ok(())
})
});
spawn_pool(&mut pool_tx, ignore_results(task));
pool_tx.spawn(ignore_results(task)).unwrap();
start_txs.push(start_tx);
final_rxs.push(final_rx);
}
for start_tx in start_txs {
block_on(start_tx.send("ping")).unwrap();
start_tx.send("ping").wait().unwrap();
}
for final_rx in final_rxs {
{#![cfg(feature = "unstable-futures")]
block_on(final_rx.next()).unwrap();
}
{#![cfg(not(feature = "unstable-futures"))]
block_on(final_rx.into_future()).unwrap();
}
final_rx.wait().next().unwrap().unwrap();
}
// Shutdown the pool
await_shutdown(pool.shutdown());
pool.shutdown().wait().unwrap();
}
}
#[test]
fn global_executor_is_configured() {
let pool = ThreadPool::new();
let mut tx = pool.sender().clone();
let tx = pool.sender().clone();
let (signal_tx, signal_rx) = mpsc::channel();
spawn_pool(&mut tx, lazy(move || {
spawn_default(lazy(move || {
tx.spawn(lazy(move || {
tokio_executor::spawn(lazy(move || {
signal_tx.send(()).unwrap();
Ok(())
}));
Ok(())
}));
})).unwrap();
signal_rx.recv().unwrap();
await_shutdown(pool.shutdown());
pool.shutdown().wait().unwrap();
}
#[test]
fn new_threadpool_is_idle() {
let pool = ThreadPool::new();
await_shutdown(pool.shutdown_on_idle());
pool.shutdown_on_idle().wait().unwrap();
}
#[test]
fn busy_threadpool_is_not_idle() {
#[cfg(not(feature = "unstable-futures"))]
use futures::sync::oneshot;
#[cfg(feature = "unstable-futures")]
use futures2::channel::oneshot;
// let pool = ThreadPool::new();
let pool = Builder::new()
.pool_size(4)
.max_blocking(2)
.build();
let mut tx = pool.sender().clone();
let tx = pool.sender().clone();
let (term_tx, term_rx) = oneshot::channel();
spawn_pool(&mut tx, term_rx.then(|_| {
tx.spawn(term_rx.then(|_| {
Ok(())
}));
})).unwrap();
let mut idle = pool.shutdown_on_idle();
struct IdleFut<'a>(&'a mut Shutdown);
#[cfg(not(feature = "unstable-futures"))]
impl<'a> Future for IdleFut<'a> {
type Item = ();
type Error = ();
@@ -506,31 +405,20 @@ fn busy_threadpool_is_not_idle() {
}
}
#[cfg(feature = "unstable-futures")]
impl<'a> Future for IdleFut<'a> {
type Item = ();
type Error = ();
fn poll(&mut self, cx: &mut futures2::task::Context) -> Poll<(), ()> {
assert!(self.0.poll(cx).unwrap().is_pending());
Ok(Async::Ready(()))
}
}
block_on(IdleFut(&mut idle)).unwrap();
IdleFut(&mut idle).wait().unwrap();
term_tx.send(()).unwrap();
await_shutdown(idle);
idle.wait().unwrap();
}
#[test]
fn panic_in_task() {
let pool = ThreadPool::new();
let mut tx = pool.sender().clone();
let tx = pool.sender().clone();
struct Boom;
#[cfg(not(feature = "unstable-futures"))]
impl Future for Boom {
type Item = ();
type Error = ();
@@ -540,25 +428,15 @@ fn panic_in_task() {
}
}
#[cfg(feature = "unstable-futures")]
impl Future for Boom {
type Item = ();
type Error = ();
fn poll(&mut self, _cx: &mut futures2::task::Context) -> Poll<(), ()> {
panic!();
}
}
impl Drop for Boom {
fn drop(&mut self) {
assert!(::std::thread::panicking());
}
}
spawn_pool(&mut tx, Boom);
tx.spawn(Boom).unwrap();
await_shutdown(pool.shutdown_on_idle());
pool.shutdown_on_idle().wait().unwrap();
}
#[test]