diff options
Diffstat (limited to 'libgo/go/net/sendfile_test.go')
-rw-r--r-- | libgo/go/net/sendfile_test.go | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/libgo/go/net/sendfile_test.go b/libgo/go/net/sendfile_test.go index 2255e7c..3b98277 100644 --- a/libgo/go/net/sendfile_test.go +++ b/libgo/go/net/sendfile_test.go @@ -2,9 +2,12 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build !js + package net import ( + "bytes" "crypto/sha256" "encoding/hex" "fmt" @@ -88,3 +91,122 @@ func TestSendfile(t *testing.T) { t.Error(err) } } + +func TestSendfileParts(t *testing.T) { + ln, err := newLocalListener("tcp") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + + errc := make(chan error, 1) + go func(ln Listener) { + // Wait for a connection. + conn, err := ln.Accept() + if err != nil { + errc <- err + close(errc) + return + } + + go func() { + defer close(errc) + defer conn.Close() + + f, err := os.Open(twain) + if err != nil { + errc <- err + return + } + defer f.Close() + + for i := 0; i < 3; i++ { + // Return file data using io.CopyN, which should use + // sendFile if available. + _, err = io.CopyN(conn, f, 3) + if err != nil { + errc <- err + return + } + } + }() + }(ln) + + c, err := Dial("tcp", ln.Addr().String()) + if err != nil { + t.Fatal(err) + } + defer c.Close() + + buf := new(bytes.Buffer) + buf.ReadFrom(c) + + if want, have := "Produced ", buf.String(); have != want { + t.Errorf("unexpected server reply %q, want %q", have, want) + } + + for err := range errc { + t.Error(err) + } +} + +func TestSendfileSeeked(t *testing.T) { + ln, err := newLocalListener("tcp") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + + const seekTo = 65 << 10 + const sendSize = 10 << 10 + + errc := make(chan error, 1) + go func(ln Listener) { + // Wait for a connection. + conn, err := ln.Accept() + if err != nil { + errc <- err + close(errc) + return + } + + go func() { + defer close(errc) + defer conn.Close() + + f, err := os.Open(twain) + if err != nil { + errc <- err + return + } + defer f.Close() + if _, err := f.Seek(seekTo, os.SEEK_SET); err != nil { + errc <- err + return + } + + _, err = io.CopyN(conn, f, sendSize) + if err != nil { + errc <- err + return + } + }() + }(ln) + + c, err := Dial("tcp", ln.Addr().String()) + if err != nil { + t.Fatal(err) + } + defer c.Close() + + buf := new(bytes.Buffer) + buf.ReadFrom(c) + + if buf.Len() != sendSize { + t.Errorf("Got %d bytes; want %d", buf.Len(), sendSize) + } + + for err := range errc { + t.Error(err) + } +} |