-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilelock_test.go
More file actions
223 lines (189 loc) · 5.47 KB
/
filelock_test.go
File metadata and controls
223 lines (189 loc) · 5.47 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
package main
import (
"fmt"
"sync"
"testing"
)
/*
File Locking System Tests (filelock_test.go)
WHAT IS BEING TESTED:
- Constructor validation: NewFileLock() returns properly initialized instance
- Basic locking: Files can be locked successfully, including edge cases
- Double locking prevention: Same file cannot be locked twice
- Lock status checking: Lock state queries and isolation between filenames
- Unlocking: Unlocked files can be locked again
- Graceful error handling: Unlocking non-existent locks doesn't panic
- Concurrency safety: Only one goroutine can acquire the same lock
- Parallel locking: Different files can be locked simultaneously
- Lock lifecycle: Repeated lock/unlock cycles work correctly
- Empty string validation: Empty filenames are rejected (returns false)
WHAT IS NOT BEING TESTED:
- Memory cleanup after many lock/unlock cycles
- Behavior with extremely long filenames (>1000 chars)
- Performance under high contention
- Lock timeouts or expiration
- Persistence across process restarts
- Path traversal protection in filenames
NOTES:
- Empty string filenames are NOT allowed - Lock("") and IsLocked("") return false
QUESTIONS:
- What's the maximum supported filename length? Should there be validation or limits?
- Should there be any filename sanitization (e.g., path traversal protection)?
*/
// TestNewFileLock validates constructor creates properly initialized instance
func TestNewFileLock(t *testing.T) {
fl := NewFileLock()
if fl == nil {
t.Fatal("NewFileLock() returned nil")
}
if fl.m == nil {
t.Error("FileLock map is nil")
}
}
// TestFileLock_Lock tests basic locking functionality including edge cases
func TestFileLock_Lock(t *testing.T) {
tests := []struct {
name string
filename string
want bool
}{
{"lock new file", "test.avro", true},
{"lock empty string", "", false},
{"lock file with spaces", "test file.avro", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fl := NewFileLock()
got := fl.Lock(tt.filename)
if got != tt.want {
t.Errorf("Lock() = %v, want %v", got, tt.want)
}
})
}
}
// TestFileLock_DoubleLock ensures the same file cannot be locked twice (critical for preventing duplicate uploads)
func TestFileLock_DoubleLock(t *testing.T) {
fl := NewFileLock()
filename := "test.avro"
// First lock should succeed
if !fl.Lock(filename) {
t.Error("First lock should succeed")
}
// Second lock should fail
if fl.Lock(filename) {
t.Error("Second lock should fail")
}
}
func TestFileLock_IsLocked(t *testing.T) {
fl := NewFileLock()
filename := "test.avro"
// File should not be locked initially
if fl.IsLocked(filename) {
t.Error("File should not be locked initially")
}
// Lock the file
fl.Lock(filename)
// File should now be locked
if !fl.IsLocked(filename) {
t.Error("File should be locked after Lock()")
}
// Different file should not be locked
if fl.IsLocked("other.avro") {
t.Error("Different file should not be locked")
}
}
func TestFileLock_Unlock(t *testing.T) {
fl := NewFileLock()
filename := "test.avro"
// Lock the file first
fl.Lock(filename)
if !fl.IsLocked(filename) {
t.Error("File should be locked")
}
// Unlock the file
fl.Unlock(filename)
if fl.IsLocked(filename) {
t.Error("File should be unlocked after Unlock()")
}
// Should be able to lock again after unlock
if !fl.Lock(filename) {
t.Error("Should be able to lock again after unlock")
}
}
func TestFileLock_UnlockNonExistent(t *testing.T) {
fl := NewFileLock()
// Unlocking a file that was never locked should not panic
fl.Unlock("nonexistent.avro")
// Test passes if no panic occurs
}
// TestFileLock_ConcurrentAccess tests that only one goroutine can acquire the same lock (core concurrency safety)
func TestFileLock_ConcurrentAccess(t *testing.T) {
fl := NewFileLock()
filename := "concurrent.avro"
var successCount int
var mu sync.Mutex
const goroutines = 10
var wg sync.WaitGroup
wg.Add(goroutines)
// Launch multiple goroutines trying to lock the same file
for i := 0; i < goroutines; i++ {
go func() {
defer wg.Done()
if fl.Lock(filename) {
mu.Lock()
successCount++
mu.Unlock()
}
}()
}
wg.Wait()
// Only one goroutine should have successfully locked the file
if successCount != 1 {
t.Errorf("Expected exactly 1 successful lock, got %d", successCount)
}
// The file should still be locked
if !fl.IsLocked(filename) {
t.Error("File should still be locked after concurrent access")
}
}
func TestFileLock_ConcurrentDifferentFiles(t *testing.T) {
fl := NewFileLock()
const goroutines = 10
var wg sync.WaitGroup
wg.Add(goroutines)
// Launch goroutines trying to lock different files
for i := 0; i < goroutines; i++ {
go func(fileNum int) {
defer wg.Done()
filename := fmt.Sprintf("file%d.avro", fileNum)
if !fl.Lock(filename) {
t.Errorf("Should be able to lock different file %s", filename)
}
}(i)
}
wg.Wait()
// All files should be locked
for i := 0; i < goroutines; i++ {
filename := fmt.Sprintf("file%d.avro", i)
if !fl.IsLocked(filename) {
t.Errorf("File %s should be locked", filename)
}
}
}
func TestFileLock_LockUnlockCycle(t *testing.T) {
fl := NewFileLock()
filename := "cycle.avro"
const cycles = 100
for i := 0; i < cycles; i++ {
if !fl.Lock(filename) {
t.Fatalf("Lock failed on cycle %d", i)
}
if !fl.IsLocked(filename) {
t.Fatalf("File not locked on cycle %d", i)
}
fl.Unlock(filename)
if fl.IsLocked(filename) {
t.Fatalf("File still locked after unlock on cycle %d", i)
}
}
}