-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_script_test.go
173 lines (133 loc) · 4.33 KB
/
cmd_script_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
package redisson
import (
"context"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
const testScript = `return ARGV[1]`
func testScriptExists(ctx context.Context, c Cmdable) []string {
scriptExists := c.ScriptExists(ctx, "1")
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 1)
So(scriptExists.Val()[0], ShouldBeFalse)
s := c.CreateScript(testScript)
l := s.Load(ctx)
So(l.Err(), ShouldBeNil)
So(l.Val(), ShouldNotBeEmpty)
scriptExists = c.ScriptExists(ctx, l.Val(), "1")
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 2)
So(scriptExists.Val()[0], ShouldBeTrue)
So(scriptExists.Val()[1], ShouldBeFalse)
sd := c.ScriptFlush(ctx)
So(sd.Err(), ShouldBeNil)
So(sd.Val(), ShouldEqual, OK)
scriptExists = c.ScriptExists(ctx, l.Val())
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 1)
So(scriptExists.Val()[0], ShouldBeFalse)
return nil
}
func testScriptFlush(ctx context.Context, c Cmdable) []string {
s := c.CreateScript(testScript)
l := s.Load(ctx)
So(l.Err(), ShouldBeNil)
So(l.Val(), ShouldNotBeEmpty)
scriptExists := c.ScriptExists(ctx, l.Val())
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 1)
So(scriptExists.Val()[0], ShouldBeTrue)
sd := c.ScriptFlush(ctx)
So(sd.Err(), ShouldBeNil)
So(sd.Val(), ShouldEqual, OK)
scriptExists = c.ScriptExists(ctx, l.Val())
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 1)
So(scriptExists.Val()[0], ShouldBeFalse)
return nil
}
func testScriptLoad(ctx context.Context, c Cmdable) []string {
l := c.ScriptLoad(ctx, testScript)
So(l.Err(), ShouldBeNil)
So(l.Val(), ShouldNotBeEmpty)
scriptExists := c.ScriptExists(ctx, l.Val())
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 1)
So(scriptExists.Val()[0], ShouldBeTrue)
sd := c.ScriptFlush(ctx)
So(sd.Err(), ShouldBeNil)
So(sd.Val(), ShouldEqual, OK)
scriptExists = c.ScriptExists(ctx, l.Val())
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 1)
So(scriptExists.Val()[0], ShouldBeFalse)
return nil
}
func testScriptKill(ctx context.Context, c Cmdable) []string {
l := c.ScriptLoad(ctx, testScript)
So(l.Err(), ShouldBeNil)
So(l.Val(), ShouldNotBeEmpty)
sk := c.ScriptKill(ctx)
So(sk.Err(), ShouldNotBeNil)
So(sk.Err().Error(), ShouldEqual, "NOTBUSY No scripts in execution right now.")
sd := c.ScriptFlush(ctx)
So(sd.Err(), ShouldBeNil)
So(sd.Val(), ShouldEqual, OK)
scriptExists := c.ScriptExists(ctx, l.Val())
So(scriptExists.Err(), ShouldBeNil)
So(len(scriptExists.Val()), ShouldEqual, 1)
So(scriptExists.Val()[0], ShouldBeFalse)
return nil
}
func testScriptEval(ctx context.Context, c Cmdable) []string {
var key, value = "mykey", "hello"
cmd := c.Eval(ctx, testScript, []string{key}, value)
So(cmd.Err(), ShouldBeNil)
So(cmd.Val(), ShouldEqual, value)
sd := c.ScriptFlush(ctx)
So(sd.Err(), ShouldBeNil)
So(sd.Val(), ShouldEqual, OK)
return nil
}
func testScriptEvalSha(ctx context.Context, c Cmdable) []string {
var key, value = "mykey", "hello"
cmd := c.EvalSha(ctx, testScript, []string{key}, value)
So(cmd.Err(), ShouldNotBeNil)
So(cmd.Err().Error(), ShouldEqual, "NOSCRIPT No matching script. Please use EVAL.")
l := c.ScriptLoad(ctx, testScript)
So(l.Err(), ShouldBeNil)
So(l.Val(), ShouldNotBeEmpty)
cmd = c.EvalSha(ctx, l.Val(), []string{key}, value)
So(cmd.Err(), ShouldBeNil)
So(cmd.Val(), ShouldEqual, value)
sd := c.ScriptFlush(ctx)
So(sd.Err(), ShouldBeNil)
So(sd.Val(), ShouldEqual, OK)
return nil
}
func testScriptRun(ctx context.Context, c Cmdable) []string {
var value = "hello"
s := c.CreateScript(testScript)
l := s.Load(ctx)
So(l.Err(), ShouldBeNil)
So(l.Val(), ShouldNotBeEmpty)
cmd := s.Run(ctx, nil, value)
So(cmd.Err(), ShouldBeNil)
So(cmd.Val(), ShouldEqual, value)
sd := c.ScriptFlush(ctx)
So(sd.Err(), ShouldBeNil)
So(sd.Val(), ShouldEqual, OK)
return nil
}
func scriptTestUnits() []TestUnit {
return []TestUnit{
{CommandScriptExists, testScriptExists},
{CommandScriptFlush, testScriptFlush},
{CommandScriptLoad, testScriptLoad},
{CommandScriptKill, testScriptKill},
{CommandEval, testScriptEval},
{CommandEvalSha, testScriptEvalSha},
{CommandEvalSha, testScriptRun},
}
}
func TestClient_Script(t *testing.T) { doTestUnits(t, scriptTestUnits) }