mod sync_vec; use async_trait::async_trait; use speare::{Ctx, Node, Process}; use sync_vec::SyncVec; use tokio::task; #[derive(Debug, PartialEq, Clone, Copy)] enum TestMsg { Foo, Bar, } struct Foo; #[async_trait] impl Process for Foo { type Props = SyncVec; type Msg = TestMsg; type Err = (); async fn init(_: &mut Ctx) -> Result { Ok(Foo) } async fn handle(&mut self, msg: Self::Msg, ctx: &mut Ctx) -> Result<(), Self::Err> { ctx.props().push(msg).await; Ok(()) } } struct Bar; #[async_trait] impl Process for Bar { type Props = SyncVec; type Msg = TestMsg; type Err = (); async fn init(_: &mut Ctx) -> Result { Ok(Bar) } async fn handle(&mut self, msg: Self::Msg, ctx: &mut Ctx) -> Result<(), Self::Err> { ctx.props().push(msg).await; Ok(()) } } #[allow(clippy::disallowed_names)] #[tokio::test] async fn sends_msgs_in_correct_order() { // Arrange let mut node = Node::default(); let recvd: SyncVec<_> = Default::default(); let foo = node.spawn::(recvd.clone()); let bar = node.spawn::(recvd.clone()); // Act foo.send(TestMsg::Foo); bar.send(TestMsg::Bar); task::yield_now().await; // Assert assert_eq!(vec![TestMsg::Foo, TestMsg::Bar], recvd.clone_vec().await) }