nonblock_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.Errorf("io.ReadFull failed: %s", err)
  90. return
  91. }
  92. if !bytes.Equal(expectedData[:n], data[:n]) {
  93. t.Errorf("bytes.Equal failed")
  94. return
  95. }
  96. }
  97. readersEndpoint.Done()
  98. n, err := r.Read(data)
  99. for n == 0 && err == nil {
  100. n, err = r.Read(data)
  101. }
  102. if n != 0 || err != io.EOF {
  103. t.Errorf("expected io.EOF failed")
  104. return
  105. }
  106. }
  107. writer := func(w io.Writer, isClosed func() bool, seed int) {
  108. defer writers.Done()
  109. PRNG := rand.New(rand.NewSource(int64(seed)))
  110. data := make([]byte, maxIO)
  111. for i := 0; i < messages; i++ {
  112. n := int(1 + PRNG.Int31n(int32(maxIO)))
  113. PRNG.Read(data[:n])
  114. m, err := w.Write(data[:n])
  115. if err != nil {
  116. if isClosed() {
  117. return
  118. }
  119. t.Errorf("w.Write failed: %s", err)
  120. return
  121. }
  122. if m != n {
  123. t.Errorf("w.Write failed: unexpected number of bytes written")
  124. return
  125. }
  126. }
  127. }
  128. isClosed := func() bool {
  129. return nio0.IsClosed() || nio1.IsClosed()
  130. }
  131. readers.Add(2)
  132. readersMidpoint.Add(2)
  133. readersEndpoint.Add(2)
  134. go reader(nio0, isClosed, 0)
  135. go reader(nio1, isClosed, 1)
  136. writers.Add(2)
  137. go writer(nio0, isClosed, 1)
  138. go writer(nio1, isClosed, 0)
  139. readersMidpoint.Wait()
  140. if iteration%2 == 0 {
  141. readersEndpoint.Wait()
  142. }
  143. nio0.Close()
  144. nio1.Close()
  145. writers.Wait()
  146. readers.Wait()
  147. }
  148. }