2020-12-12 11:00:42 +03: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 09:51:58 +03:00
|
|
|
use std::cmp::max;
|
2020-12-12 11:00:42 +03:00
|
|
|
use std::thread;
|
|
|
|
|
2021-05-15 19:16:31 +03:00
|
|
|
use jujutsu_lib::repo::ReadonlyRepo;
|
|
|
|
use jujutsu_lib::{dag_walk, testutils};
|
2020-12-12 11:00:42 +03:00
|
|
|
use test_case::test_case;
|
|
|
|
|
2021-04-29 09:51:58 +03:00
|
|
|
fn count_non_merge_operations(repo: &ReadonlyRepo) -> usize {
|
2021-03-11 02:48:32 +03:00
|
|
|
let op_store = repo.op_store();
|
2021-03-14 08:38:37 +03:00
|
|
|
let op_id = repo.op_id().clone();
|
2020-12-12 11:00:42 +03:00
|
|
|
let mut num_ops = 0;
|
|
|
|
|
|
|
|
for op_id in dag_walk::bfs(
|
2021-03-11 02:22:04 +03:00
|
|
|
vec![op_id],
|
2020-12-12 11:00:42 +03:00
|
|
|
Box::new(|op_id| op_id.clone()),
|
2021-06-14 10:18:38 +03:00
|
|
|
Box::new(|op_id| op_store.read_operation(op_id).unwrap().parents),
|
2020-12-12 11:00:42 +03:00
|
|
|
) {
|
|
|
|
if op_store.read_operation(&op_id).unwrap().parents.len() <= 1 {
|
|
|
|
num_ops += 1;
|
|
|
|
}
|
|
|
|
}
|
2020-12-30 10:37:09 +03:00
|
|
|
num_ops
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test_case(false ; "local store")]
|
|
|
|
#[test_case(true ; "git store")]
|
|
|
|
fn test_commit_parallel(use_git: bool) {
|
|
|
|
// This loads a Repo instance and creates and commits many concurrent
|
|
|
|
// transactions from it. It then reloads the repo. That should merge all the
|
|
|
|
// operations and all commits should be visible.
|
|
|
|
let settings = testutils::user_settings();
|
2021-04-11 19:23:16 +03:00
|
|
|
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2021-04-29 09:51:58 +03:00
|
|
|
let num_threads = max(num_cpus::get(), 4);
|
2020-12-12 11:00:42 +03:00
|
|
|
let mut threads = vec![];
|
2021-04-29 09:51:58 +03:00
|
|
|
for _ in 0..num_threads {
|
2020-12-12 11:00:42 +03:00
|
|
|
let settings = settings.clone();
|
|
|
|
let repo = repo.clone();
|
|
|
|
let handle = thread::spawn(move || {
|
|
|
|
testutils::create_random_commit(&settings, &repo)
|
|
|
|
.write_to_new_transaction(&repo, "test");
|
|
|
|
});
|
|
|
|
threads.push(handle);
|
|
|
|
}
|
|
|
|
for thread in threads {
|
|
|
|
thread.join().ok().unwrap();
|
|
|
|
}
|
2021-05-19 23:30:28 +03:00
|
|
|
let repo = repo.reload();
|
2020-12-12 11:00:42 +03:00
|
|
|
// One commit per thread plus the commit from the initial checkout on top of the
|
|
|
|
// root commit
|
2021-04-29 09:51:58 +03:00
|
|
|
assert_eq!(repo.view().heads().len(), num_threads + 1);
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2020-12-30 10:37:09 +03:00
|
|
|
// One operation for initializing the repo (containing the root id and the
|
|
|
|
// initial working copy commit).
|
2021-04-29 09:51:58 +03:00
|
|
|
assert_eq!(count_non_merge_operations(&repo), num_threads + 1);
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test_case(false ; "local store")]
|
|
|
|
#[test_case(true ; "git store")]
|
|
|
|
fn test_commit_parallel_instances(use_git: bool) {
|
|
|
|
// Like the test above but creates a new repo instance for every thread, which
|
|
|
|
// makes it behave very similar to separate processes.
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let (_temp_dir, repo) = testutils::init_repo(&settings, use_git);
|
|
|
|
|
2021-04-29 09:51:58 +03:00
|
|
|
let num_threads = max(num_cpus::get(), 4);
|
2020-12-12 11:00:42 +03:00
|
|
|
let mut threads = vec![];
|
2021-04-29 09:51:58 +03:00
|
|
|
for _ in 0..num_threads {
|
2020-12-12 11:00:42 +03:00
|
|
|
let settings = settings.clone();
|
2021-01-04 10:11:22 +03:00
|
|
|
let repo = ReadonlyRepo::load(&settings, repo.working_copy_path().clone()).unwrap();
|
2020-12-12 11:00:42 +03:00
|
|
|
let handle = thread::spawn(move || {
|
|
|
|
testutils::create_random_commit(&settings, &repo)
|
|
|
|
.write_to_new_transaction(&repo, "test");
|
|
|
|
});
|
|
|
|
threads.push(handle);
|
|
|
|
}
|
|
|
|
for thread in threads {
|
|
|
|
thread.join().ok().unwrap();
|
|
|
|
}
|
|
|
|
// One commit per thread plus the commit from the initial checkout on top of the
|
|
|
|
// root commit
|
2021-01-04 10:11:22 +03:00
|
|
|
let repo = ReadonlyRepo::load(&settings, repo.working_copy_path().clone()).unwrap();
|
2021-04-29 09:51:58 +03:00
|
|
|
assert_eq!(repo.view().heads().len(), num_threads + 1);
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2020-12-30 10:37:09 +03:00
|
|
|
// One operation for initializing the repo (containing the root id and the
|
|
|
|
// initial working copy commit).
|
2021-04-29 09:51:58 +03:00
|
|
|
assert_eq!(count_non_merge_operations(&repo), num_threads + 1);
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|