1
//! Test doubles for the eval link: one records the frames it is sent, one
2
//! lets a test inject the replies.
3

            
4
use std::sync::{Arc, Mutex};
5

            
6
use tokio::sync::mpsc;
7

            
8
use super::ConsoleEval;
9

            
10
const REPLY_SLOTS: usize = 32;
11

            
12
/// The frames a recording console received, oldest first.
13
#[derive(Clone, Default)]
14
pub(crate) struct Sent(Arc<Mutex<Vec<String>>>);
15

            
16
impl Sent {
17
113
    pub(crate) fn frames(&self) -> Vec<String> {
18
113
        self.0
19
113
            .lock()
20
113
            .map(|frames| frames.clone())
21
            .unwrap_or_default()
22
113
    }
23

            
24
1201
    fn record(&self, frame: &str) {
25
1201
        if let Ok(mut frames) = self.0.lock() {
26
1201
            frames.push(frame.to_owned());
27
        }
28
1201
    }
29
}
30

            
31
605
fn double(replies: mpsc::Receiver<String>) -> (ConsoleEval, Sent) {
32
605
    let sent = Sent::default();
33
605
    let recorder = sent.clone();
34
1201
    let frame = Box::new(move |frame: &str| {
35
1201
        recorder.record(frame);
36
1201
        None
37
1201
    });
38
605
    (ConsoleEval::over(frame, replies, None), sent)
39
605
}
40

            
41
impl ConsoleEval {
42
    /// A console that records every frame and never replies.
43
602
    pub(crate) fn recording() -> (Self, Sent) {
44
602
        double(mpsc::channel(REPLY_SLOTS).1)
45
602
    }
46

            
47
    /// A console that records frames; the test sends the replies through the returned sender.
48
3
    pub(crate) fn scripted() -> (Self, mpsc::Sender<String>) {
49
3
        let (replies_tx, replies) = mpsc::channel(REPLY_SLOTS);
50
3
        (double(replies).0, replies_tx)
51
3
    }
52

            
53
    /// A console whose request ids ran out.
54
3
    pub(crate) fn broken() -> Self {
55
3
        let mut eval = Self::recording().0;
56
3
        eval.next_id = i64::MAX;
57
3
        eval
58
3
    }
59
}