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.
18 func TestBuffers_read(t *testing.T) {
19 const story = "once upon a time in Gopherland ... "
26 []byte("Gopherland ... "),
28 got, err := io.ReadAll(&buffers)
32 if string(got) != story {
33 t.Errorf("read %q; want %q", got, story)
35 if len(buffers) != 0 {
36 t.Errorf("len(buffers) = %d; want 0", len(buffers))
40 func TestBuffers_consume(t *testing.T) {
47 in: Buffers{[]byte("foo"), []byte("bar")},
49 want: Buffers{[]byte("foo"), []byte("bar")},
52 in: Buffers{[]byte("foo"), []byte("bar")},
54 want: Buffers{[]byte("o"), []byte("bar")},
57 in: Buffers{[]byte("foo"), []byte("bar")},
59 want: Buffers{[]byte("bar")},
62 in: Buffers{[]byte("foo"), []byte("bar")},
64 want: Buffers{[]byte("ar")},
67 in: Buffers{nil, nil, nil, []byte("bar")},
69 want: Buffers{[]byte("ar")},
72 in: Buffers{nil, nil, nil, []byte("foo")},
74 want: Buffers{[]byte("foo")},
77 in: Buffers{nil, nil, nil},
82 for i, tt := range tests {
84 in.consume(tt.consume)
85 if !reflect.DeepEqual(in, tt.want) {
86 t.Errorf("%d. after consume(%d) = %+v, want %+v", i, tt.consume, in, tt.want)
91 func TestBuffers_WriteTo(t *testing.T) {
92 for _, name := range []string{"WriteTo", "Copy"} {
93 for _, size := range []int{0, 10, 1023, 1024, 1025} {
94 t.Run(fmt.Sprintf("%s/%d", name, size), func(t *testing.T) {
95 testBuffer_writeTo(t, size, name == "Copy")
101 func testBuffer_writeTo(t *testing.T, chunks int, useCopy bool) {
102 oldHook := poll.TestHookDidWritev
103 defer func() { poll.TestHookDidWritev = oldHook }()
104 var writeLog struct {
108 poll.TestHookDidWritev = func(size int) {
110 writeLog.log = append(writeLog.log, size)
113 var want bytes.Buffer
114 for i := 0; i < chunks; i++ {
115 want.WriteByte(byte(i))
118 withTCPConnPair(t, func(c *TCPConn) error {
119 buffers := make(Buffers, chunks)
120 for i := range buffers {
121 buffers[i] = want.Bytes()[i : i+1]
126 n, err = io.Copy(c, &buffers)
128 n, err = buffers.WriteTo(c)
133 if len(buffers) != 0 {
134 return fmt.Errorf("len(buffers) = %d; want 0", len(buffers))
136 if n != int64(want.Len()) {
137 return fmt.Errorf("Buffers.WriteTo returned %d; want %d", n, want.Len())
140 }, func(c *TCPConn) error {
141 all, err := io.ReadAll(c)
142 if !bytes.Equal(all, want.Bytes()) || err != nil {
143 return fmt.Errorf("client read %q, %v; want %q, nil", all, err, want.Bytes())
146 writeLog.Lock() // no need to unlock
148 for _, v := range writeLog.log {
153 switch runtime.GOOS {
154 case "aix", "android", "darwin", "ios", "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
162 if len(writeLog.log) < wantMinCalls {
163 t.Errorf("write calls = %v < wanted min %v", len(writeLog.log), wantMinCalls)
169 wantCalls = 1 // windows will always do 1 syscall, unless sending empty buffer
171 if len(writeLog.log) != wantCalls {
172 t.Errorf("write calls = %v; want %v", len(writeLog.log), wantCalls)
175 if gotSum != wantSum {
176 t.Errorf("writev call sum = %v; want %v", gotSum, wantSum)
182 func TestWritevError(t *testing.T) {
183 if runtime.GOOS == "windows" {
184 t.Skipf("skipping the test: windows does not have problem sending large chunks of data")
187 ln := newLocalListener(t, "tcp")
189 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")