Skip to content

Commit

Permalink
Use a file lock as well as a mutex to isolate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Nov 13, 2024
1 parent 1df8c45 commit 15a37fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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
20 changes: 16 additions & 4 deletions tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ fn generate_test_cases(
}
let test_cases = test_cases.join("\n");

// Use a common mutex for all test cases.
// Locking the Nargo.toml file in the test directory so that we can run tests exclusively.
// NB a Mutex is not enough on CI because we use `cargo nextest --partition` which can run

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

View workflow job for this annotation

GitHub Actions / Code

Unknown word (nextest)
// run test cases in the group in different processes. For some reason the file lock alone
// doesn't seem to do the trick locally.
let mutex_name = format! {"TEST_MUTEX_{}", test_name.to_uppercase()};
write!(
test_file,
Expand All @@ -180,10 +183,16 @@ lazy_static::lazy_static! {{
{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 +203,9 @@ fn test_{test_name}(force_brillig: bool, inliner_aggressiveness: i64) {{
}}
{test_content}
drop(file_guard);
drop(mutex_guard);
}}
"#
)
Expand Down Expand Up @@ -363,7 +375,7 @@ fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Pa
"info",
&format!(
r#"
nargo.arg("--json");
nargo.arg("--json");
{assert_zero_opcodes}
"#,
),
Expand Down

0 comments on commit 15a37fd

Please sign in to comment.