github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/nettest/conntest_test.go (about) 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. 4 5 //go:build go1.8 6 // +build go1.8 7 8 package nettest 9 10 import ( 11 "net" 12 "os" 13 "runtime" 14 "testing" 15 ) 16 17 func TestTestConn(t *testing.T) { 18 tests := []struct{ name, network string }{ 19 {"TCP", "tcp"}, 20 {"UnixPipe", "unix"}, 21 {"UnixPacketPipe", "unixpacket"}, 22 } 23 24 for _, tt := range tests { 25 t.Run(tt.name, func(t *testing.T) { 26 if !TestableNetwork(tt.network) { 27 t.Skipf("%s not supported on %s/%s", tt.network, runtime.GOOS, runtime.GOARCH) 28 } 29 30 mp := func() (c1, c2 net.Conn, stop func(), err error) { 31 ln, err := NewLocalListener(tt.network) 32 if err != nil { 33 return nil, nil, nil, err 34 } 35 36 // Start a connection between two endpoints. 37 var err1, err2 error 38 done := make(chan bool) 39 go func() { 40 c2, err2 = ln.Accept() 41 close(done) 42 }() 43 c1, err1 = net.Dial(ln.Addr().Network(), ln.Addr().String()) 44 <-done 45 46 stop = func() { 47 if err1 == nil { 48 c1.Close() 49 } 50 if err2 == nil { 51 c2.Close() 52 } 53 ln.Close() 54 switch tt.network { 55 case "unix", "unixpacket": 56 os.Remove(ln.Addr().String()) 57 } 58 } 59 60 switch { 61 case err1 != nil: 62 stop() 63 return nil, nil, nil, err1 64 case err2 != nil: 65 stop() 66 return nil, nil, nil, err2 67 default: 68 return c1, c2, stop, nil 69 } 70 } 71 72 TestConn(t, mp) 73 }) 74 } 75 }