@@ -115,7 +115,7 @@ func newWithCloser(t *testing.T, options *Options) (*codersdk.Client, io.Closer)
115
115
return client , closer
116
116
}
117
117
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 ) {
119
119
if options == nil {
120
120
options = & Options {}
121
121
}
@@ -159,8 +159,6 @@ func NewOptions(t *testing.T, options *Options) (*httptest.Server, *coderd.Optio
159
159
}
160
160
161
161
ctx , cancelFunc := context .WithCancel (context .Background ())
162
- defer t .Cleanup (cancelFunc ) // Defer to ensure cancelFunc is executed first.
163
-
164
162
lifecycleExecutor := executor .New (
165
163
ctx ,
166
164
db ,
@@ -194,7 +192,7 @@ func NewOptions(t *testing.T, options *Options) (*httptest.Server, *coderd.Optio
194
192
options .SSHKeygenAlgorithm = gitsshkey .AlgorithmEd25519
195
193
}
196
194
197
- return srv , & coderd.Options {
195
+ return srv , cancelFunc , & coderd.Options {
198
196
AgentConnectionUpdateFrequency : 150 * time .Millisecond ,
199
197
// Force a long disconnection timeout to ensure
200
198
// agents are not marked as disconnected during slow tests.
@@ -246,19 +244,18 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
246
244
if options == nil {
247
245
options = & Options {}
248
246
}
249
- srv , newOptions := NewOptions (t , options )
247
+ srv , cancelFunc , newOptions := NewOptions (t , options )
250
248
// We set the handler after server creation for the access URL.
251
249
coderAPI := coderd .New (newOptions )
252
- t .Cleanup (func () {
253
- _ = coderAPI .Close ()
254
- })
255
250
srv .Config .Handler = coderAPI .RootHandler
256
251
var provisionerCloser io.Closer = nopcloser {}
257
252
if options .IncludeProvisionerDaemon {
258
253
provisionerCloser = NewProvisionerDaemon (t , coderAPI )
259
254
}
260
255
t .Cleanup (func () {
256
+ cancelFunc ()
261
257
_ = provisionerCloser .Close ()
258
+ _ = coderAPI .Close ()
262
259
})
263
260
return codersdk .New (coderAPI .AccessURL ), provisionerCloser , coderAPI
264
261
}
0 commit comments