| 1 | // //! Tests for channel readiness using the `Select` struct. |
| 2 | |
| 3 | // extern crate crossbeam_channel; |
| 4 | // extern crate crossbeam_utils; |
| 5 | |
| 6 | // use std::any::Any; |
| 7 | // use std::cell::Cell; |
| 8 | // use std::thread; |
| 9 | // use std::time::{Duration, Instant}; |
| 10 | |
| 11 | // use crossbeam_channel::{after, bounded, tick, unbounded}; |
| 12 | // use crossbeam_channel::{Receiver, Select, TryRecvError, TrySendError}; |
| 13 | // use crossbeam_utils::thread::scope; |
| 14 | |
| 15 | // fn ms(ms: u64) -> Duration { |
| 16 | // Duration::from_millis(ms) |
| 17 | // } |
| 18 | |
| 19 | // #[test] |
| 20 | // fn smoke1() { |
| 21 | // let (s1, r1) = unbounded::<usize>(); |
| 22 | // let (s2, r2) = unbounded::<usize>(); |
| 23 | |
| 24 | // s1.send(1).unwrap(); |
| 25 | |
| 26 | // let mut sel = Select::new(); |
| 27 | // sel.recv(&r1); |
| 28 | // sel.recv(&r2); |
| 29 | // assert_eq!(sel.ready(), 0); |
| 30 | // assert_eq!(r1.try_recv(), Ok(1)); |
| 31 | |
| 32 | // s2.send(2).unwrap(); |
| 33 | |
| 34 | // let mut sel = Select::new(); |
| 35 | // sel.recv(&r1); |
| 36 | // sel.recv(&r2); |
| 37 | // assert_eq!(sel.ready(), 1); |
| 38 | // assert_eq!(r2.try_recv(), Ok(2)); |
| 39 | // } |
| 40 | |
| 41 | // #[test] |
| 42 | // fn smoke2() { |
| 43 | // let (_s1, r1) = unbounded::<i32>(); |
| 44 | // let (_s2, r2) = unbounded::<i32>(); |
| 45 | // let (_s3, r3) = unbounded::<i32>(); |
| 46 | // let (_s4, r4) = unbounded::<i32>(); |
| 47 | // let (s5, r5) = unbounded::<i32>(); |
| 48 | |
| 49 | // s5.send(5).unwrap(); |
| 50 | |
| 51 | // let mut sel = Select::new(); |
| 52 | // sel.recv(&r1); |
| 53 | // sel.recv(&r2); |
| 54 | // sel.recv(&r3); |
| 55 | // sel.recv(&r4); |
| 56 | // sel.recv(&r5); |
| 57 | // assert_eq!(sel.ready(), 4); |
| 58 | // assert_eq!(r5.try_recv(), Ok(5)); |
| 59 | // } |
| 60 | |
| 61 | // #[test] |
| 62 | // fn disconnected() { |
| 63 | // let (s1, r1) = unbounded::<i32>(); |
| 64 | // let (s2, r2) = unbounded::<i32>(); |
| 65 | |
| 66 | // scope(|scope| { |
| 67 | // scope.spawn(|_| { |
| 68 | // drop(s1); |
| 69 | // thread::sleep(ms(500)); |
| 70 | // s2.send(5).unwrap(); |
| 71 | // }); |
| 72 | |
| 73 | // let mut sel = Select::new(); |
| 74 | // sel.recv(&r1); |
| 75 | // sel.recv(&r2); |
| 76 | // match sel.ready_timeout(ms(1000)) { |
| 77 | // Ok(0) => assert_eq!(r1.try_recv(), Err(TryRecvError::Disconnected)), |
| 78 | // _ => panic!(), |
| 79 | // } |
| 80 | |
| 81 | // r2.recv().unwrap(); |
| 82 | // }) |
| 83 | // .unwrap(); |
| 84 | |
| 85 | // let mut sel = Select::new(); |
| 86 | // sel.recv(&r1); |
| 87 | // sel.recv(&r2); |
| 88 | // match sel.ready_timeout(ms(1000)) { |
| 89 | // Ok(0) => assert_eq!(r1.try_recv(), Err(TryRecvError::Disconnected)), |
| 90 | // _ => panic!(), |
| 91 | // } |
| 92 | |
| 93 | // scope(|scope| { |
| 94 | // scope.spawn(|_| { |
| 95 | // thread::sleep(ms(500)); |
| 96 | // drop(s2); |
| 97 | // }); |
| 98 | |
| 99 | // let mut sel = Select::new(); |
| 100 | // sel.recv(&r2); |
| 101 | // match sel.ready_timeout(ms(1000)) { |
| 102 | // Ok(0) => assert_eq!(r2.try_recv(), Err(TryRecvError::Disconnected)), |
| 103 | // _ => panic!(), |
| 104 | // } |
| 105 | // }) |
| 106 | // .unwrap(); |
| 107 | // } |
| 108 | |
| 109 | // #[test] |
| 110 | // fn default() { |
| 111 | // let (s1, r1) = unbounded::<i32>(); |
| 112 | // let (s2, r2) = unbounded::<i32>(); |
| 113 | |
| 114 | // let mut sel = Select::new(); |
| 115 | // sel.recv(&r1); |
| 116 | // sel.recv(&r2); |
| 117 | // assert!(sel.try_ready().is_err()); |
| 118 | |
| 119 | // drop(s1); |
| 120 | |
| 121 | // let mut sel = Select::new(); |
| 122 | // sel.recv(&r1); |
| 123 | // sel.recv(&r2); |
| 124 | // match sel.try_ready() { |
| 125 | // Ok(0) => assert!(r1.try_recv().is_err()), |
| 126 | // _ => panic!(), |
| 127 | // } |
| 128 | |
| 129 | // s2.send(2).unwrap(); |
| 130 | |
| 131 | // let mut sel = Select::new(); |
| 132 | // sel.recv(&r2); |
| 133 | // match sel.try_ready() { |
| 134 | // Ok(0) => assert_eq!(r2.try_recv(), Ok(2)), |
| 135 | // _ => panic!(), |
| 136 | // } |
| 137 | |
| 138 | // let mut sel = Select::new(); |
| 139 | // sel.recv(&r2); |
| 140 | // assert!(sel.try_ready().is_err()); |
| 141 | |
| 142 | // let mut sel = Select::new(); |
| 143 | // assert!(sel.try_ready().is_err()); |
| 144 | // } |
| 145 | |
| 146 | // #[test] |
| 147 | // fn timeout() { |
| 148 | // let (_s1, r1) = unbounded::<i32>(); |
| 149 | // let (s2, r2) = unbounded::<i32>(); |
| 150 | |
| 151 | // scope(|scope| { |
| 152 | // scope.spawn(|_| { |
| 153 | // thread::sleep(ms(1500)); |
| 154 | // s2.send(2).unwrap(); |
| 155 | // }); |
| 156 | |
| 157 | // let mut sel = Select::new(); |
| 158 | // sel.recv(&r1); |
| 159 | // sel.recv(&r2); |
| 160 | // assert!(sel.ready_timeout(ms(1000)).is_err()); |
| 161 | |
| 162 | // let mut sel = Select::new(); |
| 163 | // sel.recv(&r1); |
| 164 | // sel.recv(&r2); |
| 165 | // match sel.ready_timeout(ms(1000)) { |
| 166 | // Ok(1) => assert_eq!(r2.try_recv(), Ok(2)), |
| 167 | // _ => panic!(), |
| 168 | // } |
| 169 | // }) |
| 170 | // .unwrap(); |
| 171 | |
| 172 | // scope(|scope| { |
| 173 | // let (s, r) = unbounded::<i32>(); |
| 174 | |
| 175 | // scope.spawn(move |_| { |
| 176 | // thread::sleep(ms(500)); |
| 177 | // drop(s); |
| 178 | // }); |
| 179 | |
| 180 | // let mut sel = Select::new(); |
| 181 | // assert!(sel.ready_timeout(ms(1000)).is_err()); |
| 182 | |
| 183 | // let mut sel = Select::new(); |
| 184 | // sel.recv(&r); |
| 185 | // match sel.try_ready() { |
| 186 | // Ok(0) => assert_eq!(r.try_recv(), Err(TryRecvError::Disconnected)), |
| 187 | // _ => panic!(), |
| 188 | // } |
| 189 | // }) |
| 190 | // .unwrap(); |
| 191 | // } |
| 192 | |
| 193 | // #[test] |
| 194 | // fn default_when_disconnected() { |
| 195 | // let (_, r) = unbounded::<i32>(); |
| 196 | |
| 197 | // let mut sel = Select::new(); |
| 198 | // sel.recv(&r); |
| 199 | // match sel.try_ready() { |
| 200 | // Ok(0) => assert_eq!(r.try_recv(), Err(TryRecvError::Disconnected)), |
| 201 | // _ => panic!(), |
| 202 | // } |
| 203 | |
| 204 | // let (_, r) = unbounded::<i32>(); |
| 205 | |
| 206 | // let mut sel = Select::new(); |
| 207 | // sel.recv(&r); |
| 208 | // match sel.ready_timeout(ms(1000)) { |
| 209 | // Ok(0) => assert_eq!(r.try_recv(), Err(TryRecvError::Disconnected)), |
| 210 | // _ => panic!(), |
| 211 | // } |
| 212 | |
| 213 | // let (s, _) = bounded::<i32>(0); |
| 214 | |
| 215 | // let mut sel = Select::new(); |
| 216 | // sel.send(&s); |
| 217 | // match sel.try_ready() { |
| 218 | // Ok(0) => assert_eq!(s.try_send(0), Err(TrySendError::Disconnected(0))), |
| 219 | // _ => panic!(), |
| 220 | // } |
| 221 | |
| 222 | // let (s, _) = bounded::<i32>(0); |
| 223 | |
| 224 | // let mut sel = Select::new(); |
| 225 | // sel.send(&s); |
| 226 | // match sel.ready_timeout(ms(1000)) { |
| 227 | // Ok(0) => assert_eq!(s.try_send(0), Err(TrySendError::Disconnected(0))), |
| 228 | // _ => panic!(), |
| 229 | // } |
| 230 | // } |
| 231 | |
| 232 | // #[test] |
| 233 | // fn default_only() { |
| 234 | // let start = Instant::now(); |
| 235 | |
| 236 | // let mut sel = Select::new(); |
| 237 | // assert!(sel.try_ready().is_err()); |
| 238 | // let now = Instant::now(); |
| 239 | // assert!(now - start <= ms(50)); |
| 240 | |
| 241 | // let start = Instant::now(); |
| 242 | // let mut sel = Select::new(); |
| 243 | // assert!(sel.ready_timeout(ms(500)).is_err()); |
| 244 | // let now = Instant::now(); |
| 245 | // assert!(now - start >= ms(450)); |
| 246 | // assert!(now - start <= ms(550)); |
| 247 | // } |
| 248 | |
| 249 | // #[test] |
| 250 | // fn unblocks() { |
| 251 | // let (s1, r1) = bounded::<i32>(0); |
| 252 | // let (s2, r2) = bounded::<i32>(0); |
| 253 | |
| 254 | // scope(|scope| { |
| 255 | // scope.spawn(|_| { |
| 256 | // thread::sleep(ms(500)); |
| 257 | // s2.send(2).unwrap(); |
| 258 | // }); |
| 259 | |
| 260 | // let mut sel = Select::new(); |
| 261 | // sel.recv(&r1); |
| 262 | // sel.recv(&r2); |
| 263 | // match sel.ready_timeout(ms(1000)) { |
| 264 | // Ok(1) => assert_eq!(r2.try_recv(), Ok(2)), |
| 265 | // _ => panic!(), |
| 266 | // } |
| 267 | // }) |
| 268 | // .unwrap(); |
| 269 | |
| 270 | // scope(|scope| { |
| 271 | // scope.spawn(|_| { |
| 272 | // thread::sleep(ms(500)); |
| 273 | // assert_eq!(r1.recv().unwrap(), 1); |
| 274 | // }); |
| 275 | |
| 276 | // let mut sel = Select::new(); |
| 277 | // let oper1 = sel.send(&s1); |
| 278 | // let oper2 = sel.send(&s2); |
| 279 | // let oper = sel.select_timeout(ms(1000)); |
| 280 | // match oper { |
| 281 | // Err(_) => panic!(), |
| 282 | // Ok(oper) => match oper.index() { |
| 283 | // i if i == oper1 => oper.send(&s1, 1).unwrap(), |
| 284 | // i if i == oper2 => panic!(), |
| 285 | // _ => unreachable!(), |
| 286 | // }, |
| 287 | // } |
| 288 | // }) |
| 289 | // .unwrap(); |
| 290 | // } |
| 291 | |
| 292 | // #[test] |
| 293 | // fn both_ready() { |
| 294 | // let (s1, r1) = bounded(0); |
| 295 | // let (s2, r2) = bounded(0); |
| 296 | |
| 297 | // scope(|scope| { |
| 298 | // scope.spawn(|_| { |
| 299 | // thread::sleep(ms(500)); |
| 300 | // s1.send(1).unwrap(); |
| 301 | // assert_eq!(r2.recv().unwrap(), 2); |
| 302 | // }); |
| 303 | |
| 304 | // for _ in 0..2 { |
| 305 | // let mut sel = Select::new(); |
| 306 | // sel.recv(&r1); |
| 307 | // sel.send(&s2); |
| 308 | // match sel.ready() { |
| 309 | // 0 => assert_eq!(r1.try_recv(), Ok(1)), |
| 310 | // 1 => s2.try_send(2).unwrap(), |
| 311 | // _ => panic!(), |
| 312 | // } |
| 313 | // } |
| 314 | // }) |
| 315 | // .unwrap(); |
| 316 | // } |
| 317 | |
| 318 | // #[test] |
| 319 | // fn cloning1() { |
| 320 | // scope(|scope| { |
| 321 | // let (s1, r1) = unbounded::<i32>(); |
| 322 | // let (_s2, r2) = unbounded::<i32>(); |
| 323 | // let (s3, r3) = unbounded::<()>(); |
| 324 | |
| 325 | // scope.spawn(move |_| { |
| 326 | // r3.recv().unwrap(); |
| 327 | // drop(s1.clone()); |
| 328 | // assert!(r3.try_recv().is_err()); |
| 329 | // s1.send(1).unwrap(); |
| 330 | // r3.recv().unwrap(); |
| 331 | // }); |
| 332 | |
| 333 | // s3.send(()).unwrap(); |
| 334 | |
| 335 | // let mut sel = Select::new(); |
| 336 | // sel.recv(&r1); |
| 337 | // sel.recv(&r2); |
| 338 | // match sel.ready() { |
| 339 | // 0 => drop(r1.try_recv()), |
| 340 | // 1 => drop(r2.try_recv()), |
| 341 | // _ => panic!(), |
| 342 | // } |
| 343 | |
| 344 | // s3.send(()).unwrap(); |
| 345 | // }) |
| 346 | // .unwrap(); |
| 347 | // } |
| 348 | |
| 349 | // #[test] |
| 350 | // fn cloning2() { |
| 351 | // let (s1, r1) = unbounded::<()>(); |
| 352 | // let (s2, r2) = unbounded::<()>(); |
| 353 | // let (_s3, _r3) = unbounded::<()>(); |
| 354 | |
| 355 | // scope(|scope| { |
| 356 | // scope.spawn(move |_| { |
| 357 | // let mut sel = Select::new(); |
| 358 | // sel.recv(&r1); |
| 359 | // sel.recv(&r2); |
| 360 | // match sel.ready() { |
| 361 | // 0 => panic!(), |
| 362 | // 1 => drop(r2.try_recv()), |
| 363 | // _ => panic!(), |
| 364 | // } |
| 365 | // }); |
| 366 | |
| 367 | // thread::sleep(ms(500)); |
| 368 | // drop(s1.clone()); |
| 369 | // s2.send(()).unwrap(); |
| 370 | // }) |
| 371 | // .unwrap(); |
| 372 | // } |
| 373 | |
| 374 | // #[test] |
| 375 | // fn preflight1() { |
| 376 | // let (s, r) = unbounded(); |
| 377 | // s.send(()).unwrap(); |
| 378 | |
| 379 | // let mut sel = Select::new(); |
| 380 | // sel.recv(&r); |
| 381 | // match sel.ready() { |
| 382 | // 0 => drop(r.try_recv()), |
| 383 | // _ => panic!(), |
| 384 | // } |
| 385 | // } |
| 386 | |
| 387 | // #[test] |
| 388 | // fn preflight2() { |
| 389 | // let (s, r) = unbounded(); |
| 390 | // drop(s.clone()); |
| 391 | // s.send(()).unwrap(); |
| 392 | // drop(s); |
| 393 | |
| 394 | // let mut sel = Select::new(); |
| 395 | // sel.recv(&r); |
| 396 | // match sel.ready() { |
| 397 | // 0 => assert_eq!(r.try_recv(), Ok(())), |
| 398 | // _ => panic!(), |
| 399 | // } |
| 400 | |
| 401 | // assert_eq!(r.try_recv(), Err(TryRecvError::Disconnected)); |
| 402 | // } |
| 403 | |
| 404 | // #[test] |
| 405 | // fn preflight3() { |
| 406 | // let (s, r) = unbounded(); |
| 407 | // drop(s.clone()); |
| 408 | // s.send(()).unwrap(); |
| 409 | // drop(s); |
| 410 | // r.recv().unwrap(); |
| 411 | |
| 412 | // let mut sel = Select::new(); |
| 413 | // sel.recv(&r); |
| 414 | // match sel.ready() { |
| 415 | // 0 => assert_eq!(r.try_recv(), Err(TryRecvError::Disconnected)), |
| 416 | // _ => panic!(), |
| 417 | // } |
| 418 | // } |
| 419 | |
| 420 | // #[test] |
| 421 | // fn duplicate_operations() { |
| 422 | // let (s, r) = unbounded::<i32>(); |
| 423 | // let hit = vec![Cell::new(false); 4]; |
| 424 | |
| 425 | // while hit.iter().map(|h| h.get()).any(|hit| !hit) { |
| 426 | // let mut sel = Select::new(); |
| 427 | // sel.recv(&r); |
| 428 | // sel.recv(&r); |
| 429 | // sel.send(&s); |
| 430 | // sel.send(&s); |
| 431 | // match sel.ready() { |
| 432 | // 0 => { |
| 433 | // assert!(r.try_recv().is_ok()); |
| 434 | // hit[0].set(true); |
| 435 | // } |
| 436 | // 1 => { |
| 437 | // assert!(r.try_recv().is_ok()); |
| 438 | // hit[1].set(true); |
| 439 | // } |
| 440 | // 2 => { |
| 441 | // assert!(s.try_send(0).is_ok()); |
| 442 | // hit[2].set(true); |
| 443 | // } |
| 444 | // 3 => { |
| 445 | // assert!(s.try_send(0).is_ok()); |
| 446 | // hit[3].set(true); |
| 447 | // } |
| 448 | // _ => panic!(), |
| 449 | // } |
| 450 | // } |
| 451 | // } |
| 452 | |
| 453 | // #[test] |
| 454 | // fn nesting() { |
| 455 | // let (s, r) = unbounded::<i32>(); |
| 456 | |
| 457 | // let mut sel = Select::new(); |
| 458 | // sel.send(&s); |
| 459 | // match sel.ready() { |
| 460 | // 0 => { |
| 461 | // assert!(s.try_send(0).is_ok()); |
| 462 | |
| 463 | // let mut sel = Select::new(); |
| 464 | // sel.recv(&r); |
| 465 | // match sel.ready() { |
| 466 | // 0 => { |
| 467 | // assert_eq!(r.try_recv(), Ok(0)); |
| 468 | |
| 469 | // let mut sel = Select::new(); |
| 470 | // sel.send(&s); |
| 471 | // match sel.ready() { |
| 472 | // 0 => { |
| 473 | // assert!(s.try_send(1).is_ok()); |
| 474 | |
| 475 | // let mut sel = Select::new(); |
| 476 | // sel.recv(&r); |
| 477 | // match sel.ready() { |
| 478 | // 0 => { |
| 479 | // assert_eq!(r.try_recv(), Ok(1)); |
| 480 | // } |
| 481 | // _ => panic!(), |
| 482 | // } |
| 483 | // } |
| 484 | // _ => panic!(), |
| 485 | // } |
| 486 | // } |
| 487 | // _ => panic!(), |
| 488 | // } |
| 489 | // } |
| 490 | // _ => panic!(), |
| 491 | // } |
| 492 | // } |
| 493 | |
| 494 | // #[test] |
| 495 | // fn stress_recv() { |
| 496 | // const COUNT: usize = 10_000; |
| 497 | |
| 498 | // let (s1, r1) = unbounded(); |
| 499 | // let (s2, r2) = bounded(5); |
| 500 | // let (s3, r3) = bounded(0); |
| 501 | |
| 502 | // scope(|scope| { |
| 503 | // scope.spawn(|_| { |
| 504 | // for i in 0..COUNT { |
| 505 | // s1.send(i).unwrap(); |
| 506 | // r3.recv().unwrap(); |
| 507 | |
| 508 | // s2.send(i).unwrap(); |
| 509 | // r3.recv().unwrap(); |
| 510 | // } |
| 511 | // }); |
| 512 | |
| 513 | // for i in 0..COUNT { |
| 514 | // for _ in 0..2 { |
| 515 | // let mut sel = Select::new(); |
| 516 | // sel.recv(&r1); |
| 517 | // sel.recv(&r2); |
| 518 | // match sel.ready() { |
| 519 | // 0 => assert_eq!(r1.try_recv(), Ok(i)), |
| 520 | // 1 => assert_eq!(r2.try_recv(), Ok(i)), |
| 521 | // _ => panic!(), |
| 522 | // } |
| 523 | |
| 524 | // s3.send(()).unwrap(); |
| 525 | // } |
| 526 | // } |
| 527 | // }) |
| 528 | // .unwrap(); |
| 529 | // } |
| 530 | |
| 531 | // #[test] |
| 532 | // fn stress_send() { |
| 533 | // const COUNT: usize = 10_000; |
| 534 | |
| 535 | // let (s1, r1) = bounded(0); |
| 536 | // let (s2, r2) = bounded(0); |
| 537 | // let (s3, r3) = bounded(100); |
| 538 | |
| 539 | // scope(|scope| { |
| 540 | // scope.spawn(|_| { |
| 541 | // for i in 0..COUNT { |
| 542 | // assert_eq!(r1.recv().unwrap(), i); |
| 543 | // assert_eq!(r2.recv().unwrap(), i); |
| 544 | // r3.recv().unwrap(); |
| 545 | // } |
| 546 | // }); |
| 547 | |
| 548 | // for i in 0..COUNT { |
| 549 | // for _ in 0..2 { |
| 550 | // let mut sel = Select::new(); |
| 551 | // sel.send(&s1); |
| 552 | // sel.send(&s2); |
| 553 | // match sel.ready() { |
| 554 | // 0 => assert!(s1.try_send(i).is_ok()), |
| 555 | // 1 => assert!(s2.try_send(i).is_ok()), |
| 556 | // _ => panic!(), |
| 557 | // } |
| 558 | // } |
| 559 | // s3.send(()).unwrap(); |
| 560 | // } |
| 561 | // }) |
| 562 | // .unwrap(); |
| 563 | // } |
| 564 | |
| 565 | // #[test] |
| 566 | // fn stress_mixed() { |
| 567 | // const COUNT: usize = 10_000; |
| 568 | |
| 569 | // let (s1, r1) = bounded(0); |
| 570 | // let (s2, r2) = bounded(0); |
| 571 | // let (s3, r3) = bounded(100); |
| 572 | |
| 573 | // scope(|scope| { |
| 574 | // scope.spawn(|_| { |
| 575 | // for i in 0..COUNT { |
| 576 | // s1.send(i).unwrap(); |
| 577 | // assert_eq!(r2.recv().unwrap(), i); |
| 578 | // r3.recv().unwrap(); |
| 579 | // } |
| 580 | // }); |
| 581 | |
| 582 | // for i in 0..COUNT { |
| 583 | // for _ in 0..2 { |
| 584 | // let mut sel = Select::new(); |
| 585 | // sel.recv(&r1); |
| 586 | // sel.send(&s2); |
| 587 | // match sel.ready() { |
| 588 | // 0 => assert_eq!(r1.try_recv(), Ok(i)), |
| 589 | // 1 => assert!(s2.try_send(i).is_ok()), |
| 590 | // _ => panic!(), |
| 591 | // } |
| 592 | // } |
| 593 | // s3.send(()).unwrap(); |
| 594 | // } |
| 595 | // }) |
| 596 | // .unwrap(); |
| 597 | // } |
| 598 | |
| 599 | // #[test] |
| 600 | // fn stress_timeout_two_threads() { |
| 601 | // const COUNT: usize = 20; |
| 602 | |
| 603 | // let (s, r) = bounded(2); |
| 604 | |
| 605 | // scope(|scope| { |
| 606 | // scope.spawn(|_| { |
| 607 | // for i in 0..COUNT { |
| 608 | // if i % 2 == 0 { |
| 609 | // thread::sleep(ms(500)); |
| 610 | // } |
| 611 | |
| 612 | // let done = false; |
| 613 | // while !done { |
| 614 | // let mut sel = Select::new(); |
| 615 | // sel.send(&s); |
| 616 | // match sel.ready_timeout(ms(100)) { |
| 617 | // Err(_) => {} |
| 618 | // Ok(0) => { |
| 619 | // assert!(s.try_send(i).is_ok()); |
| 620 | // break; |
| 621 | // } |
| 622 | // Ok(_) => panic!(), |
| 623 | // } |
| 624 | // } |
| 625 | // } |
| 626 | // }); |
| 627 | |
| 628 | // scope.spawn(|_| { |
| 629 | // for i in 0..COUNT { |
| 630 | // if i % 2 == 0 { |
| 631 | // thread::sleep(ms(500)); |
| 632 | // } |
| 633 | |
| 634 | // let mut done = false; |
| 635 | // while !done { |
| 636 | // let mut sel = Select::new(); |
| 637 | // sel.recv(&r); |
| 638 | // match sel.ready_timeout(ms(100)) { |
| 639 | // Err(_) => {} |
| 640 | // Ok(0) => { |
| 641 | // assert_eq!(r.try_recv(), Ok(i)); |
| 642 | // done = true; |
| 643 | // } |
| 644 | // Ok(_) => panic!(), |
| 645 | // } |
| 646 | // } |
| 647 | // } |
| 648 | // }); |
| 649 | // }) |
| 650 | // .unwrap(); |
| 651 | // } |
| 652 | |
| 653 | // #[test] |
| 654 | // fn send_recv_same_channel() { |
| 655 | // let (s, r) = bounded::<i32>(0); |
| 656 | // let mut sel = Select::new(); |
| 657 | // sel.send(&s); |
| 658 | // sel.recv(&r); |
| 659 | // assert!(sel.ready_timeout(ms(100)).is_err()); |
| 660 | |
| 661 | // let (s, r) = unbounded::<i32>(); |
| 662 | // let mut sel = Select::new(); |
| 663 | // sel.send(&s); |
| 664 | // sel.recv(&r); |
| 665 | // match sel.ready_timeout(ms(100)) { |
| 666 | // Err(_) => panic!(), |
| 667 | // Ok(0) => assert!(s.try_send(0).is_ok()), |
| 668 | // Ok(_) => panic!(), |
| 669 | // } |
| 670 | // } |
| 671 | |
| 672 | // #[test] |
| 673 | // fn channel_through_channel() { |
| 674 | // const COUNT: usize = 1000; |
| 675 | |
| 676 | // type T = Box<dyn Any + Send>; |
| 677 | |
| 678 | // for cap in 1..4 { |
| 679 | // let (s, r) = bounded::<T>(cap); |
| 680 | |
| 681 | // scope(|scope| { |
| 682 | // scope.spawn(move |_| { |
| 683 | // let mut s = s; |
| 684 | |
| 685 | // for _ in 0..COUNT { |
| 686 | // let (new_s, new_r) = bounded(cap); |
| 687 | // let new_r: T = Box::new(Some(new_r)); |
| 688 | |
| 689 | // { |
| 690 | // let mut sel = Select::new(); |
| 691 | // sel.send(&s); |
| 692 | // match sel.ready() { |
| 693 | // 0 => assert!(s.try_send(new_r).is_ok()), |
| 694 | // _ => panic!(), |
| 695 | // } |
| 696 | // } |
| 697 | |
| 698 | // s = new_s; |
| 699 | // } |
| 700 | // }); |
| 701 | |
| 702 | // scope.spawn(move |_| { |
| 703 | // let mut r = r; |
| 704 | |
| 705 | // for _ in 0..COUNT { |
| 706 | // let new = { |
| 707 | // let mut sel = Select::new(); |
| 708 | // sel.recv(&r); |
| 709 | // match sel.ready() { |
| 710 | // 0 => r |
| 711 | // .try_recv() |
| 712 | // .unwrap() |
| 713 | // .downcast_mut::<Option<Receiver<T>>>() |
| 714 | // .unwrap() |
| 715 | // .take() |
| 716 | // .unwrap(), |
| 717 | // _ => panic!(), |
| 718 | // } |
| 719 | // }; |
| 720 | // r = new; |
| 721 | // } |
| 722 | // }); |
| 723 | // }) |
| 724 | // .unwrap(); |
| 725 | // } |
| 726 | // } |
| 727 | |
| 728 | // #[test] |
| 729 | // fn fairness1() { |
| 730 | // const COUNT: usize = 10_000; |
| 731 | |
| 732 | // let (s1, r1) = bounded::<()>(COUNT); |
| 733 | // let (s2, r2) = unbounded::<()>(); |
| 734 | |
| 735 | // for _ in 0..COUNT { |
| 736 | // s1.send(()).unwrap(); |
| 737 | // s2.send(()).unwrap(); |
| 738 | // } |
| 739 | |
| 740 | // let hits = vec![Cell::new(0usize); 4]; |
| 741 | // for _ in 0..COUNT { |
| 742 | // let after = after(ms(0)); |
| 743 | // let tick = tick(ms(0)); |
| 744 | |
| 745 | // let mut sel = Select::new(); |
| 746 | // sel.recv(&r1); |
| 747 | // sel.recv(&r2); |
| 748 | // sel.recv(&after); |
| 749 | // sel.recv(&tick); |
| 750 | // match sel.ready() { |
| 751 | // 0 => { |
| 752 | // r1.try_recv().unwrap(); |
| 753 | // hits[0].set(hits[0].get() + 1); |
| 754 | // } |
| 755 | // 1 => { |
| 756 | // r2.try_recv().unwrap(); |
| 757 | // hits[1].set(hits[1].get() + 1); |
| 758 | // } |
| 759 | // 2 => { |
| 760 | // after.try_recv().unwrap(); |
| 761 | // hits[2].set(hits[2].get() + 1); |
| 762 | // } |
| 763 | // 3 => { |
| 764 | // tick.try_recv().unwrap(); |
| 765 | // hits[3].set(hits[3].get() + 1); |
| 766 | // } |
| 767 | // _ => panic!(), |
| 768 | // } |
| 769 | // } |
| 770 | // assert!(hits.iter().all(|x| x.get() >= COUNT / hits.len() / 2)); |
| 771 | // } |
| 772 | |
| 773 | // #[test] |
| 774 | // fn fairness2() { |
| 775 | // const COUNT: usize = 10_000; |
| 776 | |
| 777 | // let (s1, r1) = unbounded::<()>(); |
| 778 | // let (s2, r2) = bounded::<()>(1); |
| 779 | // let (s3, r3) = bounded::<()>(0); |
| 780 | |
| 781 | // scope(|scope| { |
| 782 | // scope.spawn(|_| { |
| 783 | // for _ in 0..COUNT { |
| 784 | // let mut sel = Select::new(); |
| 785 | // let mut oper1 = None; |
| 786 | // let mut oper2 = None; |
| 787 | // if s1.is_empty() { |
| 788 | // oper1 = Some(sel.send(&s1)); |
| 789 | // } |
| 790 | // if s2.is_empty() { |
| 791 | // oper2 = Some(sel.send(&s2)); |
| 792 | // } |
| 793 | // let oper3 = sel.send(&s3); |
| 794 | // let oper = sel.select(); |
| 795 | // match oper.index() { |
| 796 | // i if Some(i) == oper1 => assert!(oper.send(&s1, ()).is_ok()), |
| 797 | // i if Some(i) == oper2 => assert!(oper.send(&s2, ()).is_ok()), |
| 798 | // i if i == oper3 => assert!(oper.send(&s3, ()).is_ok()), |
| 799 | // _ => unreachable!(), |
| 800 | // } |
| 801 | // } |
| 802 | // }); |
| 803 | |
| 804 | // let hits = vec![Cell::new(0usize); 3]; |
| 805 | // for _ in 0..COUNT { |
| 806 | // let mut sel = Select::new(); |
| 807 | // sel.recv(&r1); |
| 808 | // sel.recv(&r2); |
| 809 | // sel.recv(&r3); |
| 810 | // loop { |
| 811 | // match sel.ready() { |
| 812 | // 0 => { |
| 813 | // if r1.try_recv().is_ok() { |
| 814 | // hits[0].set(hits[0].get() + 1); |
| 815 | // break; |
| 816 | // } |
| 817 | // } |
| 818 | // 1 => { |
| 819 | // if r2.try_recv().is_ok() { |
| 820 | // hits[1].set(hits[1].get() + 1); |
| 821 | // break; |
| 822 | // } |
| 823 | // } |
| 824 | // 2 => { |
| 825 | // if r3.try_recv().is_ok() { |
| 826 | // hits[2].set(hits[2].get() + 1); |
| 827 | // break; |
| 828 | // } |
| 829 | // } |
| 830 | // _ => unreachable!(), |
| 831 | // } |
| 832 | // } |
| 833 | // } |
| 834 | // assert!(hits.iter().all(|x| x.get() >= COUNT / hits.len() / 10)); |
| 835 | // }) |
| 836 | // .unwrap(); |
| 837 | // } |