fix: Clean up tokio test tasks

This commit is contained in:
Arne Beer 2024-06-23 13:49:31 +02:00
parent 9d7f2675ce
commit 3324e48228
No known key found for this signature in database
GPG key ID: CC9408F679023B65
2 changed files with 15 additions and 2 deletions

View file

@ -5,7 +5,6 @@ use std::{fs::create_dir_all, path::PathBuf};
use anyhow::{bail, Context, Result};
use log::warn;
use network::message_handler;
use process_handler::initiate_shutdown;
use pueue_lib::error::Error;
use pueue_lib::network::certificate::create_certificates;

View file

@ -12,6 +12,7 @@ use tokio::io::{self, AsyncWriteExt};
use pueue::daemon::run;
use pueue_lib::settings::*;
use tokio::task::JoinHandle;
use crate::helper::*;
@ -22,6 +23,18 @@ pub struct PueueDaemon {
pub tempdir: TempDir,
#[allow(dead_code)]
pub pid: i32,
// The async join handle of the daemon function.
// Can be used to abort the daemon manually.
pub join_handle: JoinHandle<Result<()>>,
}
/// Implement a custom drop for the Daemon test struct
impl Drop for PueueDaemon {
fn drop(&mut self) {
// The daemon runs in background tokio task.
// Use this handle to make sure that it gets always killed.
self.join_handle.abort_handle();
}
}
/// A helper function which creates some test config, sets up a temporary directory and spawns
@ -44,7 +57,7 @@ pub async fn daemon_with_settings(settings: Settings, tempdir: TempDir) -> Resul
let pueue_dir = tempdir.path();
let path = pueue_dir.to_path_buf();
// Start/spin off the daemon and get its PID
tokio::spawn(run_and_handle_error(path, true));
let join_handle = tokio::spawn(run_and_handle_error(path, true));
let pid = get_pid(&settings.shared.pid_path()).await?;
let sleep = 50;
@ -61,6 +74,7 @@ pub async fn daemon_with_settings(settings: Settings, tempdir: TempDir) -> Resul
settings,
tempdir,
pid,
join_handle,
});
}