Skip to content

Commit fb35e01

Browse files
committed
Fix context passing
1 parent b8776cb commit fb35e01

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

coderd/coderdtest/coderdtest.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer)
115115
return client, closer
116116
}
117117

118-
func NewOptions(t *testing.T, options *Options) (*httptest.Server, *coderd.Options) {
118+
func NewOptions(t *testing.T, options *Options) (*httptest.Server, context.CancelFunc, *coderd.Options) {
119119
if options == nil {
120120
options = &Options{}
121121
}
@@ -159,8 +159,6 @@ func NewOptions(t *testing.T, options *Options) (*httptest.Server, *coderd.Optio
159159
}
160160

161161
ctx, cancelFunc := context.WithCancel(context.Background())
162-
defer t.Cleanup(cancelFunc) // Defer to ensure cancelFunc is executed first.
163-
164162
lifecycleExecutor := executor.New(
165163
ctx,
166164
db,
@@ -194,7 +192,7 @@ func NewOptions(t *testing.T, options *Options) (*httptest.Server, *coderd.Optio
194192
options.SSHKeygenAlgorithm = gitsshkey.AlgorithmEd25519
195193
}
196194

197-
return srv, &coderd.Options{
195+
return srv, cancelFunc, &coderd.Options{
198196
AgentConnectionUpdateFrequency: 150 * time.Millisecond,
199197
// Force a long disconnection timeout to ensure
200198
// agents are not marked as disconnected during slow tests.
@@ -246,10 +244,11 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
246244
if options == nil {
247245
options = &Options{}
248246
}
249-
srv, newOptions := NewOptions(t, options)
247+
srv, cancelFunc, newOptions := NewOptions(t, options)
250248
// We set the handler after server creation for the access URL.
251249
coderAPI := coderd.New(newOptions)
252250
t.Cleanup(func() {
251+
cancelFunc()
253252
_ = coderAPI.Close()
254253
})
255254
srv.Config.Handler = coderAPI.RootHandler

enterprise/coderd/coderdenttest/coderdenttest.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
5252
if options.Options == nil {
5353
options.Options = &coderdtest.Options{}
5454
}
55-
srv, oop := coderdtest.NewOptions(t, options.Options)
55+
srv, cancelFunc, oop := coderdtest.NewOptions(t, options.Options)
5656
coderAPI, err := coderd.New(context.Background(), &coderd.Options{
5757
AuditLogging: true,
5858
Options: oop,
@@ -68,6 +68,7 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
6868
provisionerCloser = coderdtest.NewProvisionerDaemon(t, coderAPI.AGPL)
6969
}
7070
t.Cleanup(func() {
71+
cancelFunc()
7172
_ = provisionerCloser.Close()
7273
_ = coderAPI.Close()
7374
})

0 commit comments

Comments
 (0)