-
Notifications
You must be signed in to change notification settings - Fork 0
/
cas_test.go
66 lines (63 loc) · 1.32 KB
/
cas_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
package redislock
import (
"context"
"errors"
"testing"
"github.com/WeiJiadong/redislock/internal"
"gopkg.in/go-playground/assert.v1"
)
func TestNewCas(t *testing.T) {
ctx := context.TODO()
cli := getClient(ctx)
type args struct {
key string
val string
newVal *internal.CasVal
oldVal *internal.CasVal
}
type wants struct {
cnt int64
err error
val *internal.CasVal
}
tests := []struct {
name string
args args
wants wants
}{
{
name: "普通case:",
args: args{
key: "Key1",
val: "Val1",
newVal: &internal.CasVal{
Val: "new",
},
oldVal: &internal.CasVal{
Val: "old",
},
},
wants: wants{
cnt: int64(0),
err: nil,
val: &internal.CasVal{
Val: "new",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// 先清理,防止其他类别Key存在导致出错
cli.Del(ctx, tt.args.key)
locker := NewCas(tt.args.key, tt.args.val)
// 先执行cas
assert.Equal(t, locker.Set(ctx, cli, tt.args.key, tt.args.oldVal, tt.args.newVal), tt.wants.err)
assert.Equal(t, locker.Set(ctx, cli, tt.args.key, tt.args.oldVal, tt.args.newVal),
errors.New(internal.ErrCasConflict))
val, err := locker.Get(ctx, cli, tt.args.key)
assert.Equal(t, val, tt.args.newVal)
assert.Equal(t, err, tt.wants.err)
})
}
}