2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-04-29 06:51:58 +00:00
|
|
|
use std::cmp::max;
|
2021-03-14 17:37:28 +00:00
|
|
|
use std::collections::HashSet;
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::thread;
|
|
|
|
|
2021-05-15 16:16:31 +00:00
|
|
|
use jujutsu_lib::commit_builder::CommitBuilder;
|
2021-05-17 04:55:51 +00:00
|
|
|
use jujutsu_lib::repo_path::RepoPath;
|
2021-05-15 16:16:31 +00:00
|
|
|
use jujutsu_lib::testutils;
|
|
|
|
use jujutsu_lib::working_copy::CheckoutError;
|
2021-11-21 22:39:34 +00:00
|
|
|
use jujutsu_lib::workspace::Workspace;
|
2020-12-12 08:00:42 +00:00
|
|
|
use test_case::test_case;
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2020-12-12 08:00:42 +00:00
|
|
|
fn test_concurrent_checkout(use_git: bool) {
|
|
|
|
// Test that we error out if a concurrent checkout is detected (i.e. if the
|
|
|
|
// current checkout changed on disk after we read it).
|
|
|
|
let settings = testutils::user_settings();
|
2021-11-21 22:39:34 +00:00
|
|
|
let mut test_workspace1 = testutils::init_repo(&settings, use_git);
|
|
|
|
let repo1 = test_workspace1.repo.clone();
|
|
|
|
let workspace1_root = test_workspace1.workspace.workspace_root().clone();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-09-11 16:45:06 +00:00
|
|
|
let mut tx1 = repo1.start_transaction("test");
|
2021-11-21 22:39:34 +00:00
|
|
|
let commit1 = testutils::create_random_commit(&settings, &repo1)
|
2020-12-12 08:00:42 +00:00
|
|
|
.set_open(true)
|
2021-09-11 16:45:06 +00:00
|
|
|
.write_to_repo(tx1.mut_repo());
|
2021-11-21 22:39:34 +00:00
|
|
|
let commit2 = testutils::create_random_commit(&settings, &repo1)
|
2020-12-12 08:00:42 +00:00
|
|
|
.set_open(true)
|
2021-09-11 16:45:06 +00:00
|
|
|
.write_to_repo(tx1.mut_repo());
|
2021-11-21 22:39:34 +00:00
|
|
|
let commit3 = testutils::create_random_commit(&settings, &repo1)
|
2020-12-12 08:00:42 +00:00
|
|
|
.set_open(true)
|
2021-09-11 16:45:06 +00:00
|
|
|
.write_to_repo(tx1.mut_repo());
|
|
|
|
tx1.commit();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
// Check out commit1
|
2021-11-21 22:39:34 +00:00
|
|
|
let wc1 = test_workspace1.workspace.working_copy_mut();
|
2020-12-19 07:50:10 +00:00
|
|
|
wc1.check_out(commit1).unwrap();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-11-21 22:39:34 +00:00
|
|
|
// Check out commit2 from another process (simulated by another workspace
|
|
|
|
// instance)
|
|
|
|
let mut workspace2 = Workspace::load(&settings, workspace1_root.clone()).unwrap();
|
|
|
|
workspace2
|
|
|
|
.working_copy_mut()
|
|
|
|
.check_out(commit2.clone())
|
|
|
|
.unwrap();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
// Checking out another commit (via the first repo instance) should now fail.
|
|
|
|
assert_eq!(
|
2020-12-19 07:50:10 +00:00
|
|
|
wc1.check_out(commit3),
|
2020-12-12 08:00:42 +00:00
|
|
|
Err(CheckoutError::ConcurrentCheckout)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check that the commit2 is still checked out on disk.
|
2021-11-21 22:39:34 +00:00
|
|
|
let workspace3 = Workspace::load(&settings, workspace1_root).unwrap();
|
2020-12-12 08:00:42 +00:00
|
|
|
assert_eq!(
|
2021-11-21 22:39:34 +00:00
|
|
|
workspace3.working_copy().current_tree_id(),
|
2020-12-12 08:00:42 +00:00
|
|
|
commit2.tree().id().clone()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2020-12-12 08:00:42 +00:00
|
|
|
fn test_checkout_parallel(use_git: bool) {
|
|
|
|
// Test that concurrent checkouts by different processes (simulated by using
|
|
|
|
// different repo instances) is safe.
|
|
|
|
let settings = testutils::user_settings();
|
2021-11-21 22:39:34 +00:00
|
|
|
let mut test_workspace = testutils::init_repo(&settings, use_git);
|
2021-11-21 07:46:54 +00:00
|
|
|
let repo = &test_workspace.repo;
|
2020-12-12 08:00:42 +00:00
|
|
|
let store = repo.store();
|
2021-11-21 22:39:34 +00:00
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2021-04-29 06:51:58 +00:00
|
|
|
let num_threads = max(num_cpus::get(), 4);
|
2021-08-11 18:18:37 +00:00
|
|
|
let mut tree_ids = HashSet::new();
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut commit_ids = vec![];
|
2021-09-11 16:45:06 +00:00
|
|
|
let mut tx = repo.start_transaction("test");
|
2021-04-29 06:51:58 +00:00
|
|
|
for i in 0..num_threads {
|
2021-05-17 05:47:31 +00:00
|
|
|
let path = RepoPath::from_internal_string(format!("file{}", i).as_str());
|
2021-11-21 07:46:54 +00:00
|
|
|
let tree = testutils::create_tree(repo, &[(&path, "contents")]);
|
2021-08-11 18:18:37 +00:00
|
|
|
tree_ids.insert(tree.id().clone());
|
2020-12-12 08:00:42 +00:00
|
|
|
let commit = CommitBuilder::for_new_commit(&settings, store, tree.id().clone())
|
|
|
|
.set_open(true)
|
2021-09-11 16:45:06 +00:00
|
|
|
.write_to_repo(tx.mut_repo());
|
2020-12-12 08:00:42 +00:00
|
|
|
commit_ids.push(commit.id().clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create another commit just so we can test the update stats reliably from the
|
|
|
|
// first update
|
2021-05-17 05:47:31 +00:00
|
|
|
let tree = testutils::create_tree(
|
2021-11-21 07:46:54 +00:00
|
|
|
repo,
|
2021-05-17 05:47:31 +00:00
|
|
|
&[(&RepoPath::from_internal_string("other file"), "contents")],
|
|
|
|
);
|
2020-12-12 08:00:42 +00:00
|
|
|
let commit = CommitBuilder::for_new_commit(&settings, store, tree.id().clone())
|
|
|
|
.set_open(true)
|
2021-03-17 05:27:12 +00:00
|
|
|
.write_to_repo(tx.mut_repo());
|
2020-12-12 08:00:42 +00:00
|
|
|
tx.commit();
|
2021-11-21 22:39:34 +00:00
|
|
|
test_workspace
|
|
|
|
.workspace
|
|
|
|
.working_copy_mut()
|
|
|
|
.check_out(commit)
|
|
|
|
.unwrap();
|
2020-12-12 08:00:42 +00:00
|
|
|
|
|
|
|
let mut threads = vec![];
|
|
|
|
for commit_id in &commit_ids {
|
2021-08-11 18:18:37 +00:00
|
|
|
let tree_ids = tree_ids.clone();
|
2020-12-12 08:00:42 +00:00
|
|
|
let commit_id = commit_id.clone();
|
|
|
|
let settings = settings.clone();
|
2021-11-21 22:39:34 +00:00
|
|
|
let workspace_root = workspace_root.clone();
|
2020-12-12 08:00:42 +00:00
|
|
|
let handle = thread::spawn(move || {
|
2021-11-21 22:39:34 +00:00
|
|
|
let mut workspace = Workspace::load(&settings, workspace_root).unwrap();
|
|
|
|
let commit = workspace
|
|
|
|
.repo_loader()
|
|
|
|
.store()
|
|
|
|
.get_commit(&commit_id)
|
|
|
|
.unwrap();
|
|
|
|
let stats = workspace.working_copy_mut().check_out(commit).unwrap();
|
2020-12-12 08:00:42 +00:00
|
|
|
assert_eq!(stats.updated_files, 0);
|
|
|
|
assert_eq!(stats.added_files, 1);
|
|
|
|
assert_eq!(stats.removed_files, 1);
|
2021-08-11 18:18:37 +00:00
|
|
|
// 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.
|
2021-11-21 22:39:34 +00:00
|
|
|
let locked_wc = workspace.working_copy_mut().write_tree();
|
2021-08-11 18:18:37 +00:00
|
|
|
let new_tree_id = locked_wc.new_tree_id();
|
|
|
|
locked_wc.discard();
|
|
|
|
assert!(tree_ids.contains(&new_tree_id));
|
2020-12-12 08:00:42 +00:00
|
|
|
});
|
|
|
|
threads.push(handle);
|
|
|
|
}
|
|
|
|
for thread in threads {
|
|
|
|
thread.join().ok().unwrap();
|
|
|
|
}
|
|
|
|
}
|