aector 0.1.1

Dynamic implementation of the actor model with a smart built-in testing framework.
Documentation
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
use std::any::{Any, TypeId};
use std::collections::{VecDeque};
use std::fmt::{Debug, Formatter};
use crate::actor::{Actor, MailboxType};
use crate::{Addr, Message};
use crate::behavior::{Behavior, BehaviorBuilder, BehaviorAction, StateCheckMessage};
use crate::testing::actor_test::ResponseDyn::Check;
use crate::testing::actor_test::TestActorState::{PendingResponse, Ready};
use thiserror::Error;


#[derive(Error, Debug)]
enum ActorTestError {
    #[error("Invalid message order")]
    InvalidMessageOrder,
    #[error("Given criteria not fulfilled")]
    CriteriaNotMet,
    #[error("State check failed")]
    StateCheckFailed
}

/// This type represents an expected message response.
pub enum Response<M: Any + Send> {
    Ask(fn(M) -> bool),
    Tell(fn(M) -> bool),
    Check
}

impl<M> From<Response<M>> for ResponseDyn
where
    M: Any + Send
{
    fn from(res_t: Response<M>) -> Self {
        let type_id = TypeId::of::<M>();
        match res_t {
            Response::Ask(criteria) => {
                let crit_wrapper = ResponseDyn::wrap(criteria);
                ResponseDyn::Ask(type_id, crit_wrapper)
            }
            Response::Tell(criteria) => {
                let crit_wrapper = ResponseDyn::wrap(criteria);
                ResponseDyn::Tell(type_id, crit_wrapper)
            }
            Response::Check => {
                ResponseDyn::Check
            }
        }
    }
}

/// Dynamically typed responses. Only used to store responses internally (since generics cant be
/// directly stored in Vec)
enum ResponseDyn {
    Ask(TypeId, Box<dyn Fn(Message) -> bool + Send>),
    Tell(TypeId, Box<dyn Fn(Message) -> bool + Send>),
    Check
}

impl ResponseDyn {
    /// Wraps a given type and criteria into a dynamically typed enum
    pub fn tell<M: Any + Send>(criteria: fn(M) -> bool) -> ResponseDyn {
        let crit_wrapped = Self::wrap(criteria);
        ResponseDyn::Tell(TypeId::of::<M>(), crit_wrapped)
    }

    /// Wraps the given, generically typed closure into a dynamically typed, boxed closure.
    fn wrap<M: Any + Send>(criteria: fn(M) -> bool) -> Box<dyn Fn(Message) -> bool + Send> {
        let crit_wrapper = Box::new(move |msg: Message| -> bool {
            // downcasting generic message into concrete type
            if msg.instance_of::<M>() {
                // note: m.sender is totally ignored here i.e. can be Some(tx) or None
                let m = msg.downcast::<M>();
                // passing downcasted message on to user defined handler
                criteria(*m)
            } else {
                // this case should never occur, but if it does something has gone really wrong
                panic!("Invalid downcasting operation!")
            }
        });
        return crit_wrapper;
    }
}

/// Represents the state of the FSM of the testing actor.
enum TestActorState {
    Ready,
    PendingResponse(ResponseDyn)
}

/// Represents test-tasks defined by the user.
enum TestTask<S> {
    Tell(Message, u32),
    Ask(Message, ResponseDyn, u32),
    Check(fn(&S) -> bool, u32),
    Expect(ResponseDyn, u32),
    Exit
}

impl<S> Debug for TestTask<S> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            TestTask::Tell(_, nr) => {
                write!(f, "Tell #{}", nr)
            }
            TestTask::Ask(_, _, nr) => {
                write!(f, "Ask #{}", nr)
            }
            TestTask::Check(_, nr) => {
                write!(f, "Check #{}", nr)
            }
            TestTask::Expect(_, nr) => {
                write!(f, "Expect #{}", nr)
            }
            TestTask::Exit => {
                write!(f, "Exit")
            }
        }

    }
}

/// This struct is used to store the testing state of an [TestActor]
pub struct TestActor<S> {
    addr: Addr,
    tasks: VecDeque<TestTask<S>>,
    test_state: TestActorState
}

/// Message used to reschedule messages to [TestActor].
enum TestActorMessage {
    RunNext
}

