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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Probe of the actor testing system
//!
//! This object is used for controlled interaction with testing actor through sends messages to they
//! and receive messages with some expectations. For more complicated comments, see module level
//! documentation.

use crate::common::tsafe::TSafe;
use crate::testkit::actors::test_local_actor_system::TestLocalActorSystem;
use crate::actors::actor_ref_factory::ActorRefFactory;
use crate::actors::abstract_actor_ref::ActorRef;
use crate::actors::actor::{Actor, HandleResult};
use crate::actors::props::Props;
use crate::actors::watcher::events::Terminated;
use crate::actors::actor_context::ActorContext;
use crate::actors::message::Message;
use crate::actors::actor::PoisonPill;
use std::sync::{Arc, Mutex, Condvar, MutexGuard};
use std::any::Any;
use std::time::{ Duration, SystemTime };
use std::thread;

type Matcher = Box<Fn(Message) -> bool + Send>;

pub struct TestProbe {
    /// Probe name
    name: String,

    /// Work actor system
    system: TSafe<TestLocalActorSystem>,

    /// Default timeout for operations
    timeout: Duration,

    /// Internal actor
    inner_actor: Box<ActorRef>,

    /// Probe conditional variable. This var may unlock probe after receive some message or
    /// timer thread
    probe_cvar: Arc<Condvar>,

    /// Mutex for probe_cvar
    probe_cvar_m: Arc<Mutex<bool>>,

    /// Marker for the internal actor which indicates that he may consume the message
    actor_may_work: TSafe<bool>,

    /// Internal action  conditional variable. This var may unlock actor for consume some message
    actor_cvar: Arc<Condvar>,

    /// Internal probe timer
    timer: timer::Timer,

    /// List of current matchers
    matchers: TSafe<Vec<Matcher>>,

    /// Results of apply matchers list to the incoming messages
    match_results: TSafe<Vec<Option<bool>>>,

    /// Sender of the last received message
    last_sender: TSafe<ActorRef>,

    /// Last handled messages
    messages: TSafe<Vec<Message>>
}

impl TestProbe {

    /// Initialize new probe. This is internal construct, do not try use it directly
    pub fn new(system: TSafe<TestLocalActorSystem>, name: Option<&str>) -> TestProbe {
        let probe_cvar = Arc::new(Condvar::new());
        let actor_cvar = Arc::new(Condvar::new());
        let actor_cvar_m = Arc::new(Mutex::new(false));
        let matchers = tsafe!(Vec::new());
        let match_results = tsafe!(Vec::new());
        let actor_may_work = tsafe!(false);
        let last_sender = tsafe!(system.lock().unwrap().dead_letters());
        let messages = tsafe!(Vec::new());

        let actor = TestProbeActor::new(
            probe_cvar.clone(),
            matchers.clone(),
            match_results.clone(),
            actor_cvar.clone(),
             actor_cvar_m,
             actor_may_work.clone(),
            last_sender.clone(),
            messages.clone()
        );
        let actor = tsafe!(actor);
        let inner_actor = system.lock().unwrap().actor_of(Props::new(actor), name);
        let name = if name.is_some() {
            String::from(name.unwrap())
        } else {
            String::from("no_name")
        };

        TestProbe {
            name,
            system,
            timeout: Duration::from_secs(3),
            inner_actor: Box::new(inner_actor),
            probe_cvar,
            probe_cvar_m: Arc::new(Mutex::new(false)),
            actor_may_work,
            actor_cvar,
            timer: timer::Timer::new(),
            matchers,
            match_results,
            last_sender,
            messages
        }
    }

    /// Return internal actor reference
    pub fn aref(&mut self) -> ActorRef {
        self.inner_actor.clone()
    }

    /// Set default expects timeout
    pub fn set_timeout(&mut self, timeout: Duration) {
        self.timeout = timeout;
    }

    /// Send message to some actor
    pub fn send(&mut self, target: &mut ActorRef, msg: Message) {
        target.tell(msg, Some(&self.inner_actor))
    }

    /// Reply to the last sender with specified message
    pub fn reply(&mut self, msg: Message) {
        let mut last_sender = self.last_sender.lock().unwrap();
        last_sender.tell(msg, Some(&self.inner_actor))
    }

