diff --git a/Cargo.toml b/Cargo.toml index 51229035..cdf298c1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ slog-stdlog = "4.0" slog-scope = "4.3.0" num_cpus = "1.13.0" dashmap = "3" -tokio = { version = "^1", features = ["rt-multi-thread", "macros"] } +tokio = { version = "^1", features = ["rt-multi-thread", "macros", "time"] } [dev-dependencies] diff --git a/riker-macros/Cargo.toml b/riker-macros/Cargo.toml index 6ffd84c6..5c4157f7 100644 --- a/riker-macros/Cargo.toml +++ b/riker-macros/Cargo.toml @@ -20,3 +20,4 @@ proc-macro2 = "1.0" [dev-dependencies] riker = { path = ".." } +tokio = { version = "^1", features = ["rt-multi-thread", "macros", "time"] } diff --git a/riker-macros/tests/macro.rs b/riker-macros/tests/macro.rs index 4f5b4966..ee389f67 100644 --- a/riker-macros/tests/macro.rs +++ b/riker-macros/tests/macro.rs @@ -33,8 +33,8 @@ impl Receive for NewActor { } } -#[test] -fn run_derived_actor() { +#[tokio::test] +async fn run_derived_actor() { let sys = ActorSystem::new().unwrap(); let act = sys.actor_of::("act").unwrap(); @@ -45,7 +45,7 @@ fn run_derived_actor() { // wait until all direct children of the user root are terminated while sys.user_root().has_children() { // in order to lower cpu usage, sleep here - std::thread::sleep(std::time::Duration::from_millis(50)); + tokio::time::sleep(std::time::Duration::from_millis(50)).await; } } @@ -80,8 +80,8 @@ impl Receive for GenericActor>("act").unwrap(); @@ -92,7 +92,7 @@ fn run_derived_generic_actor() { // wait until all direct children of the user root are terminated while sys.user_root().has_children() { // in order to lower cpu usage, sleep here - std::thread::sleep(std::time::Duration::from_millis(50)); + tokio::time::sleep(std::time::Duration::from_millis(50)).await; } } @@ -131,8 +131,8 @@ impl Receive> for GenericMsgActor { } } -#[test] -fn run_generic_message_actor() { +#[tokio::test] +async fn run_generic_message_actor() { let sys = ActorSystem::new().unwrap(); let act = sys.actor_of::("act").unwrap(); @@ -145,7 +145,7 @@ fn run_generic_message_actor() { // wait until all direct children of the user root are terminated while sys.user_root().has_children() { // in order to lower cpu usage, sleep here - std::thread::sleep(std::time::Duration::from_millis(50)); + tokio::time::sleep(std::time::Duration::from_millis(50)).await; } } @@ -202,8 +202,8 @@ impl Receive for PathMsgActor { } } -#[test] -fn run_path_message_actor() { +#[tokio::test] +async fn run_path_message_actor() { let sys = ActorSystem::new().unwrap(); let act = sys.actor_of::("act").unwrap(); @@ -219,6 +219,6 @@ fn run_path_message_actor() { // wait until all direct children of the user root are terminated while sys.user_root().has_children() { // in order to lower cpu usage, sleep here - std::thread::sleep(std::time::Duration::from_millis(50)); + tokio::time::sleep(std::time::Duration::from_millis(50)).await; } } diff --git a/src/actor.rs b/src/actor.rs index b503439a..64508b6c 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -255,12 +255,14 @@ impl Actor for Box { /// } /// } /// -/// // main -/// let sys = ActorSystem::new().unwrap(); -/// let actor = sys.actor_of::("my-actor").unwrap(); +/// #[tokio::main] +/// async fn main() { +/// let sys = ActorSystem::new().unwrap(); +/// let actor = sys.actor_of::("my-actor").unwrap(); /// -/// actor.tell(Foo, None); -/// actor.tell(Bar, None); +/// actor.tell(Foo, None); +/// actor.tell(Bar, None); +/// } /// ``` pub trait Receive { type Msg: Message; diff --git a/src/actor/props.rs b/src/actor/props.rs index da9615d6..598db3da 100644 --- a/src/actor/props.rs +++ b/src/actor/props.rs @@ -36,13 +36,16 @@ impl Props { /// # type Msg = String; /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} /// # } - /// // main - /// let sys = ActorSystem::new().unwrap(); /// - /// let props = Props::new_from(User::actor); + /// #[tokio::main] + /// async fn main() { + /// let sys = ActorSystem::new().unwrap(); /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("user", props).unwrap(); + /// let props = Props::new_from(User::actor); + /// + /// // start the actor and get an `ActorRef` + /// let actor = sys.actor_of_props("user", props).unwrap(); + /// } /// ``` #[inline] pub fn new_from(creator: F) -> Arc>> @@ -76,12 +79,15 @@ impl Props { /// # type Msg = String; /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} /// # } - /// // main - /// let sys = ActorSystem::new().unwrap(); /// - /// let props = Props::new_from_args(User::actor, "Naomi Nagata".into()); + /// #[tokio::main] + /// async fn main() { + /// let sys = ActorSystem::new().unwrap(); /// - /// let actor = sys.actor_of_props("user", props).unwrap(); + /// let props = Props::new_from_args(User::actor, "Naomi Nagata".into()); + /// + /// let actor = sys.actor_of_props("user", props).unwrap(); + /// } /// ``` /// An actor requiring multiple parameters. /// ``` @@ -105,14 +111,17 @@ impl Props { /// # type Msg = String; /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} /// # } - /// // main - /// let sys = ActorSystem::new().unwrap(); /// - /// let props = Props::new_from_args(BankAccount::actor, - /// ("James Holden".into(), "12345678".into())); + /// #[tokio::main] + /// async fn main() { + /// let sys = ActorSystem::new().unwrap(); /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("bank_account", props).unwrap(); + /// let props = Props::new_from_args(BankAccount::actor, + /// ("James Holden".into(), "12345678".into())); + /// + /// // start the actor and get an `ActorRef` + /// let actor = sys.actor_of_props("bank_account", props).unwrap(); + /// } /// ``` #[inline] pub fn new_from_args( @@ -141,13 +150,16 @@ impl Props { /// # type Msg = String; /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} /// # } - /// // main - /// let sys = ActorSystem::new().unwrap(); /// - /// let props = Props::new::(); + /// #[tokio::main] + /// async fn main() { + /// let sys = ActorSystem::new().unwrap(); + /// + /// let props = Props::new::(); /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("user", props).unwrap(); + /// // start the actor and get an `ActorRef` + /// let actor = sys.actor_of_props("user", props).unwrap(); + /// } /// ``` /// Creates an `ActorProducer` from a type which implements ActorFactory with no factory method parameters. /// @@ -168,13 +180,16 @@ impl Props { /// # type Msg = String; /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} /// # } - /// // main - /// let sys = ActorSystem::new().unwrap(); /// - /// let props = Props::new::(); + /// #[tokio::main] + /// async fn main() { + /// let sys = ActorSystem::new().unwrap(); + /// + /// let props = Props::new::(); /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("user", props).unwrap(); + /// // start the actor and get an `ActorRef` + /// let actor = sys.actor_of_props("user", props).unwrap(); + /// } /// ``` #[inline] pub fn new() -> Arc>> @@ -207,12 +222,15 @@ impl Props { /// # type Msg = String; /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} /// # } - /// // main - /// let sys = ActorSystem::new().unwrap(); /// - /// let props = Props::new_args::("Naomi Nagata".into()); + /// #[tokio::main] + /// async fn main() { + /// let sys = ActorSystem::new().unwrap(); + /// + /// let props = Props::new_args::("Naomi Nagata".into()); /// - /// let actor = sys.actor_of_props("user", props).unwrap(); + /// let actor = sys.actor_of_props("user", props).unwrap(); + /// } /// ``` /// An actor requiring multiple parameters. /// ``` @@ -236,14 +254,17 @@ impl Props { /// # type Msg = String; /// # fn recv(&mut self, _ctx: &Context, _msg: String, _sender: Sender) {} /// # } - /// // main - /// let sys = ActorSystem::new().unwrap(); /// - /// let props = Props::new_from_args(BankAccount::create_args, - /// ("James Holden".into(), "12345678".into())); + /// #[tokio::main] + /// async fn main() { + /// let sys = ActorSystem::new().unwrap(); + /// + /// let props = Props::new_from_args(BankAccount::create_args, + /// ("James Holden".into(), "12345678".into())); /// - /// // start the actor and get an `ActorRef` - /// let actor = sys.actor_of_props("bank_account", props).unwrap(); + /// // start the actor and get an `ActorRef` + /// let actor = sys.actor_of_props("bank_account", props).unwrap(); + /// } /// ``` #[inline] pub fn new_args(args: Args) -> Arc>> diff --git a/tests/actors.rs b/tests/actors.rs index 6cdfa5ea..b6aa28f9 100644 --- a/tests/actors.rs +++ b/tests/actors.rs @@ -1,8 +1,16 @@ use riker::actors::*; -use riker_testkit::probe::channel::{probe, ChannelProbe}; -use riker_testkit::probe::Probe; -use riker_testkit::test_fn; +use riker_testkit::{ + p_assert_eq, + probe::{ + Probe, + ProbeReceive, + channel::{ + ChannelProbe, + probe, + }, + }, +}; #[derive(Clone, Debug)] pub struct Add; @@ -45,75 +53,71 @@ impl Receive for Counter { } } -test_fn! { - fn actor_create() { - let sys = ActorSystem::new().unwrap(); - - assert!(sys.actor_of::("valid-name").is_ok()); - match sys.actor_of::("/") { - Ok(_) => panic!("test should not reach here"), - Err(e) => { - // test Display - assert_eq!( - e.to_string(), - "Failed to create actor. Cause: Invalid actor name (/)" - ); - assert_eq!( - format!("{}", e), - "Failed to create actor. Cause: Invalid actor name (/)" - ); - // test Debug - assert_eq!(format!("{:?}", e), "InvalidName(\"/\")"); - assert_eq!(format!("{:#?}", e), "InvalidName(\n \"/\",\n)"); - } +#[tokio::test] +async fn actor_create() { + let sys = ActorSystem::new().unwrap(); + + assert!(sys.actor_of::("valid-name").is_ok()); + match sys.actor_of::("/") { + Ok(_) => panic!("test should not reach here"), + Err(e) => { + // test Display + assert_eq!( + e.to_string(), + "Failed to create actor. Cause: Invalid actor name (/)" + ); + assert_eq!( + format!("{}", e), + "Failed to create actor. Cause: Invalid actor name (/)" + ); + // test Debug + assert_eq!(format!("{:?}", e), "InvalidName(\"/\")"); + assert_eq!(format!("{:#?}", e), "InvalidName(\n \"/\",\n)"); } - assert!(sys.actor_of::("*").is_err()); - assert!(sys.actor_of::("/a/b/c").is_err()); - assert!(sys.actor_of::("@").is_err()); - assert!(sys.actor_of::("#").is_err()); - assert!(sys.actor_of::("abc*").is_err()); - assert!(sys.actor_of::("!").is_err()); } + assert!(sys.actor_of::("*").is_err()); + assert!(sys.actor_of::("/a/b/c").is_err()); + assert!(sys.actor_of::("@").is_err()); + assert!(sys.actor_of::("#").is_err()); + assert!(sys.actor_of::("abc*").is_err()); + assert!(sys.actor_of::("!").is_err()); } -test_fn! { - fn actor_tell() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn actor_tell() { + let sys = ActorSystem::new().unwrap(); - let actor = sys.actor_of::("me").unwrap(); + let actor = sys.actor_of::("me").unwrap(); - let (probe, _listen) = probe(); - actor.tell(TestProbe(probe), None); + let (probe, mut listen) = probe(); + actor.tell(TestProbe(probe), None); - for _ in 0..1_000_000 { - actor.tell(Add, None); - } - - //p_assert_eq!(listen, ()); + for _ in 0..1_000_000 { + actor.tell(Add, None); } + p_assert_eq!(listen, ()); } -test_fn! { - fn actor_try_tell() { - let sys = ActorSystem::new().unwrap(); - - let actor = sys.actor_of::("me").unwrap(); - let actor: BasicActorRef = actor.into(); +#[tokio::test] +async fn actor_try_tell() { + let sys = ActorSystem::new().unwrap(); - let (probe, _listen) = probe(); - actor - .try_tell(CounterMsg::TestProbe(TestProbe(probe)), None) - .unwrap(); + let actor = sys.actor_of::("me").unwrap(); + let actor: BasicActorRef = actor.into(); - assert!(actor.try_tell(CounterMsg::Add(Add), None).is_ok()); - assert!(actor.try_tell("invalid-type".to_string(), None).is_err()); + let (probe, mut listen) = probe(); + actor + .try_tell(CounterMsg::TestProbe(TestProbe(probe)), None) + .unwrap(); - for _ in 0..1_000_000 { - actor.try_tell(CounterMsg::Add(Add), None).unwrap(); - } + assert!(actor.try_tell(CounterMsg::Add(Add), None).is_ok()); + assert!(actor.try_tell("invalid-type".to_string(), None).is_err()); - //p_assert_eq!(listen, ()); + for _ in 0..1_000_000 { + actor.try_tell(CounterMsg::Add(Add), None).unwrap(); } + + p_assert_eq!(listen, ()); } #[derive(Default)] @@ -155,21 +159,20 @@ impl Actor for Child { fn recv(&mut self, _: &Context, _: Self::Msg, _: Sender) {} } -test_fn! { - #[allow(dead_code)] - fn actor_stop() { - let system = ActorSystem::new().unwrap(); +#[allow(dead_code)] +#[tokio::test] +async fn actor_stop() { + let system = ActorSystem::new().unwrap(); - let parent = system.actor_of::("parent").unwrap(); + let parent = system.actor_of::("parent").unwrap(); - let (probe, _listen) = probe(); - parent.tell(TestProbe(probe), None); - system.print_tree(); + let (probe, mut listen) = probe(); + parent.tell(TestProbe(probe), None); + system.print_tree(); - // wait for the probe to arrive at the actor before attempting to stop the actor - //listen.recv(); + // wait for the probe to arrive at the actor before attempting to stop the actor + listen.recv().await; - system.stop(&parent); - //p_assert_eq!(listen, ()); - } + system.stop(&parent); + p_assert_eq!(listen, ()); } diff --git a/tests/channels.rs b/tests/channels.rs index 2dacf3a6..2b69db92 100644 --- a/tests/channels.rs +++ b/tests/channels.rs @@ -1,11 +1,16 @@ -#[macro_use] -extern crate riker_testkit; - use riker::actors::*; -use riker_testkit::probe::channel::{probe, ChannelProbe}; -use riker_testkit::probe::{Probe, ProbeReceive}; -use riker_testkit::test_fn; +use riker_testkit::{ + p_assert_eq, + probe::{ + Probe, + ProbeReceive, + channel::{ + ChannelProbe, + probe, + }, + }, +}; #[derive(Clone, Debug)] pub struct TestProbe(ChannelProbe<(), ()>); @@ -67,93 +72,91 @@ impl Receive for Subscriber { } } -test_fn! { - fn channel_publish() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn channel_publish() { + let sys = ActorSystem::new().unwrap(); - // Create the channel we'll be using - let chan: ChannelRef = channel("my-chan", &sys).unwrap(); + // Create the channel we'll be using + let chan: ChannelRef = channel("my-chan", &sys).unwrap(); - // The topic we'll be publishing to. Endow our subscriber test actor with this. - // On Subscriber's pre_start it will subscribe to this channel+topic - let topic = Topic::from("my-topic"); - let sub = sys - .actor_of_args::("sub-actor", (chan.clone(), topic.clone())) - .unwrap(); + // The topic we'll be publishing to. Endow our subscriber test actor with this. + // On Subscriber's pre_start it will subscribe to this channel+topic + let topic = Topic::from("my-topic"); + let sub = sys + .actor_of_args::("sub-actor", (chan.clone(), topic.clone())) + .unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - sub.tell(TestProbe(probe), None); + sub.tell(TestProbe(probe), None); - // wait for the probe to arrive at the actor before publishing message - listen.recv().await; + // wait for the probe to arrive at the actor before publishing message + listen.recv().await; - // Publish a test message - chan.tell( - Publish { - msg: SomeMessage, - topic, - }, - None, - ); + // Publish a test message + chan.tell( + Publish { + msg: SomeMessage, + topic, + }, + None, + ); - p_assert_eq!(listen, ()); - } + p_assert_eq!(listen, ()); } -test_fn! { - fn channel_publish_subscribe_all() { - let sys = ActorSystem::new().unwrap(); - - // Create the channel we'll be using - let chan: ChannelRef = channel("my-chan", &sys).unwrap(); - - // The '*' All topic. Endow our subscriber test actor with this. - // On Subscriber's pre_start it will subscribe to all topics on this channel. - let topic = Topic::from("*"); - let sub = sys - .actor_of_args::("sub-actor", (chan.clone(), topic)) - .unwrap(); - - let (probe, mut listen) = probe(); - - sub.tell(TestProbe(probe), None); - - // wait for the probe to arrive at the actor before publishing message - listen.recv().await; - - // Publish a test message to topic "topic-1" - chan.tell( - Publish { - msg: SomeMessage, - topic: "topic-1".into(), - }, - None, - ); - - // Publish a test message to topic "topic-2" - chan.tell( - Publish { - msg: SomeMessage, - topic: "topic-2".into(), - }, - None, - ); - - // Publish a test message to topic "topic-3" - chan.tell( - Publish { - msg: SomeMessage, - topic: "topic-3".into(), - }, - None, - ); - - // Expecting three probe events - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - } +#[tokio::test] +async fn channel_publish_subscribe_all() { + let sys = ActorSystem::new().unwrap(); + + // Create the channel we'll be using + let chan: ChannelRef = channel("my-chan", &sys).unwrap(); + + // The '*' All topic. Endow our subscriber test actor with this. + // On Subscriber's pre_start it will subscribe to all topics on this channel. + let topic = Topic::from("*"); + let sub = sys + .actor_of_args::("sub-actor", (chan.clone(), topic)) + .unwrap(); + + let (probe, mut listen) = probe(); + + sub.tell(TestProbe(probe), None); + + // wait for the probe to arrive at the actor before publishing message + listen.recv().await; + + // Publish a test message to topic "topic-1" + chan.tell( + Publish { + msg: SomeMessage, + topic: "topic-1".into(), + }, + None, + ); + + // Publish a test message to topic "topic-2" + chan.tell( + Publish { + msg: SomeMessage, + topic: "topic-2".into(), + }, + None, + ); + + // Publish a test message to topic "topic-3" + chan.tell( + Publish { + msg: SomeMessage, + topic: "topic-3".into(), + }, + None, + ); + + // Expecting three probe events + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); } #[derive(Clone, Debug)] @@ -259,37 +262,37 @@ impl Receive for EventSubscriber { } } -test_fn! { - fn channel_system_events() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn channel_system_events() { + let sys = ActorSystem::new().unwrap(); - let actor = sys.actor_of::("event-sub").unwrap(); + let actor = sys.actor_of::("event-sub").unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - actor.tell(TestProbe(probe), None); + actor.tell(TestProbe(probe), None); - // wait for the probe to arrive at the actor before attempting - // create, restart and stop - listen.recv().await; + // wait for the probe to arrive at the actor before attempting + // create, restart and stop + listen.recv().await; - // Create an actor - let dumb = sys.actor_of::("dumb-actor").unwrap(); - // ActorCreated event was received - p_assert_eq!(listen, ()); + // Create an actor + let dumb = sys.actor_of::("dumb-actor").unwrap(); + // ActorCreated event was received + p_assert_eq!(listen, ()); - // Force restart of actor - dumb.tell(Panic, None); - // ActorRestarted event was received - p_assert_eq!(listen, ()); + // Force restart of actor + dumb.tell(Panic, None); + // ActorRestarted event was received + p_assert_eq!(listen, ()); - // Terminate actor - sys.stop(&dumb); - // ActorTerminated event was receive - p_assert_eq!(listen, ()); - } + // Terminate actor + sys.stop(&dumb); + // ActorTerminated event was receive + p_assert_eq!(listen, ()); } + // *** Dead letters test *** #[actor(TestProbe, DeadLetter)] #[derive(Default)] @@ -334,25 +337,24 @@ impl Receive for DeadLetterSub { } } -test_fn! { - fn channel_dead_letters() { - let sys = ActorSystem::new().unwrap(); - let actor = sys.actor_of::("dl-subscriber").unwrap(); +#[tokio::test] +async fn channel_dead_letters() { + let sys = ActorSystem::new().unwrap(); + let actor = sys.actor_of::("dl-subscriber").unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - actor.tell(TestProbe(probe), None); + actor.tell(TestProbe(probe), None); - // wait for the probe to arrive at the actor before attempting to stop the actor - listen.recv().await; + // wait for the probe to arrive at the actor before attempting to stop the actor + listen.recv().await; - let dumb = sys.actor_of::("dumb-actor").unwrap(); + let dumb = sys.actor_of::("dumb-actor").unwrap(); - // immediately stop the actor and attempt to send a message - sys.stop(&dumb); - std::thread::sleep(std::time::Duration::from_secs(1)); - dumb.tell(SomeMessage, None); + // immediately stop the actor and attempt to send a message + sys.stop(&dumb); + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + dumb.tell(SomeMessage, None); - p_assert_eq!(listen, ()); - } + p_assert_eq!(listen, ()); } diff --git a/tests/logger.rs b/tests/logger.rs index c8862a7f..5fc3c0e1 100644 --- a/tests/logger.rs +++ b/tests/logger.rs @@ -1,7 +1,4 @@ -use futures::executor::block_on; - use riker::actors::*; -use riker_testkit::test_fn; use slog::{o, Fuse, Logger}; mod common { @@ -43,22 +40,20 @@ mod common { } } -test_fn! { - fn system_create_with_slog() { - let log = Logger::root( - Fuse(common::PrintlnDrain), - o!("version" => "v1", "run_env" => "test"), - ); - let sys = SystemBuilder::new().log(log).create().unwrap(); - block_on(sys.shutdown()).unwrap(); - } +#[tokio::test] +async fn system_create_with_slog() { + let log = Logger::root( + Fuse(common::PrintlnDrain), + o!("version" => "v1", "run_env" => "test"), + ); + let sys = SystemBuilder::new().log(log).create().unwrap(); + sys.shutdown().await.unwrap(); } // a test that logging without slog using "log" crate works -test_fn! { - fn logging_stdlog() { - log::info!("before the system"); - let _sys = ActorSystem::new().unwrap(); - log::info!("system exists"); - } +#[tokio::test] +async fn logging_stdlog() { + log::info!("before the system"); + let _sys = ActorSystem::new().unwrap(); + log::info!("system exists"); } diff --git a/tests/scheduling.rs b/tests/scheduling.rs index b2cd55e5..9fd22918 100644 --- a/tests/scheduling.rs +++ b/tests/scheduling.rs @@ -5,7 +5,6 @@ use riker::actors::*; use riker_testkit::probe::channel::{probe, ChannelProbe}; use riker_testkit::probe::{Probe, ProbeReceive}; -use riker_testkit::test_fn; use chrono::{Duration as CDuration, Utc}; use std::time::Duration; @@ -48,33 +47,31 @@ impl Receive for ScheduleOnce { } } -test_fn! { - fn schedule_once() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn schedule_once() { + let sys = ActorSystem::new().unwrap(); - let actor = sys.actor_of::("schedule-once").unwrap(); + let actor = sys.actor_of::("schedule-once").unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - // use scheduler to set up probe - sys.schedule_once(Duration::from_millis(200), actor, None, TestProbe(probe)); - p_assert_eq!(listen, ()); - } + // use scheduler to set up probe + sys.schedule_once(Duration::from_millis(200), actor, None, TestProbe(probe)); + p_assert_eq!(listen, ()); } -test_fn! { - fn schedule_at_time() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn schedule_at_time() { + let sys = ActorSystem::new().unwrap(); - let actor = sys.actor_of::("schedule-once").unwrap(); + let actor = sys.actor_of::("schedule-once").unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - // use scheduler to set up probe at a specific time - let schedule_at = Utc::now() + CDuration::milliseconds(200); - sys.schedule_at_time(schedule_at, actor, None, TestProbe(probe)); - p_assert_eq!(listen, ()); - } + // use scheduler to set up probe at a specific time + let schedule_at = Utc::now() + CDuration::milliseconds(200); + sys.schedule_at_time(schedule_at, actor, None, TestProbe(probe)); + p_assert_eq!(listen, ()); } // *** Schedule repeat test *** @@ -125,16 +122,15 @@ impl Receive for ScheduleRepeat { } } -test_fn! { - fn schedule_repeat() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn schedule_repeat() { + let sys = ActorSystem::new().unwrap(); - let actor = sys.actor_of::("schedule-repeat").unwrap(); + let actor = sys.actor_of::("schedule-repeat").unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - actor.tell(TestProbe(probe), None); + actor.tell(TestProbe(probe), None); - p_assert_eq!(listen, ()); - } + p_assert_eq!(listen, ()); } diff --git a/tests/selection.rs b/tests/selection.rs index 32bfbf1b..f330cbaa 100644 --- a/tests/selection.rs +++ b/tests/selection.rs @@ -5,7 +5,6 @@ use riker::actors::*; use riker_testkit::probe::channel::{probe, ChannelProbe}; use riker_testkit::probe::{Probe, ProbeReceive}; -use riker_testkit::test_fn; #[derive(Clone, Debug)] pub struct TestProbe(ChannelProbe<(), ()>); @@ -42,72 +41,69 @@ impl Actor for SelectTest { } } -test_fn! { - fn select_child() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn select_child() { + let sys = ActorSystem::new().unwrap(); - sys.actor_of::("select-actor").unwrap(); + sys.actor_of::("select-actor").unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - // select test actors through actor selection: /root/user/select-actor/* - let sel = sys.select("select-actor").unwrap(); + // select test actors through actor selection: /root/user/select-actor/* + let sel = sys.select("select-actor").unwrap(); - sel.try_tell(TestProbe(probe), None); + sel.try_tell(TestProbe(probe), None); - p_assert_eq!(listen, ()); - } + p_assert_eq!(listen, ()); } -test_fn! { - fn select_child_of_child() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn select_child_of_child() { + let sys = ActorSystem::new().unwrap(); - sys.actor_of::("select-actor").unwrap(); + sys.actor_of::("select-actor").unwrap(); - // delay to allow 'select-actor' pre_start to create 'child_a' and 'child_b' - // Direct messaging on the actor_ref doesn't have this same issue - std::thread::sleep(std::time::Duration::from_millis(500)); + // delay to allow 'select-actor' pre_start to create 'child_a' and 'child_b' + // Direct messaging on the actor_ref doesn't have this same issue + tokio::time::sleep(std::time::Duration::from_millis(500)).await; - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - // select test actors through actor selection: /root/user/select-actor/* - let sel = sys.select("select-actor/child_a").unwrap(); - sel.try_tell(TestProbe(probe), None); + // select test actors through actor selection: /root/user/select-actor/* + let sel = sys.select("select-actor/child_a").unwrap(); + sel.try_tell(TestProbe(probe), None); - // actors 'child_a' should fire a probe event - p_assert_eq!(listen, ()); - } + // actors 'child_a' should fire a probe event + p_assert_eq!(listen, ()); } -test_fn! { - fn select_all_children_of_child() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn select_all_children_of_child() { + let sys = ActorSystem::new().unwrap(); - sys.actor_of::("select-actor").unwrap(); + sys.actor_of::("select-actor").unwrap(); - // delay to allow 'select-actor' pre_start to create 'child_a' and 'child_b' - // Direct messaging on the actor_ref doesn't have this same issue - std::thread::sleep(std::time::Duration::from_millis(500)); + // delay to allow 'select-actor' pre_start to create 'child_a' and 'child_b' + // Direct messaging on the actor_ref doesn't have this same issue + tokio::time::sleep(std::time::Duration::from_millis(500)).await; - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - // select relative test actors through actor selection: /root/user/select-actor/* - let sel = sys.select("select-actor/*").unwrap(); - sel.try_tell(TestProbe(probe.clone()), None); + // select relative test actors through actor selection: /root/user/select-actor/* + let sel = sys.select("select-actor/*").unwrap(); + sel.try_tell(TestProbe(probe.clone()), None); - // actors 'child_a' and 'child_b' should both fire a probe event - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); + // actors 'child_a' and 'child_b' should both fire a probe event + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); - // select absolute test actors through actor selection: /root/user/select-actor/* - let sel = sys.select("/user/select-actor/*").unwrap(); - sel.try_tell(TestProbe(probe), None); + // select absolute test actors through actor selection: /root/user/select-actor/* + let sel = sys.select("/user/select-actor/*").unwrap(); + sel.try_tell(TestProbe(probe), None); - // actors 'child_a' and 'child_b' should both fire a probe event - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - } + // actors 'child_a' and 'child_b' should both fire a probe event + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); } #[derive(Clone, Default)] @@ -147,45 +143,43 @@ impl Actor for SelectTest2 { } } -test_fn! { - fn select_from_context() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn select_from_context() { + let sys = ActorSystem::new().unwrap(); - let actor = sys.actor_of::("select-actor").unwrap(); + let actor = sys.actor_of::("select-actor").unwrap(); - let (probe, mut listen) = probe(); + let (probe, mut listen) = probe(); - actor.tell(TestProbe(probe), None); + actor.tell(TestProbe(probe), None); - // seven events back expected: - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - p_assert_eq!(listen, ()); - } + // seven events back expected: + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); + p_assert_eq!(listen, ()); } -test_fn! { - fn select_paths() { - let sys = ActorSystem::new().unwrap(); - - assert!(sys.select("foo/").is_ok()); - assert!(sys.select("/foo/").is_ok()); - assert!(sys.select("/foo").is_ok()); - assert!(sys.select("/foo/..").is_ok()); - assert!(sys.select("../foo/").is_ok()); - assert!(sys.select("/foo/*").is_ok()); - assert!(sys.select("*").is_ok()); - - assert!(sys.select("foo/`").is_err()); - assert!(sys.select("foo/@").is_err()); - assert!(sys.select("!").is_err()); - assert!(sys.select("foo/$").is_err()); - assert!(sys.select("&").is_err()); - } +#[tokio::test] +async fn select_paths() { + let sys = ActorSystem::new().unwrap(); + + assert!(sys.select("foo/").is_ok()); + assert!(sys.select("/foo/").is_ok()); + assert!(sys.select("/foo").is_ok()); + assert!(sys.select("/foo/..").is_ok()); + assert!(sys.select("../foo/").is_ok()); + assert!(sys.select("/foo/*").is_ok()); + assert!(sys.select("*").is_ok()); + + assert!(sys.select("foo/`").is_err()); + assert!(sys.select("foo/@").is_err()); + assert!(sys.select("!").is_err()); + assert!(sys.select("foo/$").is_err()); + assert!(sys.select("&").is_err()); } // // *** Dead letters test *** diff --git a/tests/supervision.rs b/tests/supervision.rs index 5e8898d6..9283abd2 100644 --- a/tests/supervision.rs +++ b/tests/supervision.rs @@ -206,20 +206,19 @@ impl Receive for EscRestartSup { } } -test_fn! { - fn supervision_escalate_failed_actor() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn supervision_escalate_failed_actor() { + let sys = ActorSystem::new().unwrap(); - let sup = sys.actor_of::("supervisor").unwrap(); + let sup = sys.actor_of::("supervisor").unwrap(); - // Make the test actor panic - sup.tell(Panic, None); + // Make the test actor panic + sup.tell(Panic, None); - let (probe, mut listen) = probe::<()>(); + let (probe, mut listen) = probe::<()>(); - std::thread::sleep(std::time::Duration::from_millis(2000)); - sup.tell(TestProbe(probe), None); - p_assert_eq!(listen, ()); - sys.print_tree(); - } + tokio::time::sleep(std::time::Duration::from_millis(2000)).await; + sup.tell(TestProbe(probe), None); + p_assert_eq!(listen, ()); + sys.print_tree(); } diff --git a/tests/system.rs b/tests/system.rs index 57fff22f..61eaa8f9 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -1,20 +1,16 @@ -use futures::executor::block_on; use riker::actors::*; -use riker_testkit::test_fn; - -test_fn! { - fn system_create() { - assert!(ActorSystem::new().is_ok()); - assert!(ActorSystem::with_name("valid-name").is_ok()); - - assert!(ActorSystem::with_name("/").is_err()); - assert!(ActorSystem::with_name("*").is_err()); - assert!(ActorSystem::with_name("/a/b/c").is_err()); - assert!(ActorSystem::with_name("@").is_err()); - assert!(ActorSystem::with_name("#").is_err()); - assert!(ActorSystem::with_name("abc*").is_err()); - } +#[tokio::test] +async fn system_create() { + assert!(ActorSystem::new().is_ok()); + assert!(ActorSystem::with_name("valid-name").is_ok()); + + assert!(ActorSystem::with_name("/").is_err()); + assert!(ActorSystem::with_name("*").is_err()); + assert!(ActorSystem::with_name("/a/b/c").is_err()); + assert!(ActorSystem::with_name("@").is_err()); + assert!(ActorSystem::with_name("#").is_err()); + assert!(ActorSystem::with_name("abc*").is_err()); } struct ShutdownTest { @@ -43,65 +39,60 @@ impl Actor for ShutdownTest { fn recv(&mut self, _: &Context, _: Self::Msg, _: Sender) {} } -test_fn! { - #[allow(dead_code)] - fn system_shutdown() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +#[allow(dead_code)] +async fn system_shutdown() { + let sys = ActorSystem::new().unwrap(); - let _ = sys - .actor_of_args::("test-actor-1", 1) - .unwrap(); + let _ = sys + .actor_of_args::("test-actor-1", 1) + .unwrap(); - block_on(sys.shutdown()).unwrap(); - } + sys.shutdown().await.unwrap(); } -test_fn! { - fn system_futures_exec() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn system_futures_exec() { + let sys = ActorSystem::new().unwrap(); - for i in 0..100 { - let f = sys.run(async move { format!("some_val_{}", i) }).unwrap(); + for i in 0..100 { + let f = sys.run(async move { format!("some_val_{}", i) }).unwrap(); - assert_eq!(block_on(f).unwrap(), format!("some_val_{}", i)); - } + assert_eq!(f.await.unwrap(), format!("some_val_{}", i)); } } -test_fn! { - fn system_futures_panic() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn system_futures_panic() { + let sys = ActorSystem::new().unwrap(); - for _ in 0..100 { - let _ = sys - .run(async move { - panic!("// TEST PANIC // TEST PANIC // TEST PANIC //"); - }) - .unwrap(); - } + for _ in 0..100 { + let _ = sys + .run(async move { + panic!("// TEST PANIC // TEST PANIC // TEST PANIC //"); + }) + .unwrap(); + } - for i in 0..100 { - let f = sys.run(async move { format!("some_val_{}", i) }).unwrap(); + for i in 0..100 { + let f = sys.run(async move { format!("some_val_{}", i) }).unwrap(); - assert_eq!(block_on(f).unwrap(), format!("some_val_{}", i)); - } + assert_eq!(f.await.unwrap(), format!("some_val_{}", i)); } } -test_fn! { - fn system_load_app_config() { - let sys = ActorSystem::new().unwrap(); +#[tokio::test] +async fn system_load_app_config() { + let sys = ActorSystem::new().unwrap(); - assert_eq!(sys.config().get_int("app.some_setting").unwrap() as i64, 1); - } + assert_eq!(sys.config().get_int("app.some_setting").unwrap() as i64, 1); } -test_fn! { - fn system_builder() { - let sys = SystemBuilder::new().create().unwrap(); - block_on(sys.shutdown()).unwrap(); +#[tokio::test] +async fn system_builder() { + let sys = SystemBuilder::new().create().unwrap(); + sys.shutdown().await.unwrap(); - let sys = SystemBuilder::new().name("my-sys").create().unwrap(); - block_on(sys.shutdown()).unwrap(); - } + let sys = SystemBuilder::new().name("my-sys").create().unwrap(); + sys.shutdown().await.unwrap(); }