task: return JoinHandle from spawn (#1777)

`tokio::spawn` now returns a `JoinHandle` to obtain the result of the task:

Closes #887.
This commit is contained in:
Carl Lerche
2019-11-16 08:28:34 -08:00
committed by GitHub
parent 3f0eabe779
commit 19f1fc36bd
12 changed files with 112 additions and 81 deletions
+25 -2
View File
@@ -80,7 +80,7 @@ rt_test! {
}
#[test]
fn spawn_one() {
fn spawn_one_bg() {
let mut rt = rt();
let out = rt.block_on(async {
@@ -96,6 +96,29 @@ rt_test! {
assert_eq!(out, "ZOMG");
}
#[test]
fn spawn_one_join() {
let mut rt = rt();
let out = rt.block_on(async {
let (tx, rx) = oneshot::channel();
let handle = tokio::spawn(async move {
tx.send("ZOMG").unwrap();
"DONE"
});
let msg = assert_ok!(rx.await);
let out = assert_ok!(handle.await);
assert_eq!(out, "DONE");
msg
});
assert_eq!(out, "ZOMG");
}
#[test]
fn spawn_two() {
let mut rt = rt();
@@ -180,7 +203,7 @@ rt_test! {
tokio::spawn(poll_fn(move |_| {
assert_eq!(2, Arc::strong_count(&cnt));
Poll::Pending
Poll::<()>::Pending
}));
});