-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
154 lines (136 loc) · 2.9 KB
/
decoder_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
package go_engine_io_parser
import (
"bytes"
"errors"
"io"
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fakeReader struct {
r io.Reader
}
func (r fakeReader) Read(p []byte) (int, error) {
return r.r.Read(p)
}
type fakeReaderFeeder struct {
data []byte
supportBinary bool
returnError error
sendError error
getCounter int
putCounter int
}
func (f *fakeReaderFeeder) getReader() (io.Reader, bool, error) {
f.getCounter++
return fakeReader{bytes.NewReader(f.data)}, f.supportBinary, f.returnError
}
func (f *fakeReaderFeeder) putReader(err error) error {
f.putCounter++
f.sendError = err
return f.returnError
}
func TestDecoder(t *testing.T) {
for _, test := range tests {
feeder := fakeReaderFeeder{
data: test.data,
supportBinary: test.supportBinary,
}
d := decoder{
feeder: &feeder,
}
var packets []Packet
for i := 0; i < len(test.packets); i++ {
ft, pt, fr, err := d.NextReader()
if err != nil {
require.Equal(t, io.EOF, err)
break
}
data, err := ioutil.ReadAll(fr)
require.Nil(t, err)
packet := Packet{
ft: ft,
pt: pt,
data: data,
}
err = fr.Close()
require.Nil(t, err)
packets = append(packets, packet)
}
assert.Equal(t, test.packets, packets)
assert.Equal(t, feeder.getCounter, 1)
assert.Equal(t, feeder.putCounter, 1)
}
}
func TestDecoderNextReaderError(t *testing.T) {
feeder := fakeReaderFeeder{
data: []byte{0x00, 0x01, 0xff, '0'},
supportBinary: true,
}
d := decoder{
feeder: &feeder,
}
targetErr := errors.New("error")
feeder.returnError = targetErr
_, _, _, err := d.NextReader()
assert.Equal(t, targetErr, err)
}
func BenchmarkStringDecoder(b *testing.B) {
feeder := fakeReaderFeeder{
data: []byte("8:4你好\n6:2probe"),
supportBinary: false,
}
d := decoder{
feeder: &feeder,
}
buf := make([]byte, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 2; j++ {
_, _, r, _ := d.NextReader()
_, _ = r.Read(buf)
_ = r.Close()
}
}
}
func BenchmarkB64Decoder(b *testing.B) {
feeder := fakeReaderFeeder{
data: []byte("10:b4aGVsbG8K6:2probe"),
supportBinary: false,
}
d := decoder{
feeder: &feeder,
}
buf := make([]byte, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 2; j++ {
_, _, r, _ := d.NextReader()
r.Read(buf)
r.Close()
}
}
}
func BenchmarkBinaryDecoder(b *testing.B) {
feeder := fakeReaderFeeder{
data: []byte{
0x01, 0x07, 0xff, 0x04, 'h', 'e', 'l', 'l', 'o', '\n',
0x00, 0x08, 0xff, '4', 0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd, '\n',
0x00, 0x06, 0xff, '2', 'p', 'r', 'o', 'b', 'e',
},
supportBinary: true,
}
d := decoder{
feeder: &feeder,
}
buf := make([]byte, 10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < 3; j++ {
_, _, r, _ := d.NextReader()
r.Read(buf)
r.Close()
}
}
}