1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:34:45 -07:00

net: pass a testing.TB to newLocal* helpers

Passing in an explicit testing.TB gives two benefits:

1. It allows the helper to fail the test itself, instead of returning
   an error to the caller. A non-nil error invariably fails the
   calling test, and none of these callers bother to add detail to the
   error when logging it anyway so returning the error just added
   noise to the test bodies.

2. It allows the helper to use t.Cleanup to perform any needed cleanup
   tasks, which will be used in CL 370695 to clean up temp directories
   used as namespaces for unix socket paths.

For #34611

Change-Id: I805e701687c12de2caca955649369294229c10b4
Reviewed-on: https://go-review.googlesource.com/c/go/+/370696
Trust: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Bryan C. Mills 2021-12-09 11:42:42 -05:00 committed by Bryan Mills
parent d198a36d8c
commit b55cbbb9e7
20 changed files with 164 additions and 389 deletions

View File

@ -26,10 +26,7 @@ func TestConnAndListener(t *testing.T) {
continue
}
ls, err := newLocalServer(network)
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, network)
defer ls.teardown()
ch := make(chan error, 1)
handler := func(ls *localServer, ln Listener) { ls.transponder(ln, ch) }

View File

@ -59,10 +59,7 @@ func TestProhibitionaryDialArg(t *testing.T) {
}
func TestDialLocal(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
_, port, err := SplitHostPort(ln.Addr().String())
if err != nil {
@ -619,13 +616,9 @@ func TestDialerLocalAddr(t *testing.T) {
c.Close()
}
}
var err error
var lss [2]*localServer
for i, network := range []string{"tcp4", "tcp6"} {
lss[i], err = newLocalServer(network)
if err != nil {
t.Fatal(err)
}
lss[i] = newLocalServer(t, network)
defer lss[i].teardown()
if err := lss[i].buildup(handler); err != nil {
t.Fatal(err)
@ -725,10 +718,7 @@ func TestDialerKeepAlive(t *testing.T) {
c.Close()
}
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -826,10 +816,7 @@ func TestCancelAfterDial(t *testing.T) {
t.Skip("avoiding time.Sleep")
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
var wg sync.WaitGroup
wg.Add(1)
@ -932,11 +919,7 @@ func TestDialerControl(t *testing.T) {
if !testableNetwork(network) {
continue
}
ln, err := newLocalListener(network)
if err != nil {
t.Error(err)
continue
}
ln := newLocalListener(t, network)
defer ln.Close()
d := Dialer{Control: controlOnConnSetup}
c, err := d.Dial(network, ln.Addr().String())
@ -952,11 +935,7 @@ func TestDialerControl(t *testing.T) {
if !testableNetwork(network) {
continue
}
c1, err := newLocalPacketListener(network)
if err != nil {
t.Error(err)
continue
}
c1 := newLocalPacketListener(t, network)
if network == "unixgram" {
defer os.Remove(c1.LocalAddr().String())
}
@ -992,10 +971,7 @@ func (contextWithNonZeroDeadline) Deadline() (time.Time, bool) {
}
func TestDialWithNonZeroDeadline(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
_, port, err := SplitHostPort(ln.Addr().String())
if err != nil {

View File

@ -31,10 +31,7 @@ func TestDialContextCancelRace(t *testing.T) {
testHookCanceledDial = oldTestHookCanceledDial
}()
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
listenerDone := make(chan struct{})
go func() {
defer close(listenerDone)

View File

@ -553,10 +553,7 @@ third:
}
func TestCloseError(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
if err != nil {
@ -664,10 +661,7 @@ func TestAcceptError(t *testing.T) {
c.Close()
}
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
if err := ls.buildup(handler); err != nil {
ls.teardown()
t.Fatal(err)
@ -773,10 +767,7 @@ func TestFileError(t *testing.T) {
t.Error("should fail")
}
ln, err = newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln = newLocalListener(t, "tcp")
for i := 0; i < 3; i++ {
f, err := ln.(*TCPListener).File()

View File

@ -44,10 +44,7 @@ func TestFileConn(t *testing.T) {
var network, address string
switch tt.network {
case "udp":
c, err := newLocalPacketListener(tt.network)
if err != nil {
t.Fatal(err)
}
c := newLocalPacketListener(t, tt.network)
defer c.Close()
network = c.LocalAddr().Network()
address = c.LocalAddr().String()
@ -61,10 +58,7 @@ func TestFileConn(t *testing.T) {
var b [1]byte
c.Read(b[:])
}
ls, err := newLocalServer(tt.network)
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, tt.network)
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -148,17 +142,17 @@ func TestFileListener(t *testing.T) {
continue
}
ln1, err := newLocalListener(tt.network)
if err != nil {
t.Fatal(err)
}
ln1 := newLocalListener(t, tt.network)
switch tt.network {
case "unix", "unixpacket":
defer os.Remove(ln1.Addr().String())
}
addr := ln1.Addr()
var f *os.File
var (
f *os.File
err error
)
switch ln1 := ln1.(type) {
case *TCPListener:
f, err = ln1.File()
@ -240,17 +234,17 @@ func TestFilePacketConn(t *testing.T) {
continue
}
c1, err := newLocalPacketListener(tt.network)
if err != nil {
t.Fatal(err)
}
c1 := newLocalPacketListener(t, tt.network)
switch tt.network {
case "unixgram":
defer os.Remove(c1.LocalAddr().String())
}
addr := c1.LocalAddr()
var f *os.File
var (
f *os.File
err error
)
switch c1 := c1.(type) {
case *UDPConn:
f, err = c1.File()
@ -314,10 +308,7 @@ func TestFileCloseRace(t *testing.T) {
c.Read(b[:])
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)

View File

@ -697,10 +697,7 @@ func multicastRIBContains(ip IP) (bool, error) {
// Issue 21856.
func TestClosingListener(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
addr := ln.Addr()
go func() {
@ -738,15 +735,13 @@ func TestListenConfigControl(t *testing.T) {
if !testableNetwork(network) {
continue
}
ln, err := newLocalListener(network)
if err != nil {
t.Error(err)
continue
}
ln := newLocalListener(t, network)
address := ln.Addr().String()
// TODO: This is racy. The selected address could be reused in between
// this Close and the subsequent Listen.
ln.Close()
lc := ListenConfig{Control: controlOnConnSetup}
ln, err = lc.Listen(context.Background(), network, address)
ln, err := lc.Listen(context.Background(), network, address)
if err != nil {
t.Error(err)
continue
@ -759,18 +754,16 @@ func TestListenConfigControl(t *testing.T) {
if !testableNetwork(network) {
continue
}
c, err := newLocalPacketListener(network)
if err != nil {
t.Error(err)
continue
}
c := newLocalPacketListener(t, network)
address := c.LocalAddr().String()
// TODO: This is racy. The selected address could be reused in between
// this Close and the subsequent ListenPacket.
c.Close()
if network == "unixgram" {
os.Remove(address)
}
lc := ListenConfig{Control: controlOnConnSetup}
c, err = lc.ListenPacket(context.Background(), network, address)
c, err := lc.ListenPacket(context.Background(), network, address)
if err != nil {
t.Error(err)
continue

View File

@ -11,6 +11,7 @@ import (
"fmt"
"os"
"sync"
"testing"
"time"
)
@ -26,29 +27,44 @@ func testUnixAddr() string {
return addr
}
func newLocalListener(network string) (Listener, error) {
func newLocalListener(t testing.TB, network string) Listener {
listen := func(net, addr string) Listener {
ln, err := Listen(net, addr)
if err != nil {
t.Helper()
t.Fatal(err)
}
return ln
}
switch network {
case "tcp":
if supportsIPv4() {
if !supportsIPv6() {
return listen("tcp4", "127.0.0.1:0")
}
if ln, err := Listen("tcp4", "127.0.0.1:0"); err == nil {
return ln, nil
return ln
}
}
if supportsIPv6() {
return Listen("tcp6", "[::1]:0")
return listen("tcp6", "[::1]:0")
}
case "tcp4":
if supportsIPv4() {
return Listen("tcp4", "127.0.0.1:0")
return listen("tcp4", "127.0.0.1:0")
}
case "tcp6":
if supportsIPv6() {
return Listen("tcp6", "[::1]:0")
return listen("tcp6", "[::1]:0")
}
case "unix", "unixpacket":
return Listen(network, testUnixAddr())
return listen(network, testUnixAddr())
}
return nil, fmt.Errorf("%s is not supported", network)
t.Helper()
t.Fatalf("%s is not supported", network)
return nil
}
func newDualStackListener() (lns []*TCPListener, err error) {
@ -119,12 +135,10 @@ func (ls *localServer) teardown() error {
return nil
}
func newLocalServer(network string) (*localServer, error) {
ln, err := newLocalListener(network)
if err != nil {
return nil, err
}
return &localServer{Listener: ln, done: make(chan bool)}, nil
func newLocalServer(t testing.TB, network string) *localServer {
t.Helper()
ln := newLocalListener(t, network)
return &localServer{Listener: ln, done: make(chan bool)}
}
type streamListener struct {
@ -133,8 +147,8 @@ type streamListener struct {
done chan bool // signal that indicates server stopped
}
func (sl *streamListener) newLocalServer() (*localServer, error) {
return &localServer{Listener: sl.Listener, done: make(chan bool)}, nil
func (sl *streamListener) newLocalServer() *localServer {
return &localServer{Listener: sl.Listener, done: make(chan bool)}
}
type dualStackServer struct {
@ -286,27 +300,39 @@ func transceiver(c Conn, wb []byte, ch chan<- error) {
}
}
func newLocalPacketListener(network string) (PacketConn, error) {
func newLocalPacketListener(t testing.TB, network string) PacketConn {
listenPacket := func(net, addr string) PacketConn {
c, err := ListenPacket(net, addr)
if err != nil {
t.Helper()
t.Fatal(err)
}
return c
}
switch network {
case "udp":
if supportsIPv4() {
return ListenPacket("udp4", "127.0.0.1:0")
return listenPacket("udp4", "127.0.0.1:0")
}
if supportsIPv6() {
return ListenPacket("udp6", "[::1]:0")
return listenPacket("udp6", "[::1]:0")
}
case "udp4":
if supportsIPv4() {
return ListenPacket("udp4", "127.0.0.1:0")
return listenPacket("udp4", "127.0.0.1:0")
}
case "udp6":
if supportsIPv6() {
return ListenPacket("udp6", "[::1]:0")
return listenPacket("udp6", "[::1]:0")
}
case "unixgram":
return ListenPacket(network, testUnixAddr())
return listenPacket(network, testUnixAddr())
}
return nil, fmt.Errorf("%s is not supported", network)
t.Helper()
t.Fatalf("%s is not supported", network)
return nil
}
func newDualStackPacketListener() (cs []*UDPConn, err error) {
@ -371,20 +397,18 @@ func (ls *localPacketServer) teardown() error {
return nil
}
func newLocalPacketServer(network string) (*localPacketServer, error) {
c, err := newLocalPacketListener(network)
if err != nil {
return nil, err
}
return &localPacketServer{PacketConn: c, done: make(chan bool)}, nil
func newLocalPacketServer(t testing.TB, network string) *localPacketServer {
t.Helper()
c := newLocalPacketListener(t, network)
return &localPacketServer{PacketConn: c, done: make(chan bool)}
}
type packetListener struct {
PacketConn
}
func (pl *packetListener) newLocalServer() (*localPacketServer, error) {
return &localPacketServer{PacketConn: pl.PacketConn, done: make(chan bool)}, nil
func (pl *packetListener) newLocalServer() *localPacketServer {
return &localPacketServer{PacketConn: pl.PacketConn, done: make(chan bool)}
}
func packetTransponder(c PacketConn, ch chan<- error) {

View File

@ -33,10 +33,7 @@ func TestCloseRead(t *testing.T) {
}
t.Parallel()
ln, err := newLocalListener(network)
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, network)
switch network {
case "unix", "unixpacket":
defer os.Remove(ln.Addr().String())
@ -132,10 +129,7 @@ func TestCloseWrite(t *testing.T) {
}
}
ls, err := newLocalServer(network)
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, network)
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -189,10 +183,7 @@ func TestConnClose(t *testing.T) {
}
t.Parallel()
ln, err := newLocalListener(network)
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, network)
switch network {
case "unix", "unixpacket":
defer os.Remove(ln.Addr().String())
@ -234,10 +225,7 @@ func TestListenerClose(t *testing.T) {
}
t.Parallel()
ln, err := newLocalListener(network)
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, network)
switch network {
case "unix", "unixpacket":
defer os.Remove(ln.Addr().String())
@ -275,10 +263,7 @@ func TestPacketConnClose(t *testing.T) {
}
t.Parallel()
c, err := newLocalPacketListener(network)
if err != nil {
t.Fatal(err)
}
c := newLocalPacketListener(t, network)
switch network {
case "unixgram":
defer os.Remove(c.LocalAddr().String())
@ -303,18 +288,17 @@ func TestPacketConnClose(t *testing.T) {
func TestListenCloseListen(t *testing.T) {
const maxTries = 10
for tries := 0; tries < maxTries; tries++ {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
addr := ln.Addr().String()
// TODO: This is racy. The selected address could be reused in between this
// Close and the subsequent Listen.
if err := ln.Close(); err != nil {
if perr := parseCloseError(err, false); perr != nil {
t.Error(perr)
}
t.Fatal(err)
}
ln, err = Listen("tcp", addr)
ln, err := Listen("tcp", addr)
if err == nil {
// Success. (This test didn't always make it here earlier.)
ln.Close()
@ -360,10 +344,7 @@ func TestAcceptIgnoreAbortedConnRequest(t *testing.T) {
}
c.Close()
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -390,10 +371,7 @@ func TestZeroByteRead(t *testing.T) {
}
t.Parallel()
ln, err := newLocalListener(network)
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, network)
connc := make(chan Conn, 1)
go func() {
defer ln.Close()
@ -442,10 +420,7 @@ func TestZeroByteRead(t *testing.T) {
// runs peer1 and peer2 concurrently. withTCPConnPair returns when
// both have completed.
func withTCPConnPair(t *testing.T, peer1, peer2 func(c *TCPConn) error) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
errc := make(chan error, 2)
go func() {

View File

@ -73,10 +73,7 @@ func TestTCPConnSpecificMethods(t *testing.T) {
}
ch := make(chan error, 1)
handler := func(ls *localServer, ln Listener) { ls.transponder(ls.Listener, ch) }
ls, err := (&streamListener{Listener: ln}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&streamListener{Listener: ln}).newLocalServer()
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)

View File

@ -64,10 +64,7 @@ func TestRawConnReadWrite(t *testing.T) {
return
}
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -102,10 +99,7 @@ func TestRawConnReadWrite(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
@ -180,10 +174,7 @@ func TestRawConnControl(t *testing.T) {
}
t.Run("TCP", func(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
cc1, err := ln.(*TCPListener).SyscallConn()

View File

@ -27,10 +27,7 @@ const (
)
func TestSendfile(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
errc := make(chan error, 1)
@ -97,10 +94,7 @@ func TestSendfile(t *testing.T) {
}
func TestSendfileParts(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
errc := make(chan error, 1)
@ -155,10 +149,7 @@ func TestSendfileParts(t *testing.T) {
}
func TestSendfileSeeked(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
const seekTo = 65 << 10
@ -225,10 +216,7 @@ func TestSendfilePipe(t *testing.T) {
t.Parallel()
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
r, w, err := os.Pipe()
@ -317,10 +305,7 @@ func TestSendfilePipe(t *testing.T) {
// Issue 43822: tests that returns EOF when conn write timeout.
func TestSendfileOnWriteTimeoutExceeded(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
errc := make(chan error, 1)

View File

@ -77,10 +77,7 @@ func TestTCPServer(t *testing.T) {
}
}()
for i := 0; i < N; i++ {
ls, err := (&streamListener{Listener: ln}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&streamListener{Listener: ln}).newLocalServer()
lss = append(lss, ls)
tpchs = append(tpchs, make(chan error, 1))
}
@ -162,10 +159,7 @@ func TestUnixAndUnixpacketServer(t *testing.T) {
}
}()
for i := 0; i < N; i++ {
ls, err := (&streamListener{Listener: ln}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&streamListener{Listener: ln}).newLocalServer()
lss = append(lss, ls)
tpchs = append(tpchs, make(chan error, 1))
}
@ -270,10 +264,7 @@ func TestUDPServer(t *testing.T) {
t.Fatal(err)
}
ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&packetListener{PacketConn: c1}).newLocalServer()
defer ls.teardown()
tpch := make(chan error, 1)
handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }
@ -348,10 +339,7 @@ func TestUnixgramServer(t *testing.T) {
t.Fatal(err)
}
ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&packetListener{PacketConn: c1}).newLocalServer()
defer ls.teardown()
tpch := make(chan error, 1)
handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, tpch) }

View File

@ -46,20 +46,14 @@ type spliceTestCase struct {
}
func (tc spliceTestCase) test(t *testing.T) {
clientUp, serverUp, err := spliceTestSocketPair(tc.upNet)
if err != nil {
t.Fatal(err)
}
clientUp, serverUp := spliceTestSocketPair(t, tc.upNet)
defer serverUp.Close()
cleanup, err := startSpliceClient(clientUp, "w", tc.chunkSize, tc.totalSize)
if err != nil {
t.Fatal(err)
}
defer cleanup()
clientDown, serverDown, err := spliceTestSocketPair(tc.downNet)
if err != nil {
t.Fatal(err)
}
clientDown, serverDown := spliceTestSocketPair(t, tc.downNet)
defer serverDown.Close()
cleanup, err = startSpliceClient(clientDown, "r", tc.chunkSize, tc.totalSize)
if err != nil {
@ -103,15 +97,9 @@ func (tc spliceTestCase) test(t *testing.T) {
}
func testSpliceReaderAtEOF(t *testing.T, upNet, downNet string) {
clientUp, serverUp, err := spliceTestSocketPair(upNet)
if err != nil {
t.Fatal(err)
}
clientUp, serverUp := spliceTestSocketPair(t, upNet)
defer clientUp.Close()
clientDown, serverDown, err := spliceTestSocketPair(downNet)
if err != nil {
t.Fatal(err)
}
clientDown, serverDown := spliceTestSocketPair(t, downNet)
defer clientDown.Close()
serverUp.Close()
@ -140,7 +128,7 @@ func testSpliceReaderAtEOF(t *testing.T, upNet, downNet string) {
}()
buf := make([]byte, 3)
_, err = io.ReadFull(clientDown, buf)
_, err := io.ReadFull(clientDown, buf)
if err != nil {
t.Errorf("clientDown: %v", err)
}
@ -150,15 +138,9 @@ func testSpliceReaderAtEOF(t *testing.T, upNet, downNet string) {
}
func testSpliceIssue25985(t *testing.T, upNet, downNet string) {
front, err := newLocalListener(upNet)
if err != nil {
t.Fatal(err)
}
front := newLocalListener(t, upNet)
defer front.Close()
back, err := newLocalListener(downNet)
if err != nil {
t.Fatal(err)
}
back := newLocalListener(t, downNet)
defer back.Close()
var wg sync.WaitGroup
@ -210,16 +192,10 @@ func testSpliceIssue25985(t *testing.T, upNet, downNet string) {
}
func testSpliceNoUnixpacket(t *testing.T) {
clientUp, serverUp, err := spliceTestSocketPair("unixpacket")
if err != nil {
t.Fatal(err)
}
clientUp, serverUp := spliceTestSocketPair(t, "unixpacket")
defer clientUp.Close()
defer serverUp.Close()
clientDown, serverDown, err := spliceTestSocketPair("tcp")
if err != nil {
t.Fatal(err)
}
clientDown, serverDown := spliceTestSocketPair(t, "tcp")
defer clientDown.Close()
defer serverDown.Close()
// If splice called poll.Splice here, we'd get err == syscall.EINVAL
@ -247,10 +223,7 @@ func testSpliceNoUnixgram(t *testing.T) {
t.Fatal(err)
}
defer up.Close()
clientDown, serverDown, err := spliceTestSocketPair("tcp")
if err != nil {
t.Fatal(err)
}
clientDown, serverDown := spliceTestSocketPair(t, "tcp")
defer clientDown.Close()
defer serverDown.Close()
// Analogous to testSpliceNoUnixpacket.
@ -284,10 +257,7 @@ func (tc spliceTestCase) bench(b *testing.B) {
// To benchmark the genericReadFrom code path, set this to false.
useSplice := true
clientUp, serverUp, err := spliceTestSocketPair(tc.upNet)
if err != nil {
b.Fatal(err)
}
clientUp, serverUp := spliceTestSocketPair(b, tc.upNet)
defer serverUp.Close()
cleanup, err := startSpliceClient(clientUp, "w", tc.chunkSize, tc.chunkSize*b.N)
@ -296,10 +266,7 @@ func (tc spliceTestCase) bench(b *testing.B) {
}
defer cleanup()
clientDown, serverDown, err := spliceTestSocketPair(tc.downNet)
if err != nil {
b.Fatal(err)
}
clientDown, serverDown := spliceTestSocketPair(b, tc.downNet)
defer serverDown.Close()
cleanup, err = startSpliceClient(clientDown, "r", tc.chunkSize, tc.chunkSize*b.N)
@ -327,11 +294,9 @@ func (tc spliceTestCase) bench(b *testing.B) {
}
}
func spliceTestSocketPair(net string) (client, server Conn, err error) {
ln, err := newLocalListener(net)
if err != nil {
return nil, nil, err
}
func spliceTestSocketPair(t testing.TB, net string) (client, server Conn) {
t.Helper()
ln := newLocalListener(t, net)
defer ln.Close()
var cerr, serr error
acceptDone := make(chan struct{})
@ -345,15 +310,15 @@ func spliceTestSocketPair(net string) (client, server Conn, err error) {
if server != nil {
server.Close()
}
return nil, nil, cerr
t.Fatal(cerr)
}
if serr != nil {
if client != nil {
client.Close()
}
return nil, nil, serr
t.Fatal(serr)
}
return client, server, nil
return client, server
}
func startSpliceClient(conn Conn, op string, chunkSize, totalSize int) (func(), error) {

View File

@ -387,10 +387,7 @@ func TestIPv6LinkLocalUnicastTCP(t *testing.T) {
t.Log(err)
continue
}
ls, err := (&streamListener{Listener: ln}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&streamListener{Listener: ln}).newLocalServer()
defer ls.teardown()
ch := make(chan error, 1)
handler := func(ls *localServer, ln Listener) { ls.transponder(ln, ch) }
@ -626,10 +623,7 @@ func TestTCPSelfConnect(t *testing.T) {
t.Skip("known-broken test on windows")
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
var d Dialer
c, err := d.Dial(ln.Addr().Network(), ln.Addr().String())
if err != nil {
@ -676,10 +670,7 @@ func TestTCPBig(t *testing.T) {
for _, writev := range []bool{false, true} {
t.Run(fmt.Sprintf("writev=%v", writev), func(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
x := int(1 << 30)
@ -723,10 +714,7 @@ func TestTCPBig(t *testing.T) {
}
func TestCopyPipeIntoTCP(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
errc := make(chan error, 1)
@ -794,10 +782,7 @@ func TestCopyPipeIntoTCP(t *testing.T) {
}
func BenchmarkSetReadDeadline(b *testing.B) {
ln, err := newLocalListener("tcp")
if err != nil {
b.Fatal(err)
}
ln := newLocalListener(b, "tcp")
defer ln.Close()
var serv Conn
done := make(chan error)

View File

@ -22,10 +22,7 @@ func TestTCPSpuriousConnSetupCompletion(t *testing.T) {
t.Skip("skipping in short mode")
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
var wg sync.WaitGroup
wg.Add(1)
go func(ln Listener) {

View File

@ -93,10 +93,7 @@ func TestDialTimeout(t *testing.T) {
}
func TestDialTimeoutMaxDuration(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer func() {
if err := ln.Close(); err != nil {
t.Error(err)
@ -147,10 +144,7 @@ func TestAcceptTimeout(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
var wg sync.WaitGroup
@ -203,10 +197,7 @@ func TestAcceptTimeoutMustReturn(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
max := time.NewTimer(time.Second)
@ -249,10 +240,7 @@ func TestAcceptTimeoutMustNotReturn(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
max := time.NewTimer(100 * time.Millisecond)
@ -302,10 +290,7 @@ func TestReadTimeout(t *testing.T) {
c.Write([]byte("READ TIMEOUT TEST"))
defer c.Close()
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -354,10 +339,7 @@ func TestReadTimeoutMustNotReturn(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
@ -421,10 +403,7 @@ func TestReadFromTimeout(t *testing.T) {
c.WriteTo([]byte("READFROM TIMEOUT TEST"), dst)
}
}
ls, err := newLocalPacketServer("udp")
if err != nil {
t.Fatal(err)
}
ls := newLocalPacketServer(t, "udp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -484,10 +463,7 @@ var writeTimeoutTests = []struct {
func TestWriteTimeout(t *testing.T) {
t.Parallel()
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
for i, tt := range writeTimeoutTests {
@ -532,10 +508,7 @@ func TestWriteTimeoutMustNotReturn(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
@ -598,10 +571,7 @@ var writeToTimeoutTests = []struct {
func TestWriteToTimeout(t *testing.T) {
t.Parallel()
c1, err := newLocalPacketListener("udp")
if err != nil {
t.Fatal(err)
}
c1 := newLocalPacketListener(t, "udp")
defer c1.Close()
host, _, err := SplitHostPort(c1.LocalAddr().String())
@ -687,10 +657,7 @@ func nextTimeout(actual time.Duration) (next time.Duration, ok bool) {
}
func TestReadTimeoutFluctuation(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
@ -746,10 +713,7 @@ func TestReadTimeoutFluctuation(t *testing.T) {
}
func TestReadFromTimeoutFluctuation(t *testing.T) {
c1, err := newLocalPacketListener("udp")
if err != nil {
t.Fatal(err)
}
c1 := newLocalPacketListener(t, "udp")
defer c1.Close()
c2, err := Dial(c1.LocalAddr().Network(), c1.LocalAddr().String())
@ -810,10 +774,7 @@ func TestWriteTimeoutFluctuation(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
@ -938,10 +899,7 @@ func testVariousDeadlines(t *testing.T) {
c.Close()
}
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -1073,10 +1031,7 @@ func TestReadWriteProlongedTimeout(t *testing.T) {
}()
wg.Wait()
}
ls, err := newLocalServer("tcp")
if err != nil {
t.Fatal(err)
}
ls := newLocalServer(t, "tcp")
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)
@ -1103,10 +1058,7 @@ func TestReadWriteDeadlineRace(t *testing.T) {
N = 50
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
c, err := Dial(ln.Addr().Network(), ln.Addr().String())
@ -1156,10 +1108,7 @@ func TestReadWriteDeadlineRace(t *testing.T) {
// Issue 35367.
func TestConcurrentSetDeadline(t *testing.T) {
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
const goroutines = 8
@ -1168,6 +1117,7 @@ func TestConcurrentSetDeadline(t *testing.T) {
var c [conns]Conn
for i := 0; i < conns; i++ {
var err error
c[i], err = Dial(ln.Addr().Network(), ln.Addr().String())
if err != nil {
t.Fatal(err)

View File

@ -285,10 +285,7 @@ func TestIPv6LinkLocalUnicastUDP(t *testing.T) {
t.Log(err)
continue
}
ls, err := (&packetListener{PacketConn: c1}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&packetListener{PacketConn: c1}).newLocalServer()
defer ls.teardown()
ch := make(chan error, 1)
handler := func(ls *localPacketServer, c PacketConn) { packetTransponder(c, ch) }
@ -333,10 +330,7 @@ func TestUDPZeroBytePayload(t *testing.T) {
testenv.SkipFlaky(t, 29225)
}
c, err := newLocalPacketListener("udp")
if err != nil {
t.Fatal(err)
}
c := newLocalPacketListener(t, "udp")
defer c.Close()
for _, genericRead := range []bool{false, true} {
@ -369,10 +363,7 @@ func TestUDPZeroByteBuffer(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
c, err := newLocalPacketListener("udp")
if err != nil {
t.Fatal(err)
}
c := newLocalPacketListener(t, "udp")
defer c.Close()
b := []byte("UDP ZERO BYTE BUFFER TEST")
@ -406,10 +397,7 @@ func TestUDPReadSizeError(t *testing.T) {
t.Skipf("not supported on %s", runtime.GOOS)
}
c1, err := newLocalPacketListener("udp")
if err != nil {
t.Fatal(err)
}
c1 := newLocalPacketListener(t, "udp")
defer c1.Close()
c2, err := Dial("udp", c1.LocalAddr().String())

View File

@ -76,10 +76,7 @@ func TestUnixgramZeroBytePayload(t *testing.T) {
t.Skip("unixgram test")
}
c1, err := newLocalPacketListener("unixgram")
if err != nil {
t.Fatal(err)
}
c1 := newLocalPacketListener(t, "unixgram")
defer os.Remove(c1.LocalAddr().String())
defer c1.Close()
@ -126,10 +123,7 @@ func TestUnixgramZeroByteBuffer(t *testing.T) {
// issue 4352: Recvfrom failed with "address family not
// supported by protocol family" if zero-length buffer provided
c1, err := newLocalPacketListener("unixgram")
if err != nil {
t.Fatal(err)
}
c1 := newLocalPacketListener(t, "unixgram")
defer os.Remove(c1.LocalAddr().String())
defer c1.Close()
@ -259,10 +253,7 @@ func TestUnixConnLocalAndRemoteNames(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ls, err := (&streamListener{Listener: ln}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&streamListener{Listener: ln}).newLocalServer()
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)

View File

@ -56,10 +56,7 @@ func TestUnixConnLocalWindows(t *testing.T) {
if err != nil {
t.Fatal(err)
}
ls, err := (&streamListener{Listener: ln}).newLocalServer()
if err != nil {
t.Fatal(err)
}
ls := (&streamListener{Listener: ln}).newLocalServer()
defer ls.teardown()
if err := ls.buildup(handler); err != nil {
t.Fatal(err)

View File

@ -186,10 +186,7 @@ func TestWritevError(t *testing.T) {
t.Skipf("skipping the test: windows does not have problem sending large chunks of data")
}
ln, err := newLocalListener("tcp")
if err != nil {
t.Fatal(err)
}
ln := newLocalListener(t, "tcp")
defer ln.Close()
ch := make(chan Conn, 1)