bufconn_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package bufconn
  19. import (
  20. "fmt"
  21. "io"
  22. "net"
  23. "reflect"
  24. "testing"
  25. "time"
  26. )
  27. func testRW(r io.Reader, w io.Writer) error {
  28. for i := 0; i < 20; i++ {
  29. d := make([]byte, i)
  30. for j := 0; j < i; j++ {
  31. d[j] = byte(i - j)
  32. }
  33. var rn int
  34. var rerr error
  35. b := make([]byte, i)
  36. done := make(chan struct{})
  37. go func() {
  38. for rn < len(b) && rerr == nil {
  39. var x int
  40. x, rerr = r.Read(b[rn:])
  41. rn += x
  42. }
  43. close(done)
  44. }()
  45. wn, werr := w.Write(d)
  46. if wn != i || werr != nil {
  47. return fmt.Errorf("%v: w.Write(%v) = %v, %v; want %v, nil", i, d, wn, werr, i)
  48. }
  49. select {
  50. case <-done:
  51. case <-time.After(500 * time.Millisecond):
  52. return fmt.Errorf("%v: r.Read never returned", i)
  53. }
  54. if rn != i || rerr != nil {
  55. return fmt.Errorf("%v: r.Read = %v, %v; want %v, nil", i, rn, rerr, i)
  56. }
  57. if !reflect.DeepEqual(b, d) {
  58. return fmt.Errorf("%v: r.Read read %v; want %v", i, b, d)
  59. }
  60. }
  61. return nil
  62. }
  63. func TestPipe(t *testing.T) {
  64. p := newPipe(10)
  65. if err := testRW(p, p); err != nil {
  66. t.Fatalf(err.Error())
  67. }
  68. }
  69. func TestPipeClose(t *testing.T) {
  70. p := newPipe(10)
  71. p.Close()
  72. if _, err := p.Write(nil); err != io.ErrClosedPipe {
  73. t.Fatalf("p.Write = _, %v; want _, %v", err, io.ErrClosedPipe)
  74. }
  75. if _, err := p.Read(nil); err != io.ErrClosedPipe {
  76. t.Fatalf("p.Read = _, %v; want _, %v", err, io.ErrClosedPipe)
  77. }
  78. }
  79. func TestConn(t *testing.T) {
  80. p1, p2 := newPipe(10), newPipe(10)
  81. c1, c2 := &conn{p1, p2}, &conn{p2, p1}
  82. if err := testRW(c1, c2); err != nil {
  83. t.Fatalf(err.Error())
  84. }
  85. if err := testRW(c2, c1); err != nil {
  86. t.Fatalf(err.Error())
  87. }
  88. }
  89. func TestConnCloseWithData(t *testing.T) {
  90. lis := Listen(7)
  91. errChan := make(chan error)
  92. var lisConn net.Conn
  93. go func() {
  94. var err error
  95. if lisConn, err = lis.Accept(); err != nil {
  96. errChan <- err
  97. }
  98. close(errChan)
  99. }()
  100. dialConn, err := lis.Dial()
  101. if err != nil {
  102. t.Fatalf("Dial error: %v", err)
  103. }
  104. if err := <-errChan; err != nil {
  105. t.Fatalf("Listen error: %v", err)
  106. }
  107. // Write some data on both sides of the connection.
  108. n, err := dialConn.Write([]byte("hello"))
  109. if n != 5 || err != nil {
  110. t.Fatalf("dialConn.Write([]byte{\"hello\"}) = %v, %v; want 5, <nil>", n, err)
  111. }
  112. n, err = lisConn.Write([]byte("hello"))
  113. if n != 5 || err != nil {
  114. t.Fatalf("lisConn.Write([]byte{\"hello\"}) = %v, %v; want 5, <nil>", n, err)
  115. }
  116. // Close dial-side; writes from either side should fail.
  117. dialConn.Close()
  118. if _, err := lisConn.Write([]byte("hello")); err != io.ErrClosedPipe {
  119. t.Fatalf("lisConn.Write() = _, <nil>; want _, <non-nil>")
  120. }
  121. if _, err := dialConn.Write([]byte("hello")); err != io.ErrClosedPipe {
  122. t.Fatalf("dialConn.Write() = _, <nil>; want _, <non-nil>")
  123. }
  124. // Read from both sides; reads on lisConn should work, but dialConn should
  125. // fail.
  126. buf := make([]byte, 6)
  127. if _, err := dialConn.Read(buf); err != io.ErrClosedPipe {
  128. t.Fatalf("dialConn.Read(buf) = %v, %v; want _, io.ErrClosedPipe", n, err)
  129. }
  130. n, err = lisConn.Read(buf)
  131. if n != 5 || err != nil {
  132. t.Fatalf("lisConn.Read(buf) = %v, %v; want 5, <nil>", n, err)
  133. }
  134. }
  135. func TestListener(t *testing.T) {
  136. l := Listen(7)
  137. var s net.Conn
  138. var serr error
  139. done := make(chan struct{})
  140. go func() {
  141. s, serr = l.Accept()
  142. close(done)
  143. }()
  144. c, cerr := l.Dial()
  145. <-done
  146. if cerr != nil || serr != nil {
  147. t.Fatalf("cerr = %v, serr = %v; want nil, nil", cerr, serr)
  148. }
  149. if err := testRW(c, s); err != nil {
  150. t.Fatalf(err.Error())
  151. }
  152. if err := testRW(s, c); err != nil {
  153. t.Fatalf(err.Error())
  154. }
  155. }
  156. func TestCloseWhileDialing(t *testing.T) {
  157. l := Listen(7)
  158. var c net.Conn
  159. var err error
  160. done := make(chan struct{})
  161. go func() {
  162. c, err = l.Dial()
  163. close(done)
  164. }()
  165. l.Close()
  166. <-done
  167. if c != nil || err != errClosed {
  168. t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)
  169. }
  170. }
  171. func TestCloseWhileAccepting(t *testing.T) {
  172. l := Listen(7)
  173. var c net.Conn
  174. var err error
  175. done := make(chan struct{})
  176. go func() {
  177. c, err = l.Accept()
  178. close(done)
  179. }()
  180. l.Close()
  181. <-done
  182. if c != nil || err != errClosed {
  183. t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)
  184. }
  185. }