use std::time::Duration;
use async_broadcast::{Receiver, Sender};
use hotshot_task_impls::helpers::broadcast_event;
use tokio::{spawn, task::JoinHandle, time::timeout};
use crate::test_task::TestEvent;
pub struct CompletionTask {
pub tx: Sender<TestEvent>,
pub rx: Receiver<TestEvent>,
pub duration: Duration,
}
impl CompletionTask {
pub fn run(mut self) -> JoinHandle<()> {
spawn(async move {
if timeout(self.duration, self.wait_for_shutdown())
.await
.is_err()
{
broadcast_event(TestEvent::Shutdown, &self.tx).await;
}
})
}
async fn wait_for_shutdown(&mut self) {
while let Ok(event) = self.rx.recv_direct().await {
if matches!(event, TestEvent::Shutdown) {
tracing::error!("Completion Task shutting down");
return;
}
}
}
}
#[derive(Clone, Debug)]
pub struct TimeBasedCompletionTaskDescription {
pub duration: Duration,
}
#[derive(Clone, Debug)]
pub enum CompletionTaskDescription {
TimeBasedCompletionTaskBuilder(TimeBasedCompletionTaskDescription),
}