Repository navigation
Expand file tree
/
Copy pathlock_test.go
More file actions
115 lines (92 loc) · 2.54 KB
/
Copy pathlock_test.go
File metadata and controls
115 lines (92 loc) · 2.54 KB
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
package main
import (
"testing"
"time"
)
func TestLockBasic(t *testing.T) {
lm := newLockManager()
// Lock succeeds
if err := lm.Lock("tab1", "agent-a", 5*time.Second); err != nil {
t.Fatal(err)
}
// Same owner can re-lock (extend)
if err := lm.Lock("tab1", "agent-a", 5*time.Second); err != nil {
t.Fatal(err)
}
// Different owner blocked
if err := lm.Lock("tab1", "agent-b", 5*time.Second); err == nil {
t.Fatal("expected lock conflict")
}
// Unlock
if err := lm.Unlock("tab1", "agent-a"); err != nil {
t.Fatal(err)
}
// Now agent-b can lock
if err := lm.Lock("tab1", "agent-b", 5*time.Second); err != nil {
t.Fatal(err)
}
}
func TestLockExpiry(t *testing.T) {
lm := newLockManager()
if err := lm.Lock("tab1", "agent-a", 1*time.Millisecond); err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Millisecond)
// Expired — different owner can now lock
if err := lm.Lock("tab1", "agent-b", 5*time.Second); err != nil {
t.Fatalf("expected expired lock to allow new owner: %v", err)
}
}
func TestLockCheckAccess(t *testing.T) {
lm := newLockManager()
// No lock — anyone can access
if err := lm.CheckAccess("tab1", "anyone"); err != nil {
t.Fatal(err)
}
_ = lm.Lock("tab1", "agent-a", 5*time.Second)
// Owner can access
if err := lm.CheckAccess("tab1", "agent-a"); err != nil {
t.Fatal(err)
}
// Non-owner blocked
if err := lm.CheckAccess("tab1", "agent-b"); err == nil {
t.Fatal("expected access denied")
}
// Empty owner blocked
if err := lm.CheckAccess("tab1", ""); err == nil {
t.Fatal("expected access denied for empty owner")
}
}
func TestUnlockIdempotent(t *testing.T) {
lm := newLockManager()
// Unlocking a non-locked tab is fine
if err := lm.Unlock("tab1", "anyone"); err != nil {
t.Fatal(err)
}
}
func TestUnlockWrongOwner(t *testing.T) {
lm := newLockManager()
_ = lm.Lock("tab1", "agent-a", 5*time.Second)
if err := lm.Unlock("tab1", "agent-b"); err == nil {
t.Fatal("expected unlock denied for wrong owner")
}
}
func TestMaxTimeout(t *testing.T) {
lm := newLockManager()
_ = lm.Lock("tab1", "agent-a", 10*time.Minute) // exceeds max
lock := lm.Get("tab1")
if lock == nil {
t.Fatal("expected lock")
}
// Should be capped to maxLockTimeout (5 min)
maxExpiry := time.Now().Add(maxLockTimeout + time.Second)
if lock.ExpiresAt.After(maxExpiry) {
t.Fatalf("lock timeout not capped: expires %v", lock.ExpiresAt)
}
}
func TestLockRequiresOwner(t *testing.T) {
lm := newLockManager()
if err := lm.Lock("tab1", "", 5*time.Second); err == nil {
t.Fatal("expected error for empty owner")
}
}