gracefulstop_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 test
  19. import (
  20. "context"
  21. "fmt"
  22. "io"
  23. "net"
  24. "sync"
  25. "testing"
  26. "time"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/internal/leakcheck"
  29. testpb "google.golang.org/grpc/test/grpc_testing"
  30. )
  31. type delayListener struct {
  32. net.Listener
  33. closeCalled chan struct{}
  34. acceptCalled chan struct{}
  35. allowCloseCh chan struct{}
  36. cc *delayConn
  37. dialed bool
  38. }
  39. func (d *delayListener) Accept() (net.Conn, error) {
  40. select {
  41. case <-d.acceptCalled:
  42. // On the second call, block until closed, then return an error.
  43. <-d.closeCalled
  44. <-d.allowCloseCh
  45. return nil, fmt.Errorf("listener is closed")
  46. default:
  47. close(d.acceptCalled)
  48. conn, err := d.Listener.Accept()
  49. // Allow closing of listener only after accept.
  50. // Note: Dial can return successfully, yet Accept
  51. // might now have finished.
  52. d.allowClose()
  53. return conn, err
  54. }
  55. }
  56. func (d *delayListener) allowClose() {
  57. close(d.allowCloseCh)
  58. }
  59. func (d *delayListener) Close() error {
  60. close(d.closeCalled)
  61. go func() {
  62. <-d.allowCloseCh
  63. d.Listener.Close()
  64. }()
  65. return nil
  66. }
  67. func (d *delayListener) allowClientRead() {
  68. d.cc.allowRead()
  69. }
  70. func (d *delayListener) Dial(to time.Duration) (net.Conn, error) {
  71. if d.dialed {
  72. // Only hand out one connection (net.Dial can return more even after the
  73. // listener is closed). This is not thread-safe, but Dial should never be
  74. // called concurrently in this environment.
  75. return nil, fmt.Errorf("no more conns")
  76. }
  77. d.dialed = true
  78. c, err := net.DialTimeout("tcp", d.Listener.Addr().String(), to)
  79. if err != nil {
  80. return nil, err
  81. }
  82. d.cc = &delayConn{Conn: c, blockRead: make(chan struct{})}
  83. return d.cc, nil
  84. }
  85. type delayConn struct {
  86. net.Conn
  87. blockRead chan struct{}
  88. }
  89. func (d *delayConn) allowRead() {
  90. close(d.blockRead)
  91. }
  92. func (d *delayConn) Read(b []byte) (n int, err error) {
  93. <-d.blockRead
  94. return d.Conn.Read(b)
  95. }
  96. func TestGracefulStop(t *testing.T) {
  97. defer leakcheck.Check(t)
  98. // This test ensures GracefulStop cannot race and break RPCs on new
  99. // connections created after GracefulStop was called but before
  100. // listener.Accept() returns a "closing" error.
  101. //
  102. // Steps of this test:
  103. // 1. Start Server
  104. // 2. GracefulStop() Server after listener's Accept is called, but don't
  105. // allow Accept() to exit when Close() is called on it.
  106. // 3. Create a new connection to the server after listener.Close() is called.
  107. // Server will want to send a GoAway on the new conn, but we delay client
  108. // reads until 5.
  109. // 4. Send an RPC on the new connection.
  110. // 5. Allow the client to read the GoAway. The RPC should complete
  111. // successfully.
  112. lis, err := net.Listen("tcp", "localhost:0")
  113. if err != nil {
  114. t.Fatalf("Error listenening: %v", err)
  115. }
  116. dlis := &delayListener{
  117. Listener: lis,
  118. acceptCalled: make(chan struct{}),
  119. closeCalled: make(chan struct{}),
  120. allowCloseCh: make(chan struct{}),
  121. }
  122. d := func(_ string, to time.Duration) (net.Conn, error) { return dlis.Dial(to) }
  123. serverGotReq := make(chan struct{})
  124. ss := &stubServer{
  125. fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
  126. close(serverGotReq)
  127. _, err := stream.Recv()
  128. if err != nil {
  129. return err
  130. }
  131. return stream.Send(&testpb.StreamingOutputCallResponse{})
  132. },
  133. }
  134. s := grpc.NewServer()
  135. testpb.RegisterTestServiceServer(s, ss)
  136. // 1. Start Server
  137. wg := sync.WaitGroup{}
  138. wg.Add(1)
  139. go func() {
  140. s.Serve(dlis)
  141. wg.Done()
  142. }()
  143. // 2. GracefulStop() Server after listener's Accept is called, but don't
  144. // allow Accept() to exit when Close() is called on it.
  145. <-dlis.acceptCalled
  146. wg.Add(1)
  147. go func() {
  148. s.GracefulStop()
  149. wg.Done()
  150. }()
  151. // 3. Create a new connection to the server after listener.Close() is called.
  152. // Server will want to send a GoAway on the new conn, but we delay it
  153. // until 5.
  154. <-dlis.closeCalled // Block until GracefulStop calls dlis.Close()
  155. // Now dial. The listener's Accept method will return a valid connection,
  156. // even though GracefulStop has closed the listener.
  157. ctx, dialCancel := context.WithTimeout(context.Background(), 5*time.Second)
  158. defer dialCancel()
  159. cc, err := grpc.DialContext(ctx, "", grpc.WithInsecure(), grpc.WithBlock(), grpc.WithDialer(d))
  160. if err != nil {
  161. t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
  162. }
  163. client := testpb.NewTestServiceClient(cc)
  164. defer cc.Close()
  165. // 4. Send an RPC on the new connection.
  166. // The server would send a GOAWAY first, but we are delaying the server's
  167. // writes for now until the client writes more than the preface.
  168. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  169. stream, err := client.FullDuplexCall(ctx)
  170. if err != nil {
  171. t.Fatalf("FullDuplexCall= _, %v; want _, <nil>", err)
  172. }
  173. go func() {
  174. // 5. Allow the client to read the GoAway. The RPC should complete
  175. // successfully.
  176. <-serverGotReq
  177. dlis.allowClientRead()
  178. }()
  179. if err := stream.Send(&testpb.StreamingOutputCallRequest{}); err != nil {
  180. t.Fatalf("stream.Send(_) = %v, want <nil>", err)
  181. }
  182. if _, err := stream.Recv(); err != nil {
  183. t.Fatalf("stream.Recv() = _, %v, want _, <nil>", err)
  184. }
  185. if _, err := stream.Recv(); err != io.EOF {
  186. t.Fatalf("stream.Recv() = _, %v, want _, io.EOF", err)
  187. }
  188. // 5. happens above, then we finish the call.
  189. cancel()
  190. wg.Wait()
  191. }