seal_rs/testkit/actors/
test_local_actor_ref.rs

1//! Test variant of the LocalActorRef.
2//!
3//! This object is fully mirror of the original actor system version, but additionally extends he
4//! with various  methods for tests performing. The source code contains special comments which
5//! marks code which was fully cloned from the original actor system. All codes outside of this
6//! blocks, is code of a tests extensions.
7
8use crate::common::tsafe::TSafe;
9use crate::actors::actor_cell::ActorCell;
10use crate::actors::abstract_actor_ref::{ActorRef, AbstractActorRef};
11use crate::actors::actor_path::ActorPath;
12use crate::actors::actor::Actor;
13use std::fmt;
14use std::any::Any;
15
16pub struct TestLocalActorRef {
17
18    // ------- mirror ---------
19    pub cell: TSafe<ActorCell>,
20    pub path: TSafe<ActorPath>,
21    // --------- end ----------
22
23    /// Original actor object on which this reference links
24    pub actor: TSafe<Actor + Send>
25}
26
27
28impl TestLocalActorRef {
29
30    /// Identical to original
31    pub fn new(cell: TSafe<ActorCell>, path: TSafe<ActorPath>) -> TestLocalActorRef {
32        let actor = cell.clone().lock().unwrap().actor.clone();
33        TestLocalActorRef {
34            cell,
35            path,
36            actor
37        }
38    }
39
40    fn inner_clone(self: &Self) -> Box<TestLocalActorRef> {
41        Box::new(TestLocalActorRef {
42            cell: self.cell.clone(),
43            path: self.path.clone(),
44            actor: self.actor.clone()
45        })
46    }
47}
48
49impl AbstractActorRef for TestLocalActorRef {
50
51    /// Identical to original
52    fn tell(self: &mut Self, msg: Box<Any + Send + 'static>, rself: Option<Box<AbstractActorRef + Send>>) {
53        // ------- mirror ---------
54        let cell_cloned = self.cell.clone();
55        let path_cloned = self.path.clone();
56        let toref = Box::new(TestLocalActorRef::new(cell_cloned, path_cloned));
57        let mut cell = self.cell.lock().unwrap();
58        cell.send(&self.cell, msg, rself, toref)
59        // --------- end ----------
60    }
61
62    /// Identical to original
63    fn path(self: &mut Self) -> ActorPath {
64        // ------- mirror ---------
65        self.path.lock().unwrap().clone()
66        // --------- end ----------
67    }
68
69    /// Identical to original
70    fn cell(self: &mut Self) -> TSafe<ActorCell> {
71        // ------- mirror ---------
72        self.cell.clone()
73        // --------- end ----------
74    }
75
76    /// Identical to original
77    fn clone(self: &Self) -> ActorRef {
78        self.inner_clone()
79    }
80
81    /// Identical to original
82    fn as_any(self: &Self) -> Box<Any> {
83        Box::new(self.inner_clone())
84    }
85}
86
87impl fmt::Display for TestLocalActorRef {
88    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89        write!(f, "TestActorRef ({})", self.path.lock().unwrap())
90    }
91}
92
93/*impl Clone for TestLocalActorRef {
94    fn clone(&self) -> TestLocalActorRef {
95        TestLocalActorRef {
96            cell: self.cell.clone(),
97            path: self.path.clone(),
98            actor: self.actor.clone()
99        }
100    }
101}*/
102
103impl PartialEq for TestLocalActorRef {
104    fn eq(&self, _other: &Self) -> bool {
105        true
106        //self.path == other.path
107    }
108}
109
110impl Eq for TestLocalActorRef {}
111
112