@@ -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,10 +244,11 @@ 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
250
t .Cleanup (func () {
251
+ cancelFunc ()
253
252
_ = coderAPI .Close ()
254
253
})
255
254
srv .Config .Handler = coderAPI .RootHandler
0 commit comments