| 1 |
// Copyright 2020 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::cmp::max; |
| 16 |
use std::thread; |
| 17 |
|
| 18 |
use assert_matches::assert_matches; |
| 19 |
use jj_lib::default_backend_factories::default_working_copy_factories; |
| 20 |
use jj_lib::repo::Repo as _; |
| 21 |
use jj_lib::working_copy::CheckoutError; |
| 22 |
use jj_lib::workspace::Workspace; |
| 23 |
use pollster::FutureExt as _; |
| 24 |
use testutils::TestResult; |
| 25 |
use testutils::TestWorkspace; |
| 26 |
use testutils::assert_tree_eq; |
| 27 |
use testutils::commit_with_tree; |
| 28 |
use testutils::create_tree; |
| 29 |
use testutils::empty_snapshot_options; |
| 30 |
use testutils::repo_path; |
| 31 |
use testutils::repo_path_buf; |
| 32 |
use testutils::write_working_copy_file; |
| 33 |
|
| 34 |
#[test] |
| 35 |
fn test_concurrent_checkout() -> TestResult { |
| 36 |
// Test that we error out if a concurrent checkout is detected (i.e. if the |
| 37 |
// working-copy commit changed on disk after we read it). |
| 38 |
let settings = testutils::user_settings(); |
| 39 |
let mut test_workspace1 = TestWorkspace::init_with_settings(&settings); |
| 40 |
let repo = test_workspace1.repo.clone(); |
| 41 |
let workspace1_root = test_workspace1.workspace.workspace_root().to_owned(); |
| 42 |
|
| 43 |
let tree1 = testutils::create_random_tree(&repo); |
| 44 |
let tree2 = testutils::create_random_tree(&repo); |
| 45 |
let tree3 = testutils::create_random_tree(&repo); |
| 46 |
let commit1 = commit_with_tree(repo.store(), tree1.clone()); |
| 47 |
let commit2 = commit_with_tree(repo.store(), tree2.clone()); |
| 48 |
let commit3 = commit_with_tree(repo.store(), tree3); |
| 49 |
|
| 50 |
// Check out tree1 |
| 51 |
let ws1 = &mut test_workspace1.workspace; |
| 52 |
// The operation ID is not correct, but that doesn't matter for this test |
| 53 |
ws1.check_out(repo.op_id().clone(), None, &commit1) |
| 54 |
.block_on()?; |
| 55 |
|
| 56 |
// Check out tree2 from another process (simulated by another workspace |
| 57 |
// instance) |
| 58 |
{ |
| 59 |
let mut ws2 = Workspace::load( |
| 60 |
&settings, |
| 61 |
&workspace1_root, |
| 62 |
&test_workspace1.env.default_backend_factories(), |
| 63 |
&default_working_copy_factories(), |
| 64 |
)?; |
| 65 |
// Reload commit from the store associated with the workspace |
| 66 |
let repo = ws2.repo_loader().load_at(repo.operation()).block_on()?; |
| 67 |
let commit2 = repo.store().get_commit(commit2.id())?; |
| 68 |
ws2.check_out(repo.op_id().clone(), Some(&tree1), &commit2) |
| 69 |
.block_on()?; |
| 70 |
} |
| 71 |
|
| 72 |
// Checking out another tree (via the first workspace instance) should now fail. |
| 73 |
assert_matches!( |
| 74 |
ws1.check_out(repo.op_id().clone(), Some(&tree1), &commit3) |
| 75 |
.block_on(), |
| 76 |
Err(CheckoutError::ConcurrentCheckout) |
| 77 |
); |
| 78 |
|
| 79 |
// Check that the tree2 is still checked out on disk. |
| 80 |
let ws3 = Workspace::load( |
| 81 |
&settings, |
| 82 |
&workspace1_root, |
| 83 |
&test_workspace1.env.default_backend_factories(), |
| 84 |
&default_working_copy_factories(), |
| 85 |
)?; |
| 86 |
assert_tree_eq!(*ws3.working_copy().tree()?, tree2); |
| 87 |
Ok(()) |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_checkout_parallel() -> TestResult { |
| 92 |
// Test that concurrent checkouts by different processes (simulated by using |
| 93 |
// different repo instances) is safe. |
| 94 |
let settings = testutils::user_settings(); |
| 95 |
let mut test_workspace = TestWorkspace::init_with_settings(&settings); |
| 96 |
let repo = &test_workspace.repo; |
| 97 |
let workspace_root = test_workspace.workspace.workspace_root().to_owned(); |
| 98 |
|
| 99 |
let num_threads = max(num_cpus::get(), 4); |
| 100 |
let mut trees = vec![]; |
| 101 |
for i in 0..num_threads { |
| 102 |
let path = repo_path_buf(format!("file{i}")); |
| 103 |
let tree = create_tree(repo, &[(&path, "contents")]); |
| 104 |
trees.push(tree); |
| 105 |
} |
| 106 |
|
| 107 |
// Create another tree just so we can test the update stats reliably from the |
| 108 |
// first update |
| 109 |
let tree = create_tree(repo, &[(repo_path("other file"), "contents")]); |
| 110 |
let commit = commit_with_tree(repo.store(), tree); |
| 111 |
test_workspace |
| 112 |
.workspace |
| 113 |
.check_out(repo.op_id().clone(), None, &commit) |
| 114 |
.block_on()?; |
| 115 |
|
| 116 |
thread::scope(|s| { |
| 117 |
for tree in &trees { |
| 118 |
let test_env = &test_workspace.env; |
| 119 |
let op_id = repo.op_id().clone(); |
| 120 |
let trees = trees.clone(); |
| 121 |
let commit = commit_with_tree(repo.store(), tree.clone()); |
| 122 |
let settings = settings.clone(); |
| 123 |
let workspace_root = workspace_root.clone(); |
| 124 |
s.spawn(move || { |
| 125 |
let mut workspace = Workspace::load( |
| 126 |
&settings, |
| 127 |
&workspace_root, |
| 128 |
&test_env.default_backend_factories(), |
| 129 |
&default_working_copy_factories(), |
| 130 |
) |
| 131 |
.unwrap(); |
| 132 |
// Reload commit from the store associated with the workspace |
| 133 |
let repo = workspace |
| 134 |
.repo_loader() |
| 135 |
.load_at(repo.operation()) |
| 136 |
.block_on() |
| 137 |
.unwrap(); |
| 138 |
let commit = repo.store().get_commit(commit.id()).unwrap(); |
| 139 |
// The operation ID is not correct, but that doesn't matter for this test |
| 140 |
let stats = workspace |
| 141 |
.check_out(op_id, None, &commit) |
| 142 |
.block_on() |
| 143 |
.unwrap(); |
| 144 |
assert_eq!(stats.updated_files, 0); |
| 145 |
assert_eq!(stats.added_files, 1); |
| 146 |
assert_eq!(stats.removed_files, 1); |
| 147 |
// Check that the working copy contains one of the trees. We may see a |
| 148 |
// different tree than the one we just checked out, but since |
| 149 |
// write_tree() should take the same lock as check_out(), write_tree() |
| 150 |
// should never produce a different tree. |
| 151 |
let mut locked_ws = workspace.start_working_copy_mutation().block_on().unwrap(); |
| 152 |
let (new_tree, _stats) = locked_ws |
| 153 |
.locked_wc() |
| 154 |
.snapshot(&empty_snapshot_options()) |
| 155 |
.block_on() |
| 156 |
.unwrap(); |
| 157 |
assert!( |
| 158 |
trees |
| 159 |
.iter() |
| 160 |
.any(|tree| tree.tree_ids() == new_tree.tree_ids()) |
| 161 |
); |
| 162 |
}); |
| 163 |
} |
| 164 |
}); |
| 165 |
Ok(()) |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn test_racy_checkout() -> TestResult { |
| 170 |
let mut test_workspace = TestWorkspace::init(); |
| 171 |
let repo = &test_workspace.repo; |
| 172 |
let op_id = repo.op_id().clone(); |
| 173 |
let workspace_root = test_workspace.workspace.workspace_root().to_owned(); |
| 174 |
|
| 175 |
let path = repo_path("file"); |
| 176 |
let tree = create_tree(repo, &[(path, "1")]); |
| 177 |
let commit = commit_with_tree(repo.store(), tree.clone()); |
| 178 |
|
| 179 |
let mut num_matches = 0; |
| 180 |
for _ in 0..100 { |
| 181 |
let ws = &mut test_workspace.workspace; |
| 182 |
ws.check_out(op_id.clone(), None, &commit).block_on()?; |
| 183 |
assert_eq!( |
| 184 |
std::fs::read(path.to_fs_path_unchecked(&workspace_root))?, |
| 185 |
b"1".to_vec() |
| 186 |
); |
| 187 |
// A file written right after checkout (hopefully, from the test's perspective, |
| 188 |
// within the file system timestamp granularity) is detected as changed. |
| 189 |
write_working_copy_file(&workspace_root, path, "x"); |
| 190 |
let modified_tree = test_workspace.snapshot()?; |
| 191 |
if modified_tree.tree_ids() == tree.tree_ids() { |
| 192 |
num_matches += 1; |
| 193 |
} |
| 194 |
// Reset the state for the next round |
| 195 |
write_working_copy_file(&workspace_root, path, "1"); |
| 196 |
} |
| 197 |
assert_eq!(num_matches, 0); |
| 198 |
Ok(()) |
| 199 |
} |