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