-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrt_threadpool.rs
208 lines (190 loc) · 5.86 KB
/
rt_threadpool.rs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::time::{Duration, Instant};
use futures_01::future::Future as Future01;
use futures_util::compat::Future01CompatExt;
use tokio_compat::runtime;
#[test]
fn can_run_01_futures() {
let future_ran = Arc::new(AtomicBool::new(false));
let ran = future_ran.clone();
runtime::run(futures_01::future::lazy(move || {
future_ran.store(true, Ordering::SeqCst);
Ok(())
}));
assert!(ran.load(Ordering::SeqCst));
}
#[test]
fn can_spawn_01_futures() {
let future_ran = Arc::new(AtomicBool::new(false));
let ran = future_ran.clone();
runtime::run(futures_01::future::lazy(move || {
tokio_01::spawn(futures_01::future::lazy(move || {
future_ran.store(true, Ordering::SeqCst);
Ok(())
}));
Ok(())
}));
assert!(ran.load(Ordering::SeqCst));
}
#[test]
fn can_spawn_std_futures() {
let future_ran = Arc::new(AtomicBool::new(false));
let ran = future_ran.clone();
runtime::run(futures_01::future::lazy(move || {
tokio_02::spawn(async move {
future_ran.store(true, Ordering::SeqCst);
});
Ok(())
}));
assert!(ran.load(Ordering::SeqCst));
}
#[test]
fn tokio_01_timers_work() {
let future1_ran = Arc::new(AtomicBool::new(false));
let ran = future1_ran.clone();
let future1 = futures_01::future::lazy(|| {
let when = Instant::now() + Duration::from_millis(15);
tokio_01::timer::Delay::new(when).map(move |_| when)
})
.map(move |when| {
ran.store(true, Ordering::SeqCst);
assert!(Instant::now() >= when);
})
.map_err(|_| panic!("timer should work"));
let future2_ran = Arc::new(AtomicBool::new(false));
let ran = future2_ran.clone();
let future2 = async move {
let when = Instant::now() + Duration::from_millis(10);
tokio_01::timer::Delay::new(when).compat().await.unwrap();
ran.store(true, Ordering::SeqCst);
assert!(Instant::now() >= when);
};
runtime::run(futures_01::future::lazy(move || {
tokio_02::spawn(future2);
tokio_01::spawn(future1);
Ok(())
}));
assert!(future1_ran.load(Ordering::SeqCst));
assert!(future2_ran.load(Ordering::SeqCst));
}
#[test]
fn block_on_01_timer() {
let mut rt = runtime::Runtime::new().unwrap();
let when = Instant::now() + Duration::from_millis(10);
rt.block_on(tokio_01::timer::Delay::new(when)).unwrap();
assert!(Instant::now() >= when);
}
#[test]
fn block_on_std_01_timer() {
let mut rt = runtime::Runtime::new().unwrap();
let when = Instant::now() + Duration::from_millis(10);
rt.block_on_std(async move {
tokio_01::timer::Delay::new(when).compat().await.unwrap();
});
assert!(Instant::now() >= when);
}
#[test]
fn block_on_01_spawn() {
let mut rt = runtime::Runtime::new().unwrap();
// other tests assert that spawned 0.1 tasks actually *run*, all we care
// is that we're able to spawn it successfully.
rt.block_on(futures_01::future::lazy(|| {
tokio_01::spawn(futures_01::future::lazy(|| Ok(())))
}))
.unwrap();
}
#[test]
fn block_on_std_01_spawn() {
let mut rt = runtime::Runtime::new().unwrap();
// other tests assert that spawned 0.1 tasks actually *run*, all we care
// is that we're able to spawn it successfully.
rt.block_on_std(async { tokio_01::spawn(futures_01::future::lazy(|| Ok(()))) });
}
#[test]
fn tokio_02_spawn_blocking_works() {
let ran = Arc::new(AtomicBool::new(false));
let ran2 = ran.clone();
runtime::run_std(async move {
println!("in future, before blocking");
tokio_02::task::spawn_blocking(move || {
println!("in blocking");
ran.store(true, Ordering::SeqCst);
})
.await
.expect("blocking task panicked!");
println!("blocking done");
});
assert!(ran2.load(Ordering::SeqCst));
}
#[test]
fn tokio_02_block_in_place_works() {
let ran = Arc::new(AtomicBool::new(false));
let ran2 = ran.clone();
runtime::run_std(async move {
println!("in future, before blocking");
tokio_02::task::spawn(async move {
tokio_02::task::block_in_place(move || {
println!("in blocking");
ran.store(true, Ordering::SeqCst);
})
})
.await
.expect("blocking task panicked!");
println!("blocking done");
});
assert!(ran2.load(Ordering::SeqCst));
}
#[test]
fn block_on_twice() {
// Repro for tokio-rs/tokio-compat#10.
let mut rt = runtime::Runtime::new().unwrap();
rt.block_on_std(async {
tokio_02::spawn(async {}).await.unwrap();
println!("spawn 1 done")
});
println!("block_on 1 done");
rt.block_on_std(async {
tokio_02::spawn(async {}).await.unwrap();
println!("spawn 2 done");
});
println!("done");
}
#[test]
fn idle_after_block_on() {
let mut rt = runtime::Runtime::new().unwrap();
let ran = Arc::new(AtomicBool::new(false));
rt.block_on_std(async {
tokio_02::spawn(async {}).await.unwrap();
});
let ran2 = ran.clone();
rt.spawn_std(async move {
tokio_02::task::yield_now().await;
ran2.store(true, Ordering::SeqCst);
});
rt.shutdown_on_idle();
assert!(ran.load(Ordering::SeqCst));
}
#[test]
fn enter_exposed() {
let rt = runtime::Runtime::new().unwrap();
rt.enter(|| {
let _handle = tokio_02::runtime::Handle::current();
});
}
#[test]
fn enter_can_spawn_01_futures() {
let future_ran = Arc::new(AtomicBool::new(false));
let ran = future_ran.clone();
let rt = runtime::Runtime::new().unwrap();
rt.enter(|| {
tokio_01::spawn(futures_01::future::lazy(move || {
future_ran.store(true, Ordering::SeqCst);
Ok(())
}))
});
rt.shutdown_on_idle();
assert!(ran.load(Ordering::SeqCst));
}