1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
21 func TestBuffers_read(t
*testing
.T
) {
22 const story
= "once upon a time in Gopherland ... "
29 []byte("Gopherland ... "),
31 got
, err
:= ioutil
.ReadAll(&buffers
)
35 if string(got
) != story
{
36 t
.Errorf("read %q; want %q", got
, story
)
38 if len(buffers
) != 0 {
39 t
.Errorf("len(buffers) = %d; want 0", len(buffers
))
43 func TestBuffers_consume(t
*testing
.T
) {
50 in
: Buffers
{[]byte("foo"), []byte("bar")},
52 want
: Buffers
{[]byte("foo"), []byte("bar")},
55 in
: Buffers
{[]byte("foo"), []byte("bar")},
57 want
: Buffers
{[]byte("o"), []byte("bar")},
60 in
: Buffers
{[]byte("foo"), []byte("bar")},
62 want
: Buffers
{[]byte("bar")},
65 in
: Buffers
{[]byte("foo"), []byte("bar")},
67 want
: Buffers
{[]byte("ar")},
70 in
: Buffers
{nil, nil, nil, []byte("bar")},
72 want
: Buffers
{[]byte("ar")},
75 in
: Buffers
{nil, nil, nil, []byte("foo")},
77 want
: Buffers
{[]byte("foo")},
80 in
: Buffers
{nil, nil, nil},
85 for i
, tt
:= range tests
{
87 in
.consume(tt
.consume
)
88 if !reflect
.DeepEqual(in
, tt
.want
) {
89 t
.Errorf("%d. after consume(%d) = %+v, want %+v", i
, tt
.consume
, in
, tt
.want
)
94 func TestBuffers_WriteTo(t
*testing
.T
) {
95 for _
, name
:= range []string{"WriteTo", "Copy"} {
96 for _
, size
:= range []int{0, 10, 1023, 1024, 1025} {
97 t
.Run(fmt
.Sprintf("%s/%d", name
, size
), func(t
*testing
.T
) {
98 testBuffer_writeTo(t
, size
, name
== "Copy")
104 func testBuffer_writeTo(t
*testing
.T
, chunks
int, useCopy
bool) {
105 oldHook
:= poll
.TestHookDidWritev
106 defer func() { poll
.TestHookDidWritev
= oldHook
}()
107 var writeLog
struct {
111 poll
.TestHookDidWritev
= func(size
int) {
113 writeLog
.log
= append(writeLog
.log
, size
)
116 var want bytes
.Buffer
117 for i
:= 0; i
< chunks
; i
++ {
118 want
.WriteByte(byte(i
))
121 withTCPConnPair(t
, func(c
*TCPConn
) error
{
122 buffers
:= make(Buffers
, chunks
)
123 for i
:= range buffers
{
124 buffers
[i
] = want
.Bytes()[i
: i
+1]
129 n
, err
= io
.Copy(c
, &buffers
)
131 n
, err
= buffers
.WriteTo(c
)
136 if len(buffers
) != 0 {
137 return fmt
.Errorf("len(buffers) = %d; want 0", len(buffers
))
139 if n
!= int64(want
.Len()) {
140 return fmt
.Errorf("Buffers.WriteTo returned %d; want %d", n
, want
.Len())
143 }, func(c
*TCPConn
) error
{
144 all
, err
:= ioutil
.ReadAll(c
)
145 if !bytes
.Equal(all
, want
.Bytes()) || err
!= nil {
146 return fmt
.Errorf("client read %q, %v; want %q, nil", all
, err
, want
.Bytes())
149 writeLog
.Lock() // no need to unlock
151 for _
, v
:= range writeLog
.log
{
156 switch runtime
.GOOS
{
157 case "android", "darwin", "dragonfly", "freebsd", "linux", "netbsd", "openbsd":
165 if len(writeLog
.log
) < wantMinCalls
{
166 t
.Errorf("write calls = %v < wanted min %v", len(writeLog
.log
), wantMinCalls
)
172 wantCalls
= 1 // windows will always do 1 syscall, unless sending empty buffer
174 if len(writeLog
.log
) != wantCalls
{
175 t
.Errorf("write calls = %v; want %v", len(writeLog
.log
), wantCalls
)
178 if gotSum
!= wantSum
{
179 t
.Errorf("writev call sum = %v; want %v", gotSum
, wantSum
)
185 func TestWritevError(t
*testing
.T
) {
186 if runtime
.GOOS
== "windows" {
187 t
.Skipf("skipping the test: windows does not have problem sending large chunks of data")
190 ln
, err
:= newLocalListener("tcp")
196 ch
:= make(chan Conn
, 1)
199 c
, err
:= ln
.Accept()
206 c1
, err
:= Dial("tcp", ln
.Addr().String())
213 t
.Fatal("no server side connection")
217 // 1 GB of data should be enough to notice the connection is gone.
218 // Just a few bytes is not enough.
219 // Arrange to reuse the same 1 MB buffer so that we don't allocate much.
220 buf
:= make([]byte, 1<<20)
221 buffers
:= make(Buffers
, 1<<10)
222 for i
:= range buffers
{
225 if _
, err
:= buffers
.WriteTo(c1
); err
== nil {
226 t
.Fatal("Buffers.WriteTo(closed conn) succeeded, want error")