-
Notifications
You must be signed in to change notification settings - Fork 7
/
lock_redis_local_test.go
621 lines (520 loc) · 14.9 KB
/
lock_redis_local_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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package go_redislock
import (
"context"
"fmt"
"github.com/go-redis/redismock/v9"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
"log"
"os"
"sync"
"testing"
"time"
)
// 加锁成功,并执行业务代码
func TestLockSuccess(t *testing.T) {
ctx := context.Background()
key := "test_key_TestLockSuccess"
token := "some_token"
// 创建 redismock 客户端
db, mock := redismock.NewClientMock()
// 创建 RedisLock 实例
lock := New(ctx, db, key, WithToken(token))
// 设置模拟锁获取成功的行为
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
err := lock.Lock()
if err != nil {
t.Errorf("Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
// 在这里执行锁定期间的任务,确保任务可以在锁定期间正常执行
// ...
log.Println("Execute Business Code: start")
fmt.Println("任务执行")
log.Println("Execute Business Code: end")
}
// 加锁失败的情况
// 模拟思路如下:
// 线程1:同一个key加锁成功,执行任务5秒
// 线程2:使用此key继续加锁,期望加锁失败
func TestLockFail(t *testing.T) {
key := "test_key_TestLockFail"
token := "some_token"
ctx := context.Background()
db, mock := redismock.NewClientMock()
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
var wg sync.WaitGroup
wg.Add(2)
// 线程一:期望加锁成功
go func() {
defer wg.Done()
log.Println("线程1: 启动")
lock := New(ctx, db, key, WithToken(token))
err := lock.Lock()
if err == nil {
log.Println("线程1: 加锁成功")
} else {
t.Errorf("线程1: Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
time.Sleep(time.Second * 5)
log.Println("线程1: 任务执行")
}()
// 线程二:期望加锁失败
go func() {
defer wg.Done()
log.Println("线程2: 启动")
time.Sleep(time.Second * 2)
lock := New(ctx, db, key, WithToken(token+"task-2"))
err := lock.Lock()
if err == nil {
log.Println("线程2: 加锁成功")
t.Errorf("线程2: Lock() Lock should have failed")
} else {
log.Println("线程2: 加锁失败")
return
}
defer lock.UnLock()
time.Sleep(time.Second * 5)
log.Println("线程2: 任务执行")
}()
wg.Wait()
}
// 解锁失败的情况
// 逻辑一:使用 A Token 加锁
// 逻辑二:使用 B Token 为逻辑一加的锁解锁,期望:解锁失败
func TestUnlockFail(t *testing.T) {
key := "test_key_TestUnlockFail"
token := "some_token"
ctx := context.Background()
db, mock := redismock.NewClientMock()
lock := New(ctx, db, key, WithToken(token))
// 加锁逻辑
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
err := lock.Lock()
if err != nil {
t.Errorf("Lock() returned unexpected error: %v", err)
return
}
// 解锁逻辑
mock.ExpectEval(reentrantUnLockScript, []string{key}, token+"test-2").SetVal(nil) // 模拟解锁失败
err = lock.UnLock()
if err == nil {
t.Error("UnLock() expected to return an error, but got nil")
return
}
}
// 自旋锁成功的情况
// 线程一:自旋锁加锁成功,任务执行,最终解锁
// 线程二:自旋锁进行加锁,最终获得线程一交出的锁,执行任务
func TestSpinLockSuccess(t *testing.T) {
ctx := context.Background()
db, mock := redismock.NewClientMock()
key := "test_key_TestSpinLockSuccess"
token := "some_token"
token2 := "some_token2"
spinTimeout := time.Duration(5) * time.Second
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
mock.ExpectEval(reentrantLockScript, []string{key}, token2, lockTime.Milliseconds()).SetVal("OK")
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
lock := New(ctx, db, key, WithToken(token))
err := lock.SpinLock(spinTimeout)
if err != nil {
t.Errorf("线程1:SpinLock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
log.Println("线程1:自旋锁加锁成功")
log.Println("线程1:任务执行结束")
}()
go func() {
defer wg.Done()
time.Sleep(time.Second * 1)
lock := New(ctx, db, key, WithToken(token2))
err := lock.SpinLock(spinTimeout)
if err != nil {
t.Errorf("线程2:SpinLock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
log.Println("线程2:自旋锁加锁成功")
time.Sleep(time.Second * 2)
log.Println("线程2:任务执行结束")
}()
wg.Wait()
}
// 自旋锁超时的情况
// 线程一:自旋锁加锁且执行任务 5 秒钟
// 线程二:自旋锁设置 3 秒的超时时间获取锁,期望:获取锁失败
func TestSpinLockTimeout(t *testing.T) {
ctx := context.Background()
db, mock := redismock.NewClientMock()
key := "test_key_TestSpinLockTimeout"
token := "some_token"
token2 := "some_token2"
spinTimeout := time.Duration(5) * time.Second
spinTimeout2 := time.Duration(3) * time.Second
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
mock.ExpectEval(reentrantLockScript, []string{key}, token2, lockTime.Milliseconds()).SetVal("nil")
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
lock := New(ctx, db, key, WithToken(token))
err := lock.SpinLock(spinTimeout)
if err != nil {
t.Errorf("线程1:SpinLock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
log.Println("线程1:自旋锁加锁成功")
time.Sleep(time.Second * 5)
log.Println("线程1:任务执行结束")
}()
go func() {
defer wg.Done()
time.Sleep(time.Second * 1)
lock := New(ctx, db, key, WithToken(token2))
err := lock.SpinLock(spinTimeout2)
if err != nil {
log.Println("线程2:获取锁成功")
return
}
defer lock.UnLock()
t.Errorf("线程2:SpinLock() Timeout error")
return
}()
wg.Wait()
}
// 锁手动续期成功的情况
func TestRenewSuccess(t *testing.T) {
ctx := context.Background()
db, mock := redismock.NewClientMock()
key := "test_key_TestRenewSuccess"
token := "some_token"
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
mock.ExpectEval(reentrantRenewScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
// 设置模拟锁续期成功的行为
mock.ExpectExpire(key, lockTime).SetVal(true)
lock := New(ctx, db, key, WithToken(token))
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(time.Second * 3)
err := lock.Renew()
if err != nil {
t.Errorf("Renew() returned unexpected error: %v", err)
return
}
log.Println("锁续期成功")
}()
err := lock.Lock()
if err != nil {
t.Errorf("Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
// 模拟任务执行超过默认时间
log.Println("任务执行:开始")
time.Sleep(time.Second * 8)
log.Println("任务执行:结束")
wg.Wait()
}
// 锁手动续期失败的情况
func TestRenewFail(t *testing.T) {
ctx := context.Background()
db, mock := redismock.NewClientMock()
key := "test_key_TestRenewFail"
token := "some_token"
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
// 设置模拟锁续期成功的行为
mock.ExpectExpire(key, lockTime).SetVal(false)
lock := New(ctx, db, key, WithToken(token))
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(time.Second * 3)
err := lock.Renew()
if err == nil {
t.Errorf("Renew() returned unexpected error: %v", err)
return
}
}()
err := lock.Lock()
if err != nil {
t.Errorf("Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
// 模拟任务执行超过默认时间
log.Println("任务执行:开始")
time.Sleep(time.Second * 8)
log.Println("任务执行:结束")
wg.Wait()
}
// 测试设置默认超时时间
// 线程1:设置默认超时时间为10
// 线程2:第7秒的时候抢夺抢夺线程1的锁,预期:抢夺失败
func TestWithTimeout(t *testing.T) {
ctx := context.Background()
db, mock := redismock.NewClientMock()
key := "test_key_TestWithTimeout"
token := "some_token"
timeout := time.Duration(10) * time.Second
mock.ExpectEval(reentrantLockScript, []string{key}, token, timeout.Milliseconds()).SetVal("OK")
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
lock := New(ctx, db, key, WithToken(token), WithTimeout(timeout))
err := lock.Lock()
if err != nil {
t.Errorf("线程1:Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
time.Sleep(time.Second * timeout)
}()
go func() {
defer wg.Done()
time.Sleep(time.Second * 7)
lock := New(ctx, db, key, WithToken(token), WithTimeout(timeout))
err := lock.Lock()
if err == nil {
t.Errorf("线程2:Lock() expected lock failure")
return
}
}()
wg.Wait()
}
// 测试自动创建key和token
func TestLockCreateAutoKeyToken(t *testing.T) {
ctx := context.Background()
db, _ := redismock.NewClientMock()
key := "test_key_TestLockCreateAutoKeyToken"
New(ctx, db, key)
}
// 测试锁自动续期
func TestAutoRenew(t *testing.T) {
ctx := context.Background()
db, mock := redismock.NewClientMock()
key := "test_key_TestAutoRenew"
token := "some_token"
mock.ExpectEval(reentrantLockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
mock.ExpectEval(reentrantRenewScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
mock.ExpectExpire(key, lockTime).SetVal(true)
lock := New(ctx, db, key, WithToken(token), WithAutoRenew())
err := lock.Lock()
if err != nil {
t.Errorf("Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
time.Sleep(time.Second * 6)
}
// 测试自动续期的 Context 操作取消
//func TestAutoRenewContextCancellation(t *testing.T) {
// // 创建可取消的上下文
// ctx, cancel := context.WithCancel(context.Background())
//
// db, mock := redismock.NewClientMock()
// key := "test_key"
// token := "some_token"
//
// // 设置模拟锁获取成功的行为
// mock.ExpectEval(lockScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
// // 设置模拟锁续期成功的行为
// mock.ExpectExpire(key, lockTime).SetVal(true)
//
// // 创建 RedisLock 实例,开启自动续期
// lock := New(ctx, db, key, WithToken(token), WithAutoRenew())
//
// var wg sync.WaitGroup
// wg.Add(1)
//
// // 模拟锁自动续期的 goroutine
// go func() {
// defer wg.Done()
// // 模拟锁续期成功
// mock.ExpectEval(renewScript, []string{key}, token, lockTime.Milliseconds()).SetVal("OK")
// // 等待一段时间,模拟自动续期的过程
// time.Sleep(time.Second * 3)
// // 取消上下文,模拟上下文的取消
// cancel()
// // 等待 goroutine 退出
// time.Sleep(time.Second * 2)
// }()
//
// err := lock.Lock()
// if err != nil {
// t.Errorf("Lock() 返回意外的错误: %v", err)
// return
// }
// defer lock.UnLock()
//
// // 等待一段时间,模拟锁自动续期的过程
// time.Sleep(time.Second * 8)
//
// wg.Wait()
//}
// Redis服务器测试
// 下面的代码将借助 redis 服务器进行测试,可以更加方便的测试服务中的问题
// 你可以实用下面的命令启动一个redis容器进行测试
// docker run -d -p 6379:6379 --name go_redis_lock redis
// 注意:该服务在 GITHUB ACTIONS 并不会被测试
func getRedisClient() *redis.Client {
if os.Getenv("GITHUB_ACTIONS") == "true" {
return nil
}
rdb := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:63790",
})
// 尝试执行PING命令
// 如果执行PING命令出错,则表明连接失败
ctx := context.TODO()
_, err := rdb.Ping(ctx).Result()
if err != nil {
log.Fatalf("Failed to ping Redis: %v", err)
}
return rdb
}
func TestSevLock(t *testing.T) {
redisClient := getRedisClient()
if redisClient == nil {
log.Println("Github actions skip this test")
return
}
ctx := context.TODO()
key := "test_key_TestSevLock"
lock := New(ctx, redisClient, key)
defer lock.UnLock()
err := lock.Lock()
if err != nil {
t.Errorf("Lock() returned unexpected error: %v", err)
return
}
}
// 测试加锁成功
func TestSevLockSuccess(t *testing.T) {
redisClient := getRedisClient()
if redisClient == nil {
log.Println("Github actions skip this test")
return
}
ctx := context.Background()
key := "test_key_TestSevLockSuccess"
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
lock := New(ctx, redisClient, key)
err := lock.Lock()
if err != nil {
t.Errorf("线程一:Lock() returned unexpected error: %v", err)
return
}
time.Sleep(time.Second * 3)
defer lock.UnLock()
log.Println("线程一:执行结束")
}()
go func() {
defer wg.Done()
time.Sleep(time.Second * 1)
log.Println("线程二:开始抢夺锁资源")
lock := New(ctx, redisClient, key)
times, _ := redisClient.TTL(ctx, key).Result()
log.Println("线程二:ttl 时间:", times.Milliseconds())
err := lock.Lock()
if err == nil {
defer lock.UnLock()
t.Errorf("线程二:Lock() returned unexpected error: %v", err)
return
}
}()
wg.Wait()
}
// 测试可重入锁计数器
func TestSevLockCounter(t *testing.T) {
redisClient := getRedisClient()
if redisClient == nil {
log.Println("Github actions skip this test")
return
}
ctx := context.Background()
key := "test_key_TestSevLockCounter"
lock := New(ctx, redisClient, key)
err := lock.Lock()
if err != nil {
t.Errorf("任务1:Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
err = lock.Lock()
if err != nil {
t.Errorf("任务2:Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
}
// 测试锁自动续期
func TestSevAutoRenewSuccess(t *testing.T) {
redisClient := getRedisClient()
if redisClient == nil {
log.Println("Github actions skip this test")
return
}
ctx := context.Background()
key := "test_key_TestSevAutoRenewSuccess"
token := "some_token"
token2 := "some_token2"
var wg sync.WaitGroup
wg.Add(2)
// 线程1
go func() {
defer wg.Done()
lock := New(ctx, redisClient, key, WithToken(token), WithAutoRenew())
err := lock.Lock()
if err != nil {
t.Errorf("Lock() returned unexpected error: %v", err)
return
}
defer lock.UnLock()
log.Println("线程1:自旋锁加锁成功")
time.Sleep(time.Second * 10)
log.Println("线程1:任务执行结束")
}()
// 线程2
go func() {
defer wg.Done()
time.Sleep(time.Second * 7)
log.Println("线程2:开始抢夺锁资源")
lock := New(ctx, redisClient, key, WithToken(token2), WithAutoRenew())
err := lock.Lock()
if err == nil {
defer lock.UnLock()
t.Errorf("线程2 Lock() not expectation success")
return
}
}()
wg.Wait()
}
func TestAutoRenew5(t *testing.T) {
redisClient := getRedisClient()
if redisClient == nil {
log.Println("Github actions skip this test")
return
}
lock := New(context.TODO(), redisClient, "key", WithToken("token"),
WithAutoRenew())
err := lock.Lock()
require.NoError(t, err)
defer lock.UnLock()
time.Sleep(time.Second * 20)
}