testutils: move load_repo_at_head() to TestEnvironment

It will depend on the TestBackendData mapping.
This commit is contained in:
Yuya Nishihara 2024-11-01 14:57:34 +09:00
parent 22f2393322
commit d4786a3256
5 changed files with 36 additions and 25 deletions

View file

@ -21,7 +21,6 @@ use jj_lib::workspace::default_working_copy_factories;
use jj_lib::workspace::Workspace;
use test_case::test_case;
use testutils::create_random_commit;
use testutils::load_repo_at_head;
use testutils::TestRepoBackend;
use testutils::TestWorkspace;
@ -189,6 +188,7 @@ fn test_bad_locking_interrupted(backend: TestRepoBackend) {
// operation.
let settings = testutils::user_settings();
let test_workspace = TestWorkspace::init_with_backend(&settings, backend);
let test_env = &test_workspace.env;
let repo = &test_workspace.repo;
let mut tx = repo.start_transaction(&settings);
@ -214,10 +214,10 @@ fn test_bad_locking_interrupted(backend: TestRepoBackend) {
copy_directory(&backup_path, &op_heads_dir);
// Reload the repo and check that only the new head is present.
let reloaded_repo = load_repo_at_head(&settings, test_workspace.repo_path());
let reloaded_repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path());
assert_eq!(reloaded_repo.op_id(), &op_id);
// Reload once more to make sure that the .jj/op_heads/ directory was updated
// correctly.
let reloaded_repo = load_repo_at_head(&settings, test_workspace.repo_path());
let reloaded_repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path());
assert_eq!(reloaded_repo.op_id(), &op_id);
}

View file

