-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredigolock_test.go
448 lines (328 loc) · 9.01 KB
/
redigolock_test.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright (c) 2013 The Vubeologists. All rights reserved.
// See the license at the root of this project.
package redigolock
import (
"errors"
"fmt"
redigo "github.com/garyburd/redigo/redis"
"sync"
"testing"
"time"
)
//==============================================================================
const RedisHost = "127.0.0.1:6381"
//==============================================================================
type Redigomock struct {
FailureCall func(cmd string) (interface{}, error)
}
//==============================================================================
// Mock redigo to get full coverage of failure handling
func (m *Redigomock) Do(cmd string, args ...interface{}) (interface{}, error) {
return m.FailureCall(cmd)
}
func (m *Redigomock) Send(cmd string, args ...interface{}) error {
return errors.New("mock error")
}
func (m *Redigomock) Close() error {
return errors.New("mock error")
}
//==============================================================================
// Test a failure with redigo.Do
func Test_FailureDo(t *testing.T) {
key := "Test_FailureDo"
conn := Redigomock{}
conn.FailureCall = func(cmd string) (interface{}, error) {
return nil, errors.New("mock error")
}
lock := New(Redigoconn(&conn), key, 2000)
_, err := lock.Lock()
if err == nil {
t.Error("redigolock should have errored")
}
}
// Test a failure with redigo.Do via the function call wrapper
func Test_FailureDoFunc(t *testing.T) {
key := "Test_IncrementFuncFailure"
conn := Redigomock{}
conn.FailureCall = func(cmd string) (interface{}, error) {
return nil, errors.New("mock error")
}
lock := New(Redigoconn(&conn), key, 2000)
_, err := lock.LockFunc(func() {
})
if err == nil {
t.Error("redigolock should have errored")
}
}
// Test a failure with a Redis key existing for the lock with an unknown value
// instead of a TTL
func Test_FailureDoGetByteString(t *testing.T) {
key := "Test_FailureDoGetString"
conn := Redigomock{}
conn.FailureCall = func(cmd string) (interface{}, error) {
return []byte("abc"), nil
}
lock := New(Redigoconn(&conn), key, 2000)
_, err := lock.Lock()
if err == nil {
t.Error("redigolock should have errored")
}
}
// Test a failure with a Redis key existing for the lock with an unknown value
// type instead of a TTL
func Test_FailureDoGetString(t *testing.T) {
key := "Test_FailureDoGetString"
conn := Redigomock{}
conn.FailureCall = func(cmd string) (interface{}, error) {
return "abc", nil
}
lock := New(Redigoconn(&conn), key, 2000)
_, err := lock.Lock()
if err == nil {
t.Error("redigolock should have errored")
}
}
// Test a failure with redigo.Do("EXEC")
func Test_FailureDoExec(t *testing.T) {
key := "Test_FailureDoGetString"
conn := Redigomock{}
conn.FailureCall = func(cmd string) (interface{}, error) {
if cmd == "EXEC" {
return nil, errors.New("mock error")
}
return []byte{'1'}, nil
}
lock := New(Redigoconn(&conn), key, 2000)
_, err := lock.Lock()
if err == nil {
t.Error("redigolock should have errored")
}
}
// Test KeepAlive() failures
func Test_FailuresWithKeepAlive(t *testing.T) {
key := "Test_FailureWithKeepAlive"
conn := Redigomock{}
conn.FailureCall = func(cmd string) (interface{}, error) {
if cmd == "EXEC" {
return nil, errors.New("mock error")
}
return []byte{'1'}, nil
}
lock := New(Redigoconn(&conn), key, 2000)
err := lock.KeepAlive()
if err == nil {
t.Error("redigolock should have errored")
}
conn.FailureCall = func(cmd string) (interface{}, error) {
return []interface{}{[]byte("OK"), "abc"}, nil
}
err = lock.KeepAlive()
if err == nil {
t.Error("redigolock should have errored")
}
conn.FailureCall = func(cmd string) (interface{}, error) {
return []interface{}{[]byte("OK"), int64(0)}, nil
}
err = lock.KeepAlive()
if err == nil {
t.Error("redigolock should have errored")
}
}
//==============================================================================
// Test that general lock acquisition succeeds
func Test_Lock(t *testing.T) {
key := "Test_Lock"
conn, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Error(fmt.Sprintf("redigo.Dial failure [%s]", err))
}
lock := New(conn, key)
status, err := lock.Lock()
defer lock.UnlockIfLocked()
if err != nil {
t.Error(fmt.Sprintf("lock operation failed [%s]", err))
}
if !status {
t.Error("lock acquisition failed")
}
}
// Test that general lock acquisition succeeds when wrapping a function call
func Test_LockFunc(t *testing.T) {
key := "Test_LockFunc"
conn, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Error(fmt.Sprintf("redigo.Dial failure [%s]", err))
}
lock := New(conn, key, 2000)
status, err := lock.LockFunc(func() {
})
if err != nil {
t.Error(fmt.Sprintf("lock operation failed [%s]", err))
}
if !status {
t.Error("lock acquisition failed")
}
}
// Test that lock acquisition fails when the lock exists
func Test_BlockedLock(t *testing.T) {
key := "Test_BlockedLock"
conn, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Error(fmt.Sprintf("redigo.Dial failure [%s]", err))
}
lock1 := New(conn, key, 2001, 2000)
status, err := lock1.Lock()
defer lock1.UnlockIfLocked()
if err != nil {
t.Error(fmt.Sprintf("lock operation failed [%s]", err))
}
if !status {
t.Error("lock acquisition failed")
}
lock2 := New(conn, key, 1000)
status, err = lock2.Lock()
defer lock2.UnlockIfLocked()
if err != nil {
t.Error(fmt.Sprintf("lock operation failed [%s]", err))
}
if status {
t.Error("lock acquisition succeeded")
}
}
// Test that lock acquisition fails when the lock existed and was arbitrarily
// held after being unlocked
func Test_BlockedHold(t *testing.T) {
key := "Test_BlockedHold"
conn, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Error(fmt.Sprintf("redigo.Dial failure [%s]", err))
}
lock1 := New(conn, key, DefaultTimeout, DefaultAutoExpire, 2000)
status, err := lock1.Lock()
defer lock1.UnlockIfLocked()
if err != nil {
t.Error(fmt.Sprintf("lock operation failed [%s]", err))
}
if !status {
t.Error("lock acquisition failed")
}
lock1.Unlock()
lock2 := New(conn, key, 1000)
status, err = lock2.Lock()
defer lock2.UnlockIfLocked()
if err != nil {
t.Error(fmt.Sprintf("lock operation failed [%s]", err))
}
if status {
t.Error("lock acquisition succeeded")
}
}
// Test for race conditions when parallelising lock attempts that would cause
// a failure
func Test_MultiLock(t *testing.T) {
key := "Test_MultiLock"
res := make(chan bool)
for i := 0; i < 100; i++ {
go func() {
conn, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Error(fmt.Sprintf("redigo.Dial failure [%s]", err))
}
lock := New(conn, key, 2000, DefaultAutoExpire, DefaultHold, 5)
status, _ := lock.Lock()
defer lock.UnlockIfLocked()
res <- status
}()
}
for i := 1; i <= 100; i++ {
if !<-res {
t.Error("lock acquisition failed")
}
}
}
// Test that our locks are actually preventing race conditions by doing a
// non-atomic Redis operation surrounded by a lock
func Test_Increment(t *testing.T) {
key := "Test_Increment"
wg := new(sync.WaitGroup)
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
defer wg.Done()
conn, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Errorf("redigo.Dial failure due to '%s'", err)
return
}
lock := New(conn, key, 2000, DefaultAutoExpire, DefaultHold, 5)
status, err := lock.Lock()
if status {
// arbitrary distributed non-atomic operation
v, _ := redigo.Int(conn.Do("GET", key))
v++
time.Sleep(100000)
conn.Do("SET", key, v)
} else {
if err != nil {
t.Errorf("lock operation failure due to '%s", err)
} else {
t.Error("timed out during lock contention")
}
}
lock.UnlockIfLocked()
}()
}
wg.Wait()
conn, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Errorf("redigo.Dial failure due to '%s'", err)
}
v, _ := redigo.Int(conn.Do("GET", key))
if v != 100 {
t.Error("increment miscalculation")
}
conn.Do("DEL", key)
}
// Test that the keepalive function actually keeps the lock alive
func Test_TimeoutKeepAlive(t *testing.T) {
key := "Test_Keepalive"
wg := new(sync.WaitGroup)
conn1, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Errorf("redigo.Dial failure due to '%s'", err)
return
}
conn2, err := redigo.Dial("tcp", RedisHost)
if err != nil {
t.Errorf("redigo.Dial failure due to '%s'", err)
return
}
lock1 := New(conn1, key, 1000, 1000, 0, 5)
status, err := lock1.Lock()
if err != nil || !status {
t.Error("unable to lock")
}
wg.Add(20)
go func() {
for i := 0; i < 20; i++ {
err := lock1.KeepAlive()
if err != nil {
t.Errorf("timed out during lock contention due to '%v'", err)
}
wg.Done()
time.Sleep(time.Second / 2)
}
}()
time.Sleep(time.Second * 2)
lock2 := New(conn2, key, 1000, 1000, 0, 5)
status, err = lock2.Lock()
if status {
t.Error("should not have been able to lock")
}
wg.Wait()
time.Sleep(time.Second * 2)
status, err = lock2.Lock()
if err != nil || !status {
t.Error("should have been able to lock")
}
}