forked from frostyplanet/crossfire-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async_blocking.rs
More file actions
333 lines (318 loc) · 10.8 KB
/
test_async_blocking.rs
File metadata and controls
333 lines (318 loc) · 10.8 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
324
325
326
327
328
329
330
331
332
333
use super::common::*;
use crate::*;
use log::*;
use rstest::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
#[rstest]
#[case(spsc::bounded_tx_async_rx_blocking::<usize>(100))]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(100))]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(100))]
#[tokio::test]
async fn test_basic_1_tx_async_1_rx_blocking<T: AsyncTxTrait<usize>, R: BlockingRxTrait<usize>>(
setup_log: (), #[case] channel: (T, R),
) {
let _ = setup_log; // Disable unused var warning
let (tx, rx) = channel;
let rx_res = rx.try_recv();
assert!(rx_res.is_err());
assert!(rx_res.unwrap_err().is_empty());
let batch_1: usize = 100;
let batch_2: usize = 200;
let rt = get_runtime();
rt.spawn(async move {
for i in 0..batch_1 {
let tx_res = tx.send(i).await;
assert!(tx_res.is_ok());
}
for i in batch_1..(batch_1 + batch_2) {
assert!(tx.send(10 + i).await.is_ok());
tokio::time::sleep(Duration::from_millis(2)).await;
}
});
for _ in 0..(batch_1 + batch_2) {
match rx.recv() {
Ok(i) => {
debug!("recv {}", i);
}
Err(e) => {
panic!("error {}", e);
}
}
}
let res = rx.recv();
assert!(res.is_err());
rt.shutdown_background(); // Prevent panic on runtime drop
}
#[rstest]
#[case(spsc::bounded_tx_async_rx_blocking::<usize>(100))]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(100))]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(100))]
#[tokio::test]
async fn test_timeout_1_tx_async_1_rx_blocking<
T: AsyncTxTrait<usize>,
R: BlockingRxTrait<usize>,
>(
setup_log: (), #[case] channel: (T, R),
) {
let _ = setup_log; // Disable unused var warning
let (tx, rx) = channel;
let rx_res = rx.try_recv();
assert!(rx_res.is_err());
assert!(rx_res.unwrap_err().is_empty());
let batch_1: usize = 100;
let batch_2: usize = 200;
let rt = get_runtime();
rt.spawn(async move {
for i in 0..batch_1 {
let tx_res = tx.send(i).await;
assert!(tx_res.is_ok());
}
for i in batch_1..(batch_1 + batch_2) {
assert!(tx.send(10 + i).await.is_ok());
tokio::time::sleep(Duration::from_millis(2)).await;
}
tokio::time::sleep(Duration::from_millis(200)).await;
assert!(tx.send(123).await.is_ok());
});
for _ in 0..(batch_1 + batch_2) {
match rx.recv() {
Ok(i) => {
debug!("recv {}", i);
}
Err(e) => {
panic!("error {}", e);
}
}
}
assert!(rx.recv_timeout(Duration::from_millis(100)).is_err());
assert!(rx.recv_timeout(Duration::from_millis(200)).is_ok());
let res = rx.recv();
assert!(res.is_err());
rt.shutdown_background(); // Prevent panic on runtime drop
}
#[rstest]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(10), 8)]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(10), 100)]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(10), 1000)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 8)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 100)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 1000)]
fn test_basic_multi_tx_async_1_rx_blocking<R: BlockingRxTrait<usize>>(
setup_log: (), #[case] channel: (MAsyncTx<usize>, R), #[case] tx_count: usize,
) {
let _ = setup_log; // Disable unused var warning
let (tx, rx) = channel;
let rx_res = rx.try_recv();
assert!(rx_res.is_err());
assert!(rx_res.unwrap_err().is_empty());
let batch_1: usize = 100;
let batch_2: usize = 200;
let rt = get_runtime();
let (noti_tx, mut noti_rx) = tokio::sync::mpsc::channel::<usize>(tx_count);
for tx_i in 0..tx_count {
let _tx = tx.clone();
let _noti_tx = noti_tx.clone();
rt.spawn(async move {
for i in 0..batch_1 {
let tx_res = _tx.send(i).await;
assert!(tx_res.is_ok());
}
for i in batch_1..(batch_1 + batch_2) {
assert!(_tx.send(10 + i).await.is_ok());
tokio::time::sleep(Duration::from_millis(2)).await;
}
_noti_tx.send(tx_i).await.expect("noti send ok");
});
}
drop(tx);
for _ in 0..((batch_1 + batch_2) * tx_count) {
match rx.recv() {
Ok(i) => {
debug!("recv {}", i);
}
Err(e) => {
panic!("error {}", e);
}
}
}
let res = rx.recv();
assert!(res.is_err());
// Wait for tokio spawn exit
rt.block_on(async move {
for _ in 0..tx_count {
let _ = noti_rx.recv().await;
}
});
}
#[rstest]
#[case(spsc::bounded_tx_async_rx_blocking::<usize>(1))]
#[case(spsc::bounded_tx_async_rx_blocking::<usize>(10))]
#[case(spsc::bounded_tx_async_rx_blocking::<usize>(100))]
#[case(spsc::bounded_tx_async_rx_blocking::<usize>(1000))]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(1))]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(10))]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(100))]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(1000))]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(1))]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10))]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(100))]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(1000))]
fn test_pressure_1_tx_async_1_rx_blocking<T: AsyncTxTrait<usize>, R: BlockingRxTrait<usize>>(
setup_log: (), #[case] channel: (T, R),
) {
let _ = setup_log; // Disable unused var warning
let (tx, rx) = channel;
let counter = Arc::new(AtomicUsize::new(0));
let round: usize = 1000000;
let _round = round;
let _counter = counter.clone();
let th = thread::spawn(move || {
'A: loop {
match rx.recv() {
Ok(_) => {
_counter.as_ref().fetch_add(1, Ordering::SeqCst);
//debug!("{} {}\r", _rx_i, i);
}
Err(_) => break 'A,
}
}
debug!("rx exit");
});
let rt = get_runtime();
rt.block_on(async move {
for i in 0..round {
match tx.send(i).await {
Err(e) => panic!("{}", e),
_ => {}
}
}
debug!("tx exit");
});
let _ = th.join();
assert_eq!(counter.as_ref().load(Ordering::Acquire), round);
}
#[rstest]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(10), 8)]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(10), 10)]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(10), 100)]
#[case(mpsc::bounded_tx_async_rx_blocking::<usize>(100), 200)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 8)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 100)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 10)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(100), 200)]
fn test_pressure_multi_tx_async_1_rx_blocking<R: BlockingRxTrait<usize>>(
setup_log: (), #[case] channel: (MAsyncTx<usize>, R), #[case] tx_count: usize,
) {
let _ = setup_log; // Disable unused var warning
let (tx, rx) = channel;
let counter = Arc::new(AtomicUsize::new(0));
let round: usize = 100000;
let _round = round;
let _counter = counter.clone();
let th = thread::spawn(move || {
'A: loop {
match rx.recv() {
Ok(_) => {
_counter.as_ref().fetch_add(1, Ordering::SeqCst);
//debug!("{} {}\r", _rx_i, i);
}
Err(_) => break 'A,
}
}
debug!("rx exit");
});
let rt = get_runtime();
rt.block_on(async move {
let (noti_tx, mut noti_rx) = tokio::sync::mpsc::channel::<usize>(tx_count);
for _tx_i in 0..tx_count {
let _tx = tx.clone();
let mut _noti_tx = noti_tx.clone();
tokio::spawn(async move {
for i in 0..round {
match _tx.send(i).await {
Err(e) => panic!("{}", e),
_ => {}
}
}
let _ = _noti_tx.send(_tx_i).await;
debug!("tx {} exit", _tx_i);
});
}
drop(tx);
drop(noti_tx);
for _ in 0..(tx_count) {
match noti_rx.recv().await {
Some(_) => {}
None => break,
}
}
});
let _ = th.join();
assert_eq!(counter.as_ref().load(Ordering::Acquire), round * (tx_count));
}
#[rstest]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 8, 8)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 100, 100)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(10), 10, 1000)]
#[case(mpmc::bounded_tx_async_rx_blocking::<usize>(100), 500, 500)]
fn test_pressure_multi_tx_async_multi_rx_blocking(
setup_log: (), #[case] channel: (MAsyncTx<usize>, MRx<usize>), #[case] tx_count: usize,
#[case] rx_count: usize,
) {
let _ = setup_log; // Disable unused var warning
let (tx, rx) = channel;
let counter = Arc::new(AtomicUsize::new(0));
let round: usize = 100000;
let mut rx_th_s = Vec::new();
for _rx_i in 0..rx_count {
let _rx = rx.clone();
let _round = round;
let _counter = counter.clone();
rx_th_s.push(thread::spawn(move || {
'A: loop {
match _rx.recv() {
Ok(_) => {
_counter.as_ref().fetch_add(1, Ordering::SeqCst);
//debug!("{} {}\r", _rx_i, i);
}
Err(_) => break 'A,
}
}
debug!("rx {} exit", _rx_i);
}));
}
drop(rx);
let rt = get_runtime();
rt.block_on(async move {
let (noti_tx, mut noti_rx) = tokio::sync::mpsc::channel::<usize>(tx_count);
for _tx_i in 0..tx_count {
let _tx = tx.clone();
let mut _noti_tx = noti_tx.clone();
tokio::spawn(async move {
for i in 0..round {
match _tx.send(i).await {
Err(e) => panic!("{}", e),
_ => {}
}
}
let _ = _noti_tx.send(_tx_i).await;
debug!("tx {} exit", _tx_i);
});
}
drop(tx);
drop(noti_tx);
for _ in 0..(tx_count) {
match noti_rx.recv().await {
Some(_) => {}
None => break,
}
}
});
for th in rx_th_s {
let _ = th.join();
}
assert_eq!(counter.as_ref().load(Ordering::Acquire), round * (tx_count));
}