forked from gobwas/ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_test.go
59 lines (54 loc) · 1.41 KB
/
read_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
package ws
import (
"bytes"
"fmt"
"io"
"reflect"
"testing"
)
func TestReadHeader(t *testing.T) {
for i, test := range append([]RWTestCase{
{
Data: bits("0000 0000 0 1111111 10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000"),
// _______________________________________________________________________
// |
// Length value
Err: true,
},
}, RWTestCases...) {
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
r := bytes.NewReader(test.Data)
h, err := ReadHeader(r)
if test.Err && err == nil {
t.Errorf("expected error, got nil")
}
if !test.Err && err != nil {
t.Errorf("unexpected error: %s", err)
}
if test.Err {
return
}
if !reflect.DeepEqual(h, test.Header) {
t.Errorf("ReadHeader()\nread:\n\t%#v\nwant:\n\t%#v", h, test.Header)
}
})
}
}
func BenchmarkReadHeader(b *testing.B) {
for i, bench := range RWBenchCases {
b.Run(fmt.Sprintf("%s#%d", bench.label, i), func(b *testing.B) {
bts := MustCompileFrame(Frame{Header: bench.header})
rds := make([]io.Reader, b.N)
for i := 0; i < b.N; i++ {
rds[i] = bytes.NewReader(bts)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := ReadHeader(rds[i])
if err != nil {
b.Fatal(err)
}
}
})
}
}