agner_test_actor/
registry.rs

1use std::any::Any;
2use std::collections::HashMap;
3use std::sync::Arc;
4
5use agner_actors::{ActorID, System};
6use tokio::sync::{mpsc, Mutex, RwLock};
7
8use crate::exited::Exited;
9use crate::query::Query;
10use crate::TestActor;
11
12#[derive(Debug, Clone, Default)]
13pub struct TestActorRegistry(pub Arc<RwLock<HashMap<ActorID, TestActorEntry>>>);
14
15impl TestActorRegistry {
16    pub fn new() -> Self {
17        Default::default()
18    }
19
20    pub async fn lookup<M>(&self, actor_id: ActorID) -> Option<TestActor<M>>
21    where
22        M: Send + Sync + 'static,
23    {
24        self.0.read().await.get(&actor_id).and_then(|entry| {
25            let ctl_tx = entry.ctl_tx.downcast_ref::<mpsc::UnboundedSender<Query<M>>>().cloned()?;
26            let test_actor = TestActor {
27                system: entry.system.to_owned(),
28                actor_id,
29                exited: entry.exited.to_owned(),
30                ctl_tx,
31            };
32            Some(test_actor)
33        })
34    }
35}
36
37#[derive(Debug)]
38pub struct TestActorEntry {
39    pub system: System,
40    pub exited: Arc<Mutex<Exited>>,
41    pub ctl_tx: Box<dyn Any + Send + Sync + 'static>,
42}