extern crate funnel; use std::thread; use funnel::Funnel; #[test] fn recv_from_threads() { let mut fun = Funnel::new(); let writer1 = fun.add_receiver(); let writer2 = fun.add_receiver(); thread::spawn(move || { let _ = writer1.send(32).unwrap(); }); thread::spawn(move || { let _ = writer2.send(64).unwrap(); }); assert!(match fun.recv() { (Some(read_value), errors) => read_value == 32 && errors.len() == 0, _ => false, }); assert!(match fun.recv() { (Some(read_value), errors) => read_value == 64 && errors.len() == 0, _ => false, }); } #[test] fn recv_from_multiple_writes() { let mut fun = Funnel::new(); let writer1 = fun.add_receiver(); let writer2 = fun.add_receiver(); thread::spawn(move || { let _ = writer1.send(32).unwrap(); let _ = writer1.send(33).unwrap(); }); thread::spawn(move || { let _ = writer2.send(64).unwrap(); let _ = writer2.send(65).unwrap(); }); assert!(match fun.recv() { (Some(read_value), errors) => read_value == 32 && errors.len() == 0, _ => false, }); assert!(match fun.recv() { (Some(read_value), errors) => read_value == 64 && errors.len() == 0, _ => false, }); assert!(match fun.recv() { (Some(read_value), errors) => read_value == 33 && errors.len() == 0, _ => false, }); assert!(match fun.recv() { (Some(read_value), errors) => read_value == 65 && errors.len() == 0, _ => false, }); } #[test] fn error_instead_of_out_of_bounds() { let mut fun: Funnel = Funnel::new(); assert!(match fun.recv() { (None, errors) => errors.len() == 1 && errors[0].1 == 0, // no index _ => false, }); }