-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathcodec_siostreaming_test.go
291 lines (261 loc) · 6.32 KB
/
codec_siostreaming_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
package socketio
import (
"testing"
"utf8"
"fmt"
"bytes"
"unsafe"
"os"
)
func streamingFrame(data string, typ int, json bool) string {
utf8str := utf8.NewString(data)
switch typ {
case 0:
return "0:0:,"
case 2, 3:
return fmt.Sprintf("%d:%d:%s,", typ, utf8str.RuneCount(), data)
}
if json {
return fmt.Sprintf("%d:%d:j\n:%s,", typ, 3+utf8str.RuneCount(), data)
}
return fmt.Sprintf("%d:%d::%s,", typ, 1+utf8str.RuneCount(), data)
}
type streamingEncodeTest struct {
in interface{}
out string
}
var streamingEncodeTests = []streamingEncodeTest{
{
123,
streamingFrame("123", 1, false),
},
{
"hello, world",
streamingFrame("hello, world", 1, false),
},
{
"öäö¥£♥",
streamingFrame("öäö¥£♥", 1, false),
},
{
"öäö¥£♥",
streamingFrame("öäö¥£♥", 1, false),
},
{
heartbeat(123456),
streamingFrame("123456", 2, false),
},
{
handshake("abcdefg"),
streamingFrame("abcdefg", 3, false),
},
{
true,
streamingFrame("true", 1, true),
},
{
struct {
Boolean bool
Str string
Array []int
}{
false,
"string♥",
[]int{1, 2, 3, 4},
},
streamingFrame(`{"Boolean":false,"Str":"string♥","Array":[1,2,3,4]}`, 1, true),
},
{
[]byte("hello, world"),
streamingFrame("hello, world", 1, false),
},
}
type streamingDecodeTestMessage struct {
messageType uint8
data string
heartbeat heartbeat
}
type streamingDecodeTest struct {
in string
out []streamingDecodeTestMessage
}
// NOTE: if you change these -> adjust the benchmarks
var streamingDecodeTests = []streamingDecodeTest{
{
streamingFrame("", 1, false),
[]streamingDecodeTestMessage{{MessageText, "", -1}},
},
{
streamingFrame("123", 2, false),
[]streamingDecodeTestMessage{{MessageHeartbeat, "123", 123}},
},
{
streamingFrame("wadap!", 1, false),
[]streamingDecodeTestMessage{{MessageText, "wadap!", -1}},
},
{
streamingFrame("♥wadap!", 1, true),
[]streamingDecodeTestMessage{{MessageJSON, "♥wadap!", -1}},
},
{
streamingFrame("hello, world!", 1, true) + streamingFrame("313", 2, false) + streamingFrame("♥wadap!", 1, false),
[]streamingDecodeTestMessage{
{MessageJSON, "hello, world!", -1},
{MessageHeartbeat, "313", 313},
{MessageText, "♥wadap!", -1},
},
},
{
"1:3::fael!,",
nil,
},
{
streamingFrame("wadap!", 1, false),
[]streamingDecodeTestMessage{{MessageText, "wadap!", -1}},
},
}
func TestStreamingEncode(t *testing.T) {
codec := SIOStreamingCodec{}
enc := codec.NewEncoder()
buf := new(bytes.Buffer)
for _, test := range streamingEncodeTests {
t.Logf("in=%v out=%s", test.in, test.out)
buf.Reset()
if err := enc.Encode(buf, test.in); err != nil {
t.Fatal("Encode:", err)
}
if string(buf.Bytes()) != test.out {
t.Fatalf("Expected %q but got %q from %q", test.out, string(buf.Bytes()), test.in)
}
}
}
func TestStreamingDecode(t *testing.T) {
codec := SIOStreamingCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
var messages []Message
var err os.Error
for _, test := range streamingDecodeTests {
t.Logf("in=%s out=%v", test.in, test.out)
buf.WriteString(test.in)
if messages, err = dec.Decode(); err != nil {
if test.out == nil {
continue
}
t.Fatal("Decode:", err)
}
if test.out == nil {
t.Fatalf("Expected decode error, but got: %v, %v", messages, err)
}
if len(messages) != len(test.out) {
t.Fatalf("Expected %d messages, but got %d", len(test.out), len(messages))
}
for i, msg := range messages {
t.Logf("message: %#v", msg)
if test.out[i].messageType != msg.Type() {
t.Fatalf("Expected type %d but got %d", test.out[i].messageType, msg.Type())
}
if test.out[i].data != msg.Data() {
t.Fatalf("Expected data %q but got %q", test.out[i].data, msg.Data())
}
if test.out[i].messageType == MessageHeartbeat {
if hb, ok := msg.heartbeat(); !ok || test.out[i].heartbeat != hb {
t.Fatalf("Expected heartbeat %d but got %d (%v)", test.out[i].heartbeat, hb, err)
}
}
}
}
}
func TestStreamingDecodeStreaming(t *testing.T) {
var messages []Message
var err os.Error
codec := SIOStreamingCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
expectNothing := func(written string) {
if messages, err = dec.Decode(); err != nil || messages == nil || len(messages) != 0 {
t.Fatalf("Partial decode failed after writing %s. err=%#v messages=%#v", written, err, messages)
}
}
buf.WriteString("5")
expectNothing("5")
buf.WriteString(":9")
expectNothing("5:9")
buf.WriteString(":12345")
expectNothing("5:9:12345")
buf.WriteString("678")
expectNothing("5:9:12345678")
buf.WriteString("9")
expectNothing("5:9:123456789")
buf.WriteString(",typefornextmessagewhichshouldbeignored")
messages, err = dec.Decode()
if err != nil {
t.Fatalf("Did not expect errors: %s", err)
}
if messages == nil || len(messages) != 1 {
t.Fatalf("Expected 1 message, got: %#v", messages)
}
if messages[0].(*sioMessage).typ != 5 || messages[0].Data() != "123456789" {
t.Fatalf("Expected data 123456789 and typ 5, got: %#v", messages[0])
}
}
func BenchmarkIntEncode(b *testing.B) {
codec := SIOStreamingCodec{}
enc := codec.NewEncoder()
payload := 313313
b.SetBytes(int64(unsafe.Sizeof(payload)))
w := nopWriter{}
for i := 0; i < b.N; i++ {
enc.Encode(w, payload)
}
}
func BenchmarkStringEncode(b *testing.B) {
codec := SIOStreamingCodec{}
enc := codec.NewEncoder()
payload := "Hello, World!"
b.SetBytes(int64(len(payload)))
w := nopWriter{}
for i := 0; i < b.N; i++ {
enc.Encode(w, payload)
}
}
func BenchmarkStructEncode(b *testing.B) {
codec := SIOStreamingCodec{}
enc := codec.NewEncoder()
payload := struct {
boolean bool
str string
array []int
}{
false,
"string♥",
[]int{1, 2, 3, 4},
}
b.SetBytes(int64(unsafe.Sizeof(payload)))
w := nopWriter{}
for i := 0; i < b.N; i++ {
enc.Encode(w, payload)
}
}
func BenchmarkSingleFrameDecode(b *testing.B) {
codec := SIOStreamingCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
data := []byte(decodeTests[2].in)
b.SetBytes(int64(len(data)))
for i := 0; i < b.N; i++ {
buf.Write(data)
dec.Decode()
}
}
func BenchmarkThreeFramesDecode(b *testing.B) {
codec := SIOStreamingCodec{}
buf := new(bytes.Buffer)
dec := codec.NewDecoder(buf)
data := []byte(decodeTests[3].in)
b.SetBytes(int64(len(data)))
for i := 0; i < b.N; i++ {
buf.Write(data)
dec.Decode()
}
}