mpsc.rs 1.4 KB

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