use improv::*; #[tokio::test] async fn ask() { #[derive(Default)] struct A(usize); impl Actor for A {} #[async_trait] impl Receive for A { async fn receive(&mut self, _: M, cx: &mut Context) -> usize { self.0 += 1; if self.0 == 3 { cx.stop(); } self.0 } } struct M; impl Message for M { type Response = usize; } let addr = improv::spawn(|_| A::default()); assert_eq!(addr.ask(M).await, Ok(1)); assert_eq!(addr.ask(M).await, Ok(2)); assert_eq!(addr.ask(M).await, Ok(3)); assert!(addr.ask(M).await.is_err()); } #[cfg(feature = "test-util")] #[tokio::test] async fn probe_ok() { struct A; impl Actor for A {} #[async_trait] impl Receive> for A where T: Receive, { async fn receive(&mut self, M(addr): M, _: &mut Context) { let _ = addr.tell(Noop); } } struct B; impl Actor for B {} #[async_trait] impl Receive for B { async fn receive(&mut self, _: Noop, _: &mut Context) {} } struct M(Addr); impl Message for M { type Response = (); } struct Noop; impl Message for Noop { type Response = (); } let addr = improv::spawn(|_| A); let mut probe = Probe::::new(); assert!(addr.tell(M(probe.addr())).is_ok()); probe.receive::().await; } #[cfg(feature = "test-util")] #[tokio::test] #[should_panic] async fn probe_err() { struct A; impl Actor for A {} #[async_trait] impl Receive> for A where T: Receive, { async fn receive(&mut self, M(addr): M, _: &mut Context) { let _ = addr.tell(Noop); } } struct B; impl Actor for B {} #[async_trait] impl Receive for B { async fn receive(&mut self, _: Noop, _: &mut Context) {} } #[async_trait] impl Receive for B { async fn receive(&mut self, _: UhOh, _: &mut Context) {} } struct M(Addr); impl Message for M { type Response = (); } struct Noop; impl Message for Noop { type Response = (); } struct UhOh; impl Message for UhOh { type Response = (); } let addr = improv::spawn(|_| A); let mut probe = Probe::::new(); assert!(addr.tell(M(probe.addr())).is_ok()); probe.receive::().await; }