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.
 
 
 

164 lines
3.9 KiB

  1. /*
  2. *
  3. * Copyright 2018 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 testutils_test
  19. import (
  20. "testing"
  21. "time"
  22. "google.golang.org/grpc/internal/testutils"
  23. )
  24. func TestPipeListener(t *testing.T) {
  25. pl := testutils.NewPipeListener()
  26. recvdBytes := make(chan []byte)
  27. const want = "hello world"
  28. go func() {
  29. c, err := pl.Accept()
  30. if err != nil {
  31. t.Error(err)
  32. }
  33. read := make([]byte, len(want))
  34. _, err = c.Read(read)
  35. if err != nil {
  36. t.Error(err)
  37. }
  38. recvdBytes <- read
  39. }()
  40. dl := pl.Dialer()
  41. conn, err := dl("", time.Duration(0))
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. _, err = conn.Write([]byte(want))
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. select {
  50. case gotBytes := <-recvdBytes:
  51. got := string(gotBytes)
  52. if got != want {
  53. t.Fatalf("expected to get %s, got %s", got, want)
  54. }
  55. case <-time.After(100 * time.Millisecond):
  56. t.Fatal("timed out waiting for server to receive bytes")
  57. }
  58. }
  59. func TestUnblocking(t *testing.T) {
  60. for _, test := range []struct {
  61. desc string
  62. blockFuncShouldError bool
  63. blockFunc func(*testutils.PipeListener, chan struct{}) error
  64. unblockFunc func(*testutils.PipeListener) error
  65. }{
  66. {
  67. desc: "Accept unblocks Dial",
  68. blockFunc: func(pl *testutils.PipeListener, done chan struct{}) error {
  69. dl := pl.Dialer()
  70. _, err := dl("", time.Duration(0))
  71. close(done)
  72. return err
  73. },
  74. unblockFunc: func(pl *testutils.PipeListener) error {
  75. _, err := pl.Accept()
  76. return err
  77. },
  78. },
  79. {
  80. desc: "Close unblocks Dial",
  81. blockFuncShouldError: true, // because pl.Close will be called
  82. blockFunc: func(pl *testutils.PipeListener, done chan struct{}) error {
  83. dl := pl.Dialer()
  84. _, err := dl("", time.Duration(0))
  85. close(done)
  86. return err
  87. },
  88. unblockFunc: func(pl *testutils.PipeListener) error {
  89. return pl.Close()
  90. },
  91. },
  92. {
  93. desc: "Dial unblocks Accept",
  94. blockFunc: func(pl *testutils.PipeListener, done chan struct{}) error {
  95. _, err := pl.Accept()
  96. close(done)
  97. return err
  98. },
  99. unblockFunc: func(pl *testutils.PipeListener) error {
  100. dl := pl.Dialer()
  101. _, err := dl("", time.Duration(0))
  102. return err
  103. },
  104. },
  105. {
  106. desc: "Close unblocks Accept",
  107. blockFuncShouldError: true, // because pl.Close will be called
  108. blockFunc: func(pl *testutils.PipeListener, done chan struct{}) error {
  109. _, err := pl.Accept()
  110. close(done)
  111. return err
  112. },
  113. unblockFunc: func(pl *testutils.PipeListener) error {
  114. return pl.Close()
  115. },
  116. },
  117. } {
  118. t.Log(test.desc)
  119. testUnblocking(t, test.blockFunc, test.unblockFunc, test.blockFuncShouldError)
  120. }
  121. }
  122. func testUnblocking(t *testing.T, blockFunc func(*testutils.PipeListener, chan struct{}) error, unblockFunc func(*testutils.PipeListener) error, blockFuncShouldError bool) {
  123. pl := testutils.NewPipeListener()
  124. dialFinished := make(chan struct{})
  125. go func() {
  126. err := blockFunc(pl, dialFinished)
  127. if blockFuncShouldError && err == nil {
  128. t.Error("expected blocking func to return error because pl.Close was called, but got nil")
  129. }
  130. if !blockFuncShouldError && err != nil {
  131. t.Error(err)
  132. }
  133. }()
  134. select {
  135. case <-dialFinished:
  136. t.Fatal("expected Dial to block until pl.Close or pl.Accept")
  137. default:
  138. }
  139. if err := unblockFunc(pl); err != nil {
  140. t.Fatal(err)
  141. }
  142. select {
  143. case <-dialFinished:
  144. case <-time.After(100 * time.Millisecond):
  145. t.Fatal("expected Accept to unblock after pl.Accept was called")
  146. }
  147. }