| 1 |
use std::time::{Instant, Duration}; |
| 2 |
use flume::*; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn send_recv() { |
| 6 |
let (tx, rx) = unbounded(); |
| 7 |
for i in 0..1000 { tx.send(i).unwrap(); } |
| 8 |
for i in 0..1000 { assert_eq!(rx.try_recv().unwrap(), i); } |
| 9 |
assert!(rx.try_recv().is_err()); |
| 10 |
} |
| 11 |
|
| 12 |
#[test] |
| 13 |
fn iter() { |
| 14 |
let (tx, rx) = unbounded(); |
| 15 |
for i in 0..1000 { tx.send(i).unwrap(); } |
| 16 |
drop(tx); |
| 17 |
assert_eq!(rx.iter().sum::<u32>(), (0..1000).sum()); |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn try_iter() { |
| 22 |
let (tx, rx) = unbounded(); |
| 23 |
for i in 0..1000 { tx.send(i).unwrap(); } |
| 24 |
assert_eq!(rx.try_iter().sum::<u32>(), (0..1000).sum()); |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn iter_threaded() { |
| 29 |
let (tx, rx) = unbounded(); |
| 30 |
for i in 0..1000 { |
| 31 |
let tx = tx.clone(); |
| 32 |
std::thread::spawn(move || tx.send(i).unwrap()); |
| 33 |
} |
| 34 |
drop(tx); |
| 35 |
assert_eq!(rx.iter().sum::<u32>(), (0..1000).sum()); |
| 36 |
} |
| 37 |
|
| 38 |
#[cfg_attr(any(target_os = "macos", windows), ignore)] |
| 39 |
#[test] |
| 40 |
fn send_timeout() { |
| 41 |
let dur = Duration::from_millis(350); |
| 42 |
let max_error = Duration::from_millis(5); |
| 43 |
let dur_min = dur.checked_sub(max_error).unwrap(); |
| 44 |
let dur_max = dur.checked_add(max_error).unwrap(); |
| 45 |
|
| 46 |
let (tx, rx) = bounded(1); |
| 47 |
|
| 48 |
assert!(tx.send_timeout(42, dur).is_ok()); |
| 49 |
|
| 50 |
let then = Instant::now(); |
| 51 |
assert!(tx.send_timeout(43, dur).is_err()); |
| 52 |
let now = Instant::now(); |
| 53 |
|
| 54 |
let this = now.duration_since(then); |
| 55 |
if !(dur_min < this && this < dur_max) { |
| 56 |
panic!("timeout exceeded: {:?}", this); |
| 57 |
} |
| 58 |
|
| 59 |
assert_eq!(rx.drain().count(), 1); |
| 60 |
|
| 61 |
drop(rx); |
| 62 |
|
| 63 |
assert!(tx.send_timeout(42, Duration::from_millis(350)).is_err()); |
| 64 |
} |
| 65 |
|
| 66 |
#[cfg_attr(any(target_os = "macos", windows), ignore)] |
| 67 |
#[test] |
| 68 |
fn recv_timeout() { |
| 69 |
let dur = Duration::from_millis(350); |
| 70 |
let max_error = Duration::from_millis(5); |
| 71 |
let dur_min = dur.checked_sub(max_error).unwrap(); |
| 72 |
let dur_max = dur.checked_add(max_error).unwrap(); |
| 73 |
|
| 74 |
let (tx, rx) = unbounded(); |
| 75 |
let then = Instant::now(); |
| 76 |
assert!(rx.recv_timeout(dur).is_err()); |
| 77 |
let now = Instant::now(); |
| 78 |
|
| 79 |
let this = now.duration_since(then); |
| 80 |
if !(dur_min < this && this < dur_max) { |
| 81 |
panic!("timeout exceeded: {:?}", this); |
| 82 |
} |
| 83 |
|
| 84 |
tx.send(42).unwrap(); |
| 85 |
assert_eq!(rx.recv_timeout(dur), Ok(42)); |
| 86 |
assert!(Instant::now().duration_since(now) < max_error); |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg_attr(any(target_os = "macos", windows), ignore)] |
| 90 |
#[test] |
| 91 |
fn recv_deadline() { |
| 92 |
let dur = Duration::from_millis(350); |
| 93 |
let max_error = Duration::from_millis(5); |
| 94 |
let dur_min = dur.checked_sub(max_error).unwrap(); |
| 95 |
let dur_max = dur.checked_add(max_error).unwrap(); |
| 96 |
|
| 97 |
let (tx, rx) = unbounded(); |
| 98 |
let then = Instant::now(); |
| 99 |
assert!(rx.recv_deadline(then.checked_add(dur).unwrap()).is_err()); |
| 100 |
let now = Instant::now(); |
| 101 |
|
| 102 |
let this = now.duration_since(then); |
| 103 |
if !(dur_min < this && this < dur_max) { |
| 104 |
panic!("timeout exceeded: {:?}", this); |
| 105 |
} |
| 106 |
|
| 107 |
tx.send(42).unwrap(); |
| 108 |
assert_eq!(rx.recv_deadline(now.checked_add(dur).unwrap()), Ok(42)); |
| 109 |
assert!(Instant::now().duration_since(now) < max_error); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn recv_timeout_missed_send() { |
| 114 |
let (tx, rx) = bounded(10); |
| 115 |
|
| 116 |
assert!(rx.recv_timeout(Duration::from_millis(100)).is_err()); |
| 117 |
|
| 118 |
tx.send(42).unwrap(); |
| 119 |
|
| 120 |
assert_eq!(rx.recv(), Ok(42)); |
| 121 |
} |
| 122 |
|
| 123 |
#[test] |
| 124 |
fn disconnect_tx() { |
| 125 |
let (tx, rx) = unbounded::<()>(); |
| 126 |
drop(tx); |
| 127 |
assert!(rx.recv().is_err()); |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn disconnect_rx() { |
| 132 |
let (tx, rx) = unbounded(); |
| 133 |
drop(rx); |
| 134 |
assert!(tx.send(0).is_err()); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn drain() { |
| 139 |
let (tx, rx) = unbounded(); |
| 140 |
|
| 141 |
for i in 0..100 { |
| 142 |
tx.send(i).unwrap(); |
| 143 |
} |
| 144 |
|
| 145 |
assert_eq!(rx.drain().sum::<u32>(), (0..100).sum()); |
| 146 |
|
| 147 |
for i in 0..100 { |
| 148 |
tx.send(i).unwrap(); |
| 149 |
} |
| 150 |
|
| 151 |
for i in 0..100 { |
| 152 |
tx.send(i).unwrap(); |
| 153 |
} |
| 154 |
|
| 155 |
rx.recv().unwrap(); |
| 156 |
|
| 157 |
(1u32..100).chain(0..100).zip(rx).for_each(|(l, r)| assert_eq!(l, r)); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn try_send() { |
| 162 |
let (tx, rx) = bounded(5); |
| 163 |
|
| 164 |
for i in 0..5 { |
| 165 |
tx.try_send(i).unwrap(); |
| 166 |
} |
| 167 |
|
| 168 |
assert!(tx.try_send(42).is_err()); |
| 169 |
|
| 170 |
assert_eq!(rx.recv(), Ok(0)); |
| 171 |
|
| 172 |
assert_eq!(tx.try_send(42), Ok(())); |
| 173 |
|
| 174 |
assert_eq!(rx.recv(), Ok(1)); |
| 175 |
drop(rx); |
| 176 |
|
| 177 |
assert!(tx.try_send(42).is_err()); |
| 178 |
} |
| 179 |
|
| 180 |
#[test] |
| 181 |
fn send_bounded() { |
| 182 |
let (tx, rx) = bounded(5); |
| 183 |
|
| 184 |
for _ in 0..5 { |
| 185 |
tx.send(42).unwrap(); |
| 186 |
} |
| 187 |
|
| 188 |
let _ = rx.recv().unwrap(); |
| 189 |
|
| 190 |
tx.send(42).unwrap(); |
| 191 |
|
| 192 |
assert!(tx.try_send(42).is_err()); |
| 193 |
|
| 194 |
rx.drain(); |
| 195 |
|
| 196 |
let mut ts = Vec::new(); |
| 197 |
for _ in 0..100 { |
| 198 |
let tx = tx.clone(); |
| 199 |
ts.push(std::thread::spawn(move || { |
| 200 |
for i in 0..10000 { |
| 201 |
tx.send(i).unwrap(); |
| 202 |
} |
| 203 |
})); |
| 204 |
} |
| 205 |
|
| 206 |
drop(tx); |
| 207 |
|
| 208 |
assert_eq!(rx.iter().sum::<u64>(), (0..10000).sum::<u64>() * 100); |
| 209 |
|
| 210 |
for t in ts { |
| 211 |
t.join().unwrap(); |
| 212 |
} |
| 213 |
|
| 214 |
assert!(rx.recv().is_err()); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn rendezvous() { |
| 219 |
let (tx, rx) = bounded(0); |
| 220 |
|
| 221 |
for i in 0..5 { |
| 222 |
let tx = tx.clone(); |
| 223 |
let t = std::thread::spawn(move || { |
| 224 |
assert!(tx.try_send(()).is_err()); |
| 225 |
|
| 226 |
let then = Instant::now(); |
| 227 |
tx.send(()).unwrap(); |
| 228 |
let now = Instant::now(); |
| 229 |
|
| 230 |
assert!(now.duration_since(then) > Duration::from_millis(100), "iter = {}", i); |
| 231 |
}); |
| 232 |
|
| 233 |
std::thread::sleep(Duration::from_millis(1000)); |
| 234 |
rx.recv().unwrap(); |
| 235 |
|
| 236 |
t.join().unwrap(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
#[test] |
| 241 |
fn hydra() { |
| 242 |
let thread_num = 32; |
| 243 |
let msg_num = 1000; |
| 244 |
|
| 245 |
let (main_tx, main_rx) = unbounded::<()>(); |
| 246 |
|
| 247 |
let mut txs = Vec::new(); |
| 248 |
for _ in 0..thread_num { |
| 249 |
let main_tx = main_tx.clone(); |
| 250 |
let (tx, rx) = unbounded(); |
| 251 |
txs.push(tx); |
| 252 |
|
| 253 |
std::thread::spawn(move || { |
| 254 |
for msg in rx.iter() { |
| 255 |
main_tx.send(msg).unwrap(); |
| 256 |
} |
| 257 |
}); |
| 258 |
} |
| 259 |
|
| 260 |
drop(main_tx); |
| 261 |
|
| 262 |
for _ in 0..10 { |
| 263 |
for tx in &txs { |
| 264 |
for _ in 0..msg_num { |
| 265 |
tx.send(Default::default()).unwrap(); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
for _ in 0..thread_num { |
| 270 |
for _ in 0..msg_num { |
| 271 |
main_rx.recv().unwrap(); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
drop(txs); |
| 277 |
assert!(main_rx.recv().is_err()); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn robin() { |
| 282 |
let thread_num = 32; |
| 283 |
let msg_num = 10; |
| 284 |
|
| 285 |
let (mut main_tx, main_rx) = bounded::<()>(1); |
| 286 |
|
| 287 |
for _ in 0..thread_num { |
| 288 |
let (mut tx, rx) = bounded(100); |
| 289 |
std::mem::swap(&mut tx, &mut main_tx); |
| 290 |
|
| 291 |
std::thread::spawn(move || { |
| 292 |
for msg in rx.iter() { |
| 293 |
tx.send(msg).unwrap(); |
| 294 |
} |
| 295 |
}); |
| 296 |
} |
| 297 |
|
| 298 |
for _ in 0..10 { |
| 299 |
let main_tx = main_tx.clone(); |
| 300 |
std::thread::spawn(move || { |
| 301 |
for _ in 0..msg_num { |
| 302 |
main_tx.send(Default::default()).unwrap(); |
| 303 |
} |
| 304 |
}); |
| 305 |
|
| 306 |
for _ in 0..msg_num { |
| 307 |
main_rx.recv().unwrap(); |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#[cfg(feature = "select")] |
| 313 |
#[test] |
| 314 |
fn select_general() { |
| 315 |
#[derive(Debug, PartialEq)] |
| 316 |
struct Foo(usize); |
| 317 |
|
| 318 |
let (tx0, rx0) = bounded(1); |
| 319 |
let (tx1, rx1) = unbounded(); |
| 320 |
|
| 321 |
for (i, t) in vec![tx0.clone(), tx1].into_iter().enumerate() { |
| 322 |
std::thread::spawn(move || { |
| 323 |
std::thread::sleep(std::time::Duration::from_millis(250)); |
| 324 |
let _ = t.send(Foo(i)); |
| 325 |
}); |
| 326 |
} |
| 327 |
|
| 328 |
let x = Selector::new() |
| 329 |
.recv(&rx0, |x| x) |
| 330 |
.recv(&rx1, |x| x) |
| 331 |
.wait() |
| 332 |
.unwrap(); |
| 333 |
|
| 334 |
if x == Foo(0) { |
| 335 |
assert!(rx1.recv().unwrap() == Foo(1)); |
| 336 |
} else { |
| 337 |
assert!(rx0.recv().unwrap() == Foo(0)); |
| 338 |
} |
| 339 |
|
| 340 |
tx0.send(Foo(42)).unwrap(); |
| 341 |
|
| 342 |
let t = std::thread::spawn(move || { |
| 343 |
std::thread::sleep(std::time::Duration::from_millis(100)); |
| 344 |
assert_eq!(rx0.recv().unwrap(), Foo(42)); |
| 345 |
assert_eq!(rx0.recv().unwrap(), Foo(43)); |
| 346 |
|
| 347 |
}); |
| 348 |
|
| 349 |
Selector::new() |
| 350 |
.send(&tx0, Foo(43), |x| x) |
| 351 |
.wait() |
| 352 |
.unwrap(); |
| 353 |
|
| 354 |
t.join().unwrap(); |
| 355 |
} |
| 356 |
|
| 357 |
struct MessageWithoutDebug(u32); |
| 358 |
|
| 359 |
#[test] |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
fn std_error_without_debug() { |
| 364 |
let (tx, rx) = unbounded::<MessageWithoutDebug>(); |
| 365 |
|
| 366 |
match tx.send(MessageWithoutDebug(1)) { |
| 367 |
Ok(_) => {} |
| 368 |
Err(e) => { |
| 369 |
let _std_err: &dyn std::error::Error = &e; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
match rx.recv() { |
| 374 |
Ok(_) => {} |
| 375 |
Err(e) => { |
| 376 |
let _std_err: &dyn std::error::Error = &e; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
match tx.try_send(MessageWithoutDebug(2)) { |
| 381 |
Ok(_) => {} |
| 382 |
Err(e) => { |
| 383 |
let _std_err: &dyn std::error::Error = &e; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
match rx.try_recv() { |
| 388 |
Ok(_) => {} |
| 389 |
Err(e) => { |
| 390 |
let _std_err: &dyn std::error::Error = &e; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
match tx.send_timeout(MessageWithoutDebug(3), Duration::from_secs(1000000)) { |
| 395 |
Ok(_) => {} |
| 396 |
Err(e) => { |
| 397 |
let _std_err: &dyn std::error::Error = &e; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
match rx.recv_timeout(Duration::from_secs(10000000)) { |
| 402 |
Ok(_) => {} |
| 403 |
Err(e) => { |
| 404 |
let _std_err: &dyn std::error::Error = &e; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
#[test] |
| 410 |
fn weak_close() { |
| 411 |
let (tx, rx) = unbounded::<()>(); |
| 412 |
let weak = tx.downgrade(); |
| 413 |
drop(tx); |
| 414 |
assert!(weak.upgrade().is_none()); |
| 415 |
assert!(rx.is_disconnected()); |
| 416 |
assert!(rx.try_recv().is_err()); |
| 417 |
} |
| 418 |
|
| 419 |
#[test] |
| 420 |
fn weak_upgrade() { |
| 421 |
let (tx, rx) = unbounded(); |
| 422 |
let weak = tx.downgrade(); |
| 423 |
let tx2 = weak.upgrade().unwrap(); |
| 424 |
drop(tx); |
| 425 |
assert!(!rx.is_disconnected()); |
| 426 |
tx2.send(()).unwrap(); |
| 427 |
assert!(rx.try_recv().is_ok()); |
| 428 |
} |