Lines
96.77 %
Functions
100 %
Branches
//! Test doubles for the eval link: one records the frames it is sent, one
//! lets a test inject the replies.
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use super::ConsoleEval;
const REPLY_SLOTS: usize = 32;
/// The frames a recording console received, oldest first.
#[derive(Clone, Default)]
pub(crate) struct Sent(Arc<Mutex<Vec<String>>>);
impl Sent {
pub(crate) fn frames(&self) -> Vec<String> {
self.0
.lock()
.map(|frames| frames.clone())
.unwrap_or_default()
}
fn record(&self, frame: &str) {
if let Ok(mut frames) = self.0.lock() {
frames.push(frame.to_owned());
fn double(replies: mpsc::Receiver<String>) -> (ConsoleEval, Sent) {
let sent = Sent::default();
let recorder = sent.clone();
let frame = Box::new(move |frame: &str| {
recorder.record(frame);
None
});
(ConsoleEval::over(frame, replies, None), sent)
impl ConsoleEval {
/// A console that records every frame and never replies.
pub(crate) fn recording() -> (Self, Sent) {
double(mpsc::channel(REPLY_SLOTS).1)
/// A console that records frames; the test sends the replies through the returned sender.
pub(crate) fn scripted() -> (Self, mpsc::Sender<String>) {
let (replies_tx, replies) = mpsc::channel(REPLY_SLOTS);
(double(replies).0, replies_tx)
/// A console whose request ids ran out.
pub(crate) fn broken() -> Self {
let mut eval = Self::recording().0;
eval.next_id = i64::MAX;
eval