fix: Fix cleanup in test helpers, prefer defer in tests (#3113)

* fix: Change uses of t.Cleanup -> defer in test bodies

Mixing t.Cleanup and defer can lead to unexpected order of execution.

* fix: Ensure t.Cleanup is not aborted by require

* chore: Add helper annotations
This commit is contained in:
Mathias Fredriksson
2022-07-25 19:22:02 +03:00
committed by GitHub
parent c2cd51d8b8
commit 6916d34458
14 changed files with 60 additions and 53 deletions

View File

@ -94,6 +94,8 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
t.Cleanup(func() { close(tickerCh) })
ctx, cancelFunc := context.WithCancel(context.Background())
defer t.Cleanup(cancelFunc) // Defer to ensure cancelFunc is executed first.
lifecycleExecutor := executor.New(
ctx,
db,
@ -107,11 +109,15 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
return ctx
}
srv.Start()
t.Cleanup(srv.Close)
serverURL, err := url.Parse(srv.URL)
require.NoError(t, err)
turnServer, err := turnconn.New(nil)
require.NoError(t, err)
t.Cleanup(func() {
_ = turnServer.Close()
})
validator, err := idtoken.NewValidator(ctx, option.WithoutAuthentication())
require.NoError(t, err)
@ -138,9 +144,6 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
_ = coderdtest.NewProvisionerDaemon(t, coderAPI)
t.Cleanup(func() {
cancelFunc()
_ = turnServer.Close()
srv.Close()
_ = coderAPI.Close()
})