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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use crate::actors::prelude::*;
use crate::examples::actors::logger::logger;
use crate::actors::message::Message;
use std::any::Any;
use std::sync::{Mutex, Arc};
use match_downcast::*;
pub fn props(referal: Option<ActorRef>) -> Props {
Props::new(tsafe!(BagsMan::new(referal)))
}
mod commands {
pub struct MsgOk { pub data: u32 }
pub struct MsgOk2 { pub data: u32 }
pub struct MsgOk3 { pub data: u32 }
pub struct MsgOk4 { pub data: u32 }
pub struct MsgOk5 { pub data: u32 }
pub struct MsgOther { pub data: u32 }
pub struct MsgComplex0 { }
pub struct MsgComplex1 { }
pub struct MsgNoResponse { pub data: u32 }
pub struct ToRef { }
pub struct MutState { pub data: u32 }
pub struct Bomb {}
pub struct CreateFiction {}
pub struct MessageToFiction {}
}
mod responses {
pub struct MsgResponse { pub data: u32 }
pub struct MsgResponse2 { pub data: u32 }
pub struct MsgResponse3 { pub data: u32 }
pub struct MsgResponse4 { pub data: u32 }
pub struct OtherResponse { pub data: u32 }
}
//-------------------------
struct NonFictionActor {}
impl Actor for NonFictionActor {
fn receive(&mut self, _msg: Message, _ctx: ActorContext) -> HandleResult {
unimplemented!()
}
}
//-------------------------
pub struct BagsMan {
referal: Option<ActorRef>,
data: u32
}
impl BagsMan {
pub fn new(referal: Option<ActorRef>) -> BagsMan {
BagsMan {
referal,
data: 0
}
}
}
impl Actor for BagsMan {
fn receive(self: &mut Self, msg: Message, mut ctx: ActorContext) -> HandleResult {
let msg = msg.get();
match_downcast_ref!(msg, {
_m: commands::MsgOk => {
ctx.sender.tell(msg!(responses::MsgResponse {data: 99} ), Some(&ctx.self_))
},
_m: commands::MsgOk2 => {
ctx.sender.tell(msg!(responses::MsgResponse2 {data: 99} ), Some(&ctx.self_))
},
_m: commands::MsgOk3 => {
ctx.sender.tell(msg!(responses::MsgResponse4 {data: 99} ), Some(&ctx.self_))
},
_m: commands::MsgOk4 => {
ctx.sender.tell(msg!(responses::MsgResponse {data: 99} ), Some(&ctx.self_));
ctx.sender.tell(msg!(responses::MsgResponse2 {data: 99} ), Some(&ctx.self_));
ctx.sender.tell(msg!(responses::MsgResponse3 {data: 99} ), Some(&ctx.self_))
},
_m: commands::MsgOk5 => {
ctx.sender.tell(msg!(responses::MsgResponse {data: 99} ), Some(&ctx.self_));
ctx.sender.tell(msg!(responses::MsgResponse2 {data: 99} ), Some(&ctx.self_));
ctx.sender.tell(msg!(responses::OtherResponse {data: 99} ), Some(&ctx.self_))
},
_m: commands::MsgOther => {
// Respond with incorrect message
ctx.sender.tell(msg!(responses::OtherResponse {data: 99} ), Some(&ctx.self_))
},
_m: commands::MsgNoResponse => {
// Does not respond
},
_m: commands::MsgComplex0 => {
ctx.sender.tell(msg!(responses::MsgResponse {data: 99} ), Some(&ctx.self_))
},
_m: commands::MsgComplex1 => {
ctx.sender.tell(msg!(responses::MsgResponse2 {data: 199} ), Some(&ctx.self_))
},
m: commands::MutState => {
self.data = m.data;
},
_m: commands::Bomb => {
ctx.system().stop(&mut ctx.self_.clone());
},
_m: commands::CreateFiction => {
let mut nfa = ctx.system().actor_of(Props::new(tsafe!(NonFictionActor {})), None);
nfa.tell(msg!(commands::MessageToFiction {}), Some(&ctx.self_))
},
_m: commands::ToRef => {
let msg = msg!(logger::Log { text: String::from("Text"), target: logger::LogTarget::File });
if let Some(ref mut x) = self.referal {
x.tell(msg, Some(&ctx.self_))
}
},
_ => return Ok(false)
});
Ok(true)
}
fn as_any(&mut self) -> &Any { self }
}
#[cfg(test)]
mod tests {
use crate::testkit::actors::prelude::*;
use crate::examples::actors::logger::logger;
use std::any::Any;
use std::time::Duration;
use super::*;
#[test]
fn main() {
let mut system = TestLocalActorSystem::new();
// ================================ expect_msg ================================
test_case!("MsgOk - respond with MsgResponse");
{
let mut target = system.actor_of(self::props(None), None);
let mut probe = system.create_probe(Some("probe"));
probe.send(&mut target, msg!( commands::MsgOk { data: 99 } ));
let msg = probe.expect_msg(type_matcher!(responses::MsgResponse));
cast!(msg, responses::MsgResponse, m => {
assert_eq!(m.data, 99);
});
}
// This test will failed, uncomment for enable
// test_case!("MsgOther - respond with OtherResponse");
// {
// let (mut target, mut probe) = {
// let mut system = system.lock().unwrap();
// let mut target = system.actor_of(self::props(), None);
// let mut probe = system.create_probe(Some("probe"));
//
// (target, probe)
// };
//
// probe.send(target.clone(), msg!( commands::MsgOther { data: 99 } ));
//
// // Oops! In this expectation the probe will receive unexpected message
// probe.expect_msg(pat_matcher!(responses::MsgResponse => responses::MsgResponse { data: 99 }));
// }
// This test will failed, uncomment for enable
// test_case!("MsgNoResponse - respond with OtherResponse");
// {
// let (mut target, mut probe) = {
// let mut system = system.lock().unwrap();
// let mut target = system.actor_of(self::props(), None);
// let mut probe = system.create_probe(Some("probe"));
//
// (target, probe)
// };
//
// probe.send(target.clone(), msg!( commands::MsgNoResponse { data: 99 } ));
//
// // Oops! Actor does not respond with expected timeout
// probe.expect_msg(pat_matcher!(responses::OtherResponse => responses::OtherResponse { data: 99 }));
// }
// ================================ expect_any_of ================================
test_case!("MsgOk2 - respond with any of <MsgResponse, MsgResponse2, MsgResponse3>");
{
let mut target = system.actor_of(self::props(None), None);
let mut probe = system.create_probe(Some("probe"));
probe.send(&mut target, msg!( commands::MsgOk2 { data: 99 } ));
probe.expect_msg_any_of(
vec![
type_matcher!(responses::MsgResponse),
type_matcher!(responses::MsgResponse2),
type_matcher!(responses::MsgResponse3)
]
);
}
// This test will failed, uncomment for enable
// test_case!("MsgOk3 - respond with any of <MsgResponse, MsgResponse2, MsgResponse3>");
// {
// let (mut target, mut probe) = {
// let mut system = system.lock().unwrap();
// let mut target = system.actor_of(self::props(), None);
// let mut probe = system.create_probe(Some("probe"));
//
// (target, probe)
// };
//
// probe.send(target.clone(), msg!( commands::MsgOk3 { data: 99 } ));
//
// //Oops! Actor responds with message MsgResponse4 which does not in the set
// probe.expect_msg_any_of(
// vec![
// type_matcher!(responses::MsgResponse),
// type_matcher!(responses::MsgResponse2),
// type_matcher!(responses::MsgResponse3)
// ]
// );
// }
// This test will failed, uncomment for enable
// test_case!("MsgNoResponse - respond with any of <MsgResponse, MsgResponse2, MsgResponse3>");
// {
// let (mut target, mut probe) = {
// let mut system = system.lock().unwrap();
// let mut target = system.actor_of(self::props(), None);
// let mut probe = system.create_probe(Some("probe"));
//
// (target, probe)
// };
//
// probe.send(target.clone(), msg!( commands::MsgNoResponse { data: 99 } ));
//
// // Oops! Actor does not respond with any messages with expected timeout
// probe.expect_msg(pat_matcher!(responses::OtherResponse => responses::OtherResponse { data: 99 }));
// }
// ================================ expect_all_of ================================
test_case!("MsgOk4 - respond with all of <MsgResponse, MsgResponse2, MsgResponse3>");
{
let mut target = system.actor_of(self::props(None), None);
let mut probe = system.create_probe(Some("probe"));
probe.send(&mut target, msg!( commands::MsgOk4 { data: 99 } ));
probe.expect_msg_all_of(
vec![
type_matcher!(responses::MsgResponse),
type_matcher!(responses::MsgResponse2),
type_matcher!(responses::MsgResponse3)
]
);
}
// This test will failed, uncomment for enable
// test_case!("MsgOk5 - respond with all of <MsgResponse, MsgResponse2, MsgResponse3>");
// {
// let (mut target, mut probe) = {
// let mut system = system.lock().unwrap();
// let mut target = system.actor_of(self::props(None), None);
// let mut probe = system.create_probe(Some("probe"));
//
// (target, probe)
// };
//
// probe.send(target.clone(), msg!( commands::MsgOk5 { data: 99 } ));
//
// // Oops! Not all messages will be expected. This situation case timeout and it is correct!
// // It determines that the specified messages will be guaranteed from the stream, in what
// // order, with which intermediate messages it does not matter.
// probe.expect_msg_all_of(
// vec![
// type_matcher!(responses::MsgResponse),
// type_matcher!(responses::MsgResponse2),
// type_matcher!(responses::MsgResponse3)
// ]
// );
// }
// This test will failed, uncomment for enable
// test_case!("MsgNoResponse - respond with any of <MsgResponse, MsgResponse2, MsgResponse3>");
// {
// let (mut target, mut probe) = {
// let mut system = system.lock().unwrap();
// let mut target = system.actor_of(self::props(None), None);
// let mut probe = system.create_probe(Some("probe"));
//
// (target, probe)
// };
//
// probe.send(target.clone(), msg!( commands::MsgNoResponse { data: 99 } ));
//
// // Oops! Actor does not respond with all messages with expected timeout
// probe.expect_msg_all_of(
// vec![
// type_matcher!(responses::MsgResponse),
// type_matcher!(responses::MsgResponse2),
// type_matcher!(responses::MsgResponse3)
// ]
// );
// }
// ================================ expect_no_msg ================================
test_case!("MsgNoResponse - expect no message");
{
let mut target = system.actor_of(self::props(None), None);
let mut probe = system.create_probe(Some("probe"));
probe.send(&mut target, msg!( commands::MsgNoResponse { data: 99 } ));
probe.expect_no_msg(Duration::from_secs(1));
}
// This test will failed, uncomment for enable
// test_case!("MsgOther - not respond for any messages");
// {
// let (mut target, mut probe) = {
// let mut system = system.lock().unwrap();
// let mut target = system.actor_of(self::props(None), None);
// let mut probe = system.create_probe(Some("probe"));
//
// (target, probe)
// };
//
// probe.send(&mut target, msg!( commands::MsgOther { data: 99 } ));
//
// // Oops! In this expectation the probe should not have received any message, but received
// probe.expect_no_msg(Duration::from_secs(1));
// }
// ================================ complex chain ================================
// This test demonstrate complex messages testing. We send message to the actor, expect
// response from it, reply him and expect again some message.
test_case!("Process complex logic");
{
let mut target = system.actor_of(self::props(None), None);
let mut probe = system.create_probe(Some("probe"));
probe.send(&mut target, msg!( commands::MsgComplex0 { } ));
probe.expect_msg(pat_matcher!(responses::MsgResponse => responses::MsgResponse { data: 99 }));
probe.reply(msg!( commands::MsgComplex1 { } ));
probe.expect_msg(pat_matcher!(responses::MsgResponse2 => responses::MsgResponse2 { data: 199 }));
}
// ================================ injection tests ================================
// Referral testing - test case when you inject test probe actor as regular the target actor
// dependency. In this case, all message passed from the actor to the referal actor, will by
// intercepted by testprobe.
test_case!("Send message to the referal actor");
{
let mut probe = system.create_probe(Some("probe"));
// See to this code - we extract internal testprobe actor, and pass it as target
// actor dependency
let mut target = system.actor_of(self::props(Some(probe.aref())), None);
probe.send(&mut target, msg!(commands::ToRef {}));
probe.expect_msg(type_matcher!(logger::Log));
// And here you may reply to this message. Target actor receive is as from the referal actor
}
// ================================ access to the actor state ================================
test_case!("Get internal state");
{
let mut probe = system.create_probe(Some("probe"));
// See to this code - we extract internal testprobe actor, and pass it as target
// actor dependency
let mut target = system.actor_of(self::props(Some(probe.aref())), None);
probe.send(&mut target, msg!(commands::MutState { data: 599 }));
// Wait while message will reach the actor and mutate his state
probe.expect_no_msg(Duration::from_millis(500));
in_state! (target, BagsMan, actor => {
assert_eq!(actor.data, 599);
});
}
// ================================ death watch ================================
test_case!("Must terminate himself");
{
let mut probe = system.create_probe(Some("probe"));
// See to this code - we extract internal testprobe actor, and pass it as target
// actor dependency
let mut target = system.actor_of(self::props(Some(probe.aref())), None);
probe.watch(&target);
probe.send(&mut target, msg!(commands::Bomb { }));
probe.expect_terminated(&target);
}
// ================================ replace_actor_of ================================
test_case!("Must create actor and send him a message");
{
let mut fiction = system.create_probe(None);
let mut probe = system.create_probe(None);
let mut target = system.actor_of(self::props(Some(probe.aref())), None);
system.replace_actor_of(fiction.aref());
probe.send(&mut target, msg!(commands::CreateFiction { }));
fiction.expect_msg(type_matcher!(commands::MessageToFiction));
}
// ================================ various matchers ================================
// This section simply demonstrates, how various matchers functions may be constructed
// Match my message type
let _type_matcher = type_matcher!(logger::Log);
// Match by pattern (without guards)
let _pat_mather = pat_matcher!(logger::Log => logger::Log { text: _, target: logger::LogTarget::StdOut });
// Match by type with extended clarifications
let _extended_matcher = extended_type_matcher!(logger::Log, v => {
if v.text.len() > 100 {
true
} else {
false
}
});
// Flat matcher ( sweetened version of the raw matcher function )
let _flat_matcher = matcher!(v => {
if let Some(m) = v.get().downcast_ref::<logger::Log>() {
if m.text.len() > 100 {
match m.target {
logger::LogTarget::StdOut => true,
_ => false
}
} else {
false
}
} else {
false
}
});
// Raw matcher function, without any syntactic sugar
let _raw_matcher = Box::new(|v: &Box<Any + Send>| {
if let Some(m) = v.downcast_ref::<logger::Log>() {
if m.text.len() > 100 {
match m.target {
logger::LogTarget::StdOut => true,
_ => false
}
} else {
false
}
} else {
false
}
});
}
}