Browse Source

Dedupe TestSymbolRunner

master
Adrian Heine 2 years ago
parent
commit
445b3edd9d
  1. 29
      src/setup/mod.rs
  2. 32
      src/setup/runnable.rs
  3. 38
      src/setup/symbol_runner.rs

29
src/setup/mod.rs

@ -169,7 +169,7 @@ impl<
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::SymbolRunner;
use super::symbol_runner::TestSymbolRunner;
use crate::async_utils::run; use crate::async_utils::run;
use crate::loggers::{Entry, StoringLogger}; use crate::loggers::{Entry, StoringLogger};
use crate::resources::{FromArtifact, FromResource, Resource}; use crate::resources::{FromArtifact, FromResource, Resource};
@ -178,33 +178,11 @@ mod test {
use crate::{ImplementationBuilder, ResourceLocator, Setup}; use crate::{ImplementationBuilder, ResourceLocator, Setup};
use async_trait::async_trait; use async_trait::async_trait;
use regex::Regex; use regex::Regex;
use slog::{info, Logger};
use std::cell::RefCell; use std::cell::RefCell;
use std::error::Error; use std::error::Error;
use std::fmt::Debug; use std::fmt::Debug;
use std::rc::{Rc, Weak}; use std::rc::{Rc, Weak};
struct TestSymbolRunner {
count: Rc<RefCell<usize>>,
}
#[async_trait(?Send)]
impl SymbolRunner for TestSymbolRunner {
async fn run_symbol<S: Symbol + Debug>(
&self,
symbol: &S,
logger: &Logger,
force: bool,
) -> Result<bool, Box<dyn Error>> {
info!(logger, "run");
let run = force || !symbol.target_reached().await?;
if run {
*self.count.borrow_mut() += 1;
}
Ok(run)
}
}
#[derive(Debug, PartialEq, Eq, Hash)] #[derive(Debug, PartialEq, Eq, Hash)]
struct TestResource<T>(&'static str, T); struct TestResource<T>(&'static str, T);
impl<T> Resource for TestResource<T> { impl<T> Resource for TestResource<T> {
@ -308,10 +286,7 @@ mod test {
>, >,
StoringLogger, StoringLogger,
) { ) {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
let (count, runner) = TestSymbolRunner::new();
let logger = StoringLogger::new(); let logger = StoringLogger::new();
(count, Setup::new_with(runner, logger.clone()), logger) (count, Setup::new_with(runner, logger.clone()), logger)
} }

32
src/setup/runnable.rs

@ -53,10 +53,10 @@ for_each_tuple!(runnable_for_tuple);
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::super::symbol_runner::TestSymbolRunner;
use super::Runnable; use super::Runnable;
use crate::async_utils::run; use crate::async_utils::run;
use crate::symbols::Symbol; use crate::symbols::Symbol;
use crate::SymbolRunner;
use async_trait::async_trait; use async_trait::async_trait;
use slog::{o, Discard, Logger}; use slog::{o, Discard, Logger};
use std::cell::RefCell; use std::cell::RefCell;
@ -103,39 +103,11 @@ mod test {
} }
} }
struct TestSymbolRunner {
count: Rc<RefCell<usize>>,
}
fn get_runner() -> (Rc<RefCell<usize>>, TestSymbolRunner) {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
(count, runner)
}
#[async_trait(?Send)]
impl SymbolRunner for TestSymbolRunner {
async fn run_symbol<S: Symbol + Debug>(
&self,
symbol: &S,
_logger: &Logger,
force: bool,
) -> Result<bool, Box<dyn Error>> {
let run = force || !symbol.target_reached().await?;
if run {
*self.count.borrow_mut() += 1;
}
Ok(run)
}
}
fn run_symbol( fn run_symbol(
runnable: impl Runnable, runnable: impl Runnable,
force: bool, force: bool,
) -> (Rc<RefCell<usize>>, Result<bool, Box<dyn Error>>) { ) -> (Rc<RefCell<usize>>, Result<bool, Box<dyn Error>>) {
let (count, runner) = get_runner();
let (count, runner) = TestSymbolRunner::new();
let res = run(runnable.run(&runner, &Logger::root(Discard, o!()), force)); let res = run(runnable.run(&runner, &Logger::root(Discard, o!()), force));
(count, res) (count, res)
} }

38
src/setup/symbol_runner.rs

@ -2,9 +2,13 @@ use crate::async_utils::sleep;
use crate::symbols::Symbol; use crate::symbols::Symbol;
use async_trait::async_trait; use async_trait::async_trait;
use slog::{debug, info, o, trace, Logger}; use slog::{debug, info, o, trace, Logger};
#[cfg(test)]
use std::cell::RefCell;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
#[cfg(test)]
use std::rc::Rc;
use std::time::Duration; use std::time::Duration;
#[async_trait(?Send)] #[async_trait(?Send)]
@ -198,6 +202,40 @@ where
} }
} }
#[cfg(test)]
pub struct TestSymbolRunner {
count: Rc<RefCell<usize>>,
}
#[cfg(test)]
impl TestSymbolRunner {
pub fn new() -> (Rc<RefCell<usize>>, Self) {
let count = Rc::new(RefCell::new(0));
let runner = TestSymbolRunner {
count: Rc::clone(&count),
};
(count, runner)
}
}
#[cfg(test)]
#[async_trait(?Send)]
impl SymbolRunner for TestSymbolRunner {
async fn run_symbol<S: Symbol + Debug>(
&self,
symbol: &S,
logger: &Logger,
force: bool,
) -> Result<bool, Box<dyn Error>> {
info!(logger, "run");
let run = force || !symbol.target_reached().await?;
if run {
*self.count.borrow_mut() += 1;
}
Ok(run)
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::{DrySymbolRunner, InitializingSymbolRunner, ReportingSymbolRunner, SymbolRunner}; use super::{DrySymbolRunner, InitializingSymbolRunner, ReportingSymbolRunner, SymbolRunner};

Loading…
Cancel
Save