-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts_test.go
203 lines (166 loc) · 5.34 KB
/
scripts_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
package cache
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// TestClient_RegisterScripts tests the method RegisterScripts()
func TestClient_RegisterScripts(t *testing.T) {
t.Run("valid client - run register", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
client, err := Connect(
context.Background(),
testLocalConnectionURL,
testMaxActiveConnections,
testMaxIdleConnections,
testMaxConnLifetime,
testIdleTimeout,
false,
false,
)
assert.NoError(t, err)
assert.NotNil(t, client)
assert.NotNil(t, client.Pool)
assert.Equal(t, "", client.DependencyScriptSha)
assert.Equal(t, 0, len(client.ScriptsLoaded))
defer client.Close()
// Run register
err = client.RegisterScripts(context.Background())
assert.NoError(t, err)
assert.Equal(t, testKillDependencyHash, client.DependencyScriptSha)
assert.Equal(t, 1, len(client.ScriptsLoaded))
})
t.Run("valid client - run register 2 times", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
client, err := Connect(
context.Background(),
testLocalConnectionURL,
testMaxActiveConnections,
testMaxIdleConnections,
testMaxConnLifetime,
testIdleTimeout,
false,
false,
)
assert.NoError(t, err)
assert.NotNil(t, client)
assert.NotNil(t, client.Pool)
assert.Equal(t, "", client.DependencyScriptSha)
assert.Equal(t, 0, len(client.ScriptsLoaded))
defer client.Close()
// Run register
err = client.RegisterScripts(context.Background())
assert.NoError(t, err)
assert.Equal(t, testKillDependencyHash, client.DependencyScriptSha)
assert.Equal(t, 1, len(client.ScriptsLoaded))
// Run again (should skip)
err = client.RegisterScripts(context.Background())
assert.NoError(t, err)
assert.Equal(t, testKillDependencyHash, client.DependencyScriptSha)
assert.Equal(t, 1, len(client.ScriptsLoaded))
})
}
// ExampleClient_RegisterScripts is an example of the method RegisterScripts()
func ExampleClient_RegisterScripts() {
// Load a mocked redis for testing/examples
client, conn := loadMockRedis()
// Close connections at end of request
defer client.CloseAll(conn)
// Register known scripts
_ = client.RegisterScripts(context.Background())
fmt.Printf("scripts registered")
// Output:scripts registered
}
// TestRegisterScript is testing the method RegisterScript()
func TestRegisterScript(t *testing.T) {
t.Run("register script command using mocked redis", func(t *testing.T) {
t.Parallel()
// Load redis
client, conn := loadMockRedis()
assert.NotNil(t, client)
defer client.CloseAll(conn)
var tests = []struct {
testCase string
script string
expectedSha string
}{
{"valid script", killByDependencyLua, testKillDependencyHash},
{"another script", `return redis.call("get", KEYS[1])`, "4e6d8fc8bb01276962cce5371fa795a7763657ae"},
}
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
conn.Clear()
// The main command to test
setCmd := conn.Command(ScriptCommand, LoadCommand, test.script).Expect(test.expectedSha)
val, err := RegisterScriptRaw(client, conn, test.script)
assert.NoError(t, err)
assert.Equal(t, true, setCmd.Called)
assert.Equal(t, test.expectedSha, val)
})
}
})
t.Run("register script using real redis", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
// Load redis
client, conn, err := loadRealRedis()
assert.NotNil(t, client)
assert.NoError(t, err)
defer client.CloseAll(conn)
// Start with a fresh db
err = clearRealRedis(conn)
assert.NoError(t, err)
// Fire the command
var sha string
sha, err = RegisterScript(context.Background(), client, killByDependencyLua)
assert.NoError(t, err)
assert.Equal(t, testKillDependencyHash, sha)
// Another script
sha, err = RegisterScript(context.Background(), client, `return redis.call("get", KEYS[1])`)
assert.NoError(t, err)
assert.Equal(t, "a5260dd66ce02462c5b5231c727b3f7772c0bcc5", sha)
// Another script
sha, err = RegisterScript(context.Background(), client, lockScript)
assert.NoError(t, err)
assert.Equal(t, "7168a6204ea41fc8e23d1c42b1ebdf1ec5abff4f", sha)
// Another script
sha, err = RegisterScript(context.Background(), client, releaseLockScript)
assert.NoError(t, err)
assert.Equal(t, "3271ffa78c3ca6743c9dc476ff6cae55a9cd3cb4", sha)
})
t.Run("register script error", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
// Load redis
client, conn, err := loadRealRedis()
assert.NotNil(t, client)
assert.NoError(t, err)
defer client.CloseAll(conn)
// Start with a fresh db
err = clearRealRedis(conn)
assert.NoError(t, err)
// Fire the command
var sha string
sha, err = RegisterScript(context.Background(), client, "invalid script")
assert.Error(t, err)
assert.Equal(t, "", sha)
})
}
// ExampleRegisterScript is an example of the method RegisterScript()
func ExampleRegisterScript() {
// Load a mocked redis for testing/examples
client, _ := loadMockRedis()
// Close connections at end of request
defer client.Close()
// Register known scripts
_, _ = RegisterScript(context.Background(), client, killByDependencyLua)
fmt.Printf("registered: %s", testKillDependencyHash)
// Output:registered: a648f768f57e73e2497ccaa113d5ad9e731c5cd8
}