-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlock_test.go
More file actions
244 lines (193 loc) · 5.94 KB
/
lock_test.go
File metadata and controls
244 lines (193 loc) · 5.94 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package stack
import (
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLock_Basic(t *testing.T) {
dir := t.TempDir()
lock, err := Lock(dir)
require.NoError(t, err)
require.NotNil(t, lock)
lock.Unlock()
}
func TestLock_NilUnlockSafe(t *testing.T) {
// Unlock on nil should not panic.
var lock *FileLock
lock.Unlock()
}
func TestLock_BlocksUntilReleased(t *testing.T) {
dir := t.TempDir()
lock1, err := Lock(dir)
require.NoError(t, err)
acquired := make(chan struct{})
errCh := make(chan error, 1)
go func() {
lock2, err := Lock(dir)
if err != nil {
errCh <- err
return
}
close(acquired)
lock2.Unlock()
}()
// lock2 should be blocked while lock1 is held.
select {
case <-acquired:
t.Fatal("lock2 acquired while lock1 was still held")
case err := <-errCh:
t.Fatalf("lock2 failed: %v", err)
case <-time.After(300 * time.Millisecond):
// expected — lock2 is waiting
}
lock1.Unlock()
// After releasing lock1, lock2 should acquire promptly.
select {
case <-acquired:
// success
case err := <-errCh:
t.Fatalf("lock2 failed after lock1 released: %v", err)
case <-time.After(5 * time.Second):
t.Fatal("lock2 did not acquire after lock1 was released")
}
}
func TestLock_SerializesConcurrentAccess(t *testing.T) {
dir := t.TempDir()
// Write an initial stack file with 0 stacks.
sf := &StackFile{SchemaVersion: 1, Stacks: []Stack{}}
require.NoError(t, Save(dir, sf))
// Run 10 concurrent goroutines, each adding a stack under lock.
// Uses Lock + Load + writeStackFile for atomic read-modify-write.
errCh := make(chan error, 10)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
lock, err := Lock(dir)
if err != nil {
errCh <- fmt.Errorf("goroutine %d Lock: %w", idx, err)
return
}
defer lock.Unlock()
loaded, err := Load(dir)
if err != nil {
errCh <- fmt.Errorf("goroutine %d Load: %w", idx, err)
return
}
loaded.AddStack(makeStack("main", "branch"))
if err := writeStackFile(dir, loaded); err != nil {
errCh <- fmt.Errorf("goroutine %d writeStackFile: %w", idx, err)
}
}(i)
}
wg.Wait()
close(errCh)
for err := range errCh {
t.Error(err)
}
// All 10 stacks should be present — no lost updates.
final, err := Load(dir)
require.NoError(t, err)
assert.Len(t, final.Stacks, 10, "all concurrent writes should be preserved")
}
func TestLock_FileLeftOnDisk(t *testing.T) {
dir := t.TempDir()
lock, err := Lock(dir)
require.NoError(t, err)
lock.Unlock()
// Lock file should still exist after unlock (no os.Remove race).
_, err = os.Stat(filepath.Join(dir, lockFileName))
require.NoError(t, err, "lock file should remain on disk after unlock")
lock2, err := Lock(dir)
require.NoError(t, err, "should be able to re-lock after unlock")
lock2.Unlock()
}
func TestLock_TimesOut(t *testing.T) {
dir := t.TempDir()
// Hold the lock so the second attempt can never acquire it.
lock1, err := Lock(dir)
require.NoError(t, err)
defer lock1.Unlock()
// Save original timeout and set a short one for the test.
origTimeout := LockTimeout
LockTimeout = 200 * time.Millisecond
defer func() { LockTimeout = origTimeout }()
start := time.Now()
lock2, err := Lock(dir)
elapsed := time.Since(start)
assert.Nil(t, lock2, "should not acquire lock")
require.Error(t, err)
var lockErr *LockError
require.True(t, errors.As(err, &lockErr), "error should be *LockError, got %T", err)
assert.Contains(t, lockErr.Error(), "timed out")
// Should have waited roughly LockTimeout before giving up.
assert.GreaterOrEqual(t, elapsed, 150*time.Millisecond, "should wait near the timeout")
}
func TestSave_DetectsStaleFile(t *testing.T) {
dir := t.TempDir()
// Write an initial stack file.
sf := &StackFile{SchemaVersion: 1, Stacks: []Stack{}}
require.NoError(t, Save(dir, sf))
// Load — captures the on-disk checksum.
loaded, err := Load(dir)
require.NoError(t, err)
// Simulate another process: load, modify, save.
other, err := Load(dir)
require.NoError(t, err)
other.AddStack(makeStack("main", "sneaky"))
require.NoError(t, Save(dir, other))
// Our loaded copy tries to save — should detect staleness.
loaded.AddStack(makeStack("main", "my-branch"))
err = Save(dir, loaded)
require.Error(t, err)
var staleErr *StaleError
require.True(t, errors.As(err, &staleErr), "error should be *StaleError, got %T", err)
assert.Contains(t, staleErr.Error(), "modified by another process")
}
func TestSave_AllowsWriteWhenFileUnchanged(t *testing.T) {
dir := t.TempDir()
// Write, load, modify, save — no concurrent changes.
sf := &StackFile{SchemaVersion: 1, Stacks: []Stack{}}
require.NoError(t, Save(dir, sf))
loaded, err := Load(dir)
require.NoError(t, err)
loaded.AddStack(makeStack("main", "feature"))
require.NoError(t, Save(dir, loaded))
// Verify the write actually persisted.
final, err := Load(dir)
require.NoError(t, err)
assert.Len(t, final.Stacks, 1)
}
func TestSave_AllowsFirstWrite(t *testing.T) {
dir := t.TempDir()
// File doesn't exist — Load returns nil checksum, Save should succeed.
sf, err := Load(dir)
require.NoError(t, err)
assert.Empty(t, sf.Stacks)
sf.AddStack(makeStack("main", "first"))
require.NoError(t, Save(dir, sf), "first save to a new file should succeed")
final, err := Load(dir)
require.NoError(t, err)
assert.Len(t, final.Stacks, 1)
}
func TestSave_DoubleSaveSucceeds(t *testing.T) {
dir := t.TempDir()
sf, err := Load(dir)
require.NoError(t, err)
sf.AddStack(makeStack("main", "first"))
require.NoError(t, Save(dir, sf), "first save should succeed")
// A second Save on the same instance must not spuriously fail —
// writeStackFile refreshes loadChecksum after writing.
sf.AddStack(makeStack("main", "second"))
require.NoError(t, Save(dir, sf), "second save on same instance should succeed")
final, err := Load(dir)
require.NoError(t, err)
assert.Len(t, final.Stacks, 2)
}