summaryrefslogtreecommitdiff
path: root/internal/askcli/runlock_test.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-13 08:09:33 +0300
committerPaul Buetow <paul@buetow.org>2026-04-13 08:10:16 +0300
commitf2dd8d8a515c1a2a220836231ad1a671a5e9b73d (patch)
tree5b19585afb01b60d03d24a96b57bc7b986ea4cc0 /internal/askcli/runlock_test.go
parent56002ff942de1bfb0ce467ec37a692b8c4ca01e9 (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/askcli/runlock_test.go')
-rw-r--r--internal/askcli/runlock_test.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/internal/askcli/runlock_test.go b/internal/askcli/runlock_test.go
new file mode 100644
index 0000000..f56f214
--- /dev/null
+++ b/internal/askcli/runlock_test.go
@@ -0,0 +1,46 @@
+package askcli
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+func TestAcquireAskRepoLock_SerializesConcurrentHolders(t *testing.T) {
+ tmp := t.TempDir()
+ if err := os.MkdirAll(filepath.Join(tmp, ".git"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ var maxHeld int32
+ var cur int32
+ var wg sync.WaitGroup
+ for i := 0; i < 6; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ unlock, err := acquireAskRepoLock(context.Background(), tmp)
+ if err != nil {
+ t.Errorf("lock: %v", err)
+ return
+ }
+ defer func() { _ = unlock() }()
+ n := atomic.AddInt32(&cur, 1)
+ for {
+ old := atomic.LoadInt32(&maxHeld)
+ if n <= old || atomic.CompareAndSwapInt32(&maxHeld, old, n) {
+ break
+ }
+ }
+ time.Sleep(25 * time.Millisecond)
+ atomic.AddInt32(&cur, -1)
+ }()
+ }
+ wg.Wait()
+ if got := atomic.LoadInt32(&maxHeld); got != 1 {
+ t.Fatalf("max concurrent lock holders = %d, want 1", got)
+ }
+}