1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
// Copyright 2022 The Jujutsu Authors
//
// 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.
use itertools::Itertools as _;
use crate::common::CommandOutput;
use crate::common::TestEnvironment;
use crate::common::TestWorkDir;
#[test]
fn test_concurrent_operation_divergence() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
work_dir.run_jj(["describe", "-m", "message 1"]).success();
work_dir
.run_jj(["describe", "-m", "message 2", "--at-op", "@-"])
.success();
// "--at-op=@" disables op heads merging, and prints head operation ids.
let output = work_dir.run_jj(["op", "log", "--at-op=@"]);
insta::assert_snapshot!(output, @r#"
------- stderr -------
Error: The "@" expression resolved to more than one operation
Hint: Try specifying one of the operations by ID: b2cffe4f3026, d8ced2ea64a8
[EOF]
[exit status: 1]
"#);
// "op log --at-op" should work without merging the head operations
let output = work_dir.run_jj(["op", "log", "--at-op=d8ced2ea64a8"]);
insta::assert_snapshot!(output, @r"
@ d8ced2ea64a8 test-username@host.example.com 2001-02-03 04:05:09.000 +07:00 - 2001-02-03 04:05:09.000 +07:00
│ describe commit e8849ae12c709f2321908879bc724fdb2ab8a781
│ args: jj describe -m 'message 2' --at-op @-
○ 8f47435a3990 test-username@host.example.com 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
│ add workspace 'default'
○ 000000000000 root()
[EOF]
");
// We should be informed about the concurrent modification
let output = get_log_output(&work_dir);
insta::assert_snapshot!(output, @r"
@ message 1
│ ○ message 2
├─╯
◆
[EOF]
------- stderr -------
Concurrent modification detected, resolving automatically.
[EOF]
");
}
#[test]
fn test_concurrent_operations_auto_rebase() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
work_dir.write_file("file", "contents");
work_dir.run_jj(["describe", "-m", "initial"]).success();
work_dir.run_jj(["describe", "-m", "rewritten"]).success();
work_dir
.run_jj(["new", "--at-op=@-", "-m", "new child"])
.success();
// We should be informed about the concurrent modification
let output = get_log_output(&work_dir);
insta::assert_snapshot!(output, @r"
○ new child
@ rewritten
◆
[EOF]
------- stderr -------
Concurrent modification detected, resolving automatically.
Rebased 1 descendant commits onto commits rewritten by other operation
[EOF]
");
}
#[test]
fn test_concurrent_operations_wc_modified() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
work_dir.write_file("file", "contents\n");
work_dir.run_jj(["describe", "-m", "initial"]).success();
work_dir.run_jj(["new", "-m", "new child1"]).success();
work_dir
.run_jj(["new", "--at-op=@-", "-m", "new child2"])
.success();
work_dir.write_file("file", "modified\n");
// We should be informed about the concurrent modification
let output = get_log_output(&work_dir);
insta::assert_snapshot!(output, @r"
@ new child1
│ ○ new child2
├─╯
○ initial
◆
[EOF]
------- stderr -------
Concurrent modification detected, resolving automatically.
[EOF]
");
let output = work_dir.run_jj(["diff", "--git"]);
insta::assert_snapshot!(output, @r"
diff --git a/file b/file
index 12f00e90b6..2e0996000b 100644
--- a/file
+++ b/file
@@ -1,1 +1,1 @@
-contents
+modified
[EOF]
");
// The working copy should be committed after merging the operations
let output = work_dir.run_jj(["op", "log", "-Tdescription"]);
insta::assert_snapshot!(output, @r"
@ snapshot working copy
○ reconcile divergent operations
├─╮
○ │ new empty commit
│ ○ new empty commit
├─╯
○ describe commit 9a462e35578a347e6a3951bf7a58ad7146959a8b
○ snapshot working copy
○ add workspace 'default'
○
[EOF]
");
}
#[test]
fn test_concurrent_snapshot_wc_reloadable() {
let test_env = TestEnvironment::default();
test_env.run_jj_in(".", ["git", "init", "repo"]).success();
let work_dir = test_env.work_dir("repo");
let op_heads_dir = work_dir
.root()
.join(".jj")
.join("repo")
.join("op_heads")
.join("heads");
work_dir.write_file("base", "");
work_dir.run_jj(["commit", "-m", "initial"]).success();
// Create new commit and checkout it.
work_dir.write_file("child1", "");
work_dir.run_jj(["commit", "-m", "new child1"]).success();
let template = r#"id.short() ++ "\n" ++ description ++ "\n" ++ tags"#;
let output = work_dir.run_jj(["op", "log", "-T", template]);
insta::assert_snapshot!(output, @r"
@ a631dcf37fea
│ commit c91a0909a9d3f3d8392ba9fab88f4b40fc0810ee
│ args: jj commit -m 'new child1'
○ 2b8e6f8683dc
│ snapshot working copy
│ args: jj commit -m 'new child1'
○ 2e1c4ffb74ca
│ commit 9af4c151edead0304de97ce3a0b414552921a425
│ args: jj commit -m initial
○ cfe73d1664ae
│ snapshot working copy
│ args: jj commit -m initial
○ 8f47435a3990
│ add workspace 'default'
○ 000000000000
[EOF]
");
let template = r#"id ++ "\n""#;
let output = work_dir.run_jj(["op", "log", "--no-graph", "-T", template]);
let [op_id_after_snapshot, _, op_id_before_snapshot] =
output.stdout.raw().lines().next_array().unwrap();
insta::assert_snapshot!(op_id_after_snapshot[..12], @"a631dcf37fea");
insta::assert_snapshot!(op_id_before_snapshot[..12], @"2e1c4ffb74ca");
// Simulate a concurrent operation that began from the "initial" operation
// (before the "child1" snapshot) but finished after the "child1"
// snapshot and commit.
std::fs::rename(
op_heads_dir.join(op_id_after_snapshot),
op_heads_dir.join(op_id_before_snapshot),
)
.unwrap();
work_dir.write_file("child2", "");
let output = work_dir.run_jj(["describe", "-m", "new child2"]);
insta::assert_snapshot!(output, @r"
------- stderr -------
Working copy (@) now at: kkmpptxz 493da83e new child2
Parent commit (@-) : rlvkpnrz 15bd889d new child1
[EOF]
");
// Since the repo can be reloaded before snapshotting, "child2" should be
// a child of "child1", not of "initial".
let output = work_dir.run_jj(["log", "-T", "description", "-s"]);
insta::assert_snapshot!(output, @r"
@ new child2
│ A child2
○ new child1
│ A child1
○ initial
│ A base
◆
[EOF]
");
}
#[test]
fn test_git_head_race_condition() {
// Test for race condition where concurrent jj processes create divergent
// operations when importing/exporting Git HEAD. This test spawns two
// processes in parallel: one running `jj debug snapshot` repeatedly
// (which imports Git HEAD) and another running `jj next/prev` repeatedly
// (which exports Git HEAD). Without the fix, this would create divergent
// operations.
let test_env = TestEnvironment::default();
test_env
.run_jj_in(".", ["git", "init", "--colocate", "repo"])
.success();
let work_dir = test_env.work_dir("repo");
// Create a large initial working copy to make snapshotting slower
for j in 0..200 {
let filename = format!("file_{j}");
let content = format!("content for file {j}\n").repeat(100);
work_dir.write_file(&filename, &content);
}
work_dir.run_jj(["commit", "-m", "initial"]).success();
// Remember the initial commit for later verification
let initial_commit = work_dir
.run_jj(["log", "--no-graph", "-T=commit_id", "-r=@-"])
.success()
.stdout
.into_raw();
let initial_commit = initial_commit.trim().to_owned();
// Create additional commits to iterate through with jj next/prev
for i in 0..10 {
for j in 0..50 {
let filename = format!("file_{i}_{j}");
let content = format!("content for commit {i} file {j}\n").repeat(100);
work_dir.write_file(&filename, &content);
}
work_dir
.run_jj(["commit", "-m", &format!("commit {i}")])
.success();
}
// Extract environment from TestEnvironment to use in spawned processes
let base_cmd = test_env.new_jj_cmd();
let jj_bin = base_cmd.get_program().to_owned();
let base_env: Vec<_> = base_cmd
.get_envs()
.filter_map(|(k, v)| v.map(|v| (k.to_owned(), v.to_owned())))
// Filter out timestamp and randomness seed so each command gets different values
.filter(|(k, _)| k != "JJ_TIMESTAMP" && k != "JJ_OP_TIMESTAMP" && k != "JJ_RANDOMNESS_SEED")
.collect();
let repo_path = work_dir.root();
let duration = std::time::Duration::from_secs(5);
let start = std::time::Instant::now();
std::thread::scope(|s| {
s.spawn(|| {
while start.elapsed() < duration {
// Snapshot repeatedly without delay
let mut cmd = std::process::Command::new(&jj_bin);
cmd.current_dir(repo_path);
cmd.args(["debug", "snapshot"]);
for (key, value) in &base_env {
cmd.env(key, value);
}
let output = cmd.output().expect("Failed to spawn jj");
assert!(output.status.success(), "jj debug snapshot failed");
}
});
s.spawn(|| {
let mut count = 0;
while start.elapsed() < duration {
// Go through commit history back and forth
let direction = if count % 20 < 10 { "prev" } else { "next" };
let mut cmd = std::process::Command::new(&jj_bin);
cmd.current_dir(repo_path);
cmd.arg(direction);
for (key, value) in &base_env {
cmd.env(key, value);
}
let output = cmd.output().expect("Failed to spawn jj");
assert!(output.status.success(), "jj {direction} failed");
count += 1;
}
});
});
const IMPORT_GIT_HEAD: &str = "import git head";
// Check for concurrent operations
let output = work_dir.run_jj(["op", "log", "-T", "description", "--ignore-working-copy"]);
let concurrent_count = output
.stdout
.raw()
.lines()
.filter(|line| line.contains(IMPORT_GIT_HEAD))
.count();
if concurrent_count > 0 {
eprintln!("Found {concurrent_count} concurrent operations:");
eprintln!("{}", output.stdout.raw());
panic!("Race condition detected: {concurrent_count} concurrent operations found");
}
// Verify the operation description for importing Git HEAD hasn't changed
// First ensure we're not already on the initial commit (prev/next loop may have
// ended there)
work_dir.run_jj(["new"]).success();
// Use git to checkout the initial commit, then trigger import
std::process::Command::new("git")
.current_dir(work_dir.root())
.args(["checkout", "-q", &initial_commit])
.status()
.unwrap();
let last_op = work_dir
.run_jj(["op", "log", "-T", "description", "--limit=1"])
.success()
.stdout
.into_raw();
assert!(
last_op.contains(IMPORT_GIT_HEAD),
"Expected last operation to contain '{IMPORT_GIT_HEAD}', got: {last_op:?}"
);
}
#[must_use]
fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
work_dir.run_jj(["log", "-T", "description"])
}
|