diff options
author | Carl Lerche <me@carllerche.com> | 2018-02-27 09:56:29 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-27 09:56:29 -0800 |
commit | 8e1a9101f098f56692978bb2d4985238359891fa (patch) | |
tree | c2ab4dcb53d8dffbfcc05b91fefd540c2d13d540 /tests | |
parent | 427b7325d0c45a5371808900d039970387eb2796 (diff) |
Support current_thread::spawn from task drop (#157)
Currently, the thread-local tracking the current thread executor is not
set when a task is dropped. This means that one cannot spawn a new
future from within the drop implementation of another future.
This patch adds support for this by setting the thread-local before
releasing a task.
This implementation is a bit messy. It probably could be cleaned up, but
this is being put off in favor of trying a more comprehensive
reorganization once the current thread executor is feature complete.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/current_thread.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/current_thread.rs b/tests/current_thread.rs index 2f3b98e5..51695f40 100644 --- a/tests/current_thread.rs +++ b/tests/current_thread.rs @@ -4,6 +4,7 @@ extern crate futures; use tokio::executor::current_thread::{self, block_on_all, CurrentThread}; +use std::any::Any; use std::cell::{Cell, RefCell}; use std::rc::Rc; use std::thread; @@ -294,6 +295,48 @@ fn spawn_and_turn() { } #[test] +fn spawn_in_drop() { + let mut current_thread = CurrentThread::new(); + + let (tx, rx) = oneshot::channel(); + + struct OnDrop<F: FnOnce()>(Option<F>); + + impl<F: FnOnce()> Drop for OnDrop<F> { + fn drop(&mut self) { + (self.0.take().unwrap())(); + } + } + + struct MyFuture { + _data: Box<Any>, + } + + impl Future for MyFuture { + type Item = (); + type Error = (); + + fn poll(&mut self) -> Poll<(), ()> { + Ok(().into()) + } + } + + current_thread.spawn({ + MyFuture { + _data: Box::new(OnDrop(Some(move || { + current_thread::spawn(lazy(move || { + tx.send(()).unwrap(); + Ok(()) + })); + }))), + } + }); + + current_thread.block_on(rx).unwrap(); + current_thread.run().unwrap(); +} + +#[test] fn hammer_turn() { use futures::sync::mpsc; |