just playing with tangled
at tmp-tutorial 102 lines 3.8 kB view raw
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 15use std::cmp::max; 16use std::sync::Arc; 17use std::thread; 18 19use jj_lib::dag_walk; 20use jj_lib::repo::{ReadonlyRepo, Repo}; 21use test_case::test_case; 22use testutils::{load_repo_at_head, write_random_commit, TestWorkspace}; 23 24fn count_non_merge_operations(repo: &Arc<ReadonlyRepo>) -> usize { 25 let op_store = repo.op_store(); 26 let op_id = repo.op_id().clone(); 27 let mut num_ops = 0; 28 29 for op_id in dag_walk::dfs( 30 vec![op_id], 31 |op_id| op_id.clone(), 32 |op_id| op_store.read_operation(op_id).unwrap().parents, 33 ) { 34 if op_store.read_operation(&op_id).unwrap().parents.len() <= 1 { 35 num_ops += 1; 36 } 37 } 38 num_ops 39} 40 41#[test_case(false ; "local backend")] 42#[test_case(true ; "git backend")] 43fn test_commit_parallel(use_git: bool) { 44 // This loads a Repo instance and creates and commits many concurrent 45 // transactions from it. It then reloads the repo. That should merge all the 46 // operations and all commits should be visible. 47 let settings = testutils::user_settings(); 48 let test_workspace = TestWorkspace::init(&settings, use_git); 49 let repo = &test_workspace.repo; 50 51 let num_threads = max(num_cpus::get(), 4); 52 thread::scope(|s| { 53 for _ in 0..num_threads { 54 let settings = settings.clone(); 55 let repo = repo.clone(); 56 s.spawn(move || { 57 let mut tx = repo.start_transaction(&settings, "test"); 58 write_random_commit(tx.mut_repo(), &settings); 59 tx.commit(); 60 }); 61 } 62 }); 63 let repo = repo.reload_at_head(&settings).unwrap(); 64 // One commit per thread plus the commit from the initial working-copy on top of 65 // the root commit 66 assert_eq!(repo.view().heads().len(), num_threads + 1); 67 68 // One additional operation for initializing the repo, one for checking out the 69 // initial commit. 70 assert_eq!(count_non_merge_operations(&repo), num_threads + 2); 71} 72 73#[test_case(false ; "local backend")] 74#[test_case(true ; "git backend")] 75fn test_commit_parallel_instances(use_git: bool) { 76 // Like the test above but creates a new repo instance for every thread, which 77 // makes it behave very similar to separate processes. 78 let settings = testutils::user_settings(); 79 let test_workspace = TestWorkspace::init(&settings, use_git); 80 let repo = &test_workspace.repo; 81 82 let num_threads = max(num_cpus::get(), 4); 83 thread::scope(|s| { 84 for _ in 0..num_threads { 85 let settings = settings.clone(); 86 let repo = load_repo_at_head(&settings, repo.repo_path()); 87 s.spawn(move || { 88 let mut tx = repo.start_transaction(&settings, "test"); 89 write_random_commit(tx.mut_repo(), &settings); 90 tx.commit(); 91 }); 92 } 93 }); 94 // One commit per thread plus the commit from the initial working-copy commit on 95 // top of the root commit 96 let repo = load_repo_at_head(&settings, repo.repo_path()); 97 assert_eq!(repo.view().heads().len(), num_threads + 1); 98 99 // One addition operation for initializing the repo, one for checking out the 100 // initial commit. 101 assert_eq!(count_non_merge_operations(&repo), num_threads + 2); 102}