extern crate actix;
extern crate futures;
use futures::Future;
use actix::prelude::*;
use actix::actors::mocker::Mocker;
#[derive(Eq, PartialEq, Debug)]
struct DoStuff{}
impl Message for DoStuff {
type Result = String;
}
struct DBClientActor {}
impl Actor for DBClientActor {
type Context = Context<Self>;
}
impl Handler<DoStuff> for DBClientActor {
type Result = String;
fn handle(&mut self, msg: DoStuff, _: &mut Context<Self>) -> Self::Result {
"Reply from database".to_string()
}
}
impl Supervised for DBClientActor {}
impl Default for DBClientActor {
fn default() -> Self {
DBClientActor{}
}
}
impl SystemService for DBClientActor {
fn service_started(&mut self, _ctx: &mut Context<Self>) {}
}
#[cfg(not(test))]
type DBClientAct = DBClientActor;
#[cfg(test)]
type DBClientAct = Mocker<DBClientActor>;
fn example_function() {
let res = DBClientAct::from_registry().send(DoStuff{});
Arbiter::handle().spawn(
res.map(|res| {
println!("got reply {}", res);
Arbiter::system().do_send(actix::msgs::SystemExit(0));
}).map_err(|_| ()));
}
fn main() {
let system = System::new("test");
example_function();
system.run();
}
#[test]
fn test_stuff() {
let system = System::new("test");
let _: Addr<Syn, _> = DBClientAct::init_actor(|_| {
DBClientAct::mock(Box::new(|msg, ctx| {
assert_eq!(
msg.downcast_ref::<DoStuff>(),
Some(&DoStuff{})
);
Box::new(Some("Reply from mocker".to_string()))
}))
});
example_function();
system.run();
}