-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwlock_test.go
85 lines (82 loc) · 2.22 KB
/
rwlock_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
package redislock
import (
"context"
"errors"
"testing"
"time"
"github.com/WeiJiadong/redislock/internal"
"github.com/magiconair/properties/assert"
)
func TestNewRWMutex(t *testing.T) {
ctx := context.TODO()
cli := getClient(ctx)
type args struct {
Key string
Val string
Lease time.Duration
}
type wants struct {
ttl time.Duration
cnt int64
err error
}
tests := []struct {
name string
args args
wants wants
}{
{
name: "普通case:",
args: args{
Key: "Key1",
Val: "Val1",
Lease: 2 * time.Second,
},
wants: wants{
ttl: 2 * time.Second,
cnt: int64(0),
err: nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
locker := NewRWMutex(tt.args.Key, tt.args.Val, tt.args.Lease)
// 先清理,防止其他类别Key存在导致出错
cli.Del(ctx, tt.args.Key)
// 先加写锁
assert.Equal(t, locker.WLock(ctx, cli), tt.wants.err)
// check 是不是租约的时间
ttl, err := cli.TTL(ctx, tt.args.Key).Result()
assert.Equal(t, ttl, tt.wants.ttl)
assert.Equal(t, err, tt.wants.err)
// check 自动解锁是否成功
time.Sleep(ttl + ttl/2)
cnt, err := cli.Exists(ctx, tt.args.Key).Result()
assert.Equal(t, cnt, tt.wants.cnt)
assert.Equal(t, err, tt.wants.err)
// 加读锁
// 先清理,防止其他类别Key存在导致出错
assert.Equal(t, locker.RLock(ctx, cli), tt.wants.err)
// 加写锁
assert.Equal(t, locker.WLock(ctx, cli), tt.wants.err)
// check加锁是否成功,以及是不是租约时间
ttl, err = cli.TTL(ctx, tt.args.Key).Result()
assert.Equal(t, ttl, tt.wants.ttl)
assert.Equal(t, err, tt.wants.err)
// 释放写锁
assert.Equal(t, locker.WUnlock(ctx, cli), tt.wants.err)
// 释放读锁
assert.Equal(t, locker.RUnlock(ctx, cli), tt.wants.err)
// check 解锁是否成功
cnt, err = cli.Exists(ctx, tt.args.Key).Result()
assert.Equal(t, cnt, tt.wants.cnt)
assert.Equal(t, err, tt.wants.err)
// 重复加写锁
assert.Equal(t, locker.WLock(ctx, cli), tt.wants.err)
assert.Equal(t, locker.WLock(ctx, cli), errors.New(internal.ErrWLockConflict))
// 加读锁
assert.Equal(t, locker.RLock(ctx, cli), errors.New(internal.ErrHadWLock))
})
}
}