How can http.Server be made to hold open a connection?

3 weeks ago 34
ARTICLE AD BOX

I'd like to add a unit test for Go's http.Server.Shutdown() timing out, but there are no connections held open when the shutdown signal is sent, so it never gets to checking for timeouts, even though the timeout is in the past.

How can I convince an http.Server that it has connections open and is waiting for them to close? I'm able to send http.Client requests to it, of course, but this is a dead simple app with no I/O other than the requests themselves, so doesn't hold connections open for long. Sending a bunch at once might work, but it also might not. What I need is a deterministic pattern to reliably force the http.Server to be waiting on connections to finish in the course of a unit tests.

Thinks I've tried:

Adding a RegisterOnShutdown callback with a time.Sleep(), but the server doesn't wait for it to finish AFAICT Adding an additional route with a time.Sleep() to the server mux and calling it, but it seems to be out-of-band since there isn't a network connection Connecting to the server with an http.Client using an io.ReadCloser request body that never closes

None did the trick, alas. Is there some way to convince the server to wait long enough for Shutdown to check the context for a timeout.

Playground test case

Revised test case with the request body that stays open

Read Entire Article