google.golang.org/grpc@v1.72.2/test/gracefulstop_test.go (about) 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 19 package test 20 21 import ( 22 "context" 23 "fmt" 24 "net" 25 "testing" 26 "time" 27 28 "golang.org/x/net/http2" 29 "google.golang.org/grpc" 30 "google.golang.org/grpc/codes" 31 "google.golang.org/grpc/credentials/insecure" 32 "google.golang.org/grpc/internal/stubserver" 33 "google.golang.org/grpc/status" 34 35 testgrpc "google.golang.org/grpc/interop/grpc_testing" 36 testpb "google.golang.org/grpc/interop/grpc_testing" 37 ) 38 39 type delayListener struct { 40 net.Listener 41 closeCalled chan struct{} 42 acceptCalled chan struct{} 43 allowCloseCh chan struct{} 44 dialed bool 45 } 46 47 func (d *delayListener) Accept() (net.Conn, error) { 48 select { 49 case <-d.acceptCalled: 50 // On the second call, block until closed, then return an error. 51 <-d.closeCalled 52 <-d.allowCloseCh 53 return nil, fmt.Errorf("listener is closed") 54 default: 55 close(d.acceptCalled) 56 conn, err := d.Listener.Accept() 57 if err != nil { 58 return nil, err 59 } 60 // Allow closing of listener only after accept. 61 // Note: Dial can return successfully, yet Accept 62 // might now have finished. 63 d.allowClose() 64 return conn, nil 65 } 66 } 67 68 func (d *delayListener) allowClose() { 69 close(d.allowCloseCh) 70 } 71 func (d *delayListener) Close() error { 72 close(d.closeCalled) 73 go func() { 74 <-d.allowCloseCh 75 d.Listener.Close() 76 }() 77 return nil 78 } 79 80 func (d *delayListener) Dial(ctx context.Context) (net.Conn, error) { 81 if d.dialed { 82 // Only hand out one connection (net.Dial can return more even after the 83 // listener is closed). This is not thread-safe, but Dial should never be 84 // called concurrently in this environment. 85 return nil, fmt.Errorf("no more conns") 86 } 87 d.dialed = true 88 return (&net.Dialer{}).DialContext(ctx, "tcp", d.Listener.Addr().String()) 89 } 90 91 // TestGracefulStop ensures GracefulStop causes new connections to fail. 92 // 93 // Steps of this test: 94 // 1. Start Server 95 // 2. GracefulStop() Server after listener's Accept is called, but don't 96 // allow Accept() to exit when Close() is called on it. 97 // 3. Create a new connection to the server after listener.Close() is called. 98 // Server should close this connection immediately, before handshaking. 99 // 4. Send an RPC on the new connection. Should see Unavailable error 100 // because the ClientConn is in transient failure. 101 func (s) TestGracefulStop(t *testing.T) { 102 lis, err := net.Listen("tcp", "localhost:0") 103 if err != nil { 104 t.Fatalf("Error listenening: %v", err) 105 } 106 dlis := &delayListener{ 107 Listener: lis, 108 acceptCalled: make(chan struct{}), 109 closeCalled: make(chan struct{}), 110 allowCloseCh: make(chan struct{}), 111 } 112 113 ss := &stubserver.StubServer{ 114 Listener: dlis, 115 FullDuplexCallF: func(stream testgrpc.TestService_FullDuplexCallServer) error { 116 if _, err := stream.Recv(); err != nil { 117 return err 118 } 119 return stream.Send(&testpb.StreamingOutputCallResponse{}) 120 }, 121 S: grpc.NewServer(), 122 } 123 // 1. Start Server and start serving by calling Serve(). 124 stubserver.StartTestService(t, ss) 125 126 // 2. Call GracefulStop from a goroutine. It will trigger Close on the 127 // listener, but the listener will not actually close until a connection 128 // is accepted. 129 gracefulStopDone := make(chan struct{}) 130 <-dlis.acceptCalled 131 go func() { 132 ss.S.GracefulStop() 133 close(gracefulStopDone) 134 }() 135 136 // 3. Create a new connection to the server after listener.Close() is called. 137 // Server should close this connection immediately, before handshaking. 138 139 <-dlis.closeCalled // Block until GracefulStop calls dlis.Close() 140 141 dialer := func(ctx context.Context, _ string) (net.Conn, error) { return dlis.Dial(ctx) } 142 cc, err := grpc.NewClient("passthrough:///", grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialer)) 143 if err != nil { 144 t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err) 145 } 146 client := testgrpc.NewTestServiceClient(cc) 147 defer cc.Close() 148 149 // 4. Make an RPC. 150 // The server would send a GOAWAY first, but we are delaying the server's 151 // writes for now until the client writes more than the preface. 152 // This will cause a connection to be accepted. This will 153 // also unblock the Close method. 154 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 155 if _, err = client.FullDuplexCall(ctx); err == nil || status.Code(err) != codes.Unavailable { 156 t.Fatalf("FullDuplexCall= _, %v; want _, <status code Unavailable>", err) 157 } 158 cancel() 159 <-gracefulStopDone 160 } 161 162 // TestGracefulStopClosesConnAfterLastStream ensures that a server closes the 163 // connections to its clients when the final stream has completed after 164 // a GOAWAY. 165 func (s) TestGracefulStopClosesConnAfterLastStream(t *testing.T) { 166 167 handlerCalled := make(chan struct{}) 168 gracefulStopCalled := make(chan struct{}) 169 170 ts := &funcServer{streamingInputCall: func(testgrpc.TestService_StreamingInputCallServer) error { 171 close(handlerCalled) // Initiate call to GracefulStop. 172 <-gracefulStopCalled // Wait for GOAWAYs to be received by the client. 173 return nil 174 }} 175 176 te := newTest(t, tcpClearEnv) 177 te.startServer(ts) 178 defer te.tearDown() 179 180 te.withServerTester(func(st *serverTester) { 181 st.writeHeadersGRPC(1, "/grpc.testing.TestService/StreamingInputCall", false) 182 183 <-handlerCalled // Wait for the server to invoke its handler. 184 185 // Gracefully stop the server. 186 gracefulStopDone := make(chan struct{}) 187 go func() { 188 te.srv.GracefulStop() 189 close(gracefulStopDone) 190 }() 191 st.wantGoAway(http2.ErrCodeNo) // Server sends a GOAWAY due to GracefulStop. 192 pf := st.wantPing() // Server sends a ping to verify client receipt. 193 st.writePing(true, pf.Data) // Send ping ack to confirm. 194 st.wantGoAway(http2.ErrCodeNo) // Wait for subsequent GOAWAY to indicate no new stream processing. 195 196 close(gracefulStopCalled) // Unblock server handler. 197 198 fr := st.wantAnyFrame() // Wait for trailer. 199 hdr, ok := fr.(*http2.MetaHeadersFrame) 200 if !ok { 201 t.Fatalf("Received unexpected frame of type (%T) from server: %v; want HEADERS", fr, fr) 202 } 203 if !hdr.StreamEnded() { 204 t.Fatalf("Received unexpected HEADERS frame from server: %v; want END_STREAM set", fr) 205 } 206 207 st.wantRSTStream(http2.ErrCodeNo) // Server should send RST_STREAM because client did not half-close. 208 209 <-gracefulStopDone // Wait for GracefulStop to return. 210 }) 211 } 212 213 // TestGracefulStopBlocksUntilGRPCConnectionsTerminate ensures that 214 // GracefulStop() blocks until all ongoing RPCs finished. 215 func (s) TestGracefulStopBlocksUntilGRPCConnectionsTerminate(t *testing.T) { 216 unblockGRPCCall := make(chan struct{}) 217 grpcCallExecuting := make(chan struct{}) 218 ss := &stubserver.StubServer{ 219 UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { 220 close(grpcCallExecuting) 221 <-unblockGRPCCall 222 return &testpb.SimpleResponse{}, nil 223 }, 224 } 225 226 err := ss.Start(nil) 227 if err != nil { 228 t.Fatalf("StubServer.start failed: %s", err) 229 } 230 t.Cleanup(ss.Stop) 231 232 grpcClientCallReturned := make(chan struct{}) 233 go func() { 234 clt := ss.Client 235 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 236 defer cancel() 237 _, err := clt.UnaryCall(ctx, &testpb.SimpleRequest{}) 238 if err != nil { 239 t.Errorf("rpc failed with error: %s", err) 240 } 241 close(grpcClientCallReturned) 242 }() 243 244 gracefulStopReturned := make(chan struct{}) 245 <-grpcCallExecuting 246 go func() { 247 ss.S.GracefulStop() 248 close(gracefulStopReturned) 249 }() 250 251 select { 252 case <-gracefulStopReturned: 253 t.Error("GracefulStop returned before rpc method call ended") 254 case <-time.After(defaultTestShortTimeout): 255 } 256 257 unblockGRPCCall <- struct{}{} 258 <-grpcClientCallReturned 259 <-gracefulStopReturned 260 } 261 262 // TestStopAbortsBlockingGRPCCall ensures that when Stop() is called while an ongoing RPC 263 // is blocking that: 264 // - Stop() returns 265 // - and the RPC fails with an connection closed error on the client-side 266 func (s) TestStopAbortsBlockingGRPCCall(t *testing.T) { 267 unblockGRPCCall := make(chan struct{}) 268 grpcCallExecuting := make(chan struct{}) 269 ss := &stubserver.StubServer{ 270 UnaryCallF: func(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { 271 close(grpcCallExecuting) 272 <-unblockGRPCCall 273 return &testpb.SimpleResponse{}, nil 274 }, 275 } 276 277 err := ss.Start(nil) 278 if err != nil { 279 t.Fatalf("StubServer.start failed: %s", err) 280 } 281 t.Cleanup(ss.Stop) 282 283 grpcClientCallReturned := make(chan struct{}) 284 go func() { 285 clt := ss.Client 286 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 287 defer cancel() 288 _, err := clt.UnaryCall(ctx, &testpb.SimpleRequest{}) 289 if err == nil || !isConnClosedErr(err) { 290 t.Errorf("expected rpc to fail with connection closed error, got: %v", err) 291 } 292 close(grpcClientCallReturned) 293 }() 294 295 <-grpcCallExecuting 296 ss.S.Stop() 297 298 unblockGRPCCall <- struct{}{} 299 <-grpcClientCallReturned 300 }