Skip to content

Commit

Permalink
refactor: add_task test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Aug 20, 2023
1 parent 0b3d188 commit cafd85e
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 45 deletions.
6 changes: 3 additions & 3 deletions pueue/tests/client/integration/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn default(#[case] read_local_logs: bool) -> Result<()> {
let shared = &daemon.settings.shared;

// Add a task and wait until it started.
assert_success(add_task(shared, "sleep 1 && echo test", false).await?);
assert_success(add_task(shared, "sleep 1 && echo test").await?);
wait_for_task_condition(shared, 0, |task| task.is_running()).await?;

// Execute `follow`.
Expand All @@ -51,7 +51,7 @@ async fn last_lines(#[case] read_local_logs: bool) -> Result<()> {
let shared = &daemon.settings.shared;

// Add a task which echos 8 lines of output
assert_success(add_task(shared, "echo \"1\n2\n3\n4\n5\n6\n7\n8\" && sleep 1", false).await?);
assert_success(add_task(shared, "echo \"1\n2\n3\n4\n5\n6\n7\n8\" && sleep 1").await?);
wait_for_task_condition(shared, 0, |task| task.is_running()).await?;

// Follow the task, but only print the last 4 lines of the output.
Expand Down Expand Up @@ -113,7 +113,7 @@ async fn fail_on_disappearing(#[case] read_local_logs: bool) -> Result<()> {
let shared = &daemon.settings.shared;

// Add a task echoes something and waits for a while
assert_success(add_task(shared, "echo test && sleep 10", false).await?);
assert_success(add_task(shared, "echo test && sleep 10").await?);
wait_for_task_condition(shared, 0, |task| task.is_running()).await?;

// Reset the daemon after 2 seconds. At this point, the client will already be following the
Expand Down
8 changes: 4 additions & 4 deletions pueue/tests/client/integration/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async fn read(#[case] read_local_logs: bool) -> Result<()> {
.context("Couldn't write pueue config to temporary directory")?;

// Add a task and wait until it finishes.
assert_success(add_task(shared, "echo test", false).await?);
assert_success(add_task(shared, "echo test").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

let output = run_client_command(shared, &["log"])?;
Expand Down Expand Up @@ -58,7 +58,7 @@ async fn read_truncated(#[case] read_local_logs: bool) -> Result<()> {
.context("Couldn't write pueue config to temporary directory")?;

// Add a task and wait until it finishes.
assert_success(add_task(shared, "echo '1\n2\n3\n4\n5\n6\n7\n8\n9\n10'", false).await?);
assert_success(add_task(shared, "echo '1\n2\n3\n4\n5\n6\n7\n8\n9\n10'").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

let output = run_client_command(shared, &["log", "--lines=5"])?;
Expand Down Expand Up @@ -94,7 +94,7 @@ async fn colored() -> Result<()> {
let shared = &daemon.settings.shared;

// Add a task and wait until it finishes.
assert_success(add_task(shared, "echo test", false).await?);
assert_success(add_task(shared, "echo test").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

let output = run_client_command(shared, &["--color", "always", "log"])?;
Expand All @@ -121,7 +121,7 @@ async fn json() -> Result<()> {
let shared = &daemon.settings.shared;

// Add a task and wait until it finishes.
assert_success(add_task(shared, "echo test", false).await?);
assert_success(add_task(shared, "echo test").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

let output = run_client_command(shared, &["log", "--json"])?;
Expand Down
8 changes: 4 additions & 4 deletions pueue/tests/client/integration/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async fn restart_and_edit_task_command() -> Result<()> {
let shared = &daemon.settings.shared;

// Create a task and wait for it to finish.
assert_success(add_task(shared, "ls", false).await?);
assert_success(add_task(shared, "ls").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

// Set the editor to a command which replaces the temporary file's content.
Expand All @@ -39,7 +39,7 @@ async fn restart_and_edit_task_path() -> Result<()> {
let shared = &daemon.settings.shared;

// Create a task and wait for it to finish.
assert_success(add_task(shared, "ls", false).await?);
assert_success(add_task(shared, "ls").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

// Set the editor to a command which replaces the temporary file's content.
Expand All @@ -65,7 +65,7 @@ async fn restart_and_edit_task_path_and_command() -> Result<()> {
let shared = &daemon.settings.shared;

// Create a task and wait for it to finish.
assert_success(add_task(shared, "ls", false).await.unwrap());
assert_success(add_task(shared, "ls").await.unwrap());
wait_for_task_condition(shared, 0, |task| task.is_done())
.await
.unwrap();
Expand Down Expand Up @@ -113,7 +113,7 @@ async fn normal_restart_with_edit() -> Result<()> {
let shared = &daemon.settings.shared;

// Create a task and wait for it to finish.
assert_success(add_task(shared, "ls", false).await?);
assert_success(add_task(shared, "ls").await?);
let original_task = wait_for_task_condition(shared, 0, |task| task.is_done()).await?;
assert!(
original_task.enqueued_at.is_some(),
Expand Down
4 changes: 2 additions & 2 deletions pueue/tests/client/integration/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async fn full() -> Result<()> {
// let shared = &daemon.settings.shared;
//
// // Add a task and wait until it finishes.
// assert_success(add_task(shared, "ls", false).await?);
// assert_success(add_task(shared, "ls").await?);
// wait_for_task_condition(shared, 0, |task| task.is_done()).await?;
//
// let output = run_status_without_path(shared, &["--color", "always"]).await?;
Expand Down Expand Up @@ -112,7 +112,7 @@ async fn json() -> Result<()> {
let shared = &daemon.settings.shared;

// Add a task and wait until it finishes.
assert_success(add_task(shared, "ls", false).await?);
assert_success(add_task(shared, "ls").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

let output = run_client_command(shared, &["status", "--json"])?;
Expand Down
4 changes: 2 additions & 2 deletions pueue/tests/daemon/integration/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn test_normal_add() -> Result<()> {
let pre_addition_time = Local::now();

// Add a task that instantly finishes
assert_success(add_task(shared, "sleep 0.01", false).await?);
assert_success(add_task(shared, "sleep 0.01").await?);

// Wait until the task finished and get state
let task = wait_for_task_condition(shared, 0, |task| task.is_done()).await?;
Expand Down Expand Up @@ -77,7 +77,7 @@ async fn test_add_with_immediate_start() -> Result<()> {
pause_tasks(shared, TaskSelection::All).await?;

// Tell the daemon to add a task that must be immediately started.
assert_success(add_task(shared, "sleep 60", true).await?);
assert_success(add_and_start_task(shared, "sleep 60").await?);

// Make sure the task is actually being started.
wait_for_task_condition(shared, 0, |task| task.is_running()).await?;
Expand Down
4 changes: 2 additions & 2 deletions pueue/tests/daemon/integration/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn test_add_with_alias() -> Result<()> {
create_test_alias_file(daemon.tempdir.path(), aliases)?;

// Add a task whose command should be replaced by an alias
assert_success(add_task(shared, "non_existing_cmd test", false).await?);
assert_success(add_task(shared, "non_existing_cmd test").await?);

// Wait until the task finished and get state
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;
Expand All @@ -45,7 +45,7 @@ async fn test_restart_with_alias() -> Result<()> {
let shared = &daemon.settings.shared;

// Add a task whose command that should fail and wait for it to finish.
assert_success(add_task(shared, "non_existing_cmd test", false).await?);
assert_success(add_task(shared, "non_existing_cmd test").await?);
let task = wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

// Ensure the command hasn't been mutated and the task failed.
Expand Down
4 changes: 2 additions & 2 deletions pueue/tests/daemon/integration/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn test_normal_clean() -> Result<()> {

// This should result in one failed, one finished, one running and one queued task.
for command in &["failing", "ls", "sleep 60", "ls"] {
assert_success(add_task(shared, command, false).await?);
assert_success(add_task(shared, command).await?);
}
// Wait for task2 to start. This implies that task[0,1] are done.
wait_for_task_condition(shared, 2, |task| task.is_running()).await?;
Expand Down Expand Up @@ -39,7 +39,7 @@ async fn test_successful_only_clean() -> Result<()> {

// This should result in one failed, one finished, one running and one queued task.
for command in &["failing", "ls"] {
assert_success(add_task(shared, command, false).await?);
assert_success(add_task(shared, command).await?);
}
// Wait for task2 to start. This implies task[0,1] being finished.
wait_for_task_condition(shared, 1, |task| task.is_done()).await?;
Expand Down
2 changes: 1 addition & 1 deletion pueue/tests/daemon/integration/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::helper::*;

async fn create_edited_task(shared: &Shared) -> Result<EditResponseMessage> {
// Add a task
assert_success(add_task(shared, "ls", false).await?);
assert_success(add_task(shared, "ls").await?);

// The task should now be queued
assert_eq!(get_task_status(shared, 0).await?, TaskStatus::Queued);
Expand Down
2 changes: 1 addition & 1 deletion pueue/tests/daemon/integration/environment_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn test_isolated_task_environment() -> Result<()> {
// Spawn a task which prints a special environment variable.
// This environment variable is injected into the daemon's environment.
// It shouldn't show up in the task's environment, as the task should be isolated!
assert_success(add_task(shared, "echo $PUEUED_TEST_ENV_VARIABLE", true).await?);
assert_success(add_and_start_task(shared, "echo $PUEUED_TEST_ENV_VARIABLE").await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

let log = get_task_log(shared, 0, None).await?;
Expand Down
2 changes: 1 addition & 1 deletion pueue/tests/daemon/integration/kill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn test_kill_tasks_with_pause(

// Add another task that will be normally enqueued.
for _ in 0..3 {
assert_success(add_task(shared, "sleep 60", false).await?);
assert_success(add_task(shared, "sleep 60").await?);
}

// Send the kill message
Expand Down
6 changes: 3 additions & 3 deletions pueue/tests/daemon/integration/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn test_full_log() -> Result<()> {

// Add a task that lists those files and wait for it to finish.
let command = format!("ls {tempdir_path:?}");
assert_success(add_task(shared, &command, false).await?);
assert_success(add_task(shared, &command).await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

// Request all log lines
Expand All @@ -85,7 +85,7 @@ async fn test_partial_log() -> Result<()> {

// Add a task that lists those files and wait for it to finish.
let command = format!("ls {tempdir_path:?}");
assert_success(add_task(shared, &command, false).await?);
assert_success(add_task(shared, &command).await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

// Debug output to see what the file actually looks like:
Expand Down Expand Up @@ -127,7 +127,7 @@ async fn test_correct_log_order() -> Result<()> {

// Add a task that lists those files and wait for it to finish.
let command = "echo 'test' && echo 'error' && echo 'test'";
assert_success(add_task(shared, command, false).await?);
assert_success(add_task(shared, command).await?);
wait_for_task_condition(shared, 0, |task| task.is_done()).await?;

// Request all log lines
Expand Down
6 changes: 3 additions & 3 deletions pueue/tests/daemon/integration/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async fn test_pause_daemon() -> Result<()> {
wait_for_group_status(shared, PUEUE_DEFAULT_GROUP, GroupStatus::Paused).await?;

// Add a task and give the taskmanager time to theoretically start the process
add_task(shared, "ls", false).await?;
add_task(shared, "ls").await?;
sleep_ms(500).await;

// Make sure it's not started
Expand All @@ -33,7 +33,7 @@ async fn test_pause_running_task() -> Result<()> {
let shared = &daemon.settings.shared;

// Start a long running task and make sure it's started
add_task(shared, "sleep 60", false).await?;
add_task(shared, "sleep 60").await?;
wait_for_task_condition(shared, 0, |task| task.is_running()).await?;

// This pauses the daemon
Expand All @@ -57,7 +57,7 @@ async fn test_pause_with_wait() -> Result<()> {
let shared = &daemon.settings.shared;

// Start a long running task and make sure it's started
add_task(shared, "sleep 60", false).await?;
add_task(shared, "sleep 60").await?;
wait_for_task_condition(shared, 0, |task| task.is_running()).await?;

// Pauses the default queue while waiting for tasks
Expand Down
4 changes: 2 additions & 2 deletions pueue/tests/daemon/integration/priority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn test_highest_priority_first() -> Result<()> {
pause_tasks(shared, TaskSelection::All).await?;

// Add one normal task and one with the lowest possible priority.
assert_success(add_task(shared, "sleep 10", false).await?);
assert_success(add_task(shared, "sleep 10").await?);
assert_success(add_task_with_priority(shared, "sleep 10", 1).await?);
assert_success(add_task_with_priority(shared, "sleep 10", 2).await?);

Expand All @@ -69,7 +69,7 @@ async fn test_default_priority_over_negative_priority() -> Result<()> {
// Add one normal task and one with the lowest possible priority.
assert_success(add_task_with_priority(shared, "sleep 10", -2).await?);
assert_success(add_task_with_priority(shared, "sleep 10", -1).await?);
assert_success(add_task(shared, "sleep 10", false).await?);
assert_success(add_task(shared, "sleep 10").await?);

// Resume the daemon.
start_tasks(shared, TaskSelection::All).await?;
Expand Down
2 changes: 1 addition & 1 deletion pueue/tests/daemon/integration/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn test_normal_remove() -> Result<()> {
// 4 -> queued
// 5 -> stashed
for command in &["failing", "ls", "sleep 60", "sleep 60", "ls", "ls"] {
assert_success(add_task(shared, command, false).await?);
assert_success(add_task(shared, command).await?);
}
// Wait for task2 to start. This implies task[0,1] being finished.
wait_for_task_condition(shared, 2, |task| task.is_running()).await?;
Expand Down
8 changes: 4 additions & 4 deletions pueue/tests/daemon/integration/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ async fn test_reset() -> Result<()> {
let shared = &daemon.settings.shared;

// Start a long running task and make sure it's started
add_task(shared, "ls", false).await?;
add_task(shared, "failed", false).await?;
add_task(shared, "sleep 60", false).await?;
add_task(shared, "ls", false).await?;
add_task(shared, "ls").await?;
add_task(shared, "failed").await?;
add_task(shared, "sleep 60").await?;
add_task(shared, "ls").await?;
wait_for_task_condition(shared, 2, |task| task.is_running()).await?;

// Reset the daemon
Expand Down
4 changes: 2 additions & 2 deletions pueue/tests/daemon/integration/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async fn test_restart_in_place() -> Result<()> {
let shared = &daemon.settings.shared;

// Add a single task that instantly finishes.
assert_success(add_task(shared, "sleep 0.1", false).await?);
assert_success(add_task(shared, "sleep 0.1").await?);

// Wait for task 0 to finish.
let original_task = wait_for_task_condition(shared, 0, |task| task.is_done()).await?;
Expand Down Expand Up @@ -70,7 +70,7 @@ async fn test_cannot_restart_running() -> Result<()> {
let shared = &daemon.settings.shared;

// Add a single task that instantly finishes.
assert_success(add_task(shared, "sleep 60", false).await?);
assert_success(add_task(shared, "sleep 60").await?);

// Wait for task 0 to finish.
wait_for_task_condition(shared, 0, |task| task.is_running()).await?;
Expand Down
2 changes: 1 addition & 1 deletion pueue/tests/daemon/integration/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async fn test_start_tasks(#[case] start_message: StartMessage) -> Result<()> {

// Add multiple tasks only a single one will be started by default
for _ in 0..3 {
assert_success(add_task(shared, "sleep 60", false).await?);
assert_success(add_task(shared, "sleep 60").await?);
}
// Wait for task 0 to start on its own.
// We have to do this, otherwise we'll start task 1/2 beforehand, which prevents task 0 to be
Expand Down
2 changes: 1 addition & 1 deletion pueue/tests/daemon/integration/stashed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async fn test_stash_queued_task() -> Result<()> {
wait_for_group_status(shared, "default", GroupStatus::Paused).await?;

// Add a task that's queued for execution.
add_task(shared, "sleep 10", false).await?;
add_task(shared, "sleep 10").await?;

// Stash the task
send_message(shared, Message::Stash(vec![0]))
Expand Down
9 changes: 3 additions & 6 deletions pueue/tests/helper/factories/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ use pueue_lib::settings::*;
use crate::helper::*;

/// Adds a task to the test daemon.
pub async fn add_task(shared: &Shared, command: &str, start_immediately: bool) -> Result<Message> {
let mut message = create_add_message(shared, command);
message.start_immediately = start_immediately;

send_message(shared, message)
pub async fn add_task(shared: &Shared, command: &str) -> Result<Message> {
send_message(shared, create_add_message(shared, command))
.await
.context("Failed to to add task.")
}
Expand Down Expand Up @@ -53,7 +50,7 @@ pub async fn add_task_to_group(shared: &Shared, command: &str, group: &str) -> R
/// variables to `stdout`.
pub async fn add_env_task(shared: &Shared, command: &str) -> Result<Message> {
let command = format!("echo WORKER_ID: $PUEUE_WORKER_ID; echo GROUP: $PUEUE_GROUP; {command}");
add_task(shared, &command, false).await
add_task(shared, &command).await
}

/// Just like [add_env_task], but the task get's added to specific group.
Expand Down

0 comments on commit cafd85e

Please sign in to comment.