forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
261 lines (233 loc) · 5.46 KB
/
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
// +build int
package main
import (
"bytes"
"fmt"
"reflect"
"sort"
"strings"
"sync"
"testing"
"github.com/alicebob/miniredis"
"github.com/gomodule/redigo/redis"
)
type command struct {
cmd string // 'GET', 'SET', &c.
args []interface{}
error bool // Whether the command should return an error or not.
sort bool // Sort real redis's result. Used for 'keys'.
loosely bool // Don't compare values, only structure. (for random things)
errorSub string // Both errors need this substring
}
func succ(cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: false,
}
}
func succSorted(cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: false,
sort: true,
}
}
func succLoosely(cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: false,
loosely: true,
}
}
func fail(cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: true,
}
}
// expect an error, with `sub` in both errors
func failWith(sub string, cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: true,
errorSub: sub,
}
}
// only compare the error state, not the actual error message
func failLoosely(cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: true,
loosely: true,
}
}
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Fatalf("unexpected error: %s", err.Error())
}
}
func testCommands(t *testing.T, commands ...command) {
t.Helper()
sMini, err := miniredis.Run()
ok(t, err)
defer sMini.Close()
sReal, sRealAddr := Redis()
defer sReal.Close()
runCommands(t, sRealAddr, sMini.Addr(), commands)
}
// like testCommands, but multiple connections
func testMultiCommands(t *testing.T, cs ...func(chan<- command, *miniredis.Miniredis)) {
t.Helper()
sMini, err := miniredis.Run()
ok(t, err)
defer sMini.Close()
sReal, realAddr := Redis()
defer sReal.Close()
var wg sync.WaitGroup
for _, c := range cs {
// one connections per cs
cMini, err := redis.Dial("tcp", sMini.Addr())
ok(t, err)
cReal, err := redis.Dial("tcp", realAddr)
ok(t, err)
wg.Add(1)
go func(c func(chan<- command, *miniredis.Miniredis)) {
defer wg.Done()
gen := make(chan command)
wg.Add(1)
go func() {
defer wg.Done()
c(gen, sMini)
close(gen)
}()
for cm := range gen {
runCommand(t, cMini, cReal, cm)
}
}(c)
}
wg.Wait()
}
func testAuthCommands(t *testing.T, passwd string, commands ...command) {
sMini, err := miniredis.Run()
ok(t, err)
defer sMini.Close()
sMini.RequireAuth(passwd)
sReal, sRealAddr := RedisAuth(passwd)
defer sReal.Close()
runCommands(t, sRealAddr, sMini.Addr(), commands)
}
func runCommands(t *testing.T, realAddr, miniAddr string, commands []command) {
t.Helper()
cMini, err := redis.Dial("tcp", miniAddr)
ok(t, err)
cReal, err := redis.Dial("tcp", realAddr)
ok(t, err)
for _, c := range commands {
runCommand(t, cMini, cReal, c)
}
}
func runCommand(t *testing.T, cMini, cReal redis.Conn, p command) {
t.Helper()
vReal, errReal := cReal.Do(p.cmd, p.args...)
vMini, errMini := cMini.Do(p.cmd, p.args...)
if p.error {
if errReal == nil {
t.Errorf("got no error from realredis. case: %#v", p)
return
}
if errMini == nil {
t.Errorf("got no error from miniredis. case: %#v real error: %s", p, errReal)
return
}
if p.loosely {
return
}
} else {
if errReal != nil {
t.Errorf("got an error from realredis: %v. case: %#v", errReal, p)
return
}
if errMini != nil {
t.Errorf("got an error from miniredis: %v. case: %#v", errMini, p)
return
}
}
if p.errorSub != "" {
if have, want := errReal.Error(), p.errorSub; !strings.Contains(have, want) {
t.Errorf("realredis error error. expected: %q in %q case: %#v", want, have, p)
}
if have, want := errMini.Error(), p.errorSub; !strings.Contains(have, want) {
t.Errorf("miniredis error error. expected: %q in %q case: %#v", want, have, p)
}
return
}
if !reflect.DeepEqual(errReal, errMini) {
t.Errorf("error error. expected: %#v got: %#v case: %#v", vReal, vMini, p)
return
}
// Sort the strings.
if p.sort {
sort.Sort(BytesList(vReal.([]interface{})))
sort.Sort(BytesList(vMini.([]interface{})))
}
if p.loosely {
if !looselyEqual(vReal, vMini) {
t.Errorf("value error. expected: %#v got: %#v case: %#v", vReal, vMini, p)
return
}
} else {
if !reflect.DeepEqual(vReal, vMini) {
t.Errorf("value error. expected: %#v got: %#v case: %#v", vReal, vMini, p)
return
}
}
}
// BytesList implements the sort interface for things we know is a list of
// bytes.
type BytesList []interface{}
func (b BytesList) Len() int {
return len(b)
}
func (b BytesList) Less(i, j int) bool {
return bytes.Compare(b[i].([]byte), b[j].([]byte)) < 0
}
func (b BytesList) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func looselyEqual(a, b interface{}) bool {
switch av := a.(type) {
case string:
_, ok := b.(string)
return ok
case []byte:
_, ok := b.([]byte)
return ok
case error:
_, ok := b.(error)
return ok
case []interface{}:
bv, ok := b.([]interface{})
if !ok {
return false
}
if len(av) != len(bv) {
return false
}
for i, v := range av {
if !looselyEqual(v, bv[i]) {
return false
}
}
return true
default:
panic(fmt.Sprintf("unhandled case, got a %#v", a))
}
}