@@ -209,6 +209,55 @@ func TestExecutorAutostopOK(t *testing.T) {
209
209
require .Equal (t , codersdk .WorkspaceTransitionStop , ws .LatestBuild .Transition , "expected workspace not to be running" )
210
210
}
211
211
212
+ func TestExecutorAutostopExtend (t * testing.T ) {
213
+ t .Parallel ()
214
+
215
+ var (
216
+ ctx = context .Background ()
217
+ tickCh = make (chan time.Time )
218
+ client = coderdtest .New (t , & coderdtest.Options {
219
+ AutobuildTicker : tickCh ,
220
+ IncludeProvisionerD : true ,
221
+ })
222
+ // Given: we have a user with a workspace
223
+ workspace = mustProvisionWorkspace (t , client )
224
+ originalDeadline = workspace .LatestBuild .Deadline
225
+ )
226
+ // Given: workspace is running
227
+ require .Equal (t , codersdk .WorkspaceTransitionStart , workspace .LatestBuild .Transition )
228
+ require .NotZero (t , originalDeadline )
229
+
230
+ // Given: we extend the workspace deadline
231
+ err := client .PutExtendWorkspace (ctx , workspace .ID , codersdk.PutExtendWorkspaceRequest {
232
+ Deadline : originalDeadline .Add (30 * time .Minute ),
233
+ })
234
+ require .NoError (t , err , "extend workspace deadline" )
235
+
236
+ // When: the autobuild executor ticks *after* the original deadline:
237
+ go func () {
238
+ tickCh <- originalDeadline .Add (time .Minute )
239
+ }()
240
+
241
+ // Then: nothing should happen
242
+ <- time .After (5 * time .Second )
243
+ ws := mustWorkspace (t , client , workspace .ID )
244
+ require .Equal (t , workspace .LatestBuild .ID , ws .LatestBuild .ID , "expected no further workspace builds to occur" )
245
+ require .Equal (t , codersdk .WorkspaceTransitionStart , ws .LatestBuild .Transition , "expected workspace to be running" )
246
+
247
+ // When: the autobuild executor ticks after the *new* deadline:
248
+ go func () {
249
+ tickCh <- ws .LatestBuild .Deadline .Add (time .Minute )
250
+ close (tickCh )
251
+ }()
252
+
253
+ // Then: the workspace should be stopped
254
+ <- time .After (5 * time .Second )
255
+ ws = mustWorkspace (t , client , workspace .ID )
256
+ require .NotEqual (t , workspace .LatestBuild .ID , ws .LatestBuild .ID , "expected a workspace build to occur" )
257
+ require .Equal (t , codersdk .ProvisionerJobSucceeded , ws .LatestBuild .Job .Status , "expected provisioner job to have succeeded" )
258
+ require .Equal (t , codersdk .WorkspaceTransitionStop , ws .LatestBuild .Transition , "expected workspace not to be running" )
259
+ }
260
+
212
261
func TestExecutorAutostopAlreadyStopped (t * testing.T ) {
213
262
t .Parallel ()
214
263
0 commit comments