@@ -13,6 +13,7 @@ import (
13
13
"github.com/coder/coder/v2/coderd/database/dbgen"
14
14
"github.com/coder/coder/v2/coderd/database/dbtestutil"
15
15
"github.com/coder/coder/v2/coderd/database/dbtime"
16
+ "github.com/coder/coder/v2/coderd/util/ptr"
16
17
"github.com/coder/coder/v2/testutil"
17
18
18
19
"github.com/stretchr/testify/require"
@@ -22,79 +23,80 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
22
23
t .Parallel ()
23
24
24
25
for _ , tt := range []struct {
25
- name string
26
- transition database.WorkspaceTransition
27
- jobCompletedAt sql.NullTime
28
- buildDeadline time.Time
29
- maxDeadline time.Time
30
- workspaceTTL time.Duration
31
- expectedBump time.Duration
26
+ name string
27
+ transition database.WorkspaceTransition
28
+ jobCompletedAt sql.NullTime
29
+ buildDeadlineOffset * time.Duration
30
+ maxDeadline time.Time
31
+ workspaceTTL time.Duration
32
+ expectedBump time.Duration
32
33
}{
33
34
{
34
- name : "NotFinishedYet" ,
35
- transition : database .WorkspaceTransitionStart ,
36
- jobCompletedAt : sql.NullTime {},
37
- buildDeadline : dbtime . Now (). Add (8 * time .Hour ),
38
- workspaceTTL : 8 * time .Hour ,
39
- expectedBump : 0 ,
35
+ name : "NotFinishedYet" ,
36
+ transition : database .WorkspaceTransitionStart ,
37
+ jobCompletedAt : sql.NullTime {},
38
+ buildDeadlineOffset : ptr . Ref (8 * time .Hour ),
39
+ workspaceTTL : 8 * time .Hour ,
40
+ expectedBump : 0 ,
40
41
},
41
42
{
42
- name : "ManualShutdown" ,
43
- transition : database .WorkspaceTransitionStart ,
44
- jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ()},
45
- buildDeadline : time. Time {} ,
46
- expectedBump : 0 ,
43
+ name : "ManualShutdown" ,
44
+ transition : database .WorkspaceTransitionStart ,
45
+ jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ()},
46
+ buildDeadlineOffset : nil ,
47
+ expectedBump : 0 ,
47
48
},
48
49
{
49
- name : "NotTimeToBumpYet" ,
50
- transition : database .WorkspaceTransitionStart ,
51
- jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ()},
52
- buildDeadline : dbtime . Now (). Add (8 * time .Hour ),
53
- workspaceTTL : 8 * time .Hour ,
54
- expectedBump : 0 ,
50
+ name : "NotTimeToBumpYet" ,
51
+ transition : database .WorkspaceTransitionStart ,
52
+ jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ()},
53
+ buildDeadlineOffset : ptr . Ref (8 * time .Hour ),
54
+ workspaceTTL : 8 * time .Hour ,
55
+ expectedBump : 0 ,
55
56
},
56
57
{
57
- name : "TimeToBump" ,
58
- transition : database .WorkspaceTransitionStart ,
59
- jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- 24 * time .Minute )},
60
- buildDeadline : dbtime . Now (). Add (8 * time .Hour - 24 * time .Minute ),
61
- workspaceTTL : 8 * time .Hour ,
62
- expectedBump : 8 * time .Hour ,
58
+ name : "TimeToBump" ,
59
+ transition : database .WorkspaceTransitionStart ,
60
+ jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- 24 * time .Minute )},
61
+ buildDeadlineOffset : ptr . Ref (8 * time .Hour - 24 * time .Minute ),
62
+ workspaceTTL : 8 * time .Hour ,
63
+ expectedBump : 8 * time .Hour ,
63
64
},
64
65
{
65
- name : "MaxDeadline" ,
66
- transition : database .WorkspaceTransitionStart ,
67
- jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- 24 * time .Minute )},
68
- buildDeadline : dbtime . Now (). Add (time .Minute ), // last chance to bump!
69
- maxDeadline : dbtime .Now ().Add (time .Hour ),
70
- workspaceTTL : 8 * time .Hour ,
71
- expectedBump : 1 * time .Hour ,
66
+ name : "MaxDeadline" ,
67
+ transition : database .WorkspaceTransitionStart ,
68
+ jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- 24 * time .Minute )},
69
+ buildDeadlineOffset : ptr . Ref (time .Minute ), // last chance to bump!
70
+ maxDeadline : dbtime .Now ().Add (time .Hour ),
71
+ workspaceTTL : 8 * time .Hour ,
72
+ expectedBump : 1 * time .Hour ,
72
73
},
73
74
{
74
75
// A workspace that is still running, has passed its deadline, but has not
75
76
// yet been auto-stopped should still bump the deadline.
76
- name : "PastDeadlineStillBumps" ,
77
- transition : database .WorkspaceTransitionStart ,
78
- jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- 24 * time .Minute )},
79
- buildDeadline : dbtime . Now (). Add (- time .Minute ),
80
- workspaceTTL : 8 * time .Hour ,
81
- expectedBump : 8 * time .Hour ,
77
+ name : "PastDeadlineStillBumps" ,
78
+ transition : database .WorkspaceTransitionStart ,
79
+ jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- 24 * time .Minute )},
80
+ buildDeadlineOffset : ptr . Ref (- time .Minute ),
81
+ workspaceTTL : 8 * time .Hour ,
82
+ expectedBump : 8 * time .Hour ,
82
83
},
83
84
{
84
85
// A stopped workspace should never bump.
85
- name : "StoppedWorkspace" ,
86
- transition : database .WorkspaceTransitionStop ,
87
- jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- time .Minute )},
88
- buildDeadline : dbtime . Now (). Add (- time .Minute ),
89
- workspaceTTL : 8 * time .Hour ,
90
- expectedBump : 0 ,
86
+ name : "StoppedWorkspace" ,
87
+ transition : database .WorkspaceTransitionStop ,
88
+ jobCompletedAt : sql.NullTime {Valid : true , Time : dbtime .Now ().Add (- time .Minute )},
89
+ buildDeadlineOffset : ptr . Ref (- time .Minute ),
90
+ workspaceTTL : 8 * time .Hour ,
91
+ expectedBump : 0 ,
91
92
},
92
93
} {
93
94
tt := tt
94
95
t .Run (tt .name , func (t * testing.T ) {
95
96
t .Parallel ()
96
97
97
98
var (
99
+ now = dbtime .Now ()
98
100
ctx = testutil .Context (t , testutil .WaitShort )
99
101
log = slogtest .Make (t , nil )
100
102
db , _ = dbtestutil .NewDB (t )
@@ -141,6 +143,10 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
141
143
}
142
144
143
145
// dbgen.WorkspaceBuild automatically sets deadline to now+1 hour if not set
146
+ var buildDeadline time.Time
147
+ if tt .buildDeadlineOffset != nil {
148
+ buildDeadline = now .Add (* tt .buildDeadlineOffset )
149
+ }
144
150
err := db .InsertWorkspaceBuild (ctx , database.InsertWorkspaceBuildParams {
145
151
ID : buildID ,
146
152
CreatedAt : dbtime .Now (),
@@ -152,7 +158,7 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
152
158
JobID : job .ID ,
153
159
TemplateVersionID : templateVersion .ID ,
154
160
Transition : tt .transition ,
155
- Deadline : tt . buildDeadline ,
161
+ Deadline : buildDeadline ,
156
162
MaxDeadline : tt .maxDeadline ,
157
163
})
158
164
require .NoError (t , err , "unexpected error inserting workspace build" )
@@ -162,19 +168,21 @@ func Test_ActivityBumpWorkspace(t *testing.T) {
162
168
// Validate our initial state before bump
163
169
require .Equal (t , tt .transition , bld .Transition , "unexpected transition before bump" )
164
170
require .Equal (t , tt .jobCompletedAt .Time .UTC (), job .CompletedAt .Time .UTC (), "unexpected job completed at before bump" )
165
- require .Equal (t , tt . buildDeadline .UTC (), bld .Deadline .UTC (), "unexpected build deadline before bump" )
171
+ require .Equal (t , buildDeadline .UTC (), bld .Deadline .UTC (), "unexpected build deadline before bump" )
166
172
require .Equal (t , tt .maxDeadline .UTC (), bld .MaxDeadline .UTC (), "unexpected max deadline before bump" )
167
173
require .Equal (t , tt .workspaceTTL , time .Duration (ws .Ttl .Int64 ), "unexpected workspace TTL before bump" )
168
174
169
175
workaroundWindowsTimeResolution (t )
170
176
177
+ // Bump duration is measured from the time of the bump, so we measure from here.
171
178
start := dbtime .Now ()
172
179
activityBumpWorkspace (ctx , log , db , bld .WorkspaceID )
173
180
elapsed := time .Since (start )
174
181
if elapsed > 15 * time .Second {
175
182
t .Logf ("warning: activityBumpWorkspace took longer than 15 seconds: %s" , elapsed )
176
183
}
177
- // Guessing at the approximate time of the bump here, if it happened.
184
+ // The actual bump could have happened anywhere in the elapsed time, so we
185
+ // guess at the approximate time of the bump.
178
186
approxBumpTime := start .Add (elapsed / 2 )
179
187
180
188
// Validate our state after bump
0 commit comments