use message_channel::Channel; use std::time::Duration; /// Tests basic send and receive functionality with multiple messages /// across threads. #[test] fn send_receive() { // Create channel let (sender, receiver) = Channel::create(); // Test data let expected = [42, 100, -10]; // Spawn sender thread std::thread::spawn(move || { std::thread::sleep(Duration::from_millis(10)); for message in expected { let _ = sender.send(message); std::thread::sleep(Duration::from_millis(100)); } }); // Receive messages let mut received_messages = Vec::new(); while received_messages.len() < expected.len() { while let Some(message) = receiver.try_recv() { received_messages.push(message); } } assert_eq!(received_messages.len(), expected.len()); assert_eq!(received_messages, expected); } /// Tests receiving from an empty channel. #[test] fn receive_empty_channel() { let (_, receiver) = Channel::::create(); assert!(receiver.recv().is_err()); } /// Tests concurrent send and receive operations. #[test] fn concurrent_send_receive() { let (sender, receiver) = Channel::create(); let sender_thread = std::thread::spawn(move || { for i in 0..10 { sender.send(i).unwrap(); std::thread::sleep(Duration::from_millis(10)); } }); let receiver_thread = std::thread::spawn(move || { let mut received_messages = Vec::new(); while received_messages.len() < 10 { if let Ok(message) = receiver.recv() { received_messages.push(message); } } received_messages }); sender_thread.join().unwrap(); let received_messages = receiver_thread.join().unwrap(); assert_eq!(received_messages.len(), 10); assert_eq!(received_messages, (0..10).collect::>()); }