    /// Expect a single message from an actor. Blocks called thread while message will be received or
    /// timeout was reached. Returns intercepted message instance.
    ///
    /// # Example
    ///
    /// ```
    /// probe.expect_msg(type_matcher!(some_actor::SomeMsg));
    /// let msg = probe.expect_msg(type_matcher!(some_actor::SomeMsgOther));
    /// ```
    ///
    pub fn expect_msg(&mut self, matcher: Matcher) -> Message {
        // Clean early handled messages
        {
            let mut messages = self.messages.lock().unwrap();
            messages.clear();
        }

        // Set current matcher
        *self.matchers.lock().unwrap() = vec![matcher];
        *self.match_results.lock().unwrap() = vec![None];

        // Start timer
        let _guard = self.run_probe_timer(self.timeout);

        // This sleep is need for prevent skip unlocking from the probe actor if it receive message
        // early than this code is executed
        thread::sleep(Duration::from_millis(50));

        // Permits actor process messages
        *self.actor_may_work.lock().unwrap() = true;

        // Notify probe actor for unlock (if it was locked)
        self.actor_cvar.notify_one();

        // Lock current thread for waiting result of timeout
        self.lock();


        let result = self.match_results.lock().unwrap();

        if result[0].is_some() {
            let r = result[0].unwrap();
            if r == false {
                panic!("Test probe '{}' failed in 'expect_msg' with check error ( unexpected message received )", &self.name);
            }
            let mut messages = self.messages.lock().unwrap();
            messages.pop().unwrap()
        } else {
            panic!("Test probe '{}' failed in 'expect_msg' with timeout {} ms", &self.name, self.timeout.as_millis());
        }
    }

    /// Expect an any message in the specified set from an actor. Blocks called thread while message
    /// will be received or timeout was reached. Returns intercepted messages list.
    ///
    /// # Example
    ///
    /// ```
    /// probe.expect_msg_any_of(
    ///     vec![
    ///         type_matcher!(some_actor::SomeMsg0)),
    ///         type_matcher!(some_actor::SomeMsg1)),
    ///         type_matcher!(some_actor::SomeMsg2))
    ///     ]
    /// );
    /// ```
    ///
    pub fn expect_msg_any_of(&mut self, matchers: Vec<Matcher>) -> Message {
        // Clean early handled messages
        {
            let mut messages = self.messages.lock().unwrap();
            messages.clear();
        }

        // Set current matcher
        let mut filled_results = Vec::new();
        for _ in matchers.iter() {
            filled_results.push(None);
        }
        *self.matchers.lock().unwrap() = matchers;
        *self.match_results.lock().unwrap() = filled_results;

        // Start timer
        let _guard = self.run_probe_timer(self.timeout);

        // This sleep is need for prevent skip unlocking from the probe actor if it receive message
        // early than this code is executed
        thread::sleep(Duration::from_millis(50));

        // Permits actor process messages
        *self.actor_may_work.lock().unwrap() = true;



        // Notify probe actor for unlock (if it was locked)
        self.actor_cvar.notify_one();

        // Lock current thread for waiting result of timeout
        self.lock();


        let result = self.match_results.lock().unwrap();
        let mut timeout = false;

        for r in result.iter() {
            if r.is_some() {
                timeout = false;
            }
        }

        if !timeout {
            let mut found = false;
            for r in result.iter() {
                if r.is_some() {
                    if r.unwrap() == true {
                        found = true;
                    }
                }
            }

            if !found {
                panic!("Test probe '{}' failed in 'expect_msg_any_of' with check error ( unexpected message received )", &self.name);
            }

            let mut messages = self.messages.lock().unwrap();
            messages.pop().unwrap()
        } else {
            panic!("Test probe '{}' failed in 'expect_msg_any_of' with timeout {} ms", &self.name, self.timeout.as_millis());
        }
    }

