Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: Ensure t.Cleanup is not aborted by require
  • Loading branch information
mafredri committed Jul 25, 2022
commit 628d9f207fde76be696ac8c18bf77bc03b52b628
3 changes: 0 additions & 3 deletions cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ func TestConfigSSH(t *testing.T) {
go io.Copy(ssh, conn)
}
}()
t.Cleanup(func() {
_ = listener.Close()
})

sshConfigFile, _ := sshConfigFileNames(t)

Expand Down
9 changes: 6 additions & 3 deletions coderd/coderd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
t.Cleanup(func() { close(tickerCh) })

ctx, cancelFunc := context.WithCancel(context.Background())
t.Cleanup(cancelFunc)

lifecycleExecutor := executor.New(
ctx,
db,
Expand All @@ -109,9 +111,13 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
srv.Start()
serverURL, err := url.Parse(srv.URL)
require.NoError(t, err)
t.Cleanup(srv.Close)

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)
Expand All @@ -138,9 +144,6 @@ func TestAuthorizeAllEndpoints(t *testing.T) {

_ = coderdtest.NewProvisionerDaemon(t, coderAPI)
t.Cleanup(func() {
cancelFunc()
_ = turnServer.Close()
srv.Close()
_ = coderAPI.Close()
})

Expand Down
15 changes: 10 additions & 5 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer)
}

ctx, cancelFunc := context.WithCancel(context.Background())
t.Cleanup(cancelFunc)

lifecycleExecutor := executor.New(
ctx,
db,
Expand All @@ -156,6 +158,8 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer)
return ctx
}
srv.Start()
t.Cleanup(srv.Close)

serverURL, err := url.Parse(srv.URL)
require.NoError(t, err)

Expand All @@ -166,6 +170,9 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer)

turnServer, err := turnconn.New(nil)
require.NoError(t, err)
t.Cleanup(func() {
_ = turnServer.Close()
})

// We set the handler after server creation for the access URL.
coderAPI := coderd.New(&coderd.Options{
Expand All @@ -188,18 +195,16 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer)
Authorizer: options.Authorizer,
Telemetry: telemetry.NewNoop(),
})
t.Cleanup(func() {
_ = coderAPI.Close()
})
srv.Config.Handler = coderAPI.Handler

var provisionerCloser io.Closer = nopcloser{}
if options.IncludeProvisionerD {
provisionerCloser = NewProvisionerDaemon(t, coderAPI)
}

t.Cleanup(func() {
cancelFunc()
_ = turnServer.Close()
srv.Close()
_ = coderAPI.Close()
_ = provisionerCloser.Close()
})

Expand Down