@ -20,7 +20,6 @@ use jj_lib::dag_walk;
use jj_lib::repo::ReadonlyRepo;
use jj_lib::repo::Repo;
use test_case::test_case;
use testutils::load_repo_at_head;
use testutils::write_random_commit;
use testutils::TestRepoBackend;
use testutils::TestWorkspace;
@ -81,12 +80,13 @@ fn test_commit_parallel_instances(backend: TestRepoBackend) {
// makes it behave very similar to separate processes.
let settings = testutils::user_settings();
let test_workspace = TestWorkspace::init_with_backend(&settings, backend);
let test_env = &test_workspace.env;
let num_threads = max(num_cpus::get(), 4);
thread::scope(|s| {
for _ in 0..num_threads {
let settings = settings.clone();
let repo = load_repo_at_head(&settings, test_workspace.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path());
s.spawn(move || {
let mut tx = repo.start_transaction(&settings);
write_random_commit(tx.repo_mut(), &settings);
@ -96,7 +96,7 @@ fn test_commit_parallel_instances(backend: TestRepoBackend) {
});
// One commit per thread plus the commit from the initial working-copy commit on
// top of the root commit
let repo = load_repo_at_head(&settings, test_workspace.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path());
assert_eq!(repo.view().heads().len(), num_threads + 1);
// One additional operation for the root operation, one for checking out the

View file

@ -66,7 +66,6 @@ use tempfile::TempDir;
use test_case::test_case;
use testutils::commit_transactions;
use testutils::create_random_commit;
use testutils::load_repo_at_head;
use testutils::write_random_commit;
use testutils::TestRepo;
use testutils::TestRepoBackend;
@ -3223,6 +3222,7 @@ fn test_rewrite_imported_commit() {
fn test_concurrent_write_commit() {
let settings = &testutils::user_settings();
let test_repo = TestRepo::init_with_backend(TestRepoBackend::Git);
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Try to create identical commits with different change ids. Timestamp of the
@ -3232,7 +3232,7 @@ fn test_concurrent_write_commit() {
thread::scope(|s| {
let barrier = Arc::new(Barrier::new(num_thread));
for i in 0..num_thread {
let repo = load_repo_at_head(settings, test_repo.repo_path()); // unshare loader
let repo = test_env.load_repo_at_head(settings, test_repo.repo_path()); // unshare loader
let barrier = barrier.clone();
let sender = sender.clone();
s.spawn(move || {
@ -3285,6 +3285,7 @@ fn test_concurrent_write_commit() {
fn test_concurrent_read_write_commit() {
let settings = &testutils::user_settings();
let test_repo = TestRepo::init_with_backend(TestRepoBackend::Git);
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Create unique commits and load them concurrently. In this test, we assume
@ -3318,7 +3319,7 @@ fn test_concurrent_read_write_commit() {
// Writer assigns random change id
for (i, commit_id) in commit_ids.iter().enumerate() {
let repo = load_repo_at_head(settings, test_repo.repo_path()); // unshare loader
let repo = test_env.load_repo_at_head(settings, test_repo.repo_path()); // unshare loader
let barrier = barrier.clone();
s.spawn(move || {
barrier.wait();
@ -3334,7 +3335,7 @@ fn test_concurrent_read_write_commit() {
// Reader may generate change id (if not yet assigned by the writer)
for i in 0..num_reader_thread {
let mut repo = load_repo_at_head(settings, test_repo.repo_path()); // unshare loader
let mut repo = test_env.load_repo_at_head(settings, test_repo.repo_path()); // unshare loader
let barrier = barrier.clone();
let mut pending_commit_ids = commit_ids.clone();
pending_commit_ids.rotate_left(i); // start lookup from different place

View file

@ -42,7 +42,6 @@ use jj_lib::settings::UserSettings;
use maplit::hashset;
use testutils::commit_transactions;
use testutils::create_random_commit;
use testutils::load_repo_at_head;
use testutils::test_backend::TestBackend;
use testutils::write_random_commit;
use testutils::CommitGraphBuilder;
@ -295,6 +294,7 @@ fn test_index_commits_previous_operations() {
// Test that commits visible only in previous operations are indexed.
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Remove commit B and C in one operation and make sure they're still
@ -322,7 +322,7 @@ fn test_index_commits_previous_operations() {
repo.index_store().as_any().downcast_ref().unwrap();
default_index_store.reinit().unwrap();
let repo = load_repo_at_head(&settings, test_repo.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 3 more
assert_eq!(index.num_commits(), 1 + 3);
@ -342,6 +342,7 @@ fn test_index_commits_hidden_but_referenced() {
// Test that hidden-but-referenced commits are indexed.
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Remote bookmarks are usually visible at a certain point in operation
@ -378,7 +379,7 @@ fn test_index_commits_hidden_but_referenced() {
repo.index_store().as_any().downcast_ref().unwrap();
default_index_store.reinit().unwrap();
let repo = load_repo_at_head(&settings, test_repo.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
// All commits should be reindexed
assert!(repo.index().has_id(commit_a.id()));
assert!(repo.index().has_id(commit_b.id()));
@ -389,6 +390,7 @@ fn test_index_commits_hidden_but_referenced() {
fn test_index_commits_incremental() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Create A in one operation, then B and C in another. Check that the index is
@ -420,7 +422,7 @@ fn test_index_commits_incremental() {
.unwrap();
tx.commit("test");
let repo = load_repo_at_head(&settings, test_repo.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 3 more
assert_eq!(index.num_commits(), 1 + 3);
@ -442,6 +444,7 @@ fn test_index_commits_incremental() {
fn test_index_commits_incremental_empty_transaction() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
// Create A in one operation, then just an empty transaction. Check that the
@ -464,7 +467,7 @@ fn test_index_commits_incremental_empty_transaction() {
repo.start_transaction(&settings).commit("test");
let repo = load_repo_at_head(&settings, test_repo.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
let index = as_readonly_composite(&repo);
// There should be the root commit, plus 1 more
assert_eq!(index.num_commits(), 1 + 1);
@ -613,6 +616,7 @@ fn test_index_commits_incremental_squashed() {
fn test_reindex_no_segments_dir() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
let mut tx = repo.start_transaction(&settings);
@ -625,7 +629,7 @@ fn test_reindex_no_segments_dir() {
assert!(segments_dir.is_dir());
fs::remove_dir_all(&segments_dir).unwrap();
let repo = load_repo_at_head(&settings, test_repo.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
assert!(repo.index().has_id(commit_a.id()));
}
@ -633,6 +637,7 @@ fn test_reindex_no_segments_dir() {
fn test_reindex_corrupt_segment_files() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
let mut tx = repo.start_transaction(&settings);
@ -653,7 +658,7 @@ fn test_reindex_corrupt_segment_files() {
fs::write(entry.path(), b"\0".repeat(24)).unwrap();
}
let repo = load_repo_at_head(&settings, test_repo.repo_path());
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path());
assert!(repo.index().has_id(commit_a.id()));
}
@ -710,6 +715,7 @@ fn test_reindex_from_merged_operation() {
fn test_reindex_missing_commit() {
let settings = testutils::user_settings();
let test_repo = TestRepo::init();
let test_env = &test_repo.env;
let repo = &test_repo.repo;
let mut tx = repo.start_transaction(&settings);
@ -724,7 +730,7 @@ fn test_reindex_missing_commit() {
// Remove historical head commit to simulate bad GC.
let test_backend: &TestBackend = repo.store().backend_impl().downcast_ref().unwrap();
test_backend.remove_commit_unchecked(missing_commit.id());
let repo = load_repo_at_head(&settings, test_repo.repo_path()); // discard cache
let repo = test_env.load_repo_at_head(&settings, test_repo.repo_path()); // discard cache
assert!(repo.store().get_commit(missing_commit.id()).is_err());
// Reindexing error should include the operation id where the commit

View file

@ -133,6 +133,17 @@ impl TestEnvironment {
pub fn root(&self) -> &Path {
self.temp_dir.path()
}
pub fn load_repo_at_head(
&self,
settings: &UserSettings,
repo_path: &Path,
) -> Arc<ReadonlyRepo> {
RepoLoader::init_from_file_system(settings, repo_path, &TestRepo::default_store_factories())
.unwrap()
.load_at_head(settings)
.unwrap()
}
}
pub struct TestRepo {
@ -295,13 +306,6 @@ impl TestWorkspace {
}
}
pub fn load_repo_at_head(settings: &UserSettings, repo_path: &Path) -> Arc<ReadonlyRepo> {
RepoLoader::init_from_file_system(settings, repo_path, &TestRepo::default_store_factories())
.unwrap()
.load_at_head(settings)
.unwrap()
}
pub fn commit_transactions(settings: &UserSettings, txs: Vec<Transaction>) -> Arc<ReadonlyRepo> {
let repo_loader = txs[0].base_repo().loader().clone();
let mut op_ids = vec![];