fix: Use sync.WaitGroup to await hijacked HTTP connections (#337)

WebSockets hijack the HTTP connection from the server, causing
server.Close() to not wait for these connections to fully cleanup.

This adds a global wait-group to the coderd API, which ensures all
WebSocket HTTP handlers have properly exited before returning.
This commit is contained in:
Kyle Carberry
2022-02-20 16:29:16 -06:00
committed by GitHub
parent 3c04c7f3e6
commit d04570ad29
4 changed files with 23 additions and 13 deletions

View File

@ -55,7 +55,7 @@ func New(t *testing.T) *codersdk.Client {
})
}
handler := coderd.New(&coderd.Options{
handler, closeWait := coderd.New(&coderd.Options{
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
Database: db,
Pubsub: pubsub,
@ -69,7 +69,10 @@ func New(t *testing.T) *codersdk.Client {
srv.Start()
serverURL, err := url.Parse(srv.URL)
require.NoError(t, err)
t.Cleanup(srv.Close)
t.Cleanup(func() {
srv.Close()
closeWait()
})
return codersdk.New(serverURL)
}