You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

77 lines
1.5 KiB

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