use aktrs::actor::{self, testkit::ActorTestKit, Behavior, Behaviors, Pid, SpawnOptions}; use std::time::Duration; #[aktrs::test] pub fn single_timer_ok(mut t: ActorTestKit) -> aktrs::Result<()> { let mut probe = t.create_anonymous_test_probe(); let pid = probe.pid(); let actor = Behaviors::with_context(move |cx| { cx.timers().start_single_timer(Duration::from_millis(50), || "hello, world!"); let mut pid = pid.clone(); let b = Behaviors::receive_message(move |_cx, msg: &'static str| { let _ = pid.try_tell(msg)?; Ok(Behaviors::same()) }); Ok(b.into()) }); t.spawn(actor, SpawnOptions::default()); probe.expect_message("hello, world!"); probe.expect_no_message(); Ok(()) } #[aktrs::test] pub fn single_timer_cancel(mut t: ActorTestKit) -> aktrs::Result<()> { let mut probe = t.create_anonymous_test_probe(); let pid = probe.pid(); let actor = Behaviors::with_context(move |cx| { let key = cx.timers().start_single_timer(Duration::from_millis(50), || "hello, world!"); cx.timers().cancel(&key); let mut pid = pid.clone(); let b = Behaviors::receive_message(move |_cx, msg: &'static str| { let _ = pid.try_tell(msg)?; Ok(Behaviors::same()) }); Ok(b.into()) }); t.spawn(actor, SpawnOptions::default()); probe.expect_no_message(); Ok(()) } #[aktrs::test] pub fn single_timer_cancel_all(mut t: ActorTestKit) -> aktrs::Result<()> { let mut probe = t.create_anonymous_test_probe(); let pid = probe.pid(); let actor = Behaviors::with_context(move |cx| { cx.timers().start_single_timer(Duration::from_millis(50), || "hello, world!"); cx.timers().start_single_timer(Duration::from_millis(50), || "hello, world!"); cx.timers().start_single_timer(Duration::from_millis(50), || "hello, world!"); cx.timers().cancel_all(); let mut pid = pid.clone(); let b = Behaviors::receive_message(move |_cx, msg: &'static str| { let _ = pid.try_tell(msg)?; Ok(Behaviors::same()) }); Ok(b.into()) }); t.spawn(actor, SpawnOptions::default()); probe.expect_no_message(); Ok(()) } #[aktrs::test] pub fn periodic_timer_ok(mut t: ActorTestKit) -> aktrs::Result<()> { let mut probe = t.create_anonymous_test_probe(); let pid = probe.pid(); let actor = Behaviors::with_context(move |cx| { cx.timers().start_periodic_timer(Duration::from_millis(50), || "hello, world!"); let mut pid = pid.clone(); let b = Behaviors::receive_message(move |_cx, msg: &'static str| { let _ = pid.try_tell(msg)?; Ok(Behaviors::same()) }); Ok(b.into()) }); t.spawn(actor, SpawnOptions::default()); probe.expect_message("hello, world!"); probe.expect_message("hello, world!"); probe.expect_message("hello, world!"); probe.expect_message("hello, world!"); probe.expect_message("hello, world!"); Ok(()) } #[aktrs::test] pub fn periodic_timer_cancel(mut t: ActorTestKit) -> aktrs::Result<()> { let mut probe = t.create_anonymous_test_probe(); let pid = probe.pid(); let actor = Behaviors::with_context(move |cx| { let key = cx.timers().start_periodic_timer(Duration::from_millis(50), || "hello, world!"); fn inner( pid: Pid<&'static str>, key: actor::time::Key, count: usize, ) -> Behavior<&'static str> { fn core( mut pid: Pid<&'static str>, key: actor::time::Key, count: usize, ) -> Behavior<&'static str> { Behaviors::receive_message(move |_cx, msg: &'static str| { let _ = pid.try_tell(msg); Ok(inner(pid.clone(), key.clone(), count + 1)) }) .into() } if count == 3 { Behaviors::with_context(move |cx| { cx.timers().cancel(&key); Ok(core(pid, key, count)) }) .into() } else { core(pid, key, count) } } Ok(inner(pid, key, 0)) }); t.spawn(actor, SpawnOptions::default()); probe.expect_message("hello, world!"); probe.expect_message("hello, world!"); probe.expect_message("hello, world!"); probe.expect_no_message(); Ok(()) }