    /// Expect all messages in specified set from an actor. Order of messages is not not significant.
    /// Target message may be altered with some other messages. Test will passed, when all messages
    /// from the list, will be intercepted from input messages stream. Blocks called thread while
    /// message will be received or timeout was reached. Returns intercepted messages list.
    ///
    /// # Example
    ///
    /// ```
    /// probe.expect_msg_all_of(
    ///     vec![
    ///         type_matcher!(some_actor::SomeMsg0)),
    ///         type_matcher!(some_actor::SomeMsg1)),
    ///         type_matcher!(some_actor::SomeMsg2))
    ///     ]
    /// );
    /// ```
    ///
    pub fn expect_msg_all_of(&mut self, matchers: Vec<Matcher>) -> Vec<Message> {
        // Clean early handled messages
        {
            let mut messages = self.messages.lock().unwrap();
            messages.clear();
        }

        let m_len = matchers.len();
        //Internal match results
        let mut internal_results: Vec<Option<bool>> = Vec::new();

        for _ in 0..m_len {
            internal_results.push(None);
        }
        *self.matchers.lock().unwrap() = matchers;


        let started = SystemTime::now();

        // Start timer
        let _guard = self.run_probe_timer(self.timeout);

        // This sleep is need for prevent skip unlocking from the probe actor if it receive message
        // early than this code is executed
        thread::sleep(Duration::from_millis(50));

        // Collect messages
        while true {
            // Set current matcher
            let mut filled_results = Vec::new();
            for _ in  0..m_len {
                filled_results.push(None);
            }
            *self.match_results.lock().unwrap() = filled_results;

            // Permits actor process messages
            *self.actor_may_work.lock().unwrap() = true;

            // Notify probe actor for unlock (if it was locked)
            self.actor_cvar.notify_one();

            // Lock current thread for waiting result of timeout
            self.lock();


            let result = self.match_results.lock().unwrap();
            let elapsed = started.elapsed().unwrap().as_millis();
            let timeout = elapsed >= self.timeout.as_millis();

            if !timeout {
                let mut counter = 0;
                for i in 0..m_len {

                    if internal_results[i].is_none() {
                        let r = result[i].unwrap();
                        if r {
                            internal_results[i] = result[i];
                        }
                    }

                    counter = counter + 1;
                }

                let mut must_cont = false;
                for r in internal_results.iter() {
                    if r.is_none() {
                        must_cont = true;
                    }
                }
                if must_cont {
                    continue;
                }

                //let mut all_ok = false;

                for r in internal_results.iter() {
                    if r.unwrap() == false {
                        panic!("Test probe '{}' failed in 'expect_msg_all_of' with check error ( not all received messages match the patterns )", &self.name);
                    }
                }

                break;

            } else {
                panic!("Test probe '{}' failed in 'expect_msg_all_of' with timeout {} ms", &self.name, self.timeout.as_millis());
            }
        }

        let messages = self.messages.lock().unwrap();
        messages.iter().map(|v| v.clone()).collect()
    }

    /// Expect than no one actor do not send message to this probe in specified time duration
    ///
    /// # Example
    ///
    /// ```
    /// probe.expect_no_msg(Duration::from_secs(1));
    /// ```
    ///
    pub fn expect_no_msg(&mut self, duration: Duration) {
        // Clean last match result
        //*self.match_result.lock().unwrap() = None;

        // Set 'all' matcher
        *self.matchers.lock().unwrap() = vec![matcher! { _v => true }];
        *self.match_results.lock().unwrap() = vec![None];

        // Start timer
        let _guard = self.run_probe_timer(duration);

        // This sleep is need for prevent skip unlocking from the probe actor if it receive message
        // early than this code is executed
        thread::sleep(Duration::from_millis(50));

        // Permits actor process messages
        *self.actor_may_work.lock().unwrap() = true;

        // Notify probe actor for unlock (if it was locked)
        self.actor_cvar.notify_one();

        // Lock current thread for waiting result of timeout
        self.lock();

        let result = self.match_results.lock().unwrap();

        if result[0].is_some() {
            if result[0].unwrap() == true {
                panic!("Test probe '{}' failed in 'expect_no_msg' with check error ( message was received but should not )", &self.name);
            }
        }
    }

    /// Expect termination of the target. Probe must be watch it before cull this expector.
    ///
    /// # Example
    ///
    /// ```
    ///  probe.watch(&target);
    ///  // Some other code
    ///  probe.expect_terminated(&target);
    /// ```
    ///
    pub fn expect_terminated(&mut self, target: &ActorRef) {
        let terminated_matcher = type_matcher!(Terminated);
        // Set current matcher
        *self.matchers.lock().unwrap() = vec![terminated_matcher];
        *self.match_results.lock().unwrap() = vec![None];

        // Start timer
        let _guard = self.run_probe_timer(self.timeout);

        // This sleep is need for prevent skip unlocking from the probe actor if it receive message
        // early than this code is executed
        thread::sleep(Duration::from_millis(50));

        // Permits actor process messages
        *self.actor_may_work.lock().unwrap() = true;

        // Notify probe actor for unlock (if it was locked)
        self.actor_cvar.notify_one();

        // Lock current thread for waiting result of timeout
        self.lock();


        let result = self.match_results.lock().unwrap();

        if result[0].is_some() {
            let r = result[0].unwrap();
            if r == false {
                panic!("Test probe '{}' failed in 'expect_terminated' with check error ( target must be terminated but instead sent some message )", &self.name);
            } else {
                if self.last_sender.lock().unwrap().path() != target.path() {
                    panic!("Test probe '{}' failed in 'expect_terminated' with check error ( actor terminated, but not target )", &self.name);

                }
            }
        } else {
            panic!("Test probe '{}' failed in 'expect_terminated' with timeout {} ms", &self.name, self.timeout.as_millis());
        }
    }

