-
Notifications
You must be signed in to change notification settings - Fork 42
/
lock_test.go
540 lines (513 loc) · 14.6 KB
/
lock_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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// Copyright 2021 gotomicro
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rlock
import (
"context"
"errors"
"fmt"
"go.uber.org/mock/gomock"
"testing"
"time"
"github.com/gotomicro/redis-lock/mocks"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_Lock(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testCases := []struct {
name string
mock func() redis.Cmdable
key string
expiration time.Duration
retry RetryStrategy
timeout time.Duration
wantLock *Lock
wantErr string
}{
{
name: "locked",
mock: func() redis.Cmdable {
cmdable := mocks.NewMockCmdable(ctrl)
res := redis.NewCmd(context.Background(), nil)
res.SetVal("OK")
cmdable.EXPECT().Eval(gomock.Any(), luaLock, []string{"locked-key"}, gomock.Any()).
Return(res)
return cmdable
},
key: "locked-key",
expiration: time.Minute,
retry: &FixIntervalRetry{Interval: time.Second, Max: 1},
timeout: time.Second,
wantLock: &Lock{
key: "locked-key",
expiration: time.Minute,
},
},
{
name: "not retryable",
mock: func() redis.Cmdable {
cmdable := mocks.NewMockCmdable(ctrl)
res := redis.NewCmd(context.Background(), nil)
res.SetErr(errors.New("network error"))
cmdable.EXPECT().Eval(gomock.Any(), luaLock, []string{"locked-key"}, gomock.Any()).
Return(res)
return cmdable
},
key: "locked-key",
expiration: time.Minute,
retry: &FixIntervalRetry{Interval: time.Second, Max: 1},
timeout: time.Second,
wantErr: "network error",
},
{
name: "retry over times",
mock: func() redis.Cmdable {
cmdable := mocks.NewMockCmdable(ctrl)
first := redis.NewCmd(context.Background(), nil)
first.SetErr(context.DeadlineExceeded)
cmdable.EXPECT().Eval(gomock.Any(), luaLock, []string{"retry-key"}, gomock.Any()).
Times(3).Return(first)
return cmdable
},
key: "retry-key",
expiration: time.Minute,
retry: &FixIntervalRetry{Interval: time.Millisecond, Max: 2},
timeout: time.Second,
wantErr: "rlock: 重试机会耗尽,最后一次重试错误: context deadline exceeded",
},
{
name: "retry over times-lock holded",
mock: func() redis.Cmdable {
cmdable := mocks.NewMockCmdable(ctrl)
first := redis.NewCmd(context.Background(), nil)
//first.Set
cmdable.EXPECT().Eval(gomock.Any(), luaLock, []string{"retry-key"}, gomock.Any()).
Times(3).Return(first)
return cmdable
},
key: "retry-key",
expiration: time.Minute,
retry: &FixIntervalRetry{Interval: time.Millisecond, Max: 2},
timeout: time.Second,
wantErr: "rlock: 重试机会耗尽,锁被人持有: rlock: 抢锁失败",
},
{
name: "retry and success",
mock: func() redis.Cmdable {
cmdable := mocks.NewMockCmdable(ctrl)
first := redis.NewCmd(context.Background(), nil)
first.SetVal("")
cmdable.EXPECT().Eval(gomock.Any(), luaLock, []string{"retry-key"}, gomock.Any()).
Times(2).Return(first)
second := redis.NewCmd(context.Background(), nil)
second.SetVal("OK")
cmdable.EXPECT().Eval(gomock.Any(), luaLock, []string{"retry-key"}, gomock.Any()).
Return(second)
return cmdable
},
key: "retry-key",
expiration: time.Minute,
retry: &FixIntervalRetry{Interval: time.Millisecond, Max: 3},
timeout: time.Second,
wantLock: &Lock{
key: "retry-key",
expiration: time.Minute,
},
},
{
name: "retry but timeout",
mock: func() redis.Cmdable {
cmdable := mocks.NewMockCmdable(ctrl)
first := redis.NewCmd(context.Background(), nil)
first.SetVal("")
cmdable.EXPECT().Eval(gomock.Any(), luaLock, []string{"retry-key"}, gomock.Any()).
Times(2).Return(first)
return cmdable
},
key: "retry-key",
expiration: time.Minute,
retry: &FixIntervalRetry{Interval: time.Millisecond * 550, Max: 2},
timeout: time.Second,
wantErr: "context deadline exceeded",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockRedisCmd := tc.mock()
client := NewClient(mockRedisCmd)
ctx, cancel := context.WithTimeout(context.Background(), tc.timeout)
defer cancel()
l, err := client.Lock(ctx, tc.key, tc.expiration, tc.retry, time.Second)
if tc.wantErr != "" {
assert.EqualError(t, err, tc.wantErr)
return
} else {
require.NoError(t, err)
}
assert.Equal(t, mockRedisCmd, l.client)
assert.Equal(t, tc.key, l.key)
assert.Equal(t, tc.expiration, l.expiration)
assert.NotEmpty(t, l.value)
})
}
}
func TestClient_TryLock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testCases := []struct {
name string
mock func() redis.Cmdable
key string
expiration time.Duration
wantLock *Lock
wantErr error
}{
{
// 加锁成功
name: "locked",
key: "locked-key",
expiration: time.Minute,
mock: func() redis.Cmdable {
res := mocks.NewMockCmdable(ctrl)
res.EXPECT().SetNX(gomock.Any(), "locked-key", gomock.Any(), time.Minute).
Return(redis.NewBoolResult(true, nil))
return res
},
wantLock: &Lock{
key: "locked-key",
expiration: time.Minute,
},
},
{
// mock 网络错误
name: "network error",
key: "network-key",
expiration: time.Minute,
mock: func() redis.Cmdable {
res := mocks.NewMockCmdable(ctrl)
res.EXPECT().SetNX(gomock.Any(), "network-key", gomock.Any(), time.Minute).
Return(redis.NewBoolResult(false, errors.New("network error")))
return res
},
wantErr: errors.New("network error"),
},
{
// 模拟并发竞争失败
name: "failed",
key: "failed-key",
expiration: time.Minute,
mock: func() redis.Cmdable {
res := mocks.NewMockCmdable(ctrl)
res.EXPECT().SetNX(gomock.Any(), "failed-key", gomock.Any(), time.Minute).
Return(redis.NewBoolResult(false, nil))
return res
},
wantErr: ErrFailedToPreemptLock,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mockRedisCmd := tc.mock()
client := NewClient(mockRedisCmd)
l, err := client.TryLock(context.Background(), tc.key, tc.expiration)
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, mockRedisCmd, l.client)
assert.Equal(t, tc.key, l.key)
assert.Equal(t, tc.expiration, l.expiration)
assert.NotEmpty(t, l.value)
})
}
}
func TestLock_Unlock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testCases := []struct {
name string
mock func() redis.Cmdable
wantErr error
}{
{
// 解锁成功
name: "unlocked",
mock: func() redis.Cmdable {
rdb := mocks.NewMockCmdable(ctrl)
cmd := redis.NewCmd(context.Background())
cmd.SetVal(int64(1))
rdb.EXPECT().Eval(gomock.Any(), luaUnlock, gomock.Any(), gomock.Any()).
Return(cmd)
return rdb
},
},
{
// 解锁失败,因为网络问题
name: "network error",
mock: func() redis.Cmdable {
rdb := mocks.NewMockCmdable(ctrl)
cmd := redis.NewCmd(context.Background())
cmd.SetErr(errors.New("network error"))
rdb.EXPECT().Eval(gomock.Any(), luaUnlock, gomock.Any(), gomock.Any()).
Return(cmd)
return rdb
},
wantErr: errors.New("network error"),
},
{
// 解锁失败,锁已经过期,或者被人删了
// 或者是别人的锁
name: "lock not exist",
mock: func() redis.Cmdable {
rdb := mocks.NewMockCmdable(ctrl)
cmd := redis.NewCmd(context.Background())
cmd.SetVal(int64(0))
rdb.EXPECT().Eval(gomock.Any(), luaUnlock, gomock.Any(), gomock.Any()).
Return(cmd)
return rdb
},
wantErr: ErrLockNotHold,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
l := newLock(tc.mock(), "mock-key", "mock value", time.Minute)
err := l.Unlock(context.Background())
require.Equal(t, tc.wantErr, err)
})
}
}
func ExampleLock_Refresh() {
var lock *Lock
end := make(chan struct{}, 1)
go func() {
ticker := time.NewTicker(time.Second * 30)
for {
select {
case <-ticker.C:
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
err := lock.Refresh(ctx)
cancel()
// 错误处理
if err == context.DeadlineExceeded {
// 超时,按照道理来说,你应该立刻重试
// 超时之下可能续约成功了,也可能没成功
}
if err != nil {
// 其它错误,你要考虑这个错误能不能继续处理
// 如果不能处理,你怎么通知后续业务中断?
}
case <-end:
// 你的业务退出了
}
}
}()
// 后面是你的业务
fmt.Println("Finish")
// 你的业务完成了
end <- struct{}{}
// Output:
// Finish
}
func TestClient_SingleflightLock(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
rdb := mocks.NewMockCmdable(ctrl)
cmd := redis.NewCmd(context.Background())
cmd.SetVal("OK")
rdb.EXPECT().Eval(gomock.Any(), luaLock, gomock.Any(), gomock.Any()).
Return(cmd)
client := NewClient(rdb)
// TODO 并发测试
_, err := client.SingleflightLock(context.Background(),
"key1",
time.Minute,
&FixIntervalRetry{
Interval: time.Millisecond,
Max: 3,
}, time.Second)
require.NoError(t, err)
}
func TestLock_AutoRefresh(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testCases := []struct {
name string
unlockTiming time.Duration
lock func() *Lock
interval time.Duration
timeout time.Duration
wantErr error
}{
{
name: "auto refresh success",
interval: time.Millisecond * 100,
unlockTiming: time.Second,
timeout: time.Second * 2,
lock: func() *Lock {
rdb := mocks.NewMockCmdable(ctrl)
res := redis.NewCmd(context.Background(), nil)
res.SetVal(int64(1))
rdb.EXPECT().Eval(gomock.Any(), luaRefresh, []string{"auto-refreshed"}, []any{"123", float64(60)}).
AnyTimes().Return(res)
cmd := redis.NewCmd(context.Background())
cmd.SetVal(int64(1))
rdb.EXPECT().Eval(gomock.Any(), luaUnlock, gomock.Any(), gomock.Any()).
Return(cmd)
return &Lock{
client: rdb,
key: "auto-refreshed",
value: "123",
expiration: time.Minute,
unlock: make(chan struct{}, 1),
}
},
},
{
name: "auto refresh failed",
interval: time.Millisecond * 100,
unlockTiming: time.Second,
timeout: time.Second * 2,
lock: func() *Lock {
rdb := mocks.NewMockCmdable(ctrl)
res := redis.NewCmd(context.Background(), nil)
res.SetErr(errors.New("network error"))
rdb.EXPECT().Eval(gomock.Any(), luaRefresh, []string{"auto-refreshed"}, []any{"123", float64(60)}).
AnyTimes().Return(res)
cmd := redis.NewCmd(context.Background())
cmd.SetVal(int64(1))
rdb.EXPECT().Eval(gomock.Any(), luaUnlock, gomock.Any(), gomock.Any()).
Return(cmd)
return &Lock{
client: rdb,
key: "auto-refreshed",
value: "123",
expiration: time.Minute,
unlock: make(chan struct{}, 1),
}
},
wantErr: errors.New("network error"),
},
{
name: "auto refresh timeout",
interval: time.Millisecond * 100,
unlockTiming: time.Second * 1,
timeout: time.Second * 2,
lock: func() *Lock {
rdb := mocks.NewMockCmdable(ctrl)
first := redis.NewCmd(context.Background(), nil)
first.SetErr(context.DeadlineExceeded)
rdb.EXPECT().Eval(gomock.Any(), luaRefresh, []string{"auto-refreshed"}, []any{"123", float64(60)}).Return(first)
second := redis.NewCmd(context.Background(), nil)
second.SetVal(int64(1))
rdb.EXPECT().Eval(gomock.Any(), luaRefresh, []string{"auto-refreshed"}, []any{"123", float64(60)}).AnyTimes().Return(first)
cmd := redis.NewCmd(context.Background())
cmd.SetVal(int64(1))
rdb.EXPECT().Eval(gomock.Any(), luaUnlock, gomock.Any(), gomock.Any()).
Return(cmd)
return &Lock{
client: rdb,
key: "auto-refreshed",
value: "123",
expiration: time.Minute,
unlock: make(chan struct{}, 1),
}
},
wantErr: nil,
},
}
for _, tt := range testCases {
tc := tt
t.Run(tc.name, func(t *testing.T) {
lock := tc.lock()
go func() {
time.Sleep(tc.unlockTiming)
err := lock.Unlock(context.Background())
require.NoError(t, err)
}()
err := lock.AutoRefresh(tc.interval, tc.timeout)
assert.Equal(t, tc.wantErr, err)
})
}
}
func TestLock_Refresh(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testCases := []struct {
name string
lock func() *Lock
wantErr error
}{
{
// 续约成功
name: "refreshed",
lock: func() *Lock {
rdb := mocks.NewMockCmdable(ctrl)
res := redis.NewCmd(context.Background(), nil)
res.SetVal(int64(1))
rdb.EXPECT().Eval(gomock.Any(), luaRefresh, []string{"refreshed"}, []any{"123", float64(60)}).Return(res)
return &Lock{
client: rdb,
expiration: time.Minute,
value: "123",
key: "refreshed",
}
},
},
{
// 刷新失败
name: "lock not hold",
lock: func() *Lock {
rdb := mocks.NewMockCmdable(ctrl)
res := redis.NewCmd(context.Background(), nil)
res.SetErr(redis.Nil)
rdb.EXPECT().Eval(gomock.Any(), luaRefresh, []string{"refreshed"}, []any{"123", float64(60)}).Return(res)
return &Lock{
client: rdb,
expiration: time.Minute,
value: "123",
key: "refreshed",
}
},
wantErr: redis.Nil,
},
{
// 未持有锁
name: "lock not hold",
lock: func() *Lock {
rdb := mocks.NewMockCmdable(ctrl)
res := redis.NewCmd(context.Background(), nil)
res.SetVal(int64(0))
rdb.EXPECT().Eval(gomock.Any(), luaRefresh, []string{"refreshed"}, []any{"123", float64(60)}).Return(res)
return &Lock{
client: rdb,
expiration: time.Minute,
value: "123",
key: "refreshed",
}
},
wantErr: ErrLockNotHold,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.lock().Refresh(context.Background())
assert.Equal(t, tc.wantErr, err)
})
}
}