mirror of
https://github.com/golang/net.git
synced 2026-03-31 18:37:08 +09:00
Now that the x/net module requires Go 1.25.0, the go1.25 build constraint is always satisfied. Simplify the code accordingly. Change-Id: I3d6fe4a132a26918455489b998730b494f5273c4 Reviewed-on: https://go-review.googlesource.com/c/net/+/744800 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Nicholas Husin <nsh@golang.org> Reviewed-by: Nicholas Husin <husin@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
// Copyright 2016 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package nettest
|
|
|
|
import (
|
|
"net"
|
|
"os"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestTestConn(t *testing.T) {
|
|
tests := []struct{ name, network string }{
|
|
{"TCP", "tcp"},
|
|
{"UnixPipe", "unix"},
|
|
{"UnixPacketPipe", "unixpacket"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if !TestableNetwork(tt.network) {
|
|
t.Skipf("%s not supported on %s/%s", tt.network, runtime.GOOS, runtime.GOARCH)
|
|
}
|
|
|
|
mp := func() (c1, c2 net.Conn, stop func(), err error) {
|
|
ln, err := NewLocalListener(tt.network)
|
|
if err != nil {
|
|
return nil, nil, nil, err
|
|
}
|
|
|
|
// Start a connection between two endpoints.
|
|
var err1, err2 error
|
|
done := make(chan bool)
|
|
go func() {
|
|
c2, err2 = ln.Accept()
|
|
close(done)
|
|
}()
|
|
c1, err1 = net.Dial(ln.Addr().Network(), ln.Addr().String())
|
|
<-done
|
|
|
|
stop = func() {
|
|
if err1 == nil {
|
|
c1.Close()
|
|
}
|
|
if err2 == nil {
|
|
c2.Close()
|
|
}
|
|
ln.Close()
|
|
switch tt.network {
|
|
case "unix", "unixpacket":
|
|
os.Remove(ln.Addr().String())
|
|
}
|
|
}
|
|
|
|
switch {
|
|
case err1 != nil:
|
|
stop()
|
|
return nil, nil, nil, err1
|
|
case err2 != nil:
|
|
stop()
|
|
return nil, nil, nil, err2
|
|
default:
|
|
return c1, c2, stop, nil
|
|
}
|
|
}
|
|
|
|
TestConn(t, mp)
|
|
})
|
|
}
|
|
}
|