Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): Use a file lock as well as a mutex to isolate tests cases #6508

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ dirs.workspace = true
assert_cmd = "2.0.8"
assert_fs = "1.0.10"
predicates = "2.1.5"
file-lock = "2.1.11"
fm.workspace = true
criterion.workspace = true
pprof.workspace = true
Expand Down
21 changes: 17 additions & 4 deletions tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@
}
let test_cases = test_cases.join("\n");

// Use a common mutex for all test cases.
// We need to isolate test cases in the same group, otherwise they overwrite each other's artifacts.
// On CI we use `cargo nextest`, which runs tests in different processes; for this we use a file lock.

Check warning on line 172 in tooling/nargo_cli/build.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (nextest)
// Locally we might be using `cargo test`, which run tests in the same process; in this case the file lock
// wouldn't work, becuase the process itself has the lock, and it looks like it can have N instances without

Check warning on line 174 in tooling/nargo_cli/build.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (becuase)
// any problems; for this reason we also use a `Mutex`.
let mutex_name = format! {"TEST_MUTEX_{}", test_name.to_uppercase()};
write!(
test_file,
Expand All @@ -180,10 +184,16 @@

{test_cases}
fn test_{test_name}(force_brillig: bool, inliner_aggressiveness: i64) {{
let test_program_dir = PathBuf::from("{test_dir}");

// Ignore poisoning errors if some of the matrix cases failed.
let _guard = {mutex_name}.lock().unwrap_or_else(|e| e.into_inner());
let mutex_guard = {mutex_name}.lock().unwrap_or_else(|e| e.into_inner());

let test_program_dir = PathBuf::from("{test_dir}");
let file_guard = file_lock::FileLock::lock(
test_program_dir.join("Nargo.toml"),
true,
file_lock::FileOptions::new().read(true).write(true).append(true)
).expect("failed to lock Nargo.toml");

let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir);
Expand All @@ -194,6 +204,9 @@
}}

{test_content}

drop(file_guard);
drop(mutex_guard);
}}
"#
)
Expand Down Expand Up @@ -363,7 +376,7 @@
"info",
&format!(
r#"
nargo.arg("--json");
nargo.arg("--json");
{assert_zero_opcodes}
"#,
),
Expand Down
Loading