diff --git a/Cargo.lock b/Cargo.lock index d684425d0..89ac8c5bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -582,6 +582,7 @@ dependencies = [ "git2", "hex", "maplit", + "num_cpus", "pest", "pest_derive", "protobuf", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 9911efd17..edb501f08 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -38,4 +38,5 @@ whoami = "1.0.1" zstd = "0.6.0" [dev-dependencies] +num_cpus = "1.13.0" test-case = "1.0.0" diff --git a/lib/src/lock.rs b/lib/src/lock.rs index 1ac400312..7791b148f 100644 --- a/lib/src/lock.rs +++ b/lib/src/lock.rs @@ -65,6 +65,7 @@ impl Drop for FileLock { #[cfg(test)] mod tests { + use std::cmp::max; use std::{env, thread}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -94,8 +95,9 @@ mod tests { .open(data_path.clone()) .unwrap(); data_file.write_u32::(0).unwrap(); + let num_threads = max(num_cpus::get(), 4); let mut threads = vec![]; - for _ in 0..100 { + for _ in 0..num_threads { let data_path = data_path.clone(); let lock_path = lock_path.clone(); let handle = thread::spawn(move || { @@ -116,6 +118,6 @@ mod tests { } let mut data_file = OpenOptions::new().read(true).open(data_path).unwrap(); let value = data_file.read_u32::().unwrap(); - assert_eq!(value, 100); + assert_eq!(value, num_threads as u32); } } diff --git a/lib/tests/test_commit_concurrent.rs b/lib/tests/test_commit_concurrent.rs index e68f5ada8..aa1c801e5 100644 --- a/lib/tests/test_commit_concurrent.rs +++ b/lib/tests/test_commit_concurrent.rs @@ -12,13 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::cmp::max; use std::thread; use jujube_lib::repo::ReadonlyRepo; use jujube_lib::{dag_walk, testutils}; use test_case::test_case; -fn count_non_merge_operations(repo: &ReadonlyRepo) -> u32 { +fn count_non_merge_operations(repo: &ReadonlyRepo) -> usize { let op_store = repo.op_store(); let op_id = repo.op_id().clone(); let mut num_ops = 0; @@ -45,8 +46,9 @@ fn test_commit_parallel(use_git: bool) { let settings = testutils::user_settings(); let (_temp_dir, repo) = testutils::init_repo(&settings, use_git); + let num_threads = max(num_cpus::get(), 4); let mut threads = vec![]; - for _ in 0..100 { + for _ in 0..num_threads { let settings = settings.clone(); let repo = repo.clone(); let handle = thread::spawn(move || { @@ -61,11 +63,11 @@ fn test_commit_parallel(use_git: bool) { let repo = repo.reload().unwrap(); // One commit per thread plus the commit from the initial checkout on top of the // root commit - assert_eq!(repo.view().heads().len(), 101); + assert_eq!(repo.view().heads().len(), num_threads + 1); // One operation for initializing the repo (containing the root id and the // initial working copy commit). - assert_eq!(count_non_merge_operations(&repo), 101); + assert_eq!(count_non_merge_operations(&repo), num_threads + 1); } #[cfg(unix)] @@ -77,8 +79,9 @@ fn test_commit_parallel_instances(use_git: bool) { let settings = testutils::user_settings(); let (_temp_dir, repo) = testutils::init_repo(&settings, use_git); + let num_threads = max(num_cpus::get(), 4); let mut threads = vec![]; - for _ in 0..100 { + for _ in 0..num_threads { let settings = settings.clone(); let repo = ReadonlyRepo::load(&settings, repo.working_copy_path().clone()).unwrap(); let handle = thread::spawn(move || { @@ -93,9 +96,9 @@ fn test_commit_parallel_instances(use_git: bool) { // One commit per thread plus the commit from the initial checkout on top of the // root commit let repo = ReadonlyRepo::load(&settings, repo.working_copy_path().clone()).unwrap(); - assert_eq!(repo.view().heads().len(), 101); + assert_eq!(repo.view().heads().len(), num_threads + 1); // One operation for initializing the repo (containing the root id and the // initial working copy commit). - assert_eq!(count_non_merge_operations(&repo), 101); + assert_eq!(count_non_merge_operations(&repo), num_threads + 1); } diff --git a/lib/tests/test_working_copy_concurrent.rs b/lib/tests/test_working_copy_concurrent.rs index 9d3317393..7f62e1ddf 100644 --- a/lib/tests/test_working_copy_concurrent.rs +++ b/lib/tests/test_working_copy_concurrent.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::cmp::max; use std::collections::HashSet; use std::thread; @@ -104,8 +105,9 @@ fn test_checkout_parallel(use_git: bool) { let (_temp_dir, repo) = testutils::init_repo(&settings, use_git); let store = repo.store(); + let num_threads = max(num_cpus::get(), 4); let mut commit_ids = vec![]; - for i in 0..100 { + for i in 0..num_threads { let path = FileRepoPath::from(format!("file{}", i).as_str()); let tree = testutils::create_tree(&repo, &[(&path, "contents")]); let commit = CommitBuilder::for_new_commit(&settings, store, tree.id().clone())