From 011a7b02f70cbd29ce20bdf6ed8858117b616027 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 8 May 2017 16:18:42 -0700 Subject: [PATCH] Add a test for spawn-in-drop --- tests/spawn.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/spawn.rs b/tests/spawn.rs index d6250a1f3..98434a818 100644 --- a/tests/spawn.rs +++ b/tests/spawn.rs @@ -2,11 +2,12 @@ extern crate tokio_core; extern crate env_logger; extern crate futures; +use std::any::Any; use std::sync::mpsc; use std::thread; use std::time::Duration; -use futures::Future; +use futures::{Future, Poll}; use futures::future; use futures::sync::oneshot; use tokio_core::reactor::{Core, Timeout}; @@ -95,3 +96,52 @@ fn drop_timeout_in_spawn() { lp.run(rx).unwrap(); } + +#[test] +fn spawn_in_drop() { + drop(env_logger::init()); + let mut lp = Core::new().unwrap(); + + let (tx, rx) = oneshot::channel(); + let remote = lp.remote(); + + struct OnDrop(F); + + impl Drop for OnDrop { + fn drop(&mut self) { + (self.0)(); + } + } + + struct MyFuture { + _data: Box, + } + + impl Future for MyFuture { + type Item = (); + type Error = (); + + fn poll(&mut self) -> Poll<(), ()> { + Ok(().into()) + } + } + + thread::spawn(move || { + let mut tx = Some(tx); + remote.spawn(|handle| { + let handle = handle.clone(); + MyFuture { + _data: Box::new(OnDrop(move || { + let mut tx = tx.take(); + handle.spawn_fn(move || { + tx.take().unwrap().send(()).unwrap(); + Ok(()) + }); + })), + + } + }); + }); + + lp.run(rx).unwrap(); +}