-
Notifications
You must be signed in to change notification settings - Fork 8
/
decode_test.go
157 lines (135 loc) · 4.01 KB
/
decode_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
package protocol
import (
"io"
"testing"
"github.com/stretchr/testify/require"
)
func readCommands(t testing.TB, decoder CommandDecoder) []*Command {
t.Helper()
var commands []*Command
for {
cmd, err := decoder.Decode()
if err != nil {
if err == io.EOF {
if cmd != nil {
commands = append(commands, cmd)
}
break
}
t.Fatal(err)
}
if cmd != nil {
commands = append(commands, cmd)
}
}
return commands
}
func TestJSONCommandDecoder_Decode_Single(t *testing.T) {
data := []byte(`{"id": 1}`)
decoder := GetCommandDecoder(TypeJSON, data)
commands := readCommands(t, decoder)
require.Len(t, commands, 1)
}
func TestJSONCommandDecoder_Decode_Single_ExtraNewLine(t *testing.T) {
data := []byte(`{"id": 1}
`)
decoder := GetCommandDecoder(TypeJSON, data)
commands := readCommands(t, decoder)
require.Len(t, commands, 1)
}
func TestJSONCommandDecoder_Decode_Large(t *testing.T) {
var s string
for i := 0; i < 200000; i++ {
s += "1"
}
data := []byte(`{"id": 1, "x": "` + s + `"}`)
decoder := GetCommandDecoder(TypeJSON, data)
commands := readCommands(t, decoder)
require.Len(t, commands, 1)
}
func TestJSONCommandDecoder_Decode_Many(t *testing.T) {
data := []byte(`{"id": 1}
{"id": 2}`)
decoder := GetCommandDecoder(TypeJSON, data)
commands := readCommands(t, decoder)
require.Len(t, commands, 2)
require.Equal(t, uint32(1), commands[0].Id)
require.Equal(t, uint32(2), commands[1].Id)
}
func TestJSONCommandDecoder_DifferentNumberOfMessages(t *testing.T) {
data1 := []byte(`{"id": 1}`)
data2 := []byte(`{"id": 2}
{"id": 3}`)
decoder := GetCommandDecoder(TypeJSON, data1)
commands := readCommands(t, decoder)
require.Len(t, commands, 1)
require.Equal(t, uint32(1), commands[0].Id)
err := decoder.Reset(data2)
require.NoError(t, err)
commands = readCommands(t, decoder)
require.Len(t, commands, 2)
require.Equal(t, uint32(2), commands[0].Id)
require.Equal(t, uint32(3), commands[1].Id)
err = decoder.Reset(data1)
require.NoError(t, err)
commands = readCommands(t, decoder)
require.Len(t, commands, 1)
}
func TestJSONCommandDecoder_Decode_Many_ExtraNewLine(t *testing.T) {
data := []byte(`{"subscribe":{"channel":"chat:1","recover":true,"epoch":"WHBN"},"id":222}
{"subscribe":{"channel":"chat:2","recover":true,"epoch":"yenC"},"id":223}
{"subscribe":{"channel":"chat:index"},"id":224}
`)
decoder := GetCommandDecoder(TypeJSON, data)
commands := readCommands(t, decoder)
require.Len(t, commands, 3)
require.Equal(t, "chat:1", commands[0].Subscribe.Channel)
require.Equal(t, "chat:2", commands[1].Subscribe.Channel)
require.Equal(t, "chat:index", commands[2].Subscribe.Channel)
}
func TestJSONCommandDecoder_Decode_Many_UnexpectedEOF(t *testing.T) {
data := []byte(``)
decoder := GetCommandDecoder(TypeJSON, data)
_, err := decoder.Decode()
require.Equal(t, io.ErrUnexpectedEOF, err)
}
func TestJSONCommandDecoder_Decode_Many_FormatError(t *testing.T) {
data := []byte(`{"id": 1}
{"id": 2}
`)
decoder := GetCommandDecoder(TypeJSON, data)
_, err := decoder.Decode()
require.NoError(t, err)
_, err = decoder.Decode()
require.Error(t, err)
}
func TestProtobufCommandDecoder_Decode_Many(t *testing.T) {
encoder := NewProtobufCommandEncoder()
data1, err := encoder.Encode(&Command{Id: 1})
require.NoError(t, err)
data2, err := encoder.Encode(&Command{Id: 2})
require.NoError(t, err)
data1 = append(data1, data2...)
data := make([]byte, len(data1))
copy(data, data1)
decoder := GetCommandDecoder(TypeProtobuf, data)
commands := readCommands(t, decoder)
require.Len(t, commands, 2)
if len(commands) == 2 { // Make Goland happy.
require.Equal(t, uint32(1), commands[0].Id)
require.Equal(t, uint32(2), commands[1].Id)
}
}
func TestProtobufCommandDecoder_Decode_ShortData(t *testing.T) {
encoder := NewProtobufCommandEncoder()
data1, err := encoder.Encode(&Command{Id: 1})
require.NoError(t, err)
data := make([]byte, len(data1)-2)
copy(data, data1)
decoder := GetCommandDecoder(TypeProtobuf, data)
for {
_, err = decoder.Decode()
require.ErrorIs(t, err, io.ErrShortBuffer)
break
}
}