-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #599 from Nukesor/cleanup
Cleanup
- Loading branch information
Showing
12 changed files
with
181 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use anyhow::Result; | ||
|
||
use pueue_lib::{ | ||
network::message::{KillMessage, TaskSelection}, | ||
task::*, | ||
}; | ||
|
||
use crate::helper::*; | ||
|
||
/// Test if adding a normal task works as intended. | ||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
async fn test_dependency() -> Result<()> { | ||
let (daemon, lockfile) = daemon_with_lockfile().await?; | ||
let shared = &daemon.settings.shared; | ||
|
||
// Add a task that waits until the lockfile is removed. It's added to a non-default group. | ||
add_group_with_slots(shared, "testgroup_3", 3).await?; | ||
assert_success(add_task_to_group(shared, lockfile_command(&lockfile), "testgroup_3").await?); | ||
|
||
// This task now has to wait for task 0, even though it's added to the default group and could | ||
// start right away. | ||
assert_success(add_task_with_dependencies(shared, "ls", vec![0]).await?); | ||
|
||
// Wait for a bit, the second task should still be queued. | ||
sleep_ms(500).await; | ||
|
||
// Clear the lock, the first task should now finish. | ||
clear_lock(&lockfile)?; | ||
wait_for_task_condition(shared, 0, Task::is_done).await?; | ||
|
||
// The second one should start and finish right away. | ||
wait_for_task_condition(shared, 1, Task::is_done).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Test if adding a normal task works as intended. | ||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
async fn test_failing_dependency() -> Result<()> { | ||
let (daemon, lockfile) = daemon_with_lockfile().await?; | ||
let shared = &daemon.settings.shared; | ||
|
||
// Add a task that waits until the lockfile is removed. It's added to a non-default group. | ||
add_group_with_slots(shared, "testgroup_3", 3).await?; | ||
assert_success(add_task_to_group(shared, lockfile_command(&lockfile), "testgroup_3").await?); | ||
|
||
// This task now has to wait for task 0, even though it's added to the default group and could | ||
// start right away. | ||
assert_success(add_task_with_dependencies(shared, "ls", vec![0]).await?); | ||
// Wait for a bit, the second task should still be queued. | ||
sleep_ms(500).await; | ||
|
||
// Now we kill the first task. | ||
// This should result in the second task failing. | ||
send_message( | ||
shared, | ||
KillMessage { | ||
tasks: TaskSelection::TaskIds(vec![0]), | ||
signal: None, | ||
}, | ||
) | ||
.await?; | ||
wait_for_task_condition(shared, 0, Task::failed).await?; | ||
|
||
// Now wait until the first task finishes and make sure it failed because of a failing | ||
// dependency. | ||
let task = wait_for_task_condition(shared, 1, Task::failed).await?; | ||
assert!(matches!( | ||
task.status, | ||
TaskStatus::Done { | ||
result: TaskResult::DependencyFailed, | ||
.. | ||
} | ||
)); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::{ | ||
fs::{remove_file, File}, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
use anyhow::{Context, Result}; | ||
|
||
use super::{daemon_base_setup, daemon_with_settings, PueueDaemon}; | ||
|
||
/// A helper wrapper around [`daemon`] that also creates a lockfile that can be listened to from a | ||
/// task via `inotifywait -e delete_self "$FILE"`. | ||
/// This is super useful as it allows us to have a proper lock that we may release at any point in | ||
/// time when working with timing specific issues in tests. | ||
/// | ||
/// E.g. task1 depends on task0 and we want to make sure that task1 isn't started before task0 | ||
/// ends. This mechanism can be ensured that task0 only finishes when we allow it to do so. | ||
pub async fn daemon_with_lockfile() -> Result<(PueueDaemon, PathBuf)> { | ||
let (settings, tempdir) = daemon_base_setup()?; | ||
let tempdir_path = tempdir.path().to_owned(); | ||
|
||
let daemon = daemon_with_settings(settings, tempdir).await?; | ||
|
||
let lockfile = tempdir_path.join("file.lock"); | ||
File::create(&lockfile)?; | ||
|
||
Ok((daemon, lockfile)) | ||
} | ||
|
||
pub fn lockfile_command(path: &Path) -> String { | ||
format!("inotifywait -e delete_self \"{}\"", path.to_string_lossy()) | ||
} | ||
|
||
pub fn clear_lock(path: &Path) -> Result<()> { | ||
remove_file(path).context("Failed to clear lock file")?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.