| 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::convert::Infallible; |
| 17 |
use std::sync::Arc; |
| 18 |
use std::thread; |
| 19 |
|
| 20 |
use jj_lib::dag_walk_async; |
| 21 |
use jj_lib::repo::ReadonlyRepo; |
| 22 |
use jj_lib::repo::Repo as _; |
| 23 |
use pollster::FutureExt as _; |
| 24 |
use test_case::test_case; |
| 25 |
use testutils::TestRepoBackend; |
| 26 |
use testutils::TestResult; |
| 27 |
use testutils::TestWorkspace; |
| 28 |
use testutils::write_random_commit; |
| 29 |
|
| 30 |
fn count_non_merge_operations(repo: &Arc<ReadonlyRepo>) -> usize { |
| 31 |
let op_store = repo.op_store(); |
| 32 |
let op_id = repo.op_id().clone(); |
| 33 |
let mut num_ops = 0; |
| 34 |
|
| 35 |
for op_id in dag_walk_async::dfs( |
| 36 |
vec![Ok::<_, Infallible>(op_id)], |
| 37 |
|op_id| op_id.clone(), |
| 38 |
|op_id| { |
| 39 |
op_store |
| 40 |
.read_operation(op_id) |
| 41 |
.block_on() |
| 42 |
.unwrap() |
| 43 |
.parents |
| 44 |
.into_iter() |
| 45 |
.map(Ok) |
| 46 |
}, |
| 47 |
) { |
| 48 |
let op = op_store.read_operation(&op_id.unwrap()).block_on().unwrap(); |
| 49 |
if op.parents.len() <= 1 { |
| 50 |
num_ops += 1; |
| 51 |
} |
| 52 |
} |
| 53 |
num_ops |
| 54 |
} |
| 55 |
|
| 56 |
#[test_case(TestRepoBackend::Simple ; "simple backend")] |
| 57 |
#[test_case(TestRepoBackend::Git ; "git backend")] |
| 58 |
fn test_commit_parallel(backend: TestRepoBackend) -> TestResult { |
| 59 |
// This loads a Repo instance and creates and commits many concurrent |
| 60 |
// transactions from it. It then reloads the repo. That should merge all the |
| 61 |
// operations and all commits should be visible. |
| 62 |
let test_workspace = TestWorkspace::init_with_backend(backend); |
| 63 |
let repo = &test_workspace.repo; |
| 64 |
|
| 65 |
let num_threads = max(num_cpus::get(), 4); |
| 66 |
thread::scope(|s| { |
| 67 |
for _ in 0..num_threads { |
| 68 |
let repo = repo.clone(); |
| 69 |
s.spawn(move || { |
| 70 |
let mut tx = repo.start_transaction(); |
| 71 |
write_random_commit(tx.repo_mut()); |
| 72 |
tx.commit("test").block_on().unwrap(); |
| 73 |
}); |
| 74 |
} |
| 75 |
}); |
| 76 |
let repo = repo.reload_at_head().block_on()?; |
| 77 |
// One commit per thread plus the commit from the initial working-copy on top of |
| 78 |
// the root commit |
| 79 |
assert_eq!(repo.view().heads().len(), num_threads + 1); |
| 80 |
|
| 81 |
// One additional operation for the root operation, one for checking out the |
| 82 |
// initial commit. |
| 83 |
assert_eq!(count_non_merge_operations(&repo), num_threads + 2); |
| 84 |
Ok(()) |
| 85 |
} |
| 86 |
|
| 87 |
#[test_case(TestRepoBackend::Simple ; "simple backend")] |
| 88 |
#[test_case(TestRepoBackend::Git ; "git backend")] |
| 89 |
fn test_commit_parallel_instances(backend: TestRepoBackend) -> TestResult { |
| 90 |
// Like the test above but creates a new repo instance for every thread, which |
| 91 |
// makes it behave very similar to separate processes. |
| 92 |
let settings = testutils::user_settings(); |
| 93 |
let test_workspace = TestWorkspace::init_with_backend_and_settings(backend, &settings); |
| 94 |
let test_env = &test_workspace.env; |
| 95 |
|
| 96 |
let num_threads = max(num_cpus::get(), 4); |
| 97 |
thread::scope(|s| { |
| 98 |
for _ in 0..num_threads { |
| 99 |
let settings = settings.clone(); |
| 100 |
let repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); |
| 101 |
s.spawn(move || { |
| 102 |
let mut tx = repo.start_transaction(); |
| 103 |
write_random_commit(tx.repo_mut()); |
| 104 |
tx.commit("test").block_on().unwrap(); |
| 105 |
}); |
| 106 |
} |
| 107 |
}); |
| 108 |
// One commit per thread plus the commit from the initial working-copy commit on |
| 109 |
// top of the root commit |
| 110 |
let repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); |
| 111 |
assert_eq!(repo.view().heads().len(), num_threads + 1); |
| 112 |
|
| 113 |
// One additional operation for the root operation, one for checking out the |
| 114 |
// initial commit. |
| 115 |
assert_eq!(count_non_merge_operations(&repo), num_threads + 2); |
| 116 |
Ok(()) |
| 117 |
} |