| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#[macro_use] |
| 24 |
extern crate crossbeam_channel as cc; |
| 25 |
|
| 26 |
use std::sync::mpsc::{RecvError, RecvTimeoutError, TryRecvError}; |
| 27 |
use std::sync::mpsc::{SendError, TrySendError}; |
| 28 |
use std::thread::JoinHandle; |
| 29 |
use std::time::Duration; |
| 30 |
|
| 31 |
pub struct Sender<T> { |
| 32 |
pub inner: cc::Sender<T>, |
| 33 |
} |
| 34 |
|
| 35 |
impl<T> Sender<T> { |
| 36 |
pub fn send(&self, t: T) -> Result<(), SendError<T>> { |
| 37 |
self.inner.send(t).map_err(|cc::SendError(m)| SendError(m)) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl<T> Clone for Sender<T> { |
| 42 |
fn clone(&self) -> Sender<T> { |
| 43 |
Sender { |
| 44 |
inner: self.inner.clone(), |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
pub struct SyncSender<T> { |
| 50 |
pub inner: cc::Sender<T>, |
| 51 |
} |
| 52 |
|
| 53 |
impl<T> SyncSender<T> { |
| 54 |
pub fn send(&self, t: T) -> Result<(), SendError<T>> { |
| 55 |
self.inner.send(t).map_err(|cc::SendError(m)| SendError(m)) |
| 56 |
} |
| 57 |
|
| 58 |
pub fn try_send(&self, t: T) -> Result<(), TrySendError<T>> { |
| 59 |
self.inner.try_send(t).map_err(|err| match err { |
| 60 |
cc::TrySendError::Full(m) => TrySendError::Full(m), |
| 61 |
cc::TrySendError::Disconnected(m) => TrySendError::Disconnected(m), |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl<T> Clone for SyncSender<T> { |
| 67 |
fn clone(&self) -> SyncSender<T> { |
| 68 |
SyncSender { |
| 69 |
inner: self.inner.clone(), |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
pub struct Receiver<T> { |
| 75 |
pub inner: cc::Receiver<T>, |
| 76 |
} |
| 77 |
|
| 78 |
impl<T> Receiver<T> { |
| 79 |
pub fn try_recv(&self) -> Result<T, TryRecvError> { |
| 80 |
self.inner.try_recv().map_err(|err| match err { |
| 81 |
cc::TryRecvError::Empty => TryRecvError::Empty, |
| 82 |
cc::TryRecvError::Disconnected => TryRecvError::Disconnected, |
| 83 |
}) |
| 84 |
} |
| 85 |
|
| 86 |
pub fn recv(&self) -> Result<T, RecvError> { |
| 87 |
self.inner.recv().map_err(|_| RecvError) |
| 88 |
} |
| 89 |
|
| 90 |
pub fn recv_timeout(&self, timeout: Duration) -> Result<T, RecvTimeoutError> { |
| 91 |
self.inner.recv_timeout(timeout).map_err(|err| match err { |
| 92 |
cc::RecvTimeoutError::Timeout => RecvTimeoutError::Timeout, |
| 93 |
cc::RecvTimeoutError::Disconnected => RecvTimeoutError::Disconnected, |
| 94 |
}) |
| 95 |
} |
| 96 |
|
| 97 |
pub fn iter(&self) -> Iter<T> { |
| 98 |
Iter { inner: self } |
| 99 |
} |
| 100 |
|
| 101 |
pub fn try_iter(&self) -> TryIter<T> { |
| 102 |
TryIter { inner: self } |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
impl<'a, T> IntoIterator for &'a Receiver<T> { |
| 107 |
type Item = T; |
| 108 |
type IntoIter = Iter<'a, T>; |
| 109 |
|
| 110 |
fn into_iter(self) -> Iter<'a, T> { |
| 111 |
self.iter() |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl<T> IntoIterator for Receiver<T> { |
| 116 |
type Item = T; |
| 117 |
type IntoIter = IntoIter<T>; |
| 118 |
|
| 119 |
fn into_iter(self) -> IntoIter<T> { |
| 120 |
IntoIter { inner: self } |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
pub struct TryIter<'a, T: 'a> { |
| 125 |
inner: &'a Receiver<T>, |
| 126 |
} |
| 127 |
|
| 128 |
impl<'a, T> Iterator for TryIter<'a, T> { |
| 129 |
type Item = T; |
| 130 |
|
| 131 |
fn next(&mut self) -> Option<T> { |
| 132 |
self.inner.try_recv().ok() |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
pub struct Iter<'a, T: 'a> { |
| 137 |
inner: &'a Receiver<T>, |
| 138 |
} |
| 139 |
|
| 140 |
impl<'a, T> Iterator for Iter<'a, T> { |
| 141 |
type Item = T; |
| 142 |
|
| 143 |
fn next(&mut self) -> Option<T> { |
| 144 |
self.inner.recv().ok() |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
pub struct IntoIter<T> { |
| 149 |
inner: Receiver<T>, |
| 150 |
} |
| 151 |
|
| 152 |
impl<T> Iterator for IntoIter<T> { |
| 153 |
type Item = T; |
| 154 |
|
| 155 |
fn next(&mut self) -> Option<T> { |
| 156 |
self.inner.recv().ok() |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
pub fn channel<T>() -> (Sender<T>, Receiver<T>) { |
| 161 |
let (s, r) = cc::unbounded(); |
| 162 |
let s = Sender { inner: s }; |
| 163 |
let r = Receiver { inner: r }; |
| 164 |
(s, r) |
| 165 |
} |
| 166 |
|
| 167 |
pub fn sync_channel<T>(bound: usize) -> (SyncSender<T>, Receiver<T>) { |
| 168 |
let (s, r) = cc::bounded(bound); |
| 169 |
let s = SyncSender { inner: s }; |
| 170 |
let r = Receiver { inner: r }; |
| 171 |
(s, r) |
| 172 |
} |
| 173 |
|
| 174 |
macro_rules! select { |
| 175 |
( |
| 176 |
$($name:pat = $rx:ident.$meth:ident() => $code:expr),+ |
| 177 |
) => ({ |
| 178 |
const _IS_BIASED: bool = false; |
| 179 |
|
| 180 |
crossbeam_channel_internal! { |
| 181 |
$( |
| 182 |
recv(($rx).inner) -> res => { |
| 183 |
let $name = res.map_err(|_| ::std::sync::mpsc::RecvError); |
| 184 |
$code |
| 185 |
} |
| 186 |
)+ |
| 187 |
} |
| 188 |
}) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
mod channel_tests { |
| 193 |
use super::*; |
| 194 |
|
| 195 |
use std::env; |
| 196 |
use std::thread; |
| 197 |
use std::time::{Duration, Instant}; |
| 198 |
|
| 199 |
pub fn stress_factor() -> usize { |
| 200 |
match env::var("RUST_TEST_STRESS") { |
| 201 |
Ok(val) => val.parse().unwrap(), |
| 202 |
Err(..) => 1, |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn smoke() { |
| 208 |
let (tx, rx) = channel::<i32>(); |
| 209 |
tx.send(1).unwrap(); |
| 210 |
assert_eq!(rx.recv().unwrap(), 1); |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn drop_full() { |
| 215 |
let (tx, _rx) = channel::<Box<isize>>(); |
| 216 |
tx.send(Box::new(1)).unwrap(); |
| 217 |
} |
| 218 |
|
| 219 |
#[test] |
| 220 |
fn drop_full_shared() { |
| 221 |
let (tx, _rx) = channel::<Box<isize>>(); |
| 222 |
drop(tx.clone()); |
| 223 |
drop(tx.clone()); |
| 224 |
tx.send(Box::new(1)).unwrap(); |
| 225 |
} |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn smoke_shared() { |
| 229 |
let (tx, rx) = channel::<i32>(); |
| 230 |
tx.send(1).unwrap(); |
| 231 |
assert_eq!(rx.recv().unwrap(), 1); |
| 232 |
let tx = tx.clone(); |
| 233 |
tx.send(1).unwrap(); |
| 234 |
assert_eq!(rx.recv().unwrap(), 1); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn smoke_threads() { |
| 239 |
let (tx, rx) = channel::<i32>(); |
| 240 |
let t = thread::spawn(move || { |
| 241 |
tx.send(1).unwrap(); |
| 242 |
}); |
| 243 |
assert_eq!(rx.recv().unwrap(), 1); |
| 244 |
t.join().unwrap(); |
| 245 |
} |
| 246 |
|
| 247 |
#[test] |
| 248 |
fn smoke_port_gone() { |
| 249 |
let (tx, rx) = channel::<i32>(); |
| 250 |
drop(rx); |
| 251 |
assert!(tx.send(1).is_err()); |
| 252 |
} |
| 253 |
|
| 254 |
#[test] |
| 255 |
fn smoke_shared_port_gone() { |
| 256 |
let (tx, rx) = channel::<i32>(); |
| 257 |
drop(rx); |
| 258 |
assert!(tx.send(1).is_err()) |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn smoke_shared_port_gone2() { |
| 263 |
let (tx, rx) = channel::<i32>(); |
| 264 |
drop(rx); |
| 265 |
let tx2 = tx.clone(); |
| 266 |
drop(tx); |
| 267 |
assert!(tx2.send(1).is_err()); |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
fn port_gone_concurrent() { |
| 272 |
let (tx, rx) = channel::<i32>(); |
| 273 |
let t = thread::spawn(move || { |
| 274 |
rx.recv().unwrap(); |
| 275 |
}); |
| 276 |
while tx.send(1).is_ok() {} |
| 277 |
t.join().unwrap(); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn port_gone_concurrent_shared() { |
| 282 |
let (tx, rx) = channel::<i32>(); |
| 283 |
let tx2 = tx.clone(); |
| 284 |
let t = thread::spawn(move || { |
| 285 |
rx.recv().unwrap(); |
| 286 |
}); |
| 287 |
while tx.send(1).is_ok() && tx2.send(1).is_ok() {} |
| 288 |
t.join().unwrap(); |
| 289 |
} |
| 290 |
|
| 291 |
#[test] |
| 292 |
fn smoke_chan_gone() { |
| 293 |
let (tx, rx) = channel::<i32>(); |
| 294 |
drop(tx); |
| 295 |
assert!(rx.recv().is_err()); |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn smoke_chan_gone_shared() { |
| 300 |
let (tx, rx) = channel::<()>(); |
| 301 |
let tx2 = tx.clone(); |
| 302 |
drop(tx); |
| 303 |
drop(tx2); |
| 304 |
assert!(rx.recv().is_err()); |
| 305 |
} |
| 306 |
|
| 307 |
#[test] |
| 308 |
fn chan_gone_concurrent() { |
| 309 |
let (tx, rx) = channel::<i32>(); |
| 310 |
let t = thread::spawn(move || { |
| 311 |
tx.send(1).unwrap(); |
| 312 |
tx.send(1).unwrap(); |
| 313 |
}); |
| 314 |
while rx.recv().is_ok() {} |
| 315 |
t.join().unwrap(); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn stress() { |
| 320 |
let (tx, rx) = channel::<i32>(); |
| 321 |
let t = thread::spawn(move || { |
| 322 |
for _ in 0..10000 { |
| 323 |
tx.send(1).unwrap(); |
| 324 |
} |
| 325 |
}); |
| 326 |
for _ in 0..10000 { |
| 327 |
assert_eq!(rx.recv().unwrap(), 1); |
| 328 |
} |
| 329 |
t.join().ok().unwrap(); |
| 330 |
} |
| 331 |
|
| 332 |
#[test] |
| 333 |
fn stress_shared() { |
| 334 |
const AMT: u32 = 10000; |
| 335 |
const NTHREADS: u32 = 8; |
| 336 |
let (tx, rx) = channel::<i32>(); |
| 337 |
|
| 338 |
let t = thread::spawn(move || { |
| 339 |
for _ in 0..AMT * NTHREADS { |
| 340 |
assert_eq!(rx.recv().unwrap(), 1); |
| 341 |
} |
| 342 |
match rx.try_recv() { |
| 343 |
Ok(..) => panic!(), |
| 344 |
_ => {} |
| 345 |
} |
| 346 |
}); |
| 347 |
|
| 348 |
let mut ts = Vec::with_capacity(NTHREADS as usize); |
| 349 |
for _ in 0..NTHREADS { |
| 350 |
let tx = tx.clone(); |
| 351 |
let t = thread::spawn(move || { |
| 352 |
for _ in 0..AMT { |
| 353 |
tx.send(1).unwrap(); |
| 354 |
} |
| 355 |
}); |
| 356 |
ts.push(t); |
| 357 |
} |
| 358 |
drop(tx); |
| 359 |
t.join().ok().unwrap(); |
| 360 |
for t in ts { |
| 361 |
t.join().unwrap(); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
#[test] |
| 366 |
fn send_from_outside_runtime() { |
| 367 |
let (tx1, rx1) = channel::<()>(); |
| 368 |
let (tx2, rx2) = channel::<i32>(); |
| 369 |
let t1 = thread::spawn(move || { |
| 370 |
tx1.send(()).unwrap(); |
| 371 |
for _ in 0..40 { |
| 372 |
assert_eq!(rx2.recv().unwrap(), 1); |
| 373 |
} |
| 374 |
}); |
| 375 |
rx1.recv().unwrap(); |
| 376 |
let t2 = thread::spawn(move || { |
| 377 |
for _ in 0..40 { |
| 378 |
tx2.send(1).unwrap(); |
| 379 |
} |
| 380 |
}); |
| 381 |
t1.join().ok().unwrap(); |
| 382 |
t2.join().ok().unwrap(); |
| 383 |
} |
| 384 |
|
| 385 |
#[test] |
| 386 |
fn recv_from_outside_runtime() { |
| 387 |
let (tx, rx) = channel::<i32>(); |
| 388 |
let t = thread::spawn(move || { |
| 389 |
for _ in 0..40 { |
| 390 |
assert_eq!(rx.recv().unwrap(), 1); |
| 391 |
} |
| 392 |
}); |
| 393 |
for _ in 0..40 { |
| 394 |
tx.send(1).unwrap(); |
| 395 |
} |
| 396 |
t.join().ok().unwrap(); |
| 397 |
} |
| 398 |
|
| 399 |
#[test] |
| 400 |
fn no_runtime() { |
| 401 |
let (tx1, rx1) = channel::<i32>(); |
| 402 |
let (tx2, rx2) = channel::<i32>(); |
| 403 |
let t1 = thread::spawn(move || { |
| 404 |
assert_eq!(rx1.recv().unwrap(), 1); |
| 405 |
tx2.send(2).unwrap(); |
| 406 |
}); |
| 407 |
let t2 = thread::spawn(move || { |
| 408 |
tx1.send(1).unwrap(); |
| 409 |
assert_eq!(rx2.recv().unwrap(), 2); |
| 410 |
}); |
| 411 |
t1.join().ok().unwrap(); |
| 412 |
t2.join().ok().unwrap(); |
| 413 |
} |
| 414 |
|
| 415 |
#[test] |
| 416 |
fn oneshot_single_thread_close_port_first() { |
| 417 |
|
| 418 |
let (_tx, rx) = channel::<i32>(); |
| 419 |
drop(rx); |
| 420 |
} |
| 421 |
|
| 422 |
#[test] |
| 423 |
fn oneshot_single_thread_close_chan_first() { |
| 424 |
|
| 425 |
let (tx, _rx) = channel::<i32>(); |
| 426 |
drop(tx); |
| 427 |
} |
| 428 |
|
| 429 |
#[test] |
| 430 |
fn oneshot_single_thread_send_port_close() { |
| 431 |
|
| 432 |
let (tx, rx) = channel::<Box<i32>>(); |
| 433 |
drop(rx); |
| 434 |
assert!(tx.send(Box::new(0)).is_err()); |
| 435 |
} |
| 436 |
|
| 437 |
#[test] |
| 438 |
fn oneshot_single_thread_recv_chan_close() { |
| 439 |
let (tx, rx) = channel::<i32>(); |
| 440 |
drop(tx); |
| 441 |
assert_eq!(rx.recv(), Err(RecvError)); |
| 442 |
} |
| 443 |
|
| 444 |
#[test] |
| 445 |
fn oneshot_single_thread_send_then_recv() { |
| 446 |
let (tx, rx) = channel::<Box<i32>>(); |
| 447 |
tx.send(Box::new(10)).unwrap(); |
| 448 |
assert!(*rx.recv().unwrap() == 10); |
| 449 |
} |
| 450 |
|
| 451 |
#[test] |
| 452 |
fn oneshot_single_thread_try_send_open() { |
| 453 |
let (tx, rx) = channel::<i32>(); |
| 454 |
assert!(tx.send(10).is_ok()); |
| 455 |
assert!(rx.recv().unwrap() == 10); |
| 456 |
} |
| 457 |
|
| 458 |
#[test] |
| 459 |
fn oneshot_single_thread_try_send_closed() { |
| 460 |
let (tx, rx) = channel::<i32>(); |
| 461 |
drop(rx); |
| 462 |
assert!(tx.send(10).is_err()); |
| 463 |
} |
| 464 |
|
| 465 |
#[test] |
| 466 |
fn oneshot_single_thread_try_recv_open() { |
| 467 |
let (tx, rx) = channel::<i32>(); |
| 468 |
tx.send(10).unwrap(); |
| 469 |
assert!(rx.recv() == Ok(10)); |
| 470 |
} |
| 471 |
|
| 472 |
#[test] |
| 473 |
fn oneshot_single_thread_try_recv_closed() { |
| 474 |
let (tx, rx) = channel::<i32>(); |
| 475 |
drop(tx); |
| 476 |
assert!(rx.recv().is_err()); |
| 477 |
} |
| 478 |
|
| 479 |
#[test] |
| 480 |
fn oneshot_single_thread_peek_data() { |
| 481 |
let (tx, rx) = channel::<i32>(); |
| 482 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty)); |
| 483 |
tx.send(10).unwrap(); |
| 484 |
assert_eq!(rx.try_recv(), Ok(10)); |
| 485 |
} |
| 486 |
|
| 487 |
#[test] |
| 488 |
fn oneshot_single_thread_peek_close() { |
| 489 |
let (tx, rx) = channel::<i32>(); |
| 490 |
drop(tx); |
| 491 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); |
| 492 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); |
| 493 |
} |
| 494 |
|
| 495 |
#[test] |
| 496 |
fn oneshot_single_thread_peek_open() { |
| 497 |
let (_tx, rx) = channel::<i32>(); |
| 498 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty)); |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn oneshot_multi_task_recv_then_send() { |
| 503 |
let (tx, rx) = channel::<Box<i32>>(); |
| 504 |
let t = thread::spawn(move || { |
| 505 |
assert!(*rx.recv().unwrap() == 10); |
| 506 |
}); |
| 507 |
|
| 508 |
tx.send(Box::new(10)).unwrap(); |
| 509 |
t.join().unwrap(); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn oneshot_multi_task_recv_then_close() { |
| 514 |
let (tx, rx) = channel::<Box<i32>>(); |
| 515 |
let t = thread::spawn(move || { |
| 516 |
drop(tx); |
| 517 |
}); |
| 518 |
thread::spawn(move || { |
| 519 |
assert_eq!(rx.recv(), Err(RecvError)); |
| 520 |
}) |
| 521 |
.join() |
| 522 |
.unwrap(); |
| 523 |
t.join().unwrap(); |
| 524 |
} |
| 525 |
|
| 526 |
#[test] |
| 527 |
fn oneshot_multi_thread_close_stress() { |
| 528 |
let stress_factor = stress_factor(); |
| 529 |
let mut ts = Vec::with_capacity(stress_factor); |
| 530 |
for _ in 0..stress_factor { |
| 531 |
let (tx, rx) = channel::<i32>(); |
| 532 |
let t = thread::spawn(move || { |
| 533 |
drop(rx); |
| 534 |
}); |
| 535 |
ts.push(t); |
| 536 |
drop(tx); |
| 537 |
} |
| 538 |
for t in ts { |
| 539 |
t.join().unwrap(); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn oneshot_multi_thread_send_close_stress() { |
| 545 |
let stress_factor = stress_factor(); |
| 546 |
let mut ts = Vec::with_capacity(2 * stress_factor); |
| 547 |
for _ in 0..stress_factor { |
| 548 |
let (tx, rx) = channel::<i32>(); |
| 549 |
let t = thread::spawn(move || { |
| 550 |
drop(rx); |
| 551 |
}); |
| 552 |
ts.push(t); |
| 553 |
thread::spawn(move || { |
| 554 |
let _ = tx.send(1); |
| 555 |
}) |
| 556 |
.join() |
| 557 |
.unwrap(); |
| 558 |
} |
| 559 |
for t in ts { |
| 560 |
t.join().unwrap(); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
#[test] |
| 565 |
fn oneshot_multi_thread_recv_close_stress() { |
| 566 |
let stress_factor = stress_factor(); |
| 567 |
let mut ts = Vec::with_capacity(2 * stress_factor); |
| 568 |
for _ in 0..stress_factor { |
| 569 |
let (tx, rx) = channel::<i32>(); |
| 570 |
let t = thread::spawn(move || { |
| 571 |
thread::spawn(move || { |
| 572 |
assert_eq!(rx.recv(), Err(RecvError)); |
| 573 |
}) |
| 574 |
.join() |
| 575 |
.unwrap(); |
| 576 |
}); |
| 577 |
ts.push(t); |
| 578 |
let t2 = thread::spawn(move || { |
| 579 |
let t = thread::spawn(move || { |
| 580 |
drop(tx); |
| 581 |
}); |
| 582 |
t.join().unwrap(); |
| 583 |
}); |
| 584 |
ts.push(t2); |
| 585 |
} |
| 586 |
for t in ts { |
| 587 |
t.join().unwrap(); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
#[test] |
| 592 |
fn oneshot_multi_thread_send_recv_stress() { |
| 593 |
let stress_factor = stress_factor(); |
| 594 |
let mut ts = Vec::with_capacity(stress_factor); |
| 595 |
for _ in 0..stress_factor { |
| 596 |
let (tx, rx) = channel::<Box<isize>>(); |
| 597 |
let t = thread::spawn(move || { |
| 598 |
tx.send(Box::new(10)).unwrap(); |
| 599 |
}); |
| 600 |
ts.push(t); |
| 601 |
assert!(*rx.recv().unwrap() == 10); |
| 602 |
} |
| 603 |
for t in ts { |
| 604 |
t.join().unwrap(); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
#[test] |
| 609 |
fn stream_send_recv_stress() { |
| 610 |
let stress_factor = stress_factor(); |
| 611 |
let mut ts = Vec::with_capacity(2 * stress_factor); |
| 612 |
for _ in 0..stress_factor { |
| 613 |
let (tx, rx) = channel(); |
| 614 |
|
| 615 |
if let Some(t) = send(tx, 0) { |
| 616 |
ts.push(t); |
| 617 |
} |
| 618 |
if let Some(t2) = recv(rx, 0) { |
| 619 |
ts.push(t2); |
| 620 |
} |
| 621 |
|
| 622 |
fn send(tx: Sender<Box<i32>>, i: i32) -> Option<JoinHandle<()>> { |
| 623 |
if i == 10 { |
| 624 |
return None; |
| 625 |
} |
| 626 |
|
| 627 |
Some(thread::spawn(move || { |
| 628 |
tx.send(Box::new(i)).unwrap(); |
| 629 |
send(tx, i + 1); |
| 630 |
})) |
| 631 |
} |
| 632 |
|
| 633 |
fn recv(rx: Receiver<Box<i32>>, i: i32) -> Option<JoinHandle<()>> { |
| 634 |
if i == 10 { |
| 635 |
return None; |
| 636 |
} |
| 637 |
|
| 638 |
Some(thread::spawn(move || { |
| 639 |
assert!(*rx.recv().unwrap() == i); |
| 640 |
recv(rx, i + 1); |
| 641 |
})) |
| 642 |
} |
| 643 |
} |
| 644 |
for t in ts { |
| 645 |
t.join().unwrap(); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn oneshot_single_thread_recv_timeout() { |
| 651 |
let (tx, rx) = channel(); |
| 652 |
tx.send(()).unwrap(); |
| 653 |
assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(())); |
| 654 |
assert_eq!( |
| 655 |
rx.recv_timeout(Duration::from_millis(1)), |
| 656 |
Err(RecvTimeoutError::Timeout) |
| 657 |
); |
| 658 |
tx.send(()).unwrap(); |
| 659 |
assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(())); |
| 660 |
} |
| 661 |
|
| 662 |
#[test] |
| 663 |
fn stress_recv_timeout_two_threads() { |
| 664 |
let (tx, rx) = channel(); |
| 665 |
let stress = stress_factor() + 100; |
| 666 |
let timeout = Duration::from_millis(100); |
| 667 |
|
| 668 |
let t = thread::spawn(move || { |
| 669 |
for i in 0..stress { |
| 670 |
if i % 2 == 0 { |
| 671 |
thread::sleep(timeout * 2); |
| 672 |
} |
| 673 |
tx.send(1usize).unwrap(); |
| 674 |
} |
| 675 |
}); |
| 676 |
|
| 677 |
let mut recv_count = 0; |
| 678 |
loop { |
| 679 |
match rx.recv_timeout(timeout) { |
| 680 |
Ok(n) => { |
| 681 |
assert_eq!(n, 1usize); |
| 682 |
recv_count += 1; |
| 683 |
} |
| 684 |
Err(RecvTimeoutError::Timeout) => continue, |
| 685 |
Err(RecvTimeoutError::Disconnected) => break, |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
assert_eq!(recv_count, stress); |
| 690 |
t.join().unwrap() |
| 691 |
} |
| 692 |
|
| 693 |
#[test] |
| 694 |
fn recv_timeout_upgrade() { |
| 695 |
let (tx, rx) = channel::<()>(); |
| 696 |
let timeout = Duration::from_millis(1); |
| 697 |
let _tx_clone = tx.clone(); |
| 698 |
|
| 699 |
let start = Instant::now(); |
| 700 |
assert_eq!(rx.recv_timeout(timeout), Err(RecvTimeoutError::Timeout)); |
| 701 |
assert!(Instant::now() >= start + timeout); |
| 702 |
} |
| 703 |
|
| 704 |
#[test] |
| 705 |
fn stress_recv_timeout_shared() { |
| 706 |
let (tx, rx) = channel(); |
| 707 |
let stress = stress_factor() + 100; |
| 708 |
|
| 709 |
let mut ts = Vec::with_capacity(stress); |
| 710 |
for i in 0..stress { |
| 711 |
let tx = tx.clone(); |
| 712 |
let t = thread::spawn(move || { |
| 713 |
thread::sleep(Duration::from_millis(i as u64 * 10)); |
| 714 |
tx.send(1usize).unwrap(); |
| 715 |
}); |
| 716 |
ts.push(t); |
| 717 |
} |
| 718 |
|
| 719 |
drop(tx); |
| 720 |
|
| 721 |
let mut recv_count = 0; |
| 722 |
loop { |
| 723 |
match rx.recv_timeout(Duration::from_millis(10)) { |
| 724 |
Ok(n) => { |
| 725 |
assert_eq!(n, 1usize); |
| 726 |
recv_count += 1; |
| 727 |
} |
| 728 |
Err(RecvTimeoutError::Timeout) => continue, |
| 729 |
Err(RecvTimeoutError::Disconnected) => break, |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
assert_eq!(recv_count, stress); |
| 734 |
for t in ts { |
| 735 |
t.join().unwrap(); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
#[test] |
| 740 |
fn recv_a_lot() { |
| 741 |
|
| 742 |
let (tx, rx) = channel(); |
| 743 |
for _ in 0..10000 { |
| 744 |
tx.send(()).unwrap(); |
| 745 |
} |
| 746 |
for _ in 0..10000 { |
| 747 |
rx.recv().unwrap(); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
#[test] |
| 752 |
fn shared_recv_timeout() { |
| 753 |
let (tx, rx) = channel(); |
| 754 |
let total = 5; |
| 755 |
let mut ts = Vec::with_capacity(total); |
| 756 |
for _ in 0..total { |
| 757 |
let tx = tx.clone(); |
| 758 |
let t = thread::spawn(move || { |
| 759 |
tx.send(()).unwrap(); |
| 760 |
}); |
| 761 |
ts.push(t); |
| 762 |
} |
| 763 |
|
| 764 |
for _ in 0..total { |
| 765 |
rx.recv().unwrap(); |
| 766 |
} |
| 767 |
|
| 768 |
assert_eq!( |
| 769 |
rx.recv_timeout(Duration::from_millis(1)), |
| 770 |
Err(RecvTimeoutError::Timeout) |
| 771 |
); |
| 772 |
tx.send(()).unwrap(); |
| 773 |
assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(())); |
| 774 |
for t in ts { |
| 775 |
t.join().unwrap(); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
#[test] |
| 780 |
fn shared_chan_stress() { |
| 781 |
let (tx, rx) = channel(); |
| 782 |
let total = stress_factor() + 100; |
| 783 |
let mut ts = Vec::with_capacity(total); |
| 784 |
for _ in 0..total { |
| 785 |
let tx = tx.clone(); |
| 786 |
let t = thread::spawn(move || { |
| 787 |
tx.send(()).unwrap(); |
| 788 |
}); |
| 789 |
ts.push(t); |
| 790 |
} |
| 791 |
|
| 792 |
for _ in 0..total { |
| 793 |
rx.recv().unwrap(); |
| 794 |
} |
| 795 |
for t in ts { |
| 796 |
t.join().unwrap(); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
#[test] |
| 801 |
fn test_nested_recv_iter() { |
| 802 |
let (tx, rx) = channel::<i32>(); |
| 803 |
let (total_tx, total_rx) = channel::<i32>(); |
| 804 |
|
| 805 |
let t = thread::spawn(move || { |
| 806 |
let mut acc = 0; |
| 807 |
for x in rx.iter() { |
| 808 |
acc += x; |
| 809 |
} |
| 810 |
total_tx.send(acc).unwrap(); |
| 811 |
}); |
| 812 |
|
| 813 |
tx.send(3).unwrap(); |
| 814 |
tx.send(1).unwrap(); |
| 815 |
tx.send(2).unwrap(); |
| 816 |
drop(tx); |
| 817 |
assert_eq!(total_rx.recv().unwrap(), 6); |
| 818 |
t.join().unwrap(); |
| 819 |
} |
| 820 |
|
| 821 |
#[test] |
| 822 |
fn test_recv_iter_break() { |
| 823 |
let (tx, rx) = channel::<i32>(); |
| 824 |
let (count_tx, count_rx) = channel(); |
| 825 |
|
| 826 |
let t = thread::spawn(move || { |
| 827 |
let mut count = 0; |
| 828 |
for x in rx.iter() { |
| 829 |
if count >= 3 { |
| 830 |
break; |
| 831 |
} else { |
| 832 |
count += x; |
| 833 |
} |
| 834 |
} |
| 835 |
count_tx.send(count).unwrap(); |
| 836 |
}); |
| 837 |
|
| 838 |
tx.send(2).unwrap(); |
| 839 |
tx.send(2).unwrap(); |
| 840 |
tx.send(2).unwrap(); |
| 841 |
let _ = tx.send(2); |
| 842 |
drop(tx); |
| 843 |
assert_eq!(count_rx.recv().unwrap(), 4); |
| 844 |
t.join().unwrap(); |
| 845 |
} |
| 846 |
|
| 847 |
#[test] |
| 848 |
fn test_recv_try_iter() { |
| 849 |
let (request_tx, request_rx) = channel(); |
| 850 |
let (response_tx, response_rx) = channel(); |
| 851 |
|
| 852 |
|
| 853 |
let t = thread::spawn(move || { |
| 854 |
let mut count = 0; |
| 855 |
loop { |
| 856 |
for x in response_rx.try_iter() { |
| 857 |
count += x; |
| 858 |
if count == 6 { |
| 859 |
return count; |
| 860 |
} |
| 861 |
} |
| 862 |
request_tx.send(()).unwrap(); |
| 863 |
} |
| 864 |
}); |
| 865 |
|
| 866 |
for _ in request_rx.iter() { |
| 867 |
if response_tx.send(2).is_err() { |
| 868 |
break; |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
assert_eq!(t.join().unwrap(), 6); |
| 873 |
} |
| 874 |
|
| 875 |
#[test] |
| 876 |
fn test_recv_into_iter_owned() { |
| 877 |
let mut iter = { |
| 878 |
let (tx, rx) = channel::<i32>(); |
| 879 |
tx.send(1).unwrap(); |
| 880 |
tx.send(2).unwrap(); |
| 881 |
|
| 882 |
rx.into_iter() |
| 883 |
}; |
| 884 |
assert_eq!(iter.next().unwrap(), 1); |
| 885 |
assert_eq!(iter.next().unwrap(), 2); |
| 886 |
assert_eq!(iter.next().is_none(), true); |
| 887 |
} |
| 888 |
|
| 889 |
#[test] |
| 890 |
fn test_recv_into_iter_borrowed() { |
| 891 |
let (tx, rx) = channel::<i32>(); |
| 892 |
tx.send(1).unwrap(); |
| 893 |
tx.send(2).unwrap(); |
| 894 |
drop(tx); |
| 895 |
let mut iter = (&rx).into_iter(); |
| 896 |
assert_eq!(iter.next().unwrap(), 1); |
| 897 |
assert_eq!(iter.next().unwrap(), 2); |
| 898 |
assert_eq!(iter.next().is_none(), true); |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn try_recv_states() { |
| 903 |
let (tx1, rx1) = channel::<i32>(); |
| 904 |
let (tx2, rx2) = channel::<()>(); |
| 905 |
let (tx3, rx3) = channel::<()>(); |
| 906 |
let t = thread::spawn(move || { |
| 907 |
rx2.recv().unwrap(); |
| 908 |
tx1.send(1).unwrap(); |
| 909 |
tx3.send(()).unwrap(); |
| 910 |
rx2.recv().unwrap(); |
| 911 |
drop(tx1); |
| 912 |
tx3.send(()).unwrap(); |
| 913 |
}); |
| 914 |
|
| 915 |
assert_eq!(rx1.try_recv(), Err(TryRecvError::Empty)); |
| 916 |
tx2.send(()).unwrap(); |
| 917 |
rx3.recv().unwrap(); |
| 918 |
assert_eq!(rx1.try_recv(), Ok(1)); |
| 919 |
assert_eq!(rx1.try_recv(), Err(TryRecvError::Empty)); |
| 920 |
tx2.send(()).unwrap(); |
| 921 |
rx3.recv().unwrap(); |
| 922 |
assert_eq!(rx1.try_recv(), Err(TryRecvError::Disconnected)); |
| 923 |
t.join().unwrap(); |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
#[test] |
| 929 |
fn destroy_upgraded_shared_port_when_sender_still_active() { |
| 930 |
let (tx, rx) = channel(); |
| 931 |
let (tx2, rx2) = channel(); |
| 932 |
let t = thread::spawn(move || { |
| 933 |
rx.recv().unwrap(); |
| 934 |
drop(rx); |
| 935 |
tx2.send(()).unwrap(); |
| 936 |
}); |
| 937 |
|
| 938 |
for _ in 0..5000 { |
| 939 |
thread::yield_now(); |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
let tx2 = tx.clone(); |
| 944 |
drop(tx); |
| 945 |
tx2.send(()).unwrap(); |
| 946 |
|
| 947 |
|
| 948 |
rx2.recv().unwrap(); |
| 949 |
t.join().unwrap(); |
| 950 |
} |
| 951 |
|
| 952 |
#[test] |
| 953 |
fn issue_32114() { |
| 954 |
let (tx, _) = channel(); |
| 955 |
let _ = tx.send(123); |
| 956 |
assert_eq!(tx.send(123), Err(SendError(123))); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
mod sync_channel_tests { |
| 962 |
use super::*; |
| 963 |
|
| 964 |
use std::env; |
| 965 |
use std::thread; |
| 966 |
use std::time::Duration; |
| 967 |
|
| 968 |
pub fn stress_factor() -> usize { |
| 969 |
match env::var("RUST_TEST_STRESS") { |
| 970 |
Ok(val) => val.parse().unwrap(), |
| 971 |
Err(..) => 1, |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
#[test] |
| 976 |
fn smoke() { |
| 977 |
let (tx, rx) = sync_channel::<i32>(1); |
| 978 |
tx.send(1).unwrap(); |
| 979 |
assert_eq!(rx.recv().unwrap(), 1); |
| 980 |
} |
| 981 |
|
| 982 |
#[test] |
| 983 |
fn drop_full() { |
| 984 |
let (tx, _rx) = sync_channel::<Box<isize>>(1); |
| 985 |
tx.send(Box::new(1)).unwrap(); |
| 986 |
} |
| 987 |
|
| 988 |
#[test] |
| 989 |
fn smoke_shared() { |
| 990 |
let (tx, rx) = sync_channel::<i32>(1); |
| 991 |
tx.send(1).unwrap(); |
| 992 |
assert_eq!(rx.recv().unwrap(), 1); |
| 993 |
let tx = tx.clone(); |
| 994 |
tx.send(1).unwrap(); |
| 995 |
assert_eq!(rx.recv().unwrap(), 1); |
| 996 |
} |
| 997 |
|
| 998 |
#[test] |
| 999 |
fn recv_timeout() { |
| 1000 |
let (tx, rx) = sync_channel::<i32>(1); |
| 1001 |
assert_eq!( |
| 1002 |
rx.recv_timeout(Duration::from_millis(1)), |
| 1003 |
Err(RecvTimeoutError::Timeout) |
| 1004 |
); |
| 1005 |
tx.send(1).unwrap(); |
| 1006 |
assert_eq!(rx.recv_timeout(Duration::from_millis(1)), Ok(1)); |
| 1007 |
} |
| 1008 |
|
| 1009 |
#[test] |
| 1010 |
fn smoke_threads() { |
| 1011 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1012 |
let t = thread::spawn(move || { |
| 1013 |
tx.send(1).unwrap(); |
| 1014 |
}); |
| 1015 |
assert_eq!(rx.recv().unwrap(), 1); |
| 1016 |
t.join().unwrap(); |
| 1017 |
} |
| 1018 |
|
| 1019 |
#[test] |
| 1020 |
fn smoke_port_gone() { |
| 1021 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1022 |
drop(rx); |
| 1023 |
assert!(tx.send(1).is_err()); |
| 1024 |
} |
| 1025 |
|
| 1026 |
#[test] |
| 1027 |
fn smoke_shared_port_gone2() { |
| 1028 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1029 |
drop(rx); |
| 1030 |
let tx2 = tx.clone(); |
| 1031 |
drop(tx); |
| 1032 |
assert!(tx2.send(1).is_err()); |
| 1033 |
} |
| 1034 |
|
| 1035 |
#[test] |
| 1036 |
fn port_gone_concurrent() { |
| 1037 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1038 |
let t = thread::spawn(move || { |
| 1039 |
rx.recv().unwrap(); |
| 1040 |
}); |
| 1041 |
while tx.send(1).is_ok() {} |
| 1042 |
t.join().unwrap(); |
| 1043 |
} |
| 1044 |
|
| 1045 |
#[test] |
| 1046 |
fn port_gone_concurrent_shared() { |
| 1047 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1048 |
let tx2 = tx.clone(); |
| 1049 |
let t = thread::spawn(move || { |
| 1050 |
rx.recv().unwrap(); |
| 1051 |
}); |
| 1052 |
while tx.send(1).is_ok() && tx2.send(1).is_ok() {} |
| 1053 |
t.join().unwrap(); |
| 1054 |
} |
| 1055 |
|
| 1056 |
#[test] |
| 1057 |
fn smoke_chan_gone() { |
| 1058 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1059 |
drop(tx); |
| 1060 |
assert!(rx.recv().is_err()); |
| 1061 |
} |
| 1062 |
|
| 1063 |
#[test] |
| 1064 |
fn smoke_chan_gone_shared() { |
| 1065 |
let (tx, rx) = sync_channel::<()>(0); |
| 1066 |
let tx2 = tx.clone(); |
| 1067 |
drop(tx); |
| 1068 |
drop(tx2); |
| 1069 |
assert!(rx.recv().is_err()); |
| 1070 |
} |
| 1071 |
|
| 1072 |
#[test] |
| 1073 |
fn chan_gone_concurrent() { |
| 1074 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1075 |
let t = thread::spawn(move || { |
| 1076 |
tx.send(1).unwrap(); |
| 1077 |
tx.send(1).unwrap(); |
| 1078 |
}); |
| 1079 |
while rx.recv().is_ok() {} |
| 1080 |
t.join().unwrap(); |
| 1081 |
} |
| 1082 |
|
| 1083 |
#[test] |
| 1084 |
fn stress() { |
| 1085 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1086 |
let t = thread::spawn(move || { |
| 1087 |
for _ in 0..10000 { |
| 1088 |
tx.send(1).unwrap(); |
| 1089 |
} |
| 1090 |
}); |
| 1091 |
for _ in 0..10000 { |
| 1092 |
assert_eq!(rx.recv().unwrap(), 1); |
| 1093 |
} |
| 1094 |
t.join().unwrap(); |
| 1095 |
} |
| 1096 |
|
| 1097 |
#[test] |
| 1098 |
fn stress_recv_timeout_two_threads() { |
| 1099 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1100 |
|
| 1101 |
let t = thread::spawn(move || { |
| 1102 |
for _ in 0..10000 { |
| 1103 |
tx.send(1).unwrap(); |
| 1104 |
} |
| 1105 |
}); |
| 1106 |
|
| 1107 |
let mut recv_count = 0; |
| 1108 |
loop { |
| 1109 |
match rx.recv_timeout(Duration::from_millis(1)) { |
| 1110 |
Ok(v) => { |
| 1111 |
assert_eq!(v, 1); |
| 1112 |
recv_count += 1; |
| 1113 |
} |
| 1114 |
Err(RecvTimeoutError::Timeout) => continue, |
| 1115 |
Err(RecvTimeoutError::Disconnected) => break, |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
assert_eq!(recv_count, 10000); |
| 1120 |
t.join().unwrap(); |
| 1121 |
} |
| 1122 |
|
| 1123 |
#[test] |
| 1124 |
fn stress_recv_timeout_shared() { |
| 1125 |
const AMT: u32 = 1000; |
| 1126 |
const NTHREADS: u32 = 8; |
| 1127 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1128 |
let (dtx, drx) = sync_channel::<()>(0); |
| 1129 |
|
| 1130 |
let t = thread::spawn(move || { |
| 1131 |
let mut recv_count = 0; |
| 1132 |
loop { |
| 1133 |
match rx.recv_timeout(Duration::from_millis(10)) { |
| 1134 |
Ok(v) => { |
| 1135 |
assert_eq!(v, 1); |
| 1136 |
recv_count += 1; |
| 1137 |
} |
| 1138 |
Err(RecvTimeoutError::Timeout) => continue, |
| 1139 |
Err(RecvTimeoutError::Disconnected) => break, |
| 1140 |
} |
| 1141 |
} |
| 1142 |
|
| 1143 |
assert_eq!(recv_count, AMT * NTHREADS); |
| 1144 |
assert!(rx.try_recv().is_err()); |
| 1145 |
|
| 1146 |
dtx.send(()).unwrap(); |
| 1147 |
}); |
| 1148 |
|
| 1149 |
let mut ts = Vec::with_capacity(NTHREADS as usize); |
| 1150 |
for _ in 0..NTHREADS { |
| 1151 |
let tx = tx.clone(); |
| 1152 |
let t = thread::spawn(move || { |
| 1153 |
for _ in 0..AMT { |
| 1154 |
tx.send(1).unwrap(); |
| 1155 |
} |
| 1156 |
}); |
| 1157 |
ts.push(t); |
| 1158 |
} |
| 1159 |
|
| 1160 |
drop(tx); |
| 1161 |
|
| 1162 |
drx.recv().unwrap(); |
| 1163 |
for t in ts { |
| 1164 |
t.join().unwrap(); |
| 1165 |
} |
| 1166 |
t.join().unwrap(); |
| 1167 |
} |
| 1168 |
|
| 1169 |
#[test] |
| 1170 |
fn stress_shared() { |
| 1171 |
const AMT: u32 = 1000; |
| 1172 |
const NTHREADS: u32 = 8; |
| 1173 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1174 |
let (dtx, drx) = sync_channel::<()>(0); |
| 1175 |
|
| 1176 |
let t = thread::spawn(move || { |
| 1177 |
for _ in 0..AMT * NTHREADS { |
| 1178 |
assert_eq!(rx.recv().unwrap(), 1); |
| 1179 |
} |
| 1180 |
match rx.try_recv() { |
| 1181 |
Ok(..) => panic!(), |
| 1182 |
_ => {} |
| 1183 |
} |
| 1184 |
dtx.send(()).unwrap(); |
| 1185 |
}); |
| 1186 |
|
| 1187 |
let mut ts = Vec::with_capacity(NTHREADS as usize); |
| 1188 |
for _ in 0..NTHREADS { |
| 1189 |
let tx = tx.clone(); |
| 1190 |
let t = thread::spawn(move || { |
| 1191 |
for _ in 0..AMT { |
| 1192 |
tx.send(1).unwrap(); |
| 1193 |
} |
| 1194 |
}); |
| 1195 |
ts.push(t); |
| 1196 |
} |
| 1197 |
drop(tx); |
| 1198 |
drx.recv().unwrap(); |
| 1199 |
for t in ts { |
| 1200 |
t.join().unwrap(); |
| 1201 |
} |
| 1202 |
t.join().unwrap(); |
| 1203 |
} |
| 1204 |
|
| 1205 |
#[test] |
| 1206 |
fn oneshot_single_thread_close_port_first() { |
| 1207 |
|
| 1208 |
let (_tx, rx) = sync_channel::<i32>(0); |
| 1209 |
drop(rx); |
| 1210 |
} |
| 1211 |
|
| 1212 |
#[test] |
| 1213 |
fn oneshot_single_thread_close_chan_first() { |
| 1214 |
|
| 1215 |
let (tx, _rx) = sync_channel::<i32>(0); |
| 1216 |
drop(tx); |
| 1217 |
} |
| 1218 |
|
| 1219 |
#[test] |
| 1220 |
fn oneshot_single_thread_send_port_close() { |
| 1221 |
|
| 1222 |
let (tx, rx) = sync_channel::<Box<i32>>(0); |
| 1223 |
drop(rx); |
| 1224 |
assert!(tx.send(Box::new(0)).is_err()); |
| 1225 |
} |
| 1226 |
|
| 1227 |
#[test] |
| 1228 |
fn oneshot_single_thread_recv_chan_close() { |
| 1229 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1230 |
drop(tx); |
| 1231 |
assert_eq!(rx.recv(), Err(RecvError)); |
| 1232 |
} |
| 1233 |
|
| 1234 |
#[test] |
| 1235 |
fn oneshot_single_thread_send_then_recv() { |
| 1236 |
let (tx, rx) = sync_channel::<Box<i32>>(1); |
| 1237 |
tx.send(Box::new(10)).unwrap(); |
| 1238 |
assert!(*rx.recv().unwrap() == 10); |
| 1239 |
} |
| 1240 |
|
| 1241 |
#[test] |
| 1242 |
fn oneshot_single_thread_try_send_open() { |
| 1243 |
let (tx, rx) = sync_channel::<i32>(1); |
| 1244 |
assert_eq!(tx.try_send(10), Ok(())); |
| 1245 |
assert!(rx.recv().unwrap() == 10); |
| 1246 |
} |
| 1247 |
|
| 1248 |
#[test] |
| 1249 |
fn oneshot_single_thread_try_send_closed() { |
| 1250 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1251 |
drop(rx); |
| 1252 |
assert_eq!(tx.try_send(10), Err(TrySendError::Disconnected(10))); |
| 1253 |
} |
| 1254 |
|
| 1255 |
#[test] |
| 1256 |
fn oneshot_single_thread_try_send_closed2() { |
| 1257 |
let (tx, _rx) = sync_channel::<i32>(0); |
| 1258 |
assert_eq!(tx.try_send(10), Err(TrySendError::Full(10))); |
| 1259 |
} |
| 1260 |
|
| 1261 |
#[test] |
| 1262 |
fn oneshot_single_thread_try_recv_open() { |
| 1263 |
let (tx, rx) = sync_channel::<i32>(1); |
| 1264 |
tx.send(10).unwrap(); |
| 1265 |
assert!(rx.recv() == Ok(10)); |
| 1266 |
} |
| 1267 |
|
| 1268 |
#[test] |
| 1269 |
fn oneshot_single_thread_try_recv_closed() { |
| 1270 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1271 |
drop(tx); |
| 1272 |
assert!(rx.recv().is_err()); |
| 1273 |
} |
| 1274 |
|
| 1275 |
#[test] |
| 1276 |
fn oneshot_single_thread_try_recv_closed_with_data() { |
| 1277 |
let (tx, rx) = sync_channel::<i32>(1); |
| 1278 |
tx.send(10).unwrap(); |
| 1279 |
drop(tx); |
| 1280 |
assert_eq!(rx.try_recv(), Ok(10)); |
| 1281 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); |
| 1282 |
} |
| 1283 |
|
| 1284 |
#[test] |
| 1285 |
fn oneshot_single_thread_peek_data() { |
| 1286 |
let (tx, rx) = sync_channel::<i32>(1); |
| 1287 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty)); |
| 1288 |
tx.send(10).unwrap(); |
| 1289 |
assert_eq!(rx.try_recv(), Ok(10)); |
| 1290 |
} |
| 1291 |
|
| 1292 |
#[test] |
| 1293 |
fn oneshot_single_thread_peek_close() { |
| 1294 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1295 |
drop(tx); |
| 1296 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); |
| 1297 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Disconnected)); |
| 1298 |
} |
| 1299 |
|
| 1300 |
#[test] |
| 1301 |
fn oneshot_single_thread_peek_open() { |
| 1302 |
let (_tx, rx) = sync_channel::<i32>(0); |
| 1303 |
assert_eq!(rx.try_recv(), Err(TryRecvError::Empty)); |
| 1304 |
} |
| 1305 |
|
| 1306 |
#[test] |
| 1307 |
fn oneshot_multi_task_recv_then_send() { |
| 1308 |
let (tx, rx) = sync_channel::<Box<i32>>(0); |
| 1309 |
let t = thread::spawn(move || { |
| 1310 |
assert!(*rx.recv().unwrap() == 10); |
| 1311 |
}); |
| 1312 |
|
| 1313 |
tx.send(Box::new(10)).unwrap(); |
| 1314 |
t.join().unwrap(); |
| 1315 |
} |
| 1316 |
|
| 1317 |
#[test] |
| 1318 |
fn oneshot_multi_task_recv_then_close() { |
| 1319 |
let (tx, rx) = sync_channel::<Box<i32>>(0); |
| 1320 |
let t = thread::spawn(move || { |
| 1321 |
drop(tx); |
| 1322 |
}); |
| 1323 |
thread::spawn(move || { |
| 1324 |
assert_eq!(rx.recv(), Err(RecvError)); |
| 1325 |
}) |
| 1326 |
.join() |
| 1327 |
.unwrap(); |
| 1328 |
t.join().unwrap(); |
| 1329 |
} |
| 1330 |
|
| 1331 |
#[test] |
| 1332 |
fn oneshot_multi_thread_close_stress() { |
| 1333 |
let stress_factor = stress_factor(); |
| 1334 |
let mut ts = Vec::with_capacity(stress_factor); |
| 1335 |
for _ in 0..stress_factor { |
| 1336 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1337 |
let t = thread::spawn(move || { |
| 1338 |
drop(rx); |
| 1339 |
}); |
| 1340 |
ts.push(t); |
| 1341 |
drop(tx); |
| 1342 |
} |
| 1343 |
for t in ts { |
| 1344 |
t.join().unwrap(); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
#[test] |
| 1349 |
fn oneshot_multi_thread_send_close_stress() { |
| 1350 |
let stress_factor = stress_factor(); |
| 1351 |
let mut ts = Vec::with_capacity(stress_factor); |
| 1352 |
for _ in 0..stress_factor { |
| 1353 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1354 |
let t = thread::spawn(move || { |
| 1355 |
drop(rx); |
| 1356 |
}); |
| 1357 |
ts.push(t); |
| 1358 |
thread::spawn(move || { |
| 1359 |
let _ = tx.send(1); |
| 1360 |
}) |
| 1361 |
.join() |
| 1362 |
.unwrap(); |
| 1363 |
} |
| 1364 |
for t in ts { |
| 1365 |
t.join().unwrap(); |
| 1366 |
} |
| 1367 |
} |
| 1368 |
|
| 1369 |
#[test] |
| 1370 |
fn oneshot_multi_thread_recv_close_stress() { |
| 1371 |
let stress_factor = stress_factor(); |
| 1372 |
let mut ts = Vec::with_capacity(2 * stress_factor); |
| 1373 |
for _ in 0..stress_factor { |
| 1374 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1375 |
let t = thread::spawn(move || { |
| 1376 |
thread::spawn(move || { |
| 1377 |
assert_eq!(rx.recv(), Err(RecvError)); |
| 1378 |
}) |
| 1379 |
.join() |
| 1380 |
.unwrap(); |
| 1381 |
}); |
| 1382 |
ts.push(t); |
| 1383 |
let t2 = thread::spawn(move || { |
| 1384 |
thread::spawn(move || { |
| 1385 |
drop(tx); |
| 1386 |
}); |
| 1387 |
}); |
| 1388 |
ts.push(t2); |
| 1389 |
} |
| 1390 |
for t in ts { |
| 1391 |
t.join().unwrap(); |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
#[test] |
| 1396 |
fn oneshot_multi_thread_send_recv_stress() { |
| 1397 |
let stress_factor = stress_factor(); |
| 1398 |
let mut ts = Vec::with_capacity(stress_factor); |
| 1399 |
for _ in 0..stress_factor { |
| 1400 |
let (tx, rx) = sync_channel::<Box<i32>>(0); |
| 1401 |
let t = thread::spawn(move || { |
| 1402 |
tx.send(Box::new(10)).unwrap(); |
| 1403 |
}); |
| 1404 |
ts.push(t); |
| 1405 |
assert!(*rx.recv().unwrap() == 10); |
| 1406 |
} |
| 1407 |
for t in ts { |
| 1408 |
t.join().unwrap(); |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
#[test] |
| 1413 |
fn stream_send_recv_stress() { |
| 1414 |
let stress_factor = stress_factor(); |
| 1415 |
let mut ts = Vec::with_capacity(2 * stress_factor); |
| 1416 |
for _ in 0..stress_factor { |
| 1417 |
let (tx, rx) = sync_channel::<Box<i32>>(0); |
| 1418 |
|
| 1419 |
if let Some(t) = send(tx, 0) { |
| 1420 |
ts.push(t); |
| 1421 |
} |
| 1422 |
if let Some(t) = recv(rx, 0) { |
| 1423 |
ts.push(t); |
| 1424 |
} |
| 1425 |
|
| 1426 |
fn send(tx: SyncSender<Box<i32>>, i: i32) -> Option<JoinHandle<()>> { |
| 1427 |
if i == 10 { |
| 1428 |
return None; |
| 1429 |
} |
| 1430 |
|
| 1431 |
Some(thread::spawn(move || { |
| 1432 |
tx.send(Box::new(i)).unwrap(); |
| 1433 |
send(tx, i + 1); |
| 1434 |
})) |
| 1435 |
} |
| 1436 |
|
| 1437 |
fn recv(rx: Receiver<Box<i32>>, i: i32) -> Option<JoinHandle<()>> { |
| 1438 |
if i == 10 { |
| 1439 |
return None; |
| 1440 |
} |
| 1441 |
|
| 1442 |
Some(thread::spawn(move || { |
| 1443 |
assert!(*rx.recv().unwrap() == i); |
| 1444 |
recv(rx, i + 1); |
| 1445 |
})) |
| 1446 |
} |
| 1447 |
} |
| 1448 |
for t in ts { |
| 1449 |
t.join().unwrap(); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
#[test] |
| 1454 |
fn recv_a_lot() { |
| 1455 |
|
| 1456 |
let (tx, rx) = sync_channel(10000); |
| 1457 |
for _ in 0..10000 { |
| 1458 |
tx.send(()).unwrap(); |
| 1459 |
} |
| 1460 |
for _ in 0..10000 { |
| 1461 |
rx.recv().unwrap(); |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
#[test] |
| 1466 |
fn shared_chan_stress() { |
| 1467 |
let (tx, rx) = sync_channel(0); |
| 1468 |
let total = stress_factor() + 100; |
| 1469 |
let mut ts = Vec::with_capacity(total); |
| 1470 |
for _ in 0..total { |
| 1471 |
let tx = tx.clone(); |
| 1472 |
let t = thread::spawn(move || { |
| 1473 |
tx.send(()).unwrap(); |
| 1474 |
}); |
| 1475 |
ts.push(t); |
| 1476 |
} |
| 1477 |
|
| 1478 |
for _ in 0..total { |
| 1479 |
rx.recv().unwrap(); |
| 1480 |
} |
| 1481 |
for t in ts { |
| 1482 |
t.join().unwrap(); |
| 1483 |
} |
| 1484 |
} |
| 1485 |
|
| 1486 |
#[test] |
| 1487 |
fn test_nested_recv_iter() { |
| 1488 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1489 |
let (total_tx, total_rx) = sync_channel::<i32>(0); |
| 1490 |
|
| 1491 |
let t = thread::spawn(move || { |
| 1492 |
let mut acc = 0; |
| 1493 |
for x in rx.iter() { |
| 1494 |
acc += x; |
| 1495 |
} |
| 1496 |
total_tx.send(acc).unwrap(); |
| 1497 |
}); |
| 1498 |
|
| 1499 |
tx.send(3).unwrap(); |
| 1500 |
tx.send(1).unwrap(); |
| 1501 |
tx.send(2).unwrap(); |
| 1502 |
drop(tx); |
| 1503 |
assert_eq!(total_rx.recv().unwrap(), 6); |
| 1504 |
t.join().unwrap(); |
| 1505 |
} |
| 1506 |
|
| 1507 |
#[test] |
| 1508 |
fn test_recv_iter_break() { |
| 1509 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1510 |
let (count_tx, count_rx) = sync_channel(0); |
| 1511 |
|
| 1512 |
let t = thread::spawn(move || { |
| 1513 |
let mut count = 0; |
| 1514 |
for x in rx.iter() { |
| 1515 |
if count >= 3 { |
| 1516 |
break; |
| 1517 |
} else { |
| 1518 |
count += x; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
count_tx.send(count).unwrap(); |
| 1522 |
}); |
| 1523 |
|
| 1524 |
tx.send(2).unwrap(); |
| 1525 |
tx.send(2).unwrap(); |
| 1526 |
tx.send(2).unwrap(); |
| 1527 |
let _ = tx.try_send(2); |
| 1528 |
drop(tx); |
| 1529 |
assert_eq!(count_rx.recv().unwrap(), 4); |
| 1530 |
t.join().unwrap(); |
| 1531 |
} |
| 1532 |
|
| 1533 |
#[test] |
| 1534 |
fn try_recv_states() { |
| 1535 |
let (tx1, rx1) = sync_channel::<i32>(1); |
| 1536 |
let (tx2, rx2) = sync_channel::<()>(1); |
| 1537 |
let (tx3, rx3) = sync_channel::<()>(1); |
| 1538 |
let t = thread::spawn(move || { |
| 1539 |
rx2.recv().unwrap(); |
| 1540 |
tx1.send(1).unwrap(); |
| 1541 |
tx3.send(()).unwrap(); |
| 1542 |
rx2.recv().unwrap(); |
| 1543 |
drop(tx1); |
| 1544 |
tx3.send(()).unwrap(); |
| 1545 |
}); |
| 1546 |
|
| 1547 |
assert_eq!(rx1.try_recv(), Err(TryRecvError::Empty)); |
| 1548 |
tx2.send(()).unwrap(); |
| 1549 |
rx3.recv().unwrap(); |
| 1550 |
assert_eq!(rx1.try_recv(), Ok(1)); |
| 1551 |
assert_eq!(rx1.try_recv(), Err(TryRecvError::Empty)); |
| 1552 |
tx2.send(()).unwrap(); |
| 1553 |
rx3.recv().unwrap(); |
| 1554 |
assert_eq!(rx1.try_recv(), Err(TryRecvError::Disconnected)); |
| 1555 |
t.join().unwrap(); |
| 1556 |
} |
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
#[test] |
| 1561 |
fn destroy_upgraded_shared_port_when_sender_still_active() { |
| 1562 |
let (tx, rx) = sync_channel::<()>(0); |
| 1563 |
let (tx2, rx2) = sync_channel::<()>(0); |
| 1564 |
let t = thread::spawn(move || { |
| 1565 |
rx.recv().unwrap(); |
| 1566 |
drop(rx); |
| 1567 |
tx2.send(()).unwrap(); |
| 1568 |
}); |
| 1569 |
|
| 1570 |
for _ in 0..5000 { |
| 1571 |
thread::yield_now(); |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
let tx2 = tx.clone(); |
| 1576 |
drop(tx); |
| 1577 |
tx2.send(()).unwrap(); |
| 1578 |
|
| 1579 |
|
| 1580 |
rx2.recv().unwrap(); |
| 1581 |
t.join().unwrap(); |
| 1582 |
} |
| 1583 |
|
| 1584 |
#[test] |
| 1585 |
fn send1() { |
| 1586 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1587 |
let t = thread::spawn(move || { |
| 1588 |
rx.recv().unwrap(); |
| 1589 |
}); |
| 1590 |
assert_eq!(tx.send(1), Ok(())); |
| 1591 |
t.join().unwrap(); |
| 1592 |
} |
| 1593 |
|
| 1594 |
#[test] |
| 1595 |
fn send2() { |
| 1596 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1597 |
let t = thread::spawn(move || { |
| 1598 |
drop(rx); |
| 1599 |
}); |
| 1600 |
assert!(tx.send(1).is_err()); |
| 1601 |
t.join().unwrap(); |
| 1602 |
} |
| 1603 |
|
| 1604 |
#[test] |
| 1605 |
fn send3() { |
| 1606 |
let (tx, rx) = sync_channel::<i32>(1); |
| 1607 |
assert_eq!(tx.send(1), Ok(())); |
| 1608 |
let t = thread::spawn(move || { |
| 1609 |
drop(rx); |
| 1610 |
}); |
| 1611 |
assert!(tx.send(1).is_err()); |
| 1612 |
t.join().unwrap(); |
| 1613 |
} |
| 1614 |
|
| 1615 |
#[test] |
| 1616 |
fn send4() { |
| 1617 |
let (tx, rx) = sync_channel::<i32>(0); |
| 1618 |
let tx2 = tx.clone(); |
| 1619 |
let (done, donerx) = channel(); |
| 1620 |
let done2 = done.clone(); |
| 1621 |
let t = thread::spawn(move || { |
| 1622 |
assert!(tx.send(1).is_err()); |
| 1623 |
done.send(()).unwrap(); |
| 1624 |
}); |
| 1625 |
let t2 = thread::spawn(move || { |
| 1626 |
assert!(tx2.send(2).is_err()); |
| 1627 |
done2.send(()).unwrap(); |
| 1628 |
}); |
| 1629 |
drop(rx); |
| 1630 |
donerx.recv().unwrap(); |
| 1631 |
donerx.recv().unwrap(); |
| 1632 |
t.join().unwrap(); |
| 1633 |
t2.join().unwrap(); |
| 1634 |
} |
| 1635 |
|
| 1636 |
#[test] |
| 1637 |
fn try_send1() { |
| 1638 |
let (tx, _rx) = sync_channel::<i32>(0); |
| 1639 |
assert_eq!(tx.try_send(1), Err(TrySendError::Full(1))); |
| 1640 |
} |
| 1641 |
|
| 1642 |
#[test] |
| 1643 |
fn try_send2() { |
| 1644 |
let (tx, _rx) = sync_channel::<i32>(1); |
| 1645 |
assert_eq!(tx.try_send(1), Ok(())); |
| 1646 |
assert_eq!(tx.try_send(1), Err(TrySendError::Full(1))); |
| 1647 |
} |
| 1648 |
|
| 1649 |
#[test] |
| 1650 |
fn try_send3() { |
| 1651 |
let (tx, rx) = sync_channel::<i32>(1); |
| 1652 |
assert_eq!(tx.try_send(1), Ok(())); |
| 1653 |
drop(rx); |
| 1654 |
assert_eq!(tx.try_send(1), Err(TrySendError::Disconnected(1))); |
| 1655 |
} |
| 1656 |
|
| 1657 |
#[test] |
| 1658 |
fn issue_15761() { |
| 1659 |
fn repro() { |
| 1660 |
let (tx1, rx1) = sync_channel::<()>(3); |
| 1661 |
let (tx2, rx2) = sync_channel::<()>(3); |
| 1662 |
|
| 1663 |
let _t = thread::spawn(move || { |
| 1664 |
rx1.recv().unwrap(); |
| 1665 |
tx2.try_send(()).unwrap(); |
| 1666 |
}); |
| 1667 |
|
| 1668 |
tx1.try_send(()).unwrap(); |
| 1669 |
rx2.recv().unwrap(); |
| 1670 |
} |
| 1671 |
|
| 1672 |
for _ in 0..100 { |
| 1673 |
repro() |
| 1674 |
} |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
|
| 1679 |
mod select_tests { |
| 1680 |
use super::*; |
| 1681 |
|
| 1682 |
use std::thread; |
| 1683 |
|
| 1684 |
#[test] |
| 1685 |
fn smoke() { |
| 1686 |
let (tx1, rx1) = channel::<i32>(); |
| 1687 |
let (tx2, rx2) = channel::<i32>(); |
| 1688 |
tx1.send(1).unwrap(); |
| 1689 |
select! { |
| 1690 |
foo = rx1.recv() => { assert_eq!(foo.unwrap(), 1); }, |
| 1691 |
_bar = rx2.recv() => { panic!() } |
| 1692 |
} |
| 1693 |
tx2.send(2).unwrap(); |
| 1694 |
select! { |
| 1695 |
_foo = rx1.recv() => { panic!() }, |
| 1696 |
bar = rx2.recv() => { assert_eq!(bar.unwrap(), 2) } |
| 1697 |
} |
| 1698 |
drop(tx1); |
| 1699 |
select! { |
| 1700 |
foo = rx1.recv() => { assert!(foo.is_err()); }, |
| 1701 |
_bar = rx2.recv() => { panic!() } |
| 1702 |
} |
| 1703 |
drop(tx2); |
| 1704 |
select! { |
| 1705 |
bar = rx2.recv() => { assert!(bar.is_err()); } |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
#[test] |
| 1710 |
fn smoke2() { |
| 1711 |
let (_tx1, rx1) = channel::<i32>(); |
| 1712 |
let (_tx2, rx2) = channel::<i32>(); |
| 1713 |
let (_tx3, rx3) = channel::<i32>(); |
| 1714 |
let (_tx4, rx4) = channel::<i32>(); |
| 1715 |
let (tx5, rx5) = channel::<i32>(); |
| 1716 |
tx5.send(4).unwrap(); |
| 1717 |
select! { |
| 1718 |
_foo = rx1.recv() => { panic!("1") }, |
| 1719 |
_foo = rx2.recv() => { panic!("2") }, |
| 1720 |
_foo = rx3.recv() => { panic!("3") }, |
| 1721 |
_foo = rx4.recv() => { panic!("4") }, |
| 1722 |
foo = rx5.recv() => { assert_eq!(foo.unwrap(), 4); } |
| 1723 |
} |
| 1724 |
} |
| 1725 |
|
| 1726 |
#[test] |
| 1727 |
fn closed() { |
| 1728 |
let (_tx1, rx1) = channel::<i32>(); |
| 1729 |
let (tx2, rx2) = channel::<i32>(); |
| 1730 |
drop(tx2); |
| 1731 |
|
| 1732 |
select! { |
| 1733 |
_a1 = rx1.recv() => { panic!() }, |
| 1734 |
a2 = rx2.recv() => { assert!(a2.is_err()); } |
| 1735 |
} |
| 1736 |
} |
| 1737 |
|
| 1738 |
#[test] |
| 1739 |
fn unblocks() { |
| 1740 |
let (tx1, rx1) = channel::<i32>(); |
| 1741 |
let (_tx2, rx2) = channel::<i32>(); |
| 1742 |
let (tx3, rx3) = channel::<i32>(); |
| 1743 |
|
| 1744 |
let t = thread::spawn(move || { |
| 1745 |
for _ in 0..20 { |
| 1746 |
thread::yield_now(); |
| 1747 |
} |
| 1748 |
tx1.send(1).unwrap(); |
| 1749 |
rx3.recv().unwrap(); |
| 1750 |
for _ in 0..20 { |
| 1751 |
thread::yield_now(); |
| 1752 |
} |
| 1753 |
}); |
| 1754 |
|
| 1755 |
select! { |
| 1756 |
a = rx1.recv() => { assert_eq!(a.unwrap(), 1); }, |
| 1757 |
_b = rx2.recv() => { panic!() } |
| 1758 |
} |
| 1759 |
tx3.send(1).unwrap(); |
| 1760 |
select! { |
| 1761 |
a = rx1.recv() => { assert!(a.is_err()) }, |
| 1762 |
_b = rx2.recv() => { panic!() } |
| 1763 |
} |
| 1764 |
t.join().unwrap(); |
| 1765 |
} |
| 1766 |
|
| 1767 |
#[test] |
| 1768 |
fn both_ready() { |
| 1769 |
let (tx1, rx1) = channel::<i32>(); |
| 1770 |
let (tx2, rx2) = channel::<i32>(); |
| 1771 |
let (tx3, rx3) = channel::<()>(); |
| 1772 |
|
| 1773 |
let t = thread::spawn(move || { |
| 1774 |
for _ in 0..20 { |
| 1775 |
thread::yield_now(); |
| 1776 |
} |
| 1777 |
tx1.send(1).unwrap(); |
| 1778 |
tx2.send(2).unwrap(); |
| 1779 |
rx3.recv().unwrap(); |
| 1780 |
}); |
| 1781 |
|
| 1782 |
select! { |
| 1783 |
a = rx1.recv() => { assert_eq!(a.unwrap(), 1); }, |
| 1784 |
a = rx2.recv() => { assert_eq!(a.unwrap(), 2); } |
| 1785 |
} |
| 1786 |
select! { |
| 1787 |
a = rx1.recv() => { assert_eq!(a.unwrap(), 1); }, |
| 1788 |
a = rx2.recv() => { assert_eq!(a.unwrap(), 2); } |
| 1789 |
} |
| 1790 |
assert_eq!(rx1.try_recv(), Err(TryRecvError::Empty)); |
| 1791 |
assert_eq!(rx2.try_recv(), Err(TryRecvError::Empty)); |
| 1792 |
tx3.send(()).unwrap(); |
| 1793 |
t.join().unwrap(); |
| 1794 |
} |
| 1795 |
|
| 1796 |
#[test] |
| 1797 |
fn stress() { |
| 1798 |
const AMT: i32 = 10000; |
| 1799 |
let (tx1, rx1) = channel::<i32>(); |
| 1800 |
let (tx2, rx2) = channel::<i32>(); |
| 1801 |
let (tx3, rx3) = channel::<()>(); |
| 1802 |
|
| 1803 |
let t = thread::spawn(move || { |
| 1804 |
for i in 0..AMT { |
| 1805 |
if i % 2 == 0 { |
| 1806 |
tx1.send(i).unwrap(); |
| 1807 |
} else { |
| 1808 |
tx2.send(i).unwrap(); |
| 1809 |
} |
| 1810 |
rx3.recv().unwrap(); |
| 1811 |
} |
| 1812 |
}); |
| 1813 |
|
| 1814 |
for i in 0..AMT { |
| 1815 |
select! { |
| 1816 |
i1 = rx1.recv() => { assert!(i % 2 == 0 && i == i1.unwrap()); }, |
| 1817 |
i2 = rx2.recv() => { assert!(i % 2 == 1 && i == i2.unwrap()); } |
| 1818 |
} |
| 1819 |
tx3.send(()).unwrap(); |
| 1820 |
} |
| 1821 |
t.join().unwrap(); |
| 1822 |
} |
| 1823 |
|
| 1824 |
#[allow(unused_must_use)] |
| 1825 |
#[test] |
| 1826 |
fn cloning() { |
| 1827 |
let (tx1, rx1) = channel::<i32>(); |
| 1828 |
let (_tx2, rx2) = channel::<i32>(); |
| 1829 |
let (tx3, rx3) = channel::<()>(); |
| 1830 |
|
| 1831 |
let t = thread::spawn(move || { |
| 1832 |
rx3.recv().unwrap(); |
| 1833 |
tx1.clone(); |
| 1834 |
assert_eq!(rx3.try_recv(), Err(TryRecvError::Empty)); |
| 1835 |
tx1.send(2).unwrap(); |
| 1836 |
rx3.recv().unwrap(); |
| 1837 |
}); |
| 1838 |
|
| 1839 |
tx3.send(()).unwrap(); |
| 1840 |
select! { |
| 1841 |
_i1 = rx1.recv() => {}, |
| 1842 |
_i2 = rx2.recv() => panic!() |
| 1843 |
} |
| 1844 |
tx3.send(()).unwrap(); |
| 1845 |
t.join().unwrap(); |
| 1846 |
} |
| 1847 |
|
| 1848 |
#[allow(unused_must_use)] |
| 1849 |
#[test] |
| 1850 |
fn cloning2() { |
| 1851 |
let (tx1, rx1) = channel::<i32>(); |
| 1852 |
let (_tx2, rx2) = channel::<i32>(); |
| 1853 |
let (tx3, rx3) = channel::<()>(); |
| 1854 |
|
| 1855 |
let t = thread::spawn(move || { |
| 1856 |
rx3.recv().unwrap(); |
| 1857 |
tx1.clone(); |
| 1858 |
assert_eq!(rx3.try_recv(), Err(TryRecvError::Empty)); |
| 1859 |
tx1.send(2).unwrap(); |
| 1860 |
rx3.recv().unwrap(); |
| 1861 |
}); |
| 1862 |
|
| 1863 |
tx3.send(()).unwrap(); |
| 1864 |
select! { |
| 1865 |
_i1 = rx1.recv() => {}, |
| 1866 |
_i2 = rx2.recv() => panic!() |
| 1867 |
} |
| 1868 |
tx3.send(()).unwrap(); |
| 1869 |
t.join().unwrap(); |
| 1870 |
} |
| 1871 |
|
| 1872 |
#[test] |
| 1873 |
fn cloning3() { |
| 1874 |
let (tx1, rx1) = channel::<()>(); |
| 1875 |
let (tx2, rx2) = channel::<()>(); |
| 1876 |
let (tx3, rx3) = channel::<()>(); |
| 1877 |
let t = thread::spawn(move || { |
| 1878 |
select! { |
| 1879 |
_ = rx1.recv() => panic!(), |
| 1880 |
_ = rx2.recv() => {} |
| 1881 |
} |
| 1882 |
tx3.send(()).unwrap(); |
| 1883 |
}); |
| 1884 |
|
| 1885 |
for _ in 0..1000 { |
| 1886 |
thread::yield_now(); |
| 1887 |
} |
| 1888 |
drop(tx1.clone()); |
| 1889 |
tx2.send(()).unwrap(); |
| 1890 |
rx3.recv().unwrap(); |
| 1891 |
t.join().unwrap(); |
| 1892 |
} |
| 1893 |
|
| 1894 |
#[test] |
| 1895 |
fn preflight1() { |
| 1896 |
let (tx, rx) = channel(); |
| 1897 |
tx.send(()).unwrap(); |
| 1898 |
select! { |
| 1899 |
_n = rx.recv() => {} |
| 1900 |
} |
| 1901 |
} |
| 1902 |
|
| 1903 |
#[test] |
| 1904 |
fn preflight2() { |
| 1905 |
let (tx, rx) = channel(); |
| 1906 |
tx.send(()).unwrap(); |
| 1907 |
tx.send(()).unwrap(); |
| 1908 |
select! { |
| 1909 |
_n = rx.recv() => {} |
| 1910 |
} |
| 1911 |
} |
| 1912 |
|
| 1913 |
#[test] |
| 1914 |
fn preflight3() { |
| 1915 |
let (tx, rx) = channel(); |
| 1916 |
drop(tx.clone()); |
| 1917 |
tx.send(()).unwrap(); |
| 1918 |
select! { |
| 1919 |
_n = rx.recv() => {} |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
#[test] |
| 1924 |
fn preflight4() { |
| 1925 |
let (tx, rx) = channel(); |
| 1926 |
tx.send(()).unwrap(); |
| 1927 |
select! { |
| 1928 |
_ = rx.recv() => {} |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
#[test] |
| 1933 |
fn preflight5() { |
| 1934 |
let (tx, rx) = channel(); |
| 1935 |
tx.send(()).unwrap(); |
| 1936 |
tx.send(()).unwrap(); |
| 1937 |
select! { |
| 1938 |
_ = rx.recv() => {} |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
#[test] |
| 1943 |
fn preflight6() { |
| 1944 |
let (tx, rx) = channel(); |
| 1945 |
drop(tx.clone()); |
| 1946 |
tx.send(()).unwrap(); |
| 1947 |
select! { |
| 1948 |
_ = rx.recv() => {} |
| 1949 |
} |
| 1950 |
} |
| 1951 |
|
| 1952 |
#[test] |
| 1953 |
fn preflight7() { |
| 1954 |
let (tx, rx) = channel::<()>(); |
| 1955 |
drop(tx); |
| 1956 |
select! { |
| 1957 |
_ = rx.recv() => {} |
| 1958 |
} |
| 1959 |
} |
| 1960 |
|
| 1961 |
#[test] |
| 1962 |
fn preflight8() { |
| 1963 |
let (tx, rx) = channel(); |
| 1964 |
tx.send(()).unwrap(); |
| 1965 |
drop(tx); |
| 1966 |
rx.recv().unwrap(); |
| 1967 |
select! { |
| 1968 |
_ = rx.recv() => {} |
| 1969 |
} |
| 1970 |
} |
| 1971 |
|
| 1972 |
#[test] |
| 1973 |
fn preflight9() { |
| 1974 |
let (tx, rx) = channel(); |
| 1975 |
drop(tx.clone()); |
| 1976 |
tx.send(()).unwrap(); |
| 1977 |
drop(tx); |
| 1978 |
rx.recv().unwrap(); |
| 1979 |
select! { |
| 1980 |
_ = rx.recv() => {} |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
#[test] |
| 1985 |
fn oneshot_data_waiting() { |
| 1986 |
let (tx1, rx1) = channel(); |
| 1987 |
let (tx2, rx2) = channel(); |
| 1988 |
let t = thread::spawn(move || { |
| 1989 |
select! { |
| 1990 |
_n = rx1.recv() => {} |
| 1991 |
} |
| 1992 |
tx2.send(()).unwrap(); |
| 1993 |
}); |
| 1994 |
|
| 1995 |
for _ in 0..100 { |
| 1996 |
thread::yield_now() |
| 1997 |
} |
| 1998 |
tx1.send(()).unwrap(); |
| 1999 |
rx2.recv().unwrap(); |
| 2000 |
t.join().unwrap(); |
| 2001 |
} |
| 2002 |
|
| 2003 |
#[test] |
| 2004 |
fn stream_data_waiting() { |
| 2005 |
let (tx1, rx1) = channel(); |
| 2006 |
let (tx2, rx2) = channel(); |
| 2007 |
tx1.send(()).unwrap(); |
| 2008 |
tx1.send(()).unwrap(); |
| 2009 |
rx1.recv().unwrap(); |
| 2010 |
rx1.recv().unwrap(); |
| 2011 |
let t = thread::spawn(move || { |
| 2012 |
select! { |
| 2013 |
_n = rx1.recv() => {} |
| 2014 |
} |
| 2015 |
tx2.send(()).unwrap(); |
| 2016 |
}); |
| 2017 |
|
| 2018 |
for _ in 0..100 { |
| 2019 |
thread::yield_now() |
| 2020 |
} |
| 2021 |
tx1.send(()).unwrap(); |
| 2022 |
rx2.recv().unwrap(); |
| 2023 |
t.join().unwrap(); |
| 2024 |
} |
| 2025 |
|
| 2026 |
#[test] |
| 2027 |
fn shared_data_waiting() { |
| 2028 |
let (tx1, rx1) = channel(); |
| 2029 |
let (tx2, rx2) = channel(); |
| 2030 |
drop(tx1.clone()); |
| 2031 |
tx1.send(()).unwrap(); |
| 2032 |
rx1.recv().unwrap(); |
| 2033 |
let t = thread::spawn(move || { |
| 2034 |
select! { |
| 2035 |
_n = rx1.recv() => {} |
| 2036 |
} |
| 2037 |
tx2.send(()).unwrap(); |
| 2038 |
}); |
| 2039 |
|
| 2040 |
for _ in 0..100 { |
| 2041 |
thread::yield_now() |
| 2042 |
} |
| 2043 |
tx1.send(()).unwrap(); |
| 2044 |
rx2.recv().unwrap(); |
| 2045 |
t.join().unwrap(); |
| 2046 |
} |
| 2047 |
|
| 2048 |
#[test] |
| 2049 |
fn sync1() { |
| 2050 |
let (tx, rx) = sync_channel::<i32>(1); |
| 2051 |
tx.send(1).unwrap(); |
| 2052 |
select! { |
| 2053 |
n = rx.recv() => { assert_eq!(n.unwrap(), 1); } |
| 2054 |
} |
| 2055 |
} |
| 2056 |
|
| 2057 |
#[test] |
| 2058 |
fn sync2() { |
| 2059 |
let (tx, rx) = sync_channel::<i32>(0); |
| 2060 |
let t = thread::spawn(move || { |
| 2061 |
for _ in 0..100 { |
| 2062 |
thread::yield_now() |
| 2063 |
} |
| 2064 |
tx.send(1).unwrap(); |
| 2065 |
}); |
| 2066 |
select! { |
| 2067 |
n = rx.recv() => { assert_eq!(n.unwrap(), 1); } |
| 2068 |
} |
| 2069 |
t.join().unwrap(); |
| 2070 |
} |
| 2071 |
|
| 2072 |
#[test] |
| 2073 |
fn sync3() { |
| 2074 |
let (tx1, rx1) = sync_channel::<i32>(0); |
| 2075 |
let (tx2, rx2): (Sender<i32>, Receiver<i32>) = channel(); |
| 2076 |
let t = thread::spawn(move || { |
| 2077 |
tx1.send(1).unwrap(); |
| 2078 |
}); |
| 2079 |
let t2 = thread::spawn(move || { |
| 2080 |
tx2.send(2).unwrap(); |
| 2081 |
}); |
| 2082 |
select! { |
| 2083 |
n = rx1.recv() => { |
| 2084 |
let n = n.unwrap(); |
| 2085 |
assert_eq!(n, 1); |
| 2086 |
assert_eq!(rx2.recv().unwrap(), 2); |
| 2087 |
}, |
| 2088 |
n = rx2.recv() => { |
| 2089 |
let n = n.unwrap(); |
| 2090 |
assert_eq!(n, 2); |
| 2091 |
assert_eq!(rx1.recv().unwrap(), 1); |
| 2092 |
} |
| 2093 |
} |
| 2094 |
t.join().unwrap(); |
| 2095 |
t2.join().unwrap(); |
| 2096 |
} |
| 2097 |
} |