forked from gorilla/websocket
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient_test.go
64 lines (56 loc) · 1.75 KB
/
client_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
// Copyright 2014 The Gorilla WebSocket Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package websocket
import (
"bufio"
"bytes"
"io"
"net/url"
"testing"
)
var hostPortNoPortTests = []struct {
u *url.URL
hostPort, hostNoPort string
}{
{&url.URL{Scheme: "ws", Host: "example.com"}, "example.com:80", "example.com"},
{&url.URL{Scheme: "wss", Host: "example.com"}, "example.com:443", "example.com"},
{&url.URL{Scheme: "ws", Host: "example.com:7777"}, "example.com:7777", "example.com"},
{&url.URL{Scheme: "wss", Host: "example.com:7777"}, "example.com:7777", "example.com"},
}
func TestHostPortNoPort(t *testing.T) {
for _, tt := range hostPortNoPortTests {
hostPort, hostNoPort := hostPortNoPort(tt.u)
if hostPort != tt.hostPort {
t.Errorf("hostPortNoPort(%v) returned hostPort %q, want %q", tt.u, hostPort, tt.hostPort)
}
if hostNoPort != tt.hostNoPort {
t.Errorf("hostPortNoPort(%v) returned hostNoPort %q, want %q", tt.u, hostNoPort, tt.hostNoPort)
}
}
}
type InfiniteReader struct{}
func (reader InfiniteReader) Read(b []byte) (int, error) {
for i := range b {
b[i] = 'A'
}
return len(b), nil
}
func BenchmarkSingleBufferedRead(b *testing.B) {
inf := InfiniteReader{}
singleReader := bufio.NewReaderSize(inf, 81920)
r := make([]byte, 400)
for n := 0; n < b.N; n++ {
_, _ = singleReader.Read(r)
}
}
func BenchmarkMultiBufferedRead(b *testing.B) {
inf := InfiniteReader{}
emptyR := new([]byte)
emptyRemainder := bytes.NewReader(*emptyR)
multiReader := bufio.NewReaderSize(io.MultiReader(emptyRemainder, bufio.NewReaderSize(inf, 81920)), 81920)
r := make([]byte, 400)
for n := 0; n < b.N; n++ {
_, _ = multiReader.Read(r)
}
}