Skip to content

Commit

Permalink
testutils: move load_repo_at_head() to TestEnvironment
Browse files Browse the repository at this point in the history
It will depend on the TestBackendData mapping.
  • Loading branch information
yuja committed Nov 1, 2024
1 parent 22f2393 commit d4786a3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
6 changes: 3 additions & 3 deletions lib/tests/test_bad_locking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
6 changes: 3 additions & 3 deletions lib/tests/test_commit_concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 || {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
22 changes: 14 additions & 8 deletions lib/tests/test_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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()));
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -625,14 +629,15 @@ 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()));
}

#[test]
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);
Expand All @@ -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()));
}

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
18 changes: 11 additions & 7 deletions lib/testutils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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![];
Expand Down

0 comments on commit d4786a3

Please sign in to comment.