forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
397 lines (357 loc) · 8.23 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// +build int
package main
import (
"bytes"
"context"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"testing"
"github.com/alicebob/miniredis/v2"
"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
receiveOnly bool // no command, only receive. For pubsub messages.
roundFloats int // if > 0 round floats to this many places before DeepEqual
}
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,
}
}
// round all floats to 3 decimal places
func succRound3(cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: false,
roundFloats: 3,
}
}
func fail(cmd string, args ...interface{}) command {
return command{
cmd: cmd,
args: args,
error: true,
}
}
// expect an error, with both errors containing `sub`
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,
}
}
// don't send a message, only read one. For pubsub messages.
func receive() command {
return command{
receiveOnly: 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 connection 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()
}
// like testCommands, but multiple connections
func testClients2(t *testing.T, f func(c1, c2 chan<- command)) {
t.Helper()
sMini, err := miniredis.Run()
ok(t, err)
defer sMini.Close()
sReal, realAddr := Redis()
defer sReal.Close()
type aChan struct {
c chan command
cMini, cReal redis.Conn
}
chans := [2]aChan{}
for i := range chans {
gen := make(chan command)
cMini, err := redis.Dial("tcp", sMini.Addr())
ok(t, err)
cReal, err := redis.Dial("tcp", realAddr)
ok(t, err)
chans[i] = aChan{
c: gen,
cMini: cMini,
cReal: cReal,
}
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
f(chans[0].c, chans[1].c)
cancel()
for _, c := range chans {
close(c.c)
}
}()
loop:
for {
select {
case <-ctx.Done():
break loop
case cm := <-chans[0].c:
runCommand(t, chans[0].cMini, chans[0].cReal, cm)
case cm := <-chans[1].c:
runCommand(t, chans[1].cMini, chans[1].cReal, cm)
}
}
}
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()
var (
vReal, vMini interface{}
errReal, errMini error
)
if p.receiveOnly {
vReal, errReal = cReal.Receive()
vMini, errMini = cMini.Receive()
} else {
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 p.roundFloats > 0 {
vReal = roundFloats(vReal, p.roundFloats)
vMini = roundFloats(vMini, p.roundFloats)
}
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)
dump(vReal, " --real-")
dump(vMini, " --mini-")
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 int64:
_, ok := b.(int64)
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 / %T", a, a))
}
}
func dump(r interface{}, prefix string) {
switch ls := r.(type) {
case []interface{}:
for _, k := range ls {
switch k := k.(type) {
case []byte:
fmt.Printf(" %s %s\n", prefix, string(k))
case []interface{}:
fmt.Printf(" %s:\n", prefix)
for _, c := range k {
dump(c, " -")
}
default:
fmt.Printf(" %s %#v\n", prefix, k)
}
}
case []byte:
fmt.Printf(" %s %v\n", prefix, string(ls))
default:
fmt.Printf(" %s %v\n", prefix, ls)
}
}
// round all floats
func roundFloats(r interface{}, pos int) interface{} {
switch ls := r.(type) {
case []interface{}:
var new []interface{}
for _, k := range ls {
new = append(new, roundFloats(k, pos))
}
return new
case []byte:
f, err := strconv.ParseFloat(string(ls), 64)
if err != nil {
return ls
}
return []byte(fmt.Sprintf("%.[1]*f", pos, f))
default:
fmt.Printf("unhandled type: %T FIXME\n", r)
return nil
}
}