-
Notifications
You must be signed in to change notification settings - Fork 887
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
Changes from all commits
9a5db33
628d9f2
2334d8a
d263af2
7b356b3
f245f64
b624871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment on the other instance: https://github.com/coder/coder/pull/3113/files#r928922910. |
||
|
||
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() | ||
}) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,8 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer) | |
} | ||
|
||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
defer t.Cleanup(cancelFunc) // Defer to ensure cancelFunc is executed first. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is a helper function, |
||
|
||
lifecycleExecutor := executor.New( | ||
ctx, | ||
db, | ||
|
@@ -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) | ||
|
||
|
@@ -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{ | ||
|
@@ -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() | ||
}) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assert here, to avoid interrupting
Close
below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should comment this in the file so it doesn't get reverted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of agree, but I'm less worried about this old code changing than new (incorrect) code going in. This is something everyone using
require
must be very aware of (it halts execution flow).