blob: 5d276250b349d38dc7bb7593ffba6de97bc859de (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#![warn(rust_2018_idioms)]
#![cfg(all(feature = "full", tokio_unstable))]
use tokio::runtime::LocalOptions;
use tokio::task::spawn_local;
#[test]
fn test_spawn_local_in_runtime() {
let rt = rt();
let res = rt.block_on(async move {
let (tx, rx) = tokio::sync::oneshot::channel();
spawn_local(async {
tokio::task::yield_now().await;
tx.send(5).unwrap();
});
rx.await.unwrap()
});
assert_eq!(res, 5);
}
#[test]
fn test_spawn_from_handle() {
let rt = rt();
let (tx, rx) = tokio::sync::oneshot::channel();
rt.handle().spawn(async {
tokio::task::yield_now().await;
tx.send(5).unwrap();
});
let res = rt.block_on(async move { rx.await.unwrap() });
assert_eq!(res, 5);
}
#[test]
fn test_spawn_local_on_runtime_object() {
let rt = rt();
let (tx, rx) = tokio::sync::oneshot::channel();
rt.spawn_local(async {
tokio::task::yield_now().await;
tx.send(5).unwrap();
});
let res = rt.block_on(async move { rx.await.unwrap() });
assert_eq!(res, 5);
}
#[test]
fn test_spawn_local_from_guard() {
let rt = rt();
let (tx, rx) = tokio::sync::oneshot::channel();
let _guard = rt.enter();
spawn_local(async {
tokio::task::yield_now().await;
tx.send(5).unwrap();
});
let res = rt.block_on(async move { rx.await.unwrap() });
assert_eq!(res, 5);
}
#[test]
#[should_panic]
fn test_spawn_local_from_guard_other_thread() {
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let rt = rt();
let handle = rt.handle().clone();
tx.send(handle).unwrap();
});
let handle = rx.recv().unwrap();
let _guard = handle.enter();
spawn_local(async {});
}
fn rt() -> tokio::runtime::LocalRuntime {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build_local(&LocalOptions::default())
.unwrap()
}
|