Repository navigation
Expand file tree
/
Copy pathwrite_alloc_test.go
More file actions
60 lines (54 loc) · 1.78 KB
/
Copy pathwrite_alloc_test.go
File metadata and controls
60 lines (54 loc) · 1.78 KB
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
//go:build (linux || darwin || dragonfly || freebsd || netbsd || openbsd || windows) && !race
package websocket
import (
"io"
stdhttp "net/http"
"testing"
fib "github.com/lesismal/fib"
)
// A server frame's header is built on sendFrame's stack and written beside the
// payload, so WriteMessage allocates nothing. Every platform's two-part write
// has to keep it there: on Darwin and OpenBSD, a writev the compiler could not
// see into put the header and the iovec array on the heap, two allocations
// for every message to every connection, and on Windows sys.WSABufs did the
// same. The race detector's hooks in the syscall package let it escape anyway,
// so this does not run under it.
func TestServerWriteMessageAllocs(t *testing.T) {
opened := make(chan *Connection, 1)
config := fib.DefaultConfig()
server, err := fib.New(config, fib.Binding{Addr: "127.0.0.1:0", Handler: NewHandler(HandlerFuncs{
Open: func(c *Connection, _ *stdhttp.Request) { opened <- c },
})})
if err != nil {
t.Fatal(err)
}
addr, err := server.LocalAddr()
if err != nil {
t.Fatal(err)
}
runDone := make(chan error, 1)
go func() { runDone <- server.Run() }()
defer func() {
server.Stop()
<-runDone
_ = server.Close()
}()
conn, reader := dialUpgraded(t, addr.String())
defer conn.Close()
go func() { _, _ = io.Copy(io.Discard, reader) }()
c := <-opened
// Small enough that the socket never pushes back, which would queue the
// unsent rest and count a pool's allocations instead.
payload := make([]byte, 16)
if err := c.WriteMessage(Binary, payload); err != nil {
t.Fatal(err)
}
allocs := testing.AllocsPerRun(100, func() {
if err := c.WriteMessage(Binary, payload); err != nil {
t.Fatal(err)
}
})
if allocs != 0 {
t.Fatalf("WriteMessage allocated %v times a message", allocs)
}
}