use std::time::Duration;
use std::time::Instant;
use fastimer::MakeDelay;
use fastimer::schedule::SimpleAction;
use fastimer::schedule::SimpleActionExt;
use fastimer_tokio::MakeTokioDelay;
use fastimer_tokio::TokioSpawn;
use logforth::append;
struct MySimpleAction {
name: &'static str,
counter: u8,
}
impl MySimpleAction {
fn new(name: &'static str) -> Self {
Self { name, counter: 0 }
}
}
impl SimpleAction for MySimpleAction {
fn name(&self) -> &str {
self.name
}
async fn run(&mut self) {
log::info!("[{}] starting turn {}", self.name, self.counter);
MakeTokioDelay::default()
.delay(Duration::from_secs(1))
.await;
self.counter += 1;
}
}
#[tokio::test]
async fn test_simple_action() {
logforth::starter_log::builder()
.dispatch(|d| d.append(append::Stderr::default()))
.apply();
let spawn = TokioSpawn::default();
let make_delay = MakeTokioDelay::default();
let initial_delay = Some(Duration::from_secs(1));
let shutdown = Instant::now() + Duration::from_secs(10);
MySimpleAction::new("schedule_with_fixed_delay").schedule_with_fixed_delay(
make_delay.delay_until(shutdown),
&spawn,
make_delay,
initial_delay,
Duration::from_secs(2),
);
MySimpleAction::new("schedule_at_fixed_rate").schedule_at_fixed_rate(
make_delay.delay_until(shutdown),
&spawn,
make_delay,
initial_delay,
Duration::from_secs(2),
);
make_delay
.delay_until(shutdown + Duration::from_secs(1))
.await;
}