| 1 | #!/usr/bin/env python3 |
| 2 | """Exercise command locking with disposable repositories and real jj processes.""" |
| 3 | |
| 4 | import fcntl |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import shutil |
| 8 | import signal |
| 9 | import subprocess |
| 10 | import sys |
| 11 | import tempfile |
| 12 | import time |
| 13 | import unittest |
| 14 | |
| 15 | |
| 16 | lock_file = fcntl.flock if sys.platform == "darwin" else fcntl.lockf |
| 17 | |
| 18 | |
| 19 | class CommandLockTest(unittest.TestCase): |
| 20 | def setUp(self): |
| 21 | temporary = tempfile.TemporaryDirectory(prefix="jj-lock-test-") |
| 22 | self.addCleanup(temporary.cleanup) |
| 23 | self.root = Path(temporary.name).resolve() |
| 24 | self.bin = self.root / "bin" |
| 25 | self.bin.mkdir() |
| 26 | shutil.copy2(Path(__file__).with_name("jj"), self.bin / "jj") |
| 27 | shutil.copy2(Path(__file__).with_name("client.py"), self.bin / "client.py") |
| 28 | native = Path(shutil.which("jj")) |
| 29 | if native.with_name("jj-real").exists(): |
| 30 | native = native.with_name("jj-real") |
| 31 | (self.bin / "jj-real").symlink_to(native) |
| 32 | config = self.root / "config.toml" |
| 33 | config.write_text('[user]\nname="Fixture"\nemail="fixture@example.invalid"\n' |
| 34 | '[signing]\nbehavior="drop"\n') |
| 35 | self.env = dict(os.environ, JJ_CONFIG=str(config), JJ_EDITOR="true", PROJECT_STORE_SSH="", |
| 36 | GIT_CONFIG_NOSYSTEM="1", GIT_CONFIG_GLOBAL=str(self.root / "absent")) |
| 37 | self.repo = self.root / "main" |
| 38 | self.run_jj("git", "init", "--no-colocate", str(self.repo)) |
| 39 | self.other = self.root / "other" |
| 40 | self.run_jj("git", "init", "--no-colocate", str(self.other)) |
| 41 | self.workspace = self.root / "agent workspace" |
| 42 | self.run_jj("-R", str(self.repo), "workspace", "add", str(self.workspace)) |
| 43 | self.lock = self.repo / ".jj/repo/command.lock" |
| 44 | |
| 45 | def run_jj(self, *args, cwd=None, check=True): |
| 46 | return subprocess.run([str(self.bin / "jj"), *args], cwd=cwd or self.root, |
| 47 | env=self.env, capture_output=True, text=True, timeout=10, check=check) |
| 48 | |
| 49 | def start(self, *args, cwd=None): |
| 50 | process = subprocess.Popen([str(self.bin / "jj"), *args], cwd=cwd or self.root, |
| 51 | env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 52 | text=True, start_new_session=True) |
| 53 | def cleanup(): |
| 54 | if process.poll() is None: |
| 55 | os.killpg(process.pid, signal.SIGKILL) |
| 56 | process.communicate(timeout=5) |
| 57 | self.addCleanup(cleanup) |
| 58 | return process |
| 59 | |
| 60 | def holder(self): |
| 61 | marker = self.root / "ready" |
| 62 | process = self.start("-R", str(self.repo), "util", "exec", "--", sys.executable, "-c", |
| 63 | "from pathlib import Path; import time; " |
| 64 | f"Path({str(marker)!r}).touch(); time.sleep(30)") |
| 65 | deadline = time.monotonic() + 10 |
| 66 | while not marker.exists(): |
| 67 | if process.poll() is not None or time.monotonic() > deadline: |
| 68 | self.fail("Holder did not start") |
| 69 | time.sleep(0.02) |
| 70 | with self.lock.open("r+") as lock: |
| 71 | with self.assertRaises(BlockingIOError): |
| 72 | lock_file(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 73 | return process |
| 74 | |
| 75 | def test_shared_workspace_lock_and_independent_repository(self): |
| 76 | holder = self.holder() |
| 77 | waiting = self.start("-R", str(self.workspace), "status") |
| 78 | time.sleep(0.2) |
| 79 | self.assertIsNone(waiting.poll()) |
| 80 | self.run_jj("-R", str(self.other), "describe", "-m", "independent") |
| 81 | os.killpg(holder.pid, signal.SIGKILL) |
| 82 | stdout, stderr = waiting.communicate(timeout=10) |
| 83 | self.assertEqual(waiting.returncode, 0, stderr) |
| 84 | self.assertIn("Waiting for another command", stderr) |
| 85 | self.assertNotIn("Waiting", stdout) |
| 86 | self.assertFalse((self.workspace / ".jj/command.lock").exists()) |
| 87 | |
| 88 | def test_selectors_and_snapshotting_reads_wait(self): |
| 89 | subdir = self.workspace / "subdir" |
| 90 | subdir.mkdir() |
| 91 | link = self.root / "linked" |
| 92 | link.symlink_to(subdir) |
| 93 | commands = [ |
| 94 | (["status"], link), |
| 95 | (["--repository=" + str(self.workspace), "diff"], self.root), |
| 96 | (["-R" + str(self.workspace), "log", "--no-graph", "-r", "@"], self.root), |
| 97 | (["--ignore-working-copy", "-R", str(self.workspace), "describe", "-m", "guarded"], self.root), |
| 98 | (["--config", 'user.name="-Rignored"', "-R", str(self.workspace), "status"], self.root), |
| 99 | ] |
| 100 | with self.lock.open("r+") as lock: |
| 101 | lock_file(lock, fcntl.LOCK_EX) |
| 102 | processes = [self.start(*args, cwd=cwd) for args, cwd in commands] |
| 103 | time.sleep(0.3) |
| 104 | self.assertTrue(all(process.poll() is None for process in processes)) |
| 105 | for process in processes: |
| 106 | _, stderr = process.communicate(timeout=10) |
| 107 | self.assertEqual(process.returncode, 0, stderr) |
| 108 | |
| 109 | def test_cancelled_waiter_and_failed_command_release(self): |
| 110 | with self.lock.open("r+") as lock: |
| 111 | lock_file(lock, fcntl.LOCK_EX) |
| 112 | waiting = self.start("-R", str(self.workspace), "status") |
| 113 | time.sleep(0.2) |
| 114 | waiting.send_signal(signal.SIGINT) |
| 115 | waiting.communicate(timeout=5) |
| 116 | self.assertEqual(waiting.returncode, 130) |
| 117 | self.assertNotEqual(self.run_jj("-R", str(self.repo), "unknown-command", check=False).returncode, 0) |
| 118 | self.run_jj("-R", str(self.workspace), "status") |
| 119 | self.assertEqual(self.run_jj("--version").returncode, 0) |
| 120 | |
| 121 | def test_concurrent_mutations_have_one_operation_head(self): |
| 122 | processes = [self.start("-R", str(self.repo), "new", "root()", "-m", f"fixture {i}") |
| 123 | for i in range(12)] |
| 124 | for process in processes: |
| 125 | _, stderr = process.communicate(timeout=10) |
| 126 | self.assertEqual(process.returncode, 0, stderr) |
| 127 | self.assertNotIn("Concurrent modification", stderr) |
| 128 | heads = self.repo / ".jj/repo/op_heads/heads" |
| 129 | self.assertEqual(len(list(heads.iterdir())), 1) |
| 130 | descriptions = self.run_jj("-R", str(self.repo), "log", "--no-graph", "-r", "all()", |
| 131 | "-T", 'description ++ "\n"').stdout |
| 132 | for i in range(12): |
| 133 | self.assertIn(f"fixture {i}\n", descriptions) |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | unittest.main() |