-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtests.rs
More file actions
323 lines (286 loc) · 9.92 KB
/
tests.rs
File metadata and controls
323 lines (286 loc) · 9.92 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
use crate::metrics::WORKER_ACTIVE_SECONDS;
use crate::pool::*;
use crate::task::callback::Handle;
use futures_timer::Delay;
use rand::seq::SliceRandom;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc;
use std::thread;
use std::time::*;
#[test]
fn test_basic() {
let pool = Builder::new("test_basic")
.max_thread_count(4)
.build_callback_pool();
let (tx, rx) = mpsc::channel();
// Task should be executed immediately.
let t = tx.clone();
pool.spawn(move |_: &mut Handle<'_>| t.send(1).unwrap());
assert_eq!(Ok(1), rx.recv_timeout(Duration::from_secs(1)));
// Tasks should be executed concurrently.
let mut pairs = vec![];
for _ in 0..4 {
let (tx1, rx1) = mpsc::channel();
let (tx2, rx2) = mpsc::channel();
pool.spawn(move |_: &mut Handle<'_>| {
let t = rx1.recv().unwrap();
tx2.send(t).unwrap();
});
pairs.push((tx1, rx2));
}
pairs.shuffle(&mut rand::thread_rng());
for (tx, rx) in pairs {
let value: u64 = rand::random();
tx.send(value).unwrap();
assert_eq!(value, rx.recv_timeout(Duration::from_secs(1)).unwrap());
}
// A bunch of tasks should be executed correctly.
let cases: Vec<_> = (10..1000).collect();
for id in &cases {
let t = tx.clone();
let id = *id;
pool.spawn(move |_: &mut Handle<'_>| t.send(id).unwrap());
}
let mut ans = vec![];
for _ in 10..1000 {
let r = rx.recv_timeout(Duration::from_secs(1)).unwrap();
ans.push(r);
}
ans.sort();
assert_eq!(cases, ans);
// Shutdown should only wait for at most one tasks.
for _ in 0..5 {
let t = tx.clone();
pool.spawn(move |_: &mut Handle<'_>| {
thread::sleep(Duration::from_millis(100));
t.send(0).unwrap();
});
}
pool.shutdown();
// After dropping this tx, all tx should be destructed.
drop(tx);
for _ in 0..4 {
if rx.try_recv().is_err() {
break;
}
}
// After the pool is shut down, tasks in the queue should be dropped.
// So we should get a Disconnected error.
assert_eq!(Err(mpsc::TryRecvError::Disconnected), rx.try_recv());
}
#[test]
fn test_remote() {
let pool = Builder::new("test_remote")
.max_thread_count(4)
.build_callback_pool();
// Remote should work just like pool.
let remote = pool.remote();
let (tx, rx) = mpsc::channel();
let t = tx.clone();
remote.spawn(move |_: &mut Handle<'_>| t.send(1).unwrap());
assert_eq!(Ok(1), rx.recv_timeout(Duration::from_millis(500)));
for _ in 0..5 {
let t = tx.clone();
remote.spawn(move |_: &mut Handle<'_>| {
thread::sleep(Duration::from_millis(100));
t.send(0).unwrap();
});
}
drop(tx);
// Shutdown should stop processing tasks.
pool.shutdown();
// Each thread should only wait for at most one tasks after shutdown.
for _ in 0..4 {
if rx.try_recv().is_err() {
break;
}
}
assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Disconnected));
}
#[test]
fn test_shutdown_in_pool() {
let pool = Builder::new("test_shutdown_in_pool")
.max_thread_count(4)
.build_callback_pool();
let remote = pool.remote().clone();
let (tx, rx) = mpsc::channel();
remote.spawn(move |_: &mut Handle<'_>| {
pool.shutdown();
tx.send(()).unwrap();
});
rx.recv_timeout(Duration::from_secs(1)).unwrap();
}
#[test]
fn test_shutdown_with_futures() {
let pool = Builder::new("test_shutdown_with_futures")
.max_thread_count(2)
.build_future_pool();
let remote = pool.remote().clone();
let (tx, rx) = mpsc::channel::<()>();
// One future will be notified after 300ms.
let tx2 = tx.clone();
remote.spawn(async move {
Delay::new(Duration::from_millis(300)).await;
drop(tx2);
});
// Two futures running in the pool.
thread::sleep(Duration::from_millis(50));
let tx2 = tx.clone();
remote.spawn(async move {
thread::sleep(Duration::from_millis(100));
drop(tx2);
});
let tx2 = tx.clone();
remote.spawn(async move {
thread::sleep(Duration::from_millis(100));
drop(tx2);
});
// One future in the queue.
thread::sleep(Duration::from_millis(50));
let tx = tx;
remote.spawn(async move {
drop(tx);
});
// Before the pool is shut down, txs are not all dropped.
assert_eq!(rx.try_recv(), Err(mpsc::TryRecvError::Empty));
drop(remote);
drop(pool);
// All txs should be dropped. No future leaks.
assert_eq!(
rx.recv_timeout(Duration::from_millis(500)),
Err(mpsc::RecvTimeoutError::Disconnected)
);
}
#[test]
fn test_scale_up_workers() {
let pool = Builder::new("test_scale_up")
.max_thread_count(4)
.core_thread_count(2)
.build_callback_pool();
// Make sure all workers have run and gone to sleep before spawn new tasks
thread::sleep(Duration::from_secs(1));
let mut sync_txs = vec![];
// Block all runnable (`core_thread_count`) threads
for _ in 0..2 {
let (tx, rx) = mpsc::sync_channel::<()>(0);
pool.spawn(move |_: &mut Handle<'_>| rx.recv().unwrap());
sync_txs.push(tx);
}
// Make sure all block tasks have been dispatched to `core_thread_count` threads,
// as the above block tasks may be pulled to one thread at once, then stealed
// by the other thread, which cause the below task is executed first.
thread::sleep(Duration::from_secs(1));
let (tx, rx) = mpsc::channel();
let tx1 = tx.clone();
pool.spawn(move |_: &mut Handle<'_>| tx1.send(1).unwrap());
// No runnable threads, so it should time out
assert_eq!(
rx.recv_timeout(Duration::from_secs(1)),
Err(mpsc::RecvTimeoutError::Timeout)
);
// Scale up one worker
pool.scale_workers(3);
// Due to the current implementation, the scale up can't be triggered until a new
// task has been spawned, so it should still time out
assert_eq!(
rx.recv_timeout(Duration::from_secs(1)),
Err(mpsc::RecvTimeoutError::Timeout)
);
// Spawn a new task to trigger scale up
pool.spawn(move |_: &mut Handle<'_>| tx.send(2).unwrap());
// Spawn should success, and handle the two tasks by the spawned order
assert_eq!(Ok(1), rx.recv_timeout(Duration::from_secs(1)));
assert_eq!(Ok(2), rx.recv_timeout(Duration::from_secs(1)));
// Block the new scaled thread, to prove only scaled one thread
let (tx, rx) = mpsc::sync_channel::<()>(0);
pool.spawn(move |_: &mut Handle<'_>| rx.recv().unwrap());
sync_txs.push(tx);
let (tx, rx) = mpsc::channel();
pool.spawn(move |_: &mut Handle<'_>| tx.send(1).unwrap());
// The scaled thread has been blocked, no more runnable threads, so it should time out
assert_eq!(
rx.recv_timeout(Duration::from_secs(1)),
Err(mpsc::RecvTimeoutError::Timeout)
);
// Wake up the threads that were previously blocked by the task
for tx in sync_txs {
tx.send(()).unwrap();
}
pool.shutdown();
}
#[test]
fn test_scale_down_workers() {
let pool = Builder::new("test_scale_down")
.max_thread_count(4)
.core_thread_count(3)
.build_callback_pool();
// Make sure all workers have run and gone to sleep before spawn new tasks
thread::sleep(Duration::from_secs(1));
let mut sync_txs = vec![];
// Block all runnable (`core_thread_count`) threads
for _ in 0..3 {
let (tx, rx) = mpsc::sync_channel::<()>(0);
pool.spawn(move |_: &mut Handle<'_>| rx.recv().unwrap());
sync_txs.push(tx);
}
// Make sure all block tasks have been dispatched to `core_thread_count` threads,
// as the above block tasks may be pulled by one thread at once, then stealed
// by other threads, which will cause the below task is executed first.
thread::sleep(Duration::from_secs(1));
let (tx, rx) = mpsc::channel();
pool.spawn(move |_: &mut Handle<'_>| tx.send(1).unwrap());
// No runnable threads, so it should time out
assert_eq!(
rx.recv_timeout(Duration::from_secs(1)),
Err(mpsc::RecvTimeoutError::Timeout)
);
// Scale down two workers
pool.scale_workers(1);
// Due to the current implementation, the scale down can't be triggered until thread
// finished tasks and go to sleep, so wake up them all to finish tasks first.
for tx in sync_txs {
tx.send(()).unwrap();
}
// Make sure all threads have gone to sleep before spawn a lot of new tasks
thread::sleep(Duration::from_secs(1));
let (tx, rx) = mpsc::channel();
// A bunch of tasks should be executed correctly, and as there's only one worker, the
// handled order should be the same as spawn order.
let cases: Vec<_> = (10..1000).collect();
for id in &cases {
let t = tx.clone();
let id = *id;
pool.spawn(move |_: &mut Handle<'_>| t.send(id).unwrap());
}
let mut ans = vec![];
for _ in 10..1000 {
let r = rx.recv_timeout(Duration::from_secs(1)).unwrap();
ans.push(r);
}
assert_eq!(cases, ans);
pool.shutdown();
}
#[test]
fn test_worker_active_seconds_metric() {
static ID: AtomicUsize = AtomicUsize::new(0);
let name = format!(
"test_worker_active_seconds_{}",
ID.fetch_add(1, Ordering::SeqCst)
);
let pool = Builder::new(name.clone())
.max_thread_count(1)
.build_callback_pool();
let (tx, rx) = mpsc::channel();
pool.spawn(move |_: &mut Handle<'_>| {
thread::sleep(Duration::from_millis(10));
tx.send(()).unwrap();
});
rx.recv_timeout(Duration::from_secs(1)).unwrap();
pool.shutdown();
let value = WORKER_ACTIVE_SECONDS
.get_metric_with_label_values(&[name.as_str()])
.unwrap()
.get();
assert!(value > 0.0);
}