-
Notifications
You must be signed in to change notification settings - Fork 27
/
main_test.go
87 lines (83 loc) · 2.27 KB
/
main_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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"reflect"
"testing"
)
func TestReadRedisCommand(t *testing.T) {
tests := []struct {
description string
input string
expected redisCommand
expectedError error
}{
{
description: "1: Reply",
input: "+PONG\r\n",
expected: redisCommand{reply: "PONG"},
expectedError: nil,
},
{
description: "2: Empty command",
input: "\n",
expected: redisCommand{},
expectedError: nil,
},
{
description: "3: Simple command",
input: "SYNC\r\n",
expected: redisCommand{command: []string{"SYNC"}},
expectedError: nil,
},
{
description: "4: Bulk reply",
input: "$4568\r\n",
expected: redisCommand{bulkSize: 4568},
expectedError: nil,
},
{
description: "5: Complex command",
input: "*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n",
expected: redisCommand{command: []string{"SET", "mykey", "myvalue"}},
expectedError: nil,
},
{
description: "6: Immediate EOF",
input: "+PONG",
expected: redisCommand{},
expectedError: fmt.Errorf("Failed to read command: %v", io.EOF),
},
{
description: "7: EOF in length",
input: "*3\r\n$3",
expected: redisCommand{},
expectedError: fmt.Errorf("Failed to read command: %v", io.EOF),
},
{
description: "8: EOF in data",
input: "*3\r\n$3\r\nSE",
expected: redisCommand{},
expectedError: fmt.Errorf("Failed to read argument: %v", io.ErrUnexpectedEOF),
},
{
description: "9: Unparsable length",
input: "*x\r\n",
expected: redisCommand{},
expectedError: fmt.Errorf("Unable to parse command length: strconv.ParseInt: parsing \"x\": invalid syntax"),
},
}
for _, test := range tests {
test.expected.raw = []byte(test.input)
command, err := readRedisCommand(bufio.NewReader(bytes.NewBufferString(test.input)))
if err != nil {
if test.expectedError == nil || test.expectedError.Error() != err.Error() {
t.Errorf("Unexpected error: %v (test %s)", err, test.description)
}
} else if !reflect.DeepEqual(*command, test.expected) {
t.Errorf("Output not equal to expected %#v != %#v (test %s)", *command, test.expected, test.description)
}
}
}