use meslin::*; /// Example protocol that can be used #[derive(Debug, Message, From, TryInto)] pub enum MyProtocol { A(u32), B(HelloWorld), C(Request), } #[derive(Debug, Message, From)] #[from(forward)] pub struct HelloWorld(pub String); #[tokio::test] async fn test_basic_protocol_sending() { let (sender, receiver) = mpmc::unbounded::(); tokio::task::spawn(async move { assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::A(1) )); assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::B(_) )); assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::C(_) )); assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::A(4) )); assert!(receiver.recv_async().await.is_err()); }); sender.send::(MyProtocol::A(1)).await.unwrap(); sender .send::(MyProtocol::B(HelloWorld("hello".to_string()))) .await .unwrap(); let (request, _rx) = Request::new(10); sender .try_send::(MyProtocol::C(request)) .unwrap(); sender.try_send::(MyProtocol::A(4)).unwrap(); drop(sender); } #[tokio::test] async fn test_basic_msg_sending() { let (sender, receiver) = mpmc::unbounded::(); tokio::task::spawn(async move { assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::A(1) )); assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::B(_) )); assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::C(_) )); }); sender.send_msg(1u32).await.unwrap(); sender .send_msg(HelloWorld("hello".to_string())) .await .unwrap(); let (request, _rx) = Request::new(10); sender.try_send_msg(request).unwrap(); drop(sender); } #[tokio::test] async fn test_basic_sending() { let (sender, receiver) = mpmc::unbounded::(); tokio::task::spawn(async move { assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::A(1) )); assert!(matches!( receiver.recv_async().await.unwrap(), MyProtocol::B(_) )); let MyProtocol::C(Request { msg, tx }) = receiver.recv_async().await.unwrap() else { unreachable!() }; tx.send(format!("Your number was: {msg}")).unwrap(); }); sender.send::(1u32).await.unwrap(); sender.send::("hello").await.unwrap(); let reply = sender .send::>(10u32) .await .unwrap() .await .unwrap(); assert_eq!(reply, "Your number was: 10"); } #[tokio::test] async fn priority() { let (tx, rx) = priority::unbounded::(); tx.send::(0u32).await.unwrap(); tx.send_with::(1u32, 2).await.unwrap(); tx.send_with::("hello", 3).await.unwrap(); assert!(matches!(rx.recv().await.unwrap(), (MyProtocol::B(_), _))); assert!(matches!(rx.recv().await.unwrap(), (MyProtocol::A(1), _))); assert!(matches!(rx.recv().await.unwrap(), (MyProtocol::A(0), _))); }