#![allow(dead_code, unused_doc_comments)]
use std::time::Duration;
use tracing::*;
use acton_reactive::prelude::*;
use acton_test::prelude::*;
use crate::setup::{
actors::{comedian::Comedian, counter::Counter, parent_child::Parent},
initialize_tracing,
messages::{Ping, Pong},
};
mod setup;
#[acton_test]
async fn test_launch_passing_acton() -> anyhow::Result<()> {
initialize_tracing();
let mut runtime: ActorRuntime = ActonApp::launch_async().await;
let broker_handle = runtime.broker();
let parent_config = ActorConfig::new(Ern::with_root("parent")?, None, None)?;
let broker_handle_clone = broker_handle.clone();
let _parent_handle = runtime
.spawn_actor_with_setup_fn::<Parent>(parent_config, |mut parent_builder| {
Box::pin(async move {
let child_config = ActorConfig::new(
Ern::with_root("child").expect("Could not create child ARN root"),
None,
None,
)
.expect("Couldn't create child config");
let mut runtime_clone = parent_builder.runtime().clone();
let _child_handle = runtime_clone
.spawn_actor_with_setup_fn::<Parent>(child_config, |mut child_builder| {
Box::pin(async move {
child_builder.mutate_on::<Pong>(|_actor, _envelope| {
info!("CHILD SUCCESS! PONG!");
Reply::ready()
});
let child_builder_handle = &child_builder.handle().clone();
child_builder_handle.subscribe::<Pong>().await;
child_builder.start().await
})
})
.await
.expect("Couldn't create child actor");
parent_builder
.mutate_on::<Ping>(|_actor, _envelope| {
info!("SUCCESS! PING!");
Reply::ready()
})
.mutate_on::<Pong>(|_actor, _envelope| Reply::pending(wait_and_respond()));
let parent_builder_handle = &parent_builder.handle().clone();
parent_builder_handle.subscribe::<Ping>().await;
parent_builder_handle.subscribe::<Pong>().await;
parent_builder.start().await
})
})
.await?;
broker_handle_clone.broadcast(Ping).await; broker_handle_clone.broadcast(Pong).await;
runtime.shutdown_all().await?;
Ok(())
}
async fn wait_and_respond() {
tokio::time::sleep(Duration::from_secs(1)).await;
info!("Waited, then...SUCCESS! PONG!");
}
#[acton_test]
async fn test_launchpad() -> anyhow::Result<()> {
initialize_tracing();
let mut runtime: ActorRuntime = ActonApp::launch_async().await;
let broker_handle = runtime.broker();
let _comedian_handle = runtime
.spawn_actor::<Comedian>(|mut actor_builder| {
Box::pin(async move {
actor_builder
.mutate_on::<Ping>(|_actor, _envelope| {
info!("SUCCESS! PING!");
Reply::ready()
})
.mutate_on::<Pong>(|_actor, _envelope| {
Reply::pending(async move {
info!("SUCCESS! PONG!");
})
});
actor_builder.handle().subscribe::<Ping>().await;
actor_builder.handle().subscribe::<Pong>().await;
actor_builder.start().await })
})
.await?;
let _counter_handle = runtime
.spawn_actor::<Counter>(|mut actor_builder| {
Box::pin(async move {
actor_builder.mutate_on::<Pong>(|_actor, _envelope| {
Reply::pending(async move {
info!("SUCCESS! PONG!");
})
});
actor_builder.handle().subscribe::<Pong>().await;
actor_builder.start().await })
})
.await?;
assert!(
runtime.actor_count() > 1,
"Expected more than 1 actor (broker + spawned)"
);
broker_handle.broadcast(Ping).await; broker_handle.broadcast(Pong).await;
runtime.shutdown_all().await?;
Ok(())
}