Skip to content

Commit

Permalink
Fix tests on tokio
Browse files Browse the repository at this point in the history
  • Loading branch information
mankinskin committed Jan 11, 2021
1 parent 2138bab commit 298ecba
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 438 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions riker-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ proc-macro2 = "1.0"

[dev-dependencies]
riker = { path = ".." }
tokio = { version = "^1", features = ["rt-multi-thread", "macros", "time"] }
24 changes: 12 additions & 12 deletions riker-macros/tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ impl Receive<String> for NewActor {
}
}

#[test]
fn run_derived_actor() {
#[tokio::test]
async fn run_derived_actor() {
let sys = ActorSystem::new().unwrap();

let act = sys.actor_of::<NewActor>("act").unwrap();
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -80,8 +80,8 @@ impl<A: Send + 'static, B: Send + 'static> Receive<String> for GenericActor<A, B
}
}

#[test]
fn run_derived_generic_actor() {
#[tokio::test]
async fn run_derived_generic_actor() {
let sys = ActorSystem::new().unwrap();

let act = sys.actor_of::<GenericActor<(), ()>>("act").unwrap();
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -131,8 +131,8 @@ impl Receive<Message<String>> 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::<GenericMsgActor>("act").unwrap();
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -202,8 +202,8 @@ impl Receive<test_mod::Message> 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::<PathMsgActor>("act").unwrap();
Expand All @@ -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;
}
}
12 changes: 7 additions & 5 deletions src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,14 @@ impl<A: Actor + ?Sized> Actor for Box<A> {
/// }
/// }
///
/// // main
/// let sys = ActorSystem::new().unwrap();
/// let actor = sys.actor_of::<MyActor>("my-actor").unwrap();
/// #[tokio::main]
/// async fn main() {
/// let sys = ActorSystem::new().unwrap();
/// let actor = sys.actor_of::<MyActor>("my-actor").unwrap();
///
/// actor.tell(Foo, None);
/// actor.tell(Bar, None);
/// actor.tell(Foo, None);
/// actor.tell(Bar, None);
/// }
/// ```
pub trait Receive<Msg: Message> {
type Msg: Message;
Expand Down
91 changes: 56 additions & 35 deletions src/actor/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ impl Props {
/// # type Msg = String;
/// # fn recv(&mut self, _ctx: &Context<String>, _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<A, F>(creator: F) -> Arc<Mutex<impl ActorProducer<Actor = A>>>
Expand Down Expand Up @@ -76,12 +79,15 @@ impl Props {
/// # type Msg = String;
/// # fn recv(&mut self, _ctx: &Context<String>, _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.
/// ```
Expand All @@ -105,14 +111,17 @@ impl Props {
/// # type Msg = String;
/// # fn recv(&mut self, _ctx: &Context<String>, _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<A, Args, F>(
Expand Down Expand Up @@ -141,13 +150,16 @@ impl Props {
/// # type Msg = String;
/// # fn recv(&mut self, _ctx: &Context<String>, _msg: String, _sender: Sender) {}
/// # }
/// // main
/// let sys = ActorSystem::new().unwrap();
///
/// let props = Props::new::<User>();
/// #[tokio::main]
/// async fn main() {
/// let sys = ActorSystem::new().unwrap();
///
/// let props = Props::new::<User>();
///
/// // 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.
///
Expand All @@ -168,13 +180,16 @@ impl Props {
/// # type Msg = String;
/// # fn recv(&mut self, _ctx: &Context<String>, _msg: String, _sender: Sender) {}
/// # }
/// // main
/// let sys = ActorSystem::new().unwrap();
///
/// let props = Props::new::<User>();
/// #[tokio::main]
/// async fn main() {
/// let sys = ActorSystem::new().unwrap();
///
/// let props = Props::new::<User>();
///
/// // 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<A>() -> Arc<Mutex<impl ActorProducer<Actor = A>>>
Expand Down Expand Up @@ -207,12 +222,15 @@ impl Props {
/// # type Msg = String;
/// # fn recv(&mut self, _ctx: &Context<String>, _msg: String, _sender: Sender) {}
/// # }
/// // main
/// let sys = ActorSystem::new().unwrap();
///
/// let props = Props::new_args::<User, _>("Naomi Nagata".into());
/// #[tokio::main]
/// async fn main() {
/// let sys = ActorSystem::new().unwrap();
///
/// let props = Props::new_args::<User, _>("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.
/// ```
Expand All @@ -236,14 +254,17 @@ impl Props {
/// # type Msg = String;
/// # fn recv(&mut self, _ctx: &Context<String>, _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<A, Args>(args: Args) -> Arc<Mutex<impl ActorProducer<Actor = A>>>
Expand Down
Loading

0 comments on commit 298ecba

Please sign in to comment.