diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-13 08:09:33 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-13 08:10:16 +0300 |
| commit | f2dd8d8a515c1a2a220836231ad1a671a5e9b73d (patch) | |
| tree | 5b19585afb01b60d03d24a96b57bc7b986ea4cc0 /internal/filelock/filelock_test.go | |
| parent | 56002ff942de1bfb0ce467ec37a692b8c4ca01e9 (diff) | |
ask: serialize concurrent CLI with repo lock and stale PID recovery
Add advisory lock under .git/hexai-ask.lock around Taskwarrior execution,
with metadata (PID and process basename) and Linux /proc comm checks to
remove orphan lock files when the recorded holder is gone or not ask.
Extract internal/filelock for shared flock helpers; stats uses it too.
Made-with: Cursor
Diffstat (limited to 'internal/filelock/filelock_test.go')
| -rw-r--r-- | internal/filelock/filelock_test.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/internal/filelock/filelock_test.go b/internal/filelock/filelock_test.go new file mode 100644 index 0000000..f1f5b65 --- /dev/null +++ b/internal/filelock/filelock_test.go @@ -0,0 +1,67 @@ +package filelock + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" +) + +func TestTryExclusive_SecondDescriptorWouldBlock(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "lock") + f1, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = f1.Close() }) + f2, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = f2.Close() }) + if err := TryExclusive(f1); err != nil { + t.Fatalf("first TryExclusive: %v", err) + } + err = TryExclusive(f2) + if !errors.Is(err, ErrWouldBlock) { + t.Fatalf("second TryExclusive = %v, want ErrWouldBlock", err) + } + if err := UnlockExclusive(f1); err != nil { + t.Fatal(err) + } + if err := TryExclusive(f2); err != nil { + t.Fatalf("after unlock: %v", err) + } + if err := UnlockExclusive(f2); err != nil { + t.Fatal(err) + } +} + +func TestAcquireExclusive_ContextCancelledWhileBlocked(t *testing.T) { + dir := t.TempDir() + p := filepath.Join(dir, "lock") + fHeld, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = fHeld.Close() }) + if err := TryExclusive(fHeld); err != nil { + t.Fatal(err) + } + fWait, err := os.OpenFile(p, os.O_CREATE|os.O_RDWR, 0o600) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = fWait.Close() }) + ctx, cancel := context.WithCancel(context.Background()) + cancel() + _, err = AcquireExclusive(ctx, fWait) + if !errors.Is(err, context.Canceled) { + t.Fatalf("AcquireExclusive = %v, want context.Canceled", err) + } + if err := UnlockExclusive(fHeld); err != nil { + t.Fatal(err) + } +} |
