readwrite_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2012 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. package ipv4_test
  5. import (
  6. "bytes"
  7. "net"
  8. "runtime"
  9. "strings"
  10. "sync"
  11. "testing"
  12. "golang.org/x/net/internal/nettest"
  13. "golang.org/x/net/ipv4"
  14. )
  15. func benchmarkUDPListener() (net.PacketConn, net.Addr, error) {
  16. c, err := net.ListenPacket("udp4", "127.0.0.1:0")
  17. if err != nil {
  18. return nil, nil, err
  19. }
  20. dst, err := net.ResolveUDPAddr("udp4", c.LocalAddr().String())
  21. if err != nil {
  22. c.Close()
  23. return nil, nil, err
  24. }
  25. return c, dst, nil
  26. }
  27. func BenchmarkReadWriteNetUDP(b *testing.B) {
  28. c, dst, err := benchmarkUDPListener()
  29. if err != nil {
  30. b.Fatal(err)
  31. }
  32. defer c.Close()
  33. wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
  34. b.ResetTimer()
  35. for i := 0; i < b.N; i++ {
  36. benchmarkReadWriteNetUDP(b, c, wb, rb, dst)
  37. }
  38. }
  39. func benchmarkReadWriteNetUDP(b *testing.B, c net.PacketConn, wb, rb []byte, dst net.Addr) {
  40. if _, err := c.WriteTo(wb, dst); err != nil {
  41. b.Fatal(err)
  42. }
  43. if _, _, err := c.ReadFrom(rb); err != nil {
  44. b.Fatal(err)
  45. }
  46. }
  47. func BenchmarkReadWriteIPv4UDP(b *testing.B) {
  48. c, dst, err := benchmarkUDPListener()
  49. if err != nil {
  50. b.Fatal(err)
  51. }
  52. defer c.Close()
  53. p := ipv4.NewPacketConn(c)
  54. defer p.Close()
  55. cf := ipv4.FlagTTL | ipv4.FlagInterface
  56. if err := p.SetControlMessage(cf, true); err != nil {
  57. b.Fatal(err)
  58. }
  59. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  60. wb, rb := []byte("HELLO-R-U-THERE"), make([]byte, 128)
  61. b.ResetTimer()
  62. for i := 0; i < b.N; i++ {
  63. benchmarkReadWriteIPv4UDP(b, p, wb, rb, dst, ifi)
  64. }
  65. }
  66. func benchmarkReadWriteIPv4UDP(b *testing.B, p *ipv4.PacketConn, wb, rb []byte, dst net.Addr, ifi *net.Interface) {
  67. cm := ipv4.ControlMessage{TTL: 1}
  68. if ifi != nil {
  69. cm.IfIndex = ifi.Index
  70. }
  71. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  72. b.Fatal(err)
  73. } else if n != len(wb) {
  74. b.Fatalf("got %v; want %v", n, len(wb))
  75. }
  76. if _, _, _, err := p.ReadFrom(rb); err != nil {
  77. b.Fatal(err)
  78. }
  79. }
  80. func TestPacketConnConcurrentReadWriteUnicastUDP(t *testing.T) {
  81. switch runtime.GOOS {
  82. case "nacl", "plan9", "windows":
  83. t.Skipf("not supported on %s", runtime.GOOS)
  84. }
  85. c, err := net.ListenPacket("udp4", "127.0.0.1:0")
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. defer c.Close()
  90. p := ipv4.NewPacketConn(c)
  91. defer p.Close()
  92. dst, err := net.ResolveUDPAddr("udp4", c.LocalAddr().String())
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
  97. cf := ipv4.FlagTTL | ipv4.FlagSrc | ipv4.FlagDst | ipv4.FlagInterface
  98. wb := []byte("HELLO-R-U-THERE")
  99. if err := p.SetControlMessage(cf, true); err != nil { // probe before test
  100. if nettest.ProtocolNotSupported(err) {
  101. t.Skipf("not supported on %s", runtime.GOOS)
  102. }
  103. t.Fatal(err)
  104. }
  105. var wg sync.WaitGroup
  106. reader := func() {
  107. defer wg.Done()
  108. rb := make([]byte, 128)
  109. if n, cm, _, err := p.ReadFrom(rb); err != nil {
  110. t.Error(err)
  111. return
  112. } else if !bytes.Equal(rb[:n], wb) {
  113. t.Errorf("got %v; want %v", rb[:n], wb)
  114. return
  115. } else {
  116. s := cm.String()
  117. if strings.Contains(s, ",") {
  118. t.Errorf("should be space-separated values: %s", s)
  119. }
  120. }
  121. }
  122. writer := func(toggle bool) {
  123. defer wg.Done()
  124. cm := ipv4.ControlMessage{
  125. Src: net.IPv4(127, 0, 0, 1),
  126. }
  127. if ifi != nil {
  128. cm.IfIndex = ifi.Index
  129. }
  130. if err := p.SetControlMessage(cf, toggle); err != nil {
  131. t.Error(err)
  132. return
  133. }
  134. if n, err := p.WriteTo(wb, &cm, dst); err != nil {
  135. t.Error(err)
  136. return
  137. } else if n != len(wb) {
  138. t.Errorf("short write: %v", n)
  139. return
  140. }
  141. }
  142. const N = 10
  143. wg.Add(N)
  144. for i := 0; i < N; i++ {
  145. go reader()
  146. }
  147. wg.Add(2 * N)
  148. for i := 0; i < 2*N; i++ {
  149. go writer(i%2 != 0)
  150. }
  151. wg.Add(N)
  152. for i := 0; i < N; i++ {
  153. go reader()
  154. }
  155. wg.Wait()
  156. }