-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprotocol_test.go
123 lines (107 loc) · 3.16 KB
/
protocol_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
package redplex
import (
"bufio"
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadNextFull(t *testing.T) {
tt := [][]string{
{"$7", "he\r\nllo"},
{"$0", ""},
{"$-1"},
{":1"},
{"+OK"},
{"-hello world!"},
{"*0"},
{"*-1"},
{"*2", "$5", "hello", ":2"},
}
for _, tcase := range tt {
output := bytes.NewBuffer(nil)
expected := []byte(strings.Join(tcase, "\r\n") + "\r\n")
reader := bytes.NewReader(expected)
bufferedReader := bufio.NewReader(reader)
require.Nil(t, ReadNextFull(output, bufferedReader), "unexpected error in %+v", tcase)
require.Equal(t, output.Bytes(), expected, "expected parsed %+v to be equal", tcase)
require.Zero(t, reader.Len()+bufferedReader.Buffered(), "should have consumed all of %+v", tcase)
}
}
func TestParseBulkMessage(t *testing.T) {
tt := []struct {
Message string
Expected string
Error error
}{
{"$-1\r\n", "", nil},
{"$0\r\n", "", nil},
{"$5\r\nhello\r\n", "hello", nil},
{"$5\r\nhe", "", ErrWrongMessage},
{":1\r\nasdf\r\n", "", ErrWrongMessage},
}
for _, tcase := range tt {
actual, err := ParseBulkMessage([]byte(tcase.Message))
if tcase.Error != nil {
require.Equal(t, tcase.Error, err, "unexpected error parsing %s", tcase.Message)
} else if len(tcase.Expected) == 0 {
require.Nil(t, actual, "expected empty byte slice parsing %s", tcase.Message)
} else {
require.Equal(t, []byte(tcase.Expected), actual, "unexpected result parsing %s", tcase.Message)
}
}
}
func TestParsePublishCommand(t *testing.T) {
tt := []struct {
Message string
Expected PublishCommand
Error error
}{
{"$-1\r\n", PublishCommand{}, ErrWrongMessage},
{"*3\r\n$7\r\nmessage\r\n$6\r\nsecond\r\n$5\r\nHello\r\n", PublishCommand{false, []byte("second")}, nil},
{"*4\r\n$8\r\npmessage\r\n$6\r\nsecond\r\n$2\r\ns*\r\n$5\r\nHello\r\n", PublishCommand{true, []byte("second")}, nil},
}
for _, tcase := range tt {
actual, err := ParsePublishCommand([]byte(tcase.Message))
if err != nil {
require.Equal(t, tcase.Error, err, "unexpected error parsing %s", tcase.Message)
} else {
require.Equal(t, tcase.Expected, actual, "unexpected result parsing %s", tcase.Message)
}
}
}
func TestNewRequest(t *testing.T) {
require.Equal(t,
NewRequest("PING", 0).Bytes(),
[]byte("*1\r\n$4\r\nPING\r\n"),
)
require.Equal(t,
NewRequest("SUBSCRIBE", 1).Bulk([]byte("channel-name")).Bytes(),
[]byte("*2\r\n$9\r\nSUBSCRIBE\r\n$12\r\nchannel-name\r\n"),
)
}
func TestParseRequest(t *testing.T) {
tt := [][]string{
{"ping"},
{"subscribe", "foo"},
{"subscribe", "foo", "bar"},
}
for _, tcase := range tt {
var args [][]byte
cmd := NewRequest(tcase[0], len(tcase)-1)
for _, arg := range tcase[1:] {
args = append(args, []byte(arg))
cmd.Bulk([]byte(arg))
}
method, actualArgs, err := ParseRequest(bufio.NewReader(bytes.NewReader(cmd.Bytes())))
require.Nil(t, err, "unexpected error parsing %+v", tcase)
require.Equal(t, tcase[0], method)
require.Equal(t, args, actualArgs)
}
}
func TestSubscribeResponse(t *testing.T) {
require.Equal(t,
SubscribeResponse("subscribe", []byte("first")),
[]byte("*3\r\n$9\r\nsubscribe\r\n$5\r\nfirst\r\n:1\r\n"),
)
}