blob: f56f214fcaf4a24b7ca9748748513f820326730c (
plain)
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
|
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)
}
}
|