    /// Subscribe probe fot watching events of target
    pub fn watch(&mut self, target: &ActorRef) {
        self.system.lock().unwrap().watch(&self.inner_actor, target);
        //unwatch in drop and actor stop
    }

    /// Returns the sender of the last received message.
    pub fn get_last_sender(&self) -> MutexGuard<ActorRef> {
        self.last_sender.lock().unwrap()
    }

    /// Stops the probe inner actor
    pub fn stop(&mut self) {
        // Set 'all' matcher
        *self.matchers.lock().unwrap() = vec![matcher! { _v => true }];
        *self.match_results.lock().unwrap() = vec![None];

        // Permits actor process messages
        *self.actor_may_work.lock().unwrap() = true;

        // Notify probe actor for unlock (if it was locked)
        self.actor_cvar.notify_one();

        self.system.lock().unwrap().stop(&mut self.inner_actor);
    }

    /// Internal locker for awaiting messages from the internal actor or timeout
    fn lock(&mut self) {
        self.probe_cvar.wait( self.probe_cvar_m.lock().unwrap());
    }

    /// Run probe timer witch must be unlock probe_cvar, that indicated what expectation does not
    /// satisfied with specified timeout
    fn run_probe_timer(&mut self, timeout: Duration) -> timer::Guard {
        let cvar = self.probe_cvar.clone();
        self.timer.schedule_with_delay(chrono::Duration::from_std(timeout).ok().unwrap(), move || {
            cvar.notify_one();
            //println!("xxx");
        })
    }
}

impl Drop for TestProbe {
    fn drop(&mut self) {
        self.system.lock().unwrap().stop(&mut self.inner_actor.clone());
        *self.actor_may_work.lock().unwrap() = true;
        self.actor_cvar.notify_one();
    }
}

/// Internal test actor
///
/// This actor receive all messages, pass it through list of matchers functions, and create mapped
/// results list of each matcher result. On this results list, probe will be to make decisions
/// about passing concrete test action
///
struct TestProbeActor {
    probe_cvar: Arc<Condvar>,
    matchers: TSafe<Vec<Matcher>>,
    match_results: TSafe<Vec<Option<bool>>>,
    actor_cvar: Arc<Condvar>,
    actor_cvar_m: Arc<Mutex<bool>>,
    actor_may_work: TSafe<bool>,
    last_sender: TSafe<ActorRef>,
    messages: TSafe<Vec<Message>>
}

impl TestProbeActor {
    pub fn new(
        probe_cvar: Arc<Condvar>,
        matchers: TSafe<Vec<Matcher>>,
        match_results: TSafe<Vec<Option<bool>>>,
        actor_cvar: Arc<Condvar>,
        actor_cvar_m: Arc<Mutex<bool>>,
        actor_may_work: TSafe<bool>,
        last_sender: TSafe<ActorRef>,
        messages: TSafe<Vec<Message>>) -> TestProbeActor {

        let _test_matcher = |_v: &Box<Any + Send>| {
            true
        };
        TestProbeActor {
            probe_cvar,
            matchers,
            match_results,
            actor_cvar,
            actor_cvar_m,
            actor_may_work,
            last_sender,
            messages
        }
    }

    fn lock(&mut self) {
        self.actor_cvar.wait( self.actor_cvar_m.lock().unwrap());
    }
}

impl Actor for TestProbeActor {

    fn receive(&mut self, msg: Message, ctx: ActorContext) -> HandleResult {
        if *self.actor_may_work.lock().unwrap() == false {
            self.lock();
        }
        *self.actor_may_work.lock().unwrap() = false;

        // If handled message is PoisonPill we must mark this message as unhandled
        {
            let msg = msg.get();
            if let Some(m) = msg.downcast_ref::<PoisonPill>() {
                return Ok(false);
            }
        }

        //Set the message sender
        *self.last_sender.lock().unwrap() = ctx.sender.clone();

        let matchers = self.matchers.lock().unwrap();
        let mut match_results = self.match_results.lock().unwrap();

        let mut counter = 0;
        for m in matchers.iter() {
            if match_results[counter] == None {
                let result = (m)(msg.clone());
                if result {
                    let mut messages = self.messages.lock().unwrap();
                    messages.push(msg.clone());
                }
                match_results[counter] = Some(result);
            }

            counter = counter + 1;
        }

        self.probe_cvar.notify_one();

        Ok(true)
    }

}