MCPcopy
hub / github.com/grpc/grpc-go / TestGracefulStopClosesConnAfterLastStream

Method TestGracefulStopClosesConnAfterLastStream

test/gracefulstop_test.go:167–213  ·  view source on GitHub ↗

TestGracefulStopClosesConnAfterLastStream ensures that a server closes the connections to its clients when the final stream has completed after a GOAWAY.

(t *testing.T)

Source from the content-addressed store, hash-verified

165// connections to its clients when the final stream has completed after
166// a GOAWAY.
167func (s) TestGracefulStopClosesConnAfterLastStream(t *testing.T) {
168
169 handlerCalled := make(chan struct{})
170 gracefulStopCalled := make(chan struct{})
171
172 ts := &funcServer{streamingInputCall: func(testgrpc.TestService_StreamingInputCallServer) error {
173 close(handlerCalled) // Initiate call to GracefulStop.
174 <-gracefulStopCalled // Wait for GOAWAYs to be received by the client.
175 return nil
176 }}
177
178 te := newTest(t, tcpClearEnv)
179 te.startServer(ts)
180 defer te.tearDown()
181
182 te.withServerTester(func(st *serverTester) {
183 st.writeHeadersGRPC(1, "/grpc.testing.TestService/StreamingInputCall", false)
184
185 <-handlerCalled // Wait for the server to invoke its handler.
186
187 // Gracefully stop the server.
188 gracefulStopDone := make(chan struct{})
189 go func() {
190 te.srv.GracefulStop()
191 close(gracefulStopDone)
192 }()
193 st.wantGoAway(http2.ErrCodeNo) // Server sends a GOAWAY due to GracefulStop.
194 pf := st.wantPing() // Server sends a ping to verify client receipt.
195 st.writePing(true, pf.Data) // Send ping ack to confirm.
196 st.wantGoAway(http2.ErrCodeNo) // Wait for subsequent GOAWAY to indicate no new stream processing.
197
198 close(gracefulStopCalled) // Unblock server handler.
199
200 fr := st.wantAnyFrame() // Wait for trailer.
201 hdr, ok := fr.(*http2.MetaHeadersFrame)
202 if !ok {
203 t.Fatalf("Received unexpected frame of type (%T) from server: %v; want HEADERS", fr, fr)
204 }
205 if !hdr.StreamEnded() {
206 t.Fatalf("Received unexpected HEADERS frame from server: %v; want END_STREAM set", fr)
207 }
208
209 st.wantRSTStream(http2.ErrCodeNo) // Server should send RST_STREAM because client did not half-close.
210
211 <-gracefulStopDone // Wait for GracefulStop to return.
212 })
213}
214
215// TestGracefulStopBlocksUntilGRPCConnectionsTerminate ensures that
216// GracefulStop() blocks until all ongoing RPCs finished.

Callers

nothing calls this directly

Calls 13

withServerTesterMethod · 0.80
writeHeadersGRPCMethod · 0.80
wantGoAwayMethod · 0.80
wantPingMethod · 0.80
writePingMethod · 0.80
wantAnyFrameMethod · 0.80
StreamEndedMethod · 0.80
wantRSTStreamMethod · 0.80
newTestFunction · 0.70
GracefulStopMethod · 0.65
FatalfMethod · 0.65
startServerMethod · 0.45

Tested by

no test coverage detected