Browse Source

Add DelayingSymbolRunner for testing

master
Adrian Heine 2 years ago
parent
commit
9200e83a0a
  1. 3
      src/lib.rs
  2. 3
      src/setup/mod.rs
  3. 37
      src/setup/symbol_runner.rs

3
src/lib.rs

@ -50,5 +50,6 @@ mod to_artifact;
pub use builder::{DefaultBuilder, ImplementationBuilder};
pub use locator::{DefaultLocator, DefaultPolicy, Policy, ResourceLocator};
pub use setup::{
DrySymbolRunner, InitializingSymbolRunner, ReportingSymbolRunner, Setup, SymbolRunner,
DelayingSymbolRunner, DrySymbolRunner, InitializingSymbolRunner, ReportingSymbolRunner, Setup,
SymbolRunner,
};

3
src/setup/mod.rs

@ -3,7 +3,8 @@ mod util;
pub use util::{AddResult, AddableResource};
mod symbol_runner;
pub use symbol_runner::{
DrySymbolRunner, InitializingSymbolRunner, ReportingSymbolRunner, SymbolRunner,
DelayingSymbolRunner, DrySymbolRunner, InitializingSymbolRunner, ReportingSymbolRunner,
SymbolRunner,
};
mod runnable;
mod setup;

37
src/setup/symbol_runner.rs

@ -1,9 +1,11 @@
use crate::async_utils::sleep;
use crate::loggers::Logger;
use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::fmt;
use std::fmt::Debug;
use std::time::Duration;
#[async_trait(?Send)]
pub trait SymbolRunner {
@ -96,6 +98,41 @@ impl SymbolRunner for InitializingSymbolRunner {
}
}
#[derive(Clone, Debug)]
pub struct DelayingSymbolRunner<R>(R);
impl<R> DelayingSymbolRunner<R> {
pub fn new(symbol_runner: R) -> Self {
Self(symbol_runner)
}
}
#[async_trait(?Send)]
impl<R> SymbolRunner for DelayingSymbolRunner<R>
where
R: SymbolRunner,
{
async fn run_symbol<S: Symbol + Debug, L: Logger>(
&self,
symbol: &S,
logger: &L,
force: bool,
) -> Result<bool, Box<dyn Error>> {
sleep(Duration::from_millis(
(((std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.subsec_micros()
% 20) as f32
/ 2.0)
.exp()
/ 8.0) as u64,
))
.await;
self.0.run_symbol(symbol, logger, force).await
}
}
#[derive(Clone, Debug, Default)]
pub struct DrySymbolRunner;

Loading…
Cancel
Save