impl<S: Send + 'static> TestActor<S> {

    /// Returns blanket behavior for [TestActor]. This performs the task work loop
    fn get_blanket_behavior() -> BehaviorBuilder<TestActor<S>> {
        BehaviorBuilder::new()
            .on_start(|_state: &mut TestActor<S>, ctx| {
                // trigger self-loop for going through tasks
                ctx.get_addr().tell(TestActorMessage::RunNext);
            })
            .on_tell::<StateCheckMessage<S>>(|msg, state, ctx| -> BehaviorAction<TestActor<S>> {
                match &state.test_state {
                    Ready => {
                        // panic!("Did not expect a check result in the current state");
                        return Err(Box::new(ActorTestError::InvalidMessageOrder));
                    }
                    PendingResponse(resp) => {
                        match resp {
                            Check => {
                                // this handler is triggered if a result from last check_state query is received
                                match msg {
                                    StateCheckMessage::Check(_) => {
                                        // this message is only sent to the actor to be tested and should never come back
                                    }
                                    StateCheckMessage::Result(check_result) => {
                                        state.test_state = Ready;
                                        if check_result == false {
                                            // panic!("state check failed!");
                                            return Err(Box::new(ActorTestError::StateCheckFailed));
                                        }
                                    }
                                }

                                // continue working on tasks
                                ctx.get_addr().tell(TestActorMessage::RunNext);
                            }
                            _ => {
                                // panic!("Did not expect a check result in the current state");
                                return Err(Box::new(ActorTestError::InvalidMessageOrder));
                            }
                        }
                    }
                }

                Behavior::keep()
            })
            .on_tell::<TestActorMessage>(|_msg, state, ctx| -> BehaviorAction<TestActor<S>> {
                // this handlers job is to run the given test tasks
                if let Some(task) = state.tasks.pop_front() {
                    println!("Current task: {:?}", &task);
                    match task {
                        TestTask::Tell(msg, _id) => {
                            state.addr.send(msg);
                            ctx.get_addr().tell(TestActorMessage::RunNext);
                        },
                        TestTask::Ask(mut msg, response, _id) => {
                            // fill in reply_to such that ask queries are responded to this actor
                            msg.sender = Some(ctx.get_addr());
                            state.test_state = TestActorState::PendingResponse(response);

                            // send tell message to actor
                            state.addr.send(msg);
                        },
                        TestTask::Check(check_fn, _id) => {
                            state.addr.ask(StateCheckMessage::<S>::Check(check_fn), ctx.get_addr());
                            state.test_state = PendingResponse(ResponseDyn::Check);
                        }
                        TestTask::Expect(response, _id) => {
                            state.test_state = TestActorState::PendingResponse(response);
                        }
                        TestTask::Exit => {
                            ctx.kill()
                        }
                    }
                }

                // if no message to this handler is rescheduled above the test is done
                Behavior::keep()
            })
    }

}

/// This builder is used to build a [TestActor].
pub struct ActorTestBuilder<S: Send + 'static> {
    behavior_builder: BehaviorBuilder<TestActor<S>>,
    addr: Addr,
    tasks: VecDeque<TestTask<S>>,
    test_state: TestActorState,
    task_id_gen: u32
}

/// This enum represents the possible message types an [Actor] can send.
pub enum MessageType<M> {
    Tell(M),
    Ask(M)
}

