nonblock_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // +build darwin linux
  2. /*
  3. * Copyright (c) 2017, Psiphon Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package tun
  21. import (
  22. "bytes"
  23. "fmt"
  24. "io"
  25. "math/rand"
  26. "sync"
  27. "syscall"
  28. "testing"
  29. )
  30. func TestNonblockingIO(t *testing.T) {
  31. // Exercise NonblockingIO Read/Write/Close concurrency
  32. // and interruption by opening a socket pair and relaying
  33. // data in both directions. Each side has a reader and a
  34. // writer, for a total of four goroutines performing
  35. // concurrent I/O.
  36. //
  37. // Reader/writer peers use a common PRNG seed to generate
  38. // the same stream of bytes to the reader can check that
  39. // the writer sent the expected stream of bytes.
  40. //
  41. // The test is repeated for a number of iterations. For
  42. // half the iterations, th test wait only for the midpoint
  43. // of communication, so the Close calls will interrupt
  44. // active readers and writers. For the other half, wait
  45. // for the endpoint, so the readers have received all the
  46. // expected data from the writers and are waiting to read
  47. // EOF.
  48. iterations := 10
  49. maxIO := 32768
  50. messages := 1000
  51. for iteration := 0; iteration < iterations; iteration++ {
  52. fds, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
  53. if err != nil {
  54. t.Fatalf("Socketpair failed: %s", err)
  55. }
  56. nio0, err := NewNonblockingIO(fds[0])
  57. if err != nil {
  58. t.Fatalf("NewNonblockingIO failed: %s", err)
  59. }
  60. nio1, err := NewNonblockingIO(fds[1])
  61. if err != nil {
  62. t.Fatalf("NewNonblockingIO failed: %s", err)
  63. }
  64. syscall.Close(fds[0])
  65. syscall.Close(fds[1])
  66. readers := new(sync.WaitGroup)
  67. readersMidpoint := new(sync.WaitGroup)
  68. readersEndpoint := new(sync.WaitGroup)
  69. writers := new(sync.WaitGroup)
  70. reader := func(r io.Reader, isClosed func() bool, seed int) {
  71. defer readers.Done()
  72. prng := rand.New(rand.NewSource(int64(seed)))
  73. expectedData := make([]byte, maxIO)
  74. data := make([]byte, maxIO)
  75. for i := 0; i < messages; i++ {
  76. if i%(messages/10) == 0 {
  77. fmt.Printf("#%d: %d/%d\n", seed, i, messages)
  78. }
  79. if i == messages/2 {
  80. readersMidpoint.Done()
  81. }
  82. n := int(1 + prng.Int31n(int32(maxIO)))
  83. prng.Read(expectedData[:n])
  84. _, err := io.ReadFull(r, data[:n])
  85. if err != nil {
  86. if isClosed() {
  87. return
  88. }
  89. t.Fatalf("io.ReadFull failed: %s", err)
  90. }
  91. if bytes.Compare(expectedData[:n], data[:n]) != 0 {
  92. t.Fatalf("bytes.Compare failed")
  93. }
  94. }
  95. readersEndpoint.Done()
  96. n, err := r.Read(data)
  97. for n == 0 && err == nil {
  98. n, err = r.Read(data)
  99. }
  100. if n != 0 || err != io.EOF {
  101. t.Fatalf("exected io.EOF failed")
  102. }
  103. }
  104. writer := func(w io.Writer, isClosed func() bool, seed int) {
  105. defer writers.Done()
  106. prng := rand.New(rand.NewSource(int64(seed)))
  107. data := make([]byte, maxIO)
  108. for i := 0; i < messages; i++ {
  109. n := int(1 + prng.Int31n(int32(maxIO)))
  110. prng.Read(data[:n])
  111. m, err := w.Write(data[:n])
  112. if err != nil {
  113. if isClosed() {
  114. return
  115. }
  116. t.Fatalf("w.Write failed: %s", err)
  117. }
  118. if m != n {
  119. t.Fatalf("w.Write failed: unexpected number of bytes written")
  120. }
  121. }
  122. }
  123. isClosed := func() bool {
  124. return nio0.IsClosed() || nio1.IsClosed()
  125. }
  126. readers.Add(2)
  127. readersMidpoint.Add(2)
  128. readersEndpoint.Add(2)
  129. go reader(nio0, isClosed, 0)
  130. go reader(nio1, isClosed, 1)
  131. writers.Add(2)
  132. go writer(nio0, isClosed, 1)
  133. go writer(nio1, isClosed, 0)
  134. readersMidpoint.Wait()
  135. if iteration%2 == 0 {
  136. readersEndpoint.Wait()
  137. }
  138. nio0.Close()
  139. nio1.Close()
  140. writers.Wait()
  141. readers.Wait()
  142. }
  143. }