Skip to content

Commit

Permalink
cleanup: leverage scoped thread in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed May 21, 2023
1 parent 38a7e7f commit b01614b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 85 deletions.
37 changes: 17 additions & 20 deletions lib/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,23 @@ mod tests {
.unwrap();
data_file.write_u32::<LittleEndian>(0).unwrap();
let num_threads = max(num_cpus::get(), 4);
let mut threads = vec![];
for _ in 0..num_threads {
let data_path = data_path.clone();
let lock_path = lock_path.clone();
let handle = thread::spawn(move || {
let _lock = FileLock::lock(lock_path);
let mut data_file = OpenOptions::new()
.read(true)
.open(data_path.clone())
.unwrap();
let value = data_file.read_u32::<LittleEndian>().unwrap();
thread::sleep(Duration::from_millis(1));
let mut data_file = OpenOptions::new().write(true).open(data_path).unwrap();
data_file.write_u32::<LittleEndian>(value + 1).unwrap();
});
threads.push(handle);
}
for thread in threads {
thread.join().ok().unwrap();
}
thread::scope(|s| {
for _ in 0..num_threads {
let data_path = data_path.clone();
let lock_path = lock_path.clone();
s.spawn(move || {
let _lock = FileLock::lock(lock_path);
let mut data_file = OpenOptions::new()
.read(true)
.open(data_path.clone())
.unwrap();
let value = data_file.read_u32::<LittleEndian>().unwrap();
thread::sleep(Duration::from_millis(1));
let mut data_file = OpenOptions::new().write(true).open(data_path).unwrap();
data_file.write_u32::<LittleEndian>(value + 1).unwrap();
});
}
});
let mut data_file = OpenOptions::new().read(true).open(data_path).unwrap();
let value = data_file.read_u32::<LittleEndian>().unwrap();
assert_eq!(value, num_threads as u32);
Expand Down
50 changes: 22 additions & 28 deletions lib/tests/test_commit_concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,17 @@ fn test_commit_parallel(use_git: bool) {
let repo = &test_workspace.repo;

let num_threads = max(num_cpus::get(), 4);
let mut threads = vec![];
for _ in 0..num_threads {
let settings = settings.clone();
let repo = repo.clone();
let handle = thread::spawn(move || {
let mut tx = repo.start_transaction(&settings, "test");
write_random_commit(tx.mut_repo(), &settings);
tx.commit();
});
threads.push(handle);
}
for thread in threads {
thread.join().ok().unwrap();
}
thread::scope(|s| {
for _ in 0..num_threads {
let settings = settings.clone();
let repo = repo.clone();
s.spawn(move || {
let mut tx = repo.start_transaction(&settings, "test");
write_random_commit(tx.mut_repo(), &settings);
tx.commit();
});
}
});
let repo = repo.reload_at_head(&settings).unwrap();
// One commit per thread plus the commit from the initial working-copy on top of
// the root commit
Expand All @@ -83,20 +80,17 @@ fn test_commit_parallel_instances(use_git: bool) {
let repo = &test_workspace.repo;

let num_threads = max(num_cpus::get(), 4);
let mut threads = vec![];
for _ in 0..num_threads {
let settings = settings.clone();
let repo = load_repo_at_head(&settings, repo.repo_path());
let handle = thread::spawn(move || {
let mut tx = repo.start_transaction(&settings, "test");
write_random_commit(tx.mut_repo(), &settings);
tx.commit();
});
threads.push(handle);
}
for thread in threads {
thread.join().ok().unwrap();
}
thread::scope(|s| {
for _ in 0..num_threads {
let settings = settings.clone();
let repo = load_repo_at_head(&settings, repo.repo_path());
s.spawn(move || {
let mut tx = repo.start_transaction(&settings, "test");
write_random_commit(tx.mut_repo(), &settings);
tx.commit();
});
}
});
// One commit per thread plus the commit from the initial working-copy commit on
// top of the root commit
let repo = load_repo_at_head(&settings, repo.repo_path());
Expand Down
72 changes: 35 additions & 37 deletions lib/tests/test_working_copy_concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,39 @@ fn test_checkout_parallel(use_git: bool) {
.check_out(repo.op_id().clone(), None, &tree)
.unwrap();

let mut threads = vec![];
for tree_id in &tree_ids {
let op_id = repo.op_id().clone();
let tree_ids = tree_ids.clone();
let tree_id = tree_id.clone();
let settings = settings.clone();
let workspace_root = workspace_root.clone();
let handle = thread::spawn(move || {
let mut workspace =
Workspace::load(&settings, &workspace_root, &StoreFactories::default()).unwrap();
let tree = workspace
.repo_loader()
.store()
.get_tree(&RepoPath::root(), &tree_id)
.unwrap();
// The operation ID is not correct, but that doesn't matter for this test
let stats = workspace
.working_copy_mut()
.check_out(op_id, None, &tree)
.unwrap();
assert_eq!(stats.updated_files, 0);
assert_eq!(stats.added_files, 1);
assert_eq!(stats.removed_files, 1);
// Check that the working copy contains one of the trees. We may see a
// different tree than the one we just checked out, but since
// write_tree() should take the same lock as check_out(), write_tree()
// should never produce a different tree.
let mut locked_wc = workspace.working_copy_mut().start_mutation();
let new_tree_id = locked_wc.snapshot(GitIgnoreFile::empty(), None).unwrap();
locked_wc.discard();
assert!(tree_ids.contains(&new_tree_id));
});
threads.push(handle);
}
for thread in threads {
thread.join().ok().unwrap();
}
thread::scope(|s| {
for tree_id in &tree_ids {
let op_id = repo.op_id().clone();
let tree_ids = tree_ids.clone();
let tree_id = tree_id.clone();
let settings = settings.clone();
let workspace_root = workspace_root.clone();
s.spawn(move || {
let mut workspace =
Workspace::load(&settings, &workspace_root, &StoreFactories::default())
.unwrap();
let tree = workspace
.repo_loader()
.store()
.get_tree(&RepoPath::root(), &tree_id)
.unwrap();
// The operation ID is not correct, but that doesn't matter for this test
let stats = workspace
.working_copy_mut()
.check_out(op_id, None, &tree)
.unwrap();
assert_eq!(stats.updated_files, 0);
assert_eq!(stats.added_files, 1);
assert_eq!(stats.removed_files, 1);
// Check that the working copy contains one of the trees. We may see a
// different tree than the one we just checked out, but since
// write_tree() should take the same lock as check_out(), write_tree()
// should never produce a different tree.
let mut locked_wc = workspace.working_copy_mut().start_mutation();
let new_tree_id = locked_wc.snapshot(GitIgnoreFile::empty(), None).unwrap();
locked_wc.discard();
assert!(tree_ids.contains(&new_tree_id));
});
}
});
}

0 comments on commit b01614b

Please sign in to comment.