impl<S: Send + 'static> ActorTestBuilder<S> {

    /// Creates a new [ActorTestBuilder] with some default settings which are needed to run tests.
    pub fn new(addr: Addr) -> Self {
        let blanket_behavior_builder = TestActor::get_blanket_behavior();

        ActorTestBuilder {
            behavior_builder: blanket_behavior_builder,
            addr: addr,
            tasks: VecDeque::new(),
            test_state: TestActorState::Ready,
            task_id_gen: 0
        }
    }

    /// All tasks are enumerated with a locally (test scope) unique id.
    fn next_task_id(&mut self) -> u32 {
        self.task_id_gen += 1;
        self.task_id_gen -1
    }

    /// Adds the given check function to the test list. check_fn can immutably access the whole
    /// internal state of the actor to be tested.
    pub fn check(mut self, check_fn: fn(&S) -> bool) -> Self {
        let next_id = self.next_task_id();
        self.tasks.push_back(TestTask::Check(check_fn, next_id));
        self
    }

    /// Sends the given message to the actor to be tested.
    pub fn tell<M: Any + Send>(mut self, msg: M) -> Self {
        let msg = Message::without_sender(msg);
        let next_id = self.next_task_id();
        self.tasks.push_back(TestTask::Tell(msg, next_id));
        self
    }

    /// Sends the given message to the actor to be tested and automatically inserts the [TestActor]'s
    /// address into the reply_to field. Further an expected [Response] has to be defined which is also
    /// checked.
    pub fn ask<M: Any + Send, R: Any + Send>(mut self, msg: M, expected_response: Response<R>) -> Self {
        // without_sender is used here since the addr of this TestActor is not known yet and will
        // be filled in later
        let msg = Message::without_sender(msg);
        let expected_response: ResponseDyn = expected_response.into();

        let next_id = self.next_task_id();

        match &expected_response {
            ResponseDyn::Ask(_, _) => {
                self.tasks.push_back(TestTask::Ask(msg, expected_response, next_id));
                self.set_default_ask_response_behavior::<R>()
            }
            ResponseDyn::Tell(_, _) => {
                self.tasks.push_back(TestTask::Ask(msg, expected_response, next_id));
                self.set_default_tell_response_behavior::<R>()
            }
            Check => {
                self.tasks.push_back(TestTask::Ask(msg, expected_response, next_id));
                self
            }
        }
    }

    /// Adds the default tell message handler for a given type M. This is needed such that the TestActor
    /// can receive responses of not yet defined messages.
    fn set_default_tell_response_behavior<M: Any + Send>(mut self) -> Self {
        if !self.behavior_builder.has_tell_handler(TypeId::of::<M>()) {
            // no handler for this type exists yet!

            self.behavior_builder = self.behavior_builder
                .on_tell::<M>(|msg, state, ctx| -> BehaviorAction<TestActor<S>> {

                    match &state.test_state {
                        Ready => {
                            // panic!("did not expect a tell message");
                            return Err(Box::new(ActorTestError::InvalidMessageOrder));
                        }
                        PendingResponse(resp) => {
                            match resp {
                                ResponseDyn::Ask(_, _) => {
                                    // panic!("did not expect an ask message");
                                    return Err(Box::new(ActorTestError::InvalidMessageOrder));
                                }
                                ResponseDyn::Tell(expected_type_id, criteria) => {
                                    if TypeId::of::<M>() == *expected_type_id {
                                        if criteria(Message::without_sender(msg)) == false {
                                            return Err(Box::new(ActorTestError::CriteriaNotMet));
                                            // panic!("tell message did not pass criteria check!");
                                        }
                                    }
                                }
                                Check => {
                                    // panic!("did not expect a check message")
                                    return Err(Box::new(ActorTestError::InvalidMessageOrder));
                                }
                            }
                        }
                    }

                    state.test_state = TestActorState::Ready;
                    // continue working on tasks
                    ctx.get_addr().tell(TestActorMessage::RunNext);
                    Behavior::keep()
                });
        }
        self
    }

    /// Adds the default ask message handler for a given type M. This is needed such that the TestActor
    /// can receive responses of not yet defined messages.
    fn set_default_ask_response_behavior<M: Any + Send>(mut self) -> Self {
        if !self.behavior_builder.has_ask_handler(TypeId::of::<M>()) {
            // no handler for this type exists yet!

            self.behavior_builder = self.behavior_builder
                .on_ask::<M>(|msg, state, _addr, ctx| -> BehaviorAction<TestActor<S>> {

                    match &state.test_state {
                        Ready => {
                            // panic!("did not expect an ask message");
                            return Err(Box::new(ActorTestError::InvalidMessageOrder));
                        }
                        PendingResponse(resp) => {
                            match resp {
                                ResponseDyn::Ask(expected_type_id, criteria) => {
                                    if TypeId::of::<M>() == *expected_type_id {
                                        if criteria(Message::without_sender(msg)) == false {
                                            // panic!("ask message did not pass criteria check!");
                                            return Err(Box::new(ActorTestError::CriteriaNotMet));
                                        }
                                    }
                                }
                                ResponseDyn::Tell(_, _) => {
                                    // panic!("did not expect a tell message");
                                    return Err(Box::new(ActorTestError::InvalidMessageOrder));
                                }
                                Check => {
                                    // panic!("did not expect a check message")
                                    return Err(Box::new(ActorTestError::InvalidMessageOrder));
                                }
                            }
                        }
                    }
                    // continue working on tasks
                    ctx.get_addr().tell(TestActorMessage::RunNext);
                    Behavior::keep()
                });
        }
        self
    }

    /// This function defines that a tell-message is to be received next with the given condition.
    /// If no specific condition is required a simple |msg| true can be passed in.
    pub fn expect_tell<M: Any + Send>(mut self, criteria: fn(M) -> bool) -> Self {
        let task = TestTask::<S>::Expect(ResponseDyn::tell(criteria), self.next_task_id());
        self.tasks.push_back(task);
        self.set_default_tell_response_behavior::<M>()
    }

    /// This function defines that an ask-message is to be received next with the given condition.
    /// If no specific condition is required a simple |msg| true can be passed in.
    pub fn expect_ask<M: Any + Send>(mut self, criteria: fn(M) -> bool) -> Self {
        let task = TestTask::<S>::Expect(ResponseDyn::tell(criteria), self.next_task_id());
        self.tasks.push_back(task);
        self.set_default_tell_response_behavior::<M>()
    }

    /// Consumes the builder and generates an Actor which represents the defined testing behavior.
    pub fn build(mut self) -> Actor<TestActor<S>> {
        // add exit task at end of test tasks
        self.tasks.push_back(TestTask::Exit);

        let state = TestActor {
            addr: self.addr,
            tasks: self.tasks,
            test_state: TestActorState::Ready
        };

        Actor::new(state, self.behavior_builder.build(), MailboxType::Unbounded)

    }

}