forked from tidwall/tile38
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaof_test.go
347 lines (322 loc) · 7.99 KB
/
aof_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
package tests
import (
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"sync/atomic"
"time"
"github.com/gomodule/redigo/redis"
_ "embed"
)
func subTestAOF(g *testGroup) {
g.regSubTest("loading", aof_loading_test)
g.regSubTest("migrate", aof_migrate_test)
g.regSubTest("AOF", aof_AOF_test)
g.regSubTest("AOFMD5", aof_AOFMD5_test)
g.regSubTest("AOFSHRINK", aof_AOFSHRINK_test)
g.regSubTest("READONLY", aof_READONLY_test)
}
func loadAOFAndClose(aof any) error {
mc, err := loadAOF(aof)
if mc != nil {
mc.Close()
}
return err
}
func loadAOF(aof any) (*mockServer, error) {
var aofb []byte
switch aof := aof.(type) {
case []byte:
aofb = []byte(aof)
case string:
aofb = []byte(aof)
default:
return nil, errors.New("aof is not string or bytes")
}
return mockOpenServer(MockServerOptions{
Silent: true,
Metrics: false,
AOFData: aofb,
})
}
func aof_loading_test(mc *mockServer) error {
var err error
// invalid command
err = loadAOFAndClose("asdfasdf\r\n")
if err == nil || err.Error() != "unknown command 'asdfasdf'" {
return fmt.Errorf("expected '%v', got '%v'",
"unknown command 'asdfasdf'", err)
}
// incomplete command
err = loadAOFAndClose("set fleet truck point 10 10\r\nasdfasdf")
if err != nil {
return err
}
// big aof file
var aof string
for i := 0; i < 10000; i++ {
aof += fmt.Sprintf("SET fleet truck%d POINT 10 10\r\n", i)
}
err = loadAOFAndClose(aof)
if err != nil {
return err
}
// extra zeros at various places
aof = ""
for i := 0; i < 1000; i++ {
if i%10 == 0 {
aof += string(bytes.Repeat([]byte{0}, 100))
}
aof += fmt.Sprintf("SET fleet truck%d POINT 10 10\r\n", i)
}
aof += string(bytes.Repeat([]byte{0}, 5000))
err = loadAOFAndClose(aof)
if err != nil {
return err
}
// bad protocol
aof = "*2\r\n$1\r\nh\r\n+OK\r\n"
err = loadAOFAndClose(aof)
if fmt.Sprintf("%v", err) != "Protocol error: expected '$', got '+'" {
return fmt.Errorf("expected '%v', got '%v'",
"Protocol error: expected '$', got '+'", err)
}
return nil
}
func aof_AOFMD5_test(mc *mockServer) error {
for i := 0; i < 10000; i++ {
_, err := mc.Do("SET", "fleet", rand.Int(),
"POINT", rand.Float64()*180-90, rand.Float64()*360-180)
if err != nil {
return err
}
}
aof, err := mc.readAOF()
if err != nil {
return err
}
check := func(start, size int) func(s string) error {
return func(s string) error {
sum := md5.Sum(aof[start : start+size])
val := hex.EncodeToString(sum[:])
if s != val {
return fmt.Errorf("expected '%s', got '%s'", val, s)
}
return nil
}
}
return mc.DoBatch(
Do("AOFMD5").Err("wrong number of arguments for 'aofmd5' command"),
Do("AOFMD5", 0).Err("wrong number of arguments for 'aofmd5' command"),
Do("AOFMD5", 0, 0, 1).Err("wrong number of arguments for 'aofmd5' command"),
Do("AOFMD5", -1, 0).Err("invalid argument '-1'"),
Do("AOFMD5", 1, -1).Err("invalid argument '-1'"),
Do("AOFMD5", 0, 100000000000).Err("EOF"),
Do("AOFMD5", 0, 0).Str("d41d8cd98f00b204e9800998ecf8427e"),
Do("AOFMD5", 0, 0).JSON().Str(`{"ok":true,"md5":"d41d8cd98f00b204e9800998ecf8427e"}`),
Do("AOFMD5", 0, 0).Func(check(0, 0)),
Do("AOFMD5", 0, 1).Func(check(0, 1)),
Do("AOFMD5", 0, 100).Func(check(0, 100)),
Do("AOFMD5", 1002, 4321).Func(check(1002, 4321)),
)
}
func openFollower(mc *mockServer) (conn redis.Conn, err error) {
conn, err = redis.Dial("tcp", fmt.Sprintf(":%d", mc.port),
redis.DialReadTimeout(time.Second))
if err != nil {
return nil, err
}
defer func() {
if err != nil {
conn.Close()
conn = nil
}
}()
if err := conn.Send("AOF", 0); err != nil {
return nil, err
}
if err := conn.Flush(); err != nil {
return nil, err
}
str, err := redis.String(conn.Receive())
if err != nil {
return nil, err
}
if str != "OK" {
return nil, fmt.Errorf("expected '%s', got '%s'", "OK", str)
}
return conn, nil
}
func aof_AOF_test(mc *mockServer) error {
var argss [][]interface{}
for i := 0; i < 10000; i++ {
args := []interface{}{"SET", "fleet", fmt.Sprint(rand.Int()),
"POINT", fmt.Sprint(rand.Float64()*180 - 90),
fmt.Sprint(rand.Float64()*360 - 180)}
argss = append(argss, args)
_, err := mc.Do(fmt.Sprint(args[0]), args[1:]...)
if err != nil {
return err
}
}
readAll := func() (conn redis.Conn, err error) {
conn, err = openFollower(mc)
if err != nil {
return
}
defer func() {
if err != nil {
conn.Close()
conn = nil
}
}()
var t bool
for i := 0; i < len(argss); i++ {
args, err := redis.Values(conn.Receive())
if err != nil {
return nil, err
}
if t || (len(args) == len(argss[0]) &&
fmt.Sprintf("%s", args[2]) == fmt.Sprintf("%s", argss[0][2])) {
t = true
if fmt.Sprintf("%s", args[2]) !=
fmt.Sprintf("%s", argss[i][2]) {
return nil, fmt.Errorf("expected '%s', got '%s'",
argss[i][2], args[2])
}
} else {
i--
}
}
return conn, nil
}
conn, err := readAll()
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Do("fancy") // non-existent error
if err == nil || err.Error() != "EOF" {
return fmt.Errorf("expected '%v', got '%v'", "EOF", err)
}
conn, err = readAll()
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Do("quit")
if err == nil || err.Error() != "EOF" {
return fmt.Errorf("expected '%v', got '%v'", "EOF", err)
}
return mc.DoBatch(
Do("AOF").Err("wrong number of arguments for 'aof' command"),
Do("AOF", 0, 0).Err("wrong number of arguments for 'aof' command"),
Do("AOF", -1).Err("invalid argument '-1'"),
Do("AOF", 1000000000000).Err("pos is too big, must be less that the aof_size of leader"),
)
}
func aof_AOFSHRINK_test(mc *mockServer) error {
var err error
haddr := fmt.Sprintf("localhost:%d", getNextPort())
ln, err := net.Listen("tcp", haddr)
if err != nil {
return err
}
defer ln.Close()
var msgs atomic.Int32
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
msgs.Add(1)
// println(r.URL.Path)
})
http.Serve(ln, mux)
}()
err = mc.DoBatch(
Do("SETCHAN", "mychan", "INTERSECTS", "mi:0", "BOUNDS", -10, -10, 10, 10).Str("1"),
Do("SETHOOK", "myhook", "http://"+haddr, "INTERSECTS", "mi:0", "BOUNDS", -10, -10, 10, 10).Str("1"),
Do("MASSINSERT", 5, 10000).OK(),
)
if err != nil {
return err
}
err = mc.DoBatch(
Do("AOFSHRINK").OK(),
Do("MASSINSERT", 5, 10000).OK(),
)
if err != nil {
return err
}
nmsgs := msgs.Load()
if nmsgs == 0 {
return fmt.Errorf("expected > 0, got %d", nmsgs)
}
return err
}
func aof_READONLY_test(mc *mockServer) error {
return mc.DoBatch(
Do("SET", "mykey", "myid", "POINT", "10", "10").OK(),
Do("READONLY", "yes").OK(),
Do("SET", "mykey", "myid", "POINT", "10", "10").Err("read only"),
Do("READONLY", "no").OK(),
Do("SET", "mykey", "myid", "POINT", "10", "10").OK(),
Do("READONLY").Err("wrong number of arguments for 'readonly' command"),
Do("READONLY", "maybe").Err("invalid argument 'maybe'"),
)
}
//go:embed aof_legacy
var aofLegacy []byte
func aof_migrate_test(mc *mockServer) error {
var aof []byte
for i := 0; i < 10000; i++ {
aof = append(aof, aofLegacy...)
}
var mc2 *mockServer
var err error
defer func() {
mc2.Close()
}()
mc2, err = mockOpenServer(MockServerOptions{
AOFFileName: "aof",
AOFData: aof,
Silent: true,
Metrics: true,
})
if err != nil {
return err
}
err = mc2.DoBatch(
Do("GET", "1", "2").Str(`{"type":"Point","coordinates":[20,10]}`),
)
if err != nil {
return err
}
mc2.Close()
mc2, err = mockOpenServer(MockServerOptions{
AOFFileName: "aof",
AOFData: aofLegacy[:len(aofLegacy)-1],
Silent: true,
Metrics: true,
})
if err != io.ErrUnexpectedEOF {
return fmt.Errorf("expected '%v', got '%v'", io.ErrUnexpectedEOF, err)
}
mc2.Close()
mc2, err = mockOpenServer(MockServerOptions{
AOFFileName: "aof",
AOFData: aofLegacy[1:],
Silent: true,
Metrics: true,
})
if err != io.ErrUnexpectedEOF {
return fmt.Errorf("expected '%v', got '%v'", io.ErrUnexpectedEOF, err)
}
mc2.Close()
return nil
}