mpsc.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. extern crate ralloc;
  2. mod util;
  3. use std::sync::mpsc;
  4. use std::thread;
  5. #[test]
  6. fn mpsc_queue() {
  7. util::multiply(|| {
  8. {
  9. let (tx, rx) = mpsc::channel::<Box<u64>>();
  10. let handle = thread::spawn(move || {
  11. util::acid(|| {
  12. tx.send(Box::new(0xBABAFBABAF)).unwrap();
  13. tx.send(Box::new(0xDEADBEAF)).unwrap();
  14. tx.send(Box::new(0xDECEA5E)).unwrap();
  15. tx.send(Box::new(0xDEC1A551F1E5)).unwrap();
  16. });
  17. });
  18. assert_eq!(*rx.recv().unwrap(), 0xBABAFBABAF);
  19. assert_eq!(*rx.recv().unwrap(), 0xDEADBEAF);
  20. assert_eq!(*rx.recv().unwrap(), 0xDECEA5E);
  21. assert_eq!(*rx.recv().unwrap(), 0xDEC1A551F1E5);
  22. handle.join().unwrap();
  23. }
  24. let (tx, rx) = mpsc::channel();
  25. let mut handles = Vec::new();
  26. for _ in 0..10 {
  27. util::acid(|| {
  28. let tx = tx.clone();
  29. handles.push(thread::spawn(move || {
  30. tx.send(Box::new(0xFA11BAD)).unwrap();
  31. }));
  32. });
  33. }
  34. for _ in 0..10 {
  35. assert_eq!(*rx.recv().unwrap(), 0xFA11BAD);
  36. }
  37. for i in handles {
  38. i.join().unwrap()
  39. }
  40. });
  41. }