Trait composable::TestClock
source · pub trait TestClock {
// Required method
fn advance(&mut self, duration: Duration);
}
Expand description
By implementing the TestClock
trait, TestStore
can be used to test
Reducer
s that utilize tasks, futures, or streams.
This debounce
example exercises scheduling and task cancellation; all
with deterministic control of over the (simulated) passage of time.
#[derive(Debug, Default)]
struct State {
previous: Option<Task>,
n: usize,
}
#[derive(Clone, Debug, PartialEq)]
enum Action {
Send,
Recv,
}
use Action::*;
impl Reducer for State {
type Action = Action;
type Output = Self;
fn reduce(&mut self, action: Action, send: impl Effects<Action>) {
match action {
Send => {
send.debounce(
Recv,
&mut self.previous,
Interval::Trailing(Duration::from_secs(4)),
);
}
Recv => {
self.n += 1;
}
}
}
}
let mut store = TestStore::<State>::default();
let no_change: fn(&mut State) = |State| {};
store.send(Send, no_change);
store.advance(Duration::from_secs(3));
store.send(Send, no_change);
store.advance(Duration::from_secs(8));
store.recv(Recv, |state| state.n = 1);
store.send(Send, no_change);
store.advance(Duration::from_secs(1));
store.advance(Duration::from_secs(1));
store.advance(Duration::from_secs(1));
store.advance(Duration::from_secs(1));
store.recv(Recv, |state| state.n = 2);