1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#![warn(missing_docs)]
#![recursion_limit = "512"]
mod actor_runtime;
pub mod handlers;
pub mod ids;
mod lifecycle;
pub mod linkage;
mod lite_runtime;
pub mod prelude;
pub mod signal;
pub mod system;
pub mod task;
pub mod thread;
#[cfg(test)]
mod tests {
use super::prelude::*;
use super::signal;
use anyhow::Error;
use async_trait::async_trait;
use futures::stream;
use std::time::Duration;
use tokio::time::delay_for;
#[derive(Debug)]
pub struct MyActor;
#[async_trait]
impl StartedBy<System> for MyActor {
async fn handle(&mut self, _ctx: &mut Context<Self>) -> Result<(), Error> {
Ok(())
}
}
#[async_trait]
impl InterruptedBy<System> for MyActor {
async fn handle(&mut self, ctx: &mut Context<Self>) -> Result<(), Error> {
delay_for(Duration::from_secs(3)).await;
ctx.shutdown();
Ok(())
}
}
#[derive(Debug)]
struct MsgOne;
impl Action for MsgOne {}
#[derive(Debug)]
struct MsgTwo;
impl Interaction for MsgTwo {
type Output = u8;
}
mod link {
use super::*;
use derive_more::{Deref, DerefMut, From};
#[derive(Debug, From)]
pub struct MyLink {
address: Address<MyActor>,
}
pub(super) struct LinkSignal;
impl Action for LinkSignal {}
impl MyLink {
pub async fn send_signal(&mut self) -> Result<(), Error> {
self.address.act(LinkSignal).await
}
}
#[derive(Debug, From, Deref, DerefMut)]
pub struct MyAlternativeLink {
address: Address<MyActor>,
}
}
#[async_trait]
impl Actor for MyActor {
type GroupBy = ();
}
#[async_trait]
impl ActionHandler<MsgOne> for MyActor {
async fn handle(&mut self, _: MsgOne, _ctx: &mut Context<Self>) -> Result<(), Error> {
log::info!("Received MsgOne");
Ok(())
}
}
#[async_trait]
impl Consumer<MsgOne> for MyActor {
async fn handle(&mut self, _: MsgOne, _ctx: &mut Context<Self>) -> Result<(), Error> {
log::info!("Received MsgOne (from the stream)");
Ok(())
}
}
#[async_trait]
impl InteractionHandler<MsgTwo> for MyActor {
async fn handle(&mut self, _: MsgTwo, _ctx: &mut Context<Self>) -> Result<u8, Error> {
log::info!("Received MsgTwo");
Ok(1)
}
}
#[async_trait]
impl Consumer<signal::CtrlC> for MyActor {
async fn handle(
&mut self,
_: signal::CtrlC,
_ctx: &mut Context<Self>,
) -> Result<(), Error> {
log::info!("Received CtrlC");
Ok(())
}
}
#[async_trait]
impl ActionHandler<link::LinkSignal> for MyActor {
async fn handle(
&mut self,
_: link::LinkSignal,
_ctx: &mut Context<Self>,
) -> Result<(), Error> {
log::info!("Received LinkSignal");
Ok(())
}
}
#[tokio::test]
async fn start_and_terminate() -> Result<(), Error> {
env_logger::try_init().ok();
let mut address = System::spawn(MyActor);
address.act(MsgOne).await?;
let res = address.interact(MsgTwo).await?;
assert_eq!(res, 1);
System::interrupt(&mut address)?;
address.join().await;
Ok(())
}
#[tokio::test]
async fn test_recipient() -> Result<(), Error> {
env_logger::try_init().ok();
let mut address = System::spawn(MyActor);
let action_recipient = address.action_recipient();
action_recipient.clone().act(MsgOne).await?;
let interaction_recipient = address.interaction_recipient();
let res = interaction_recipient.clone().interact(MsgTwo).await?;
assert_eq!(res, 1);
System::interrupt(&mut address)?;
address.join().await;
Ok(())
}
#[tokio::test]
async fn test_attach() -> Result<(), Error> {
env_logger::try_init().ok();
let mut address = System::spawn(MyActor);
let stream = stream::iter(vec![MsgOne, MsgOne, MsgOne]);
address.attach(stream);
System::interrupt(&mut address)?;
Ok(())
}
#[tokio::test]
async fn test_link() -> Result<(), Error> {
env_logger::try_init().ok();
let address = System::spawn(MyActor);
let mut link: link::MyLink = address.link();
link.send_signal().await?;
let mut alternative_link: link::MyAlternativeLink = address.link();
System::interrupt(&mut alternative_link)?;
alternative_link.join().await;
Ok(())
}
}