@@ -49,16 +49,21 @@ func newMetricsCache(t *testing.T, log slog.Logger, clock quartz.Clock, interval
49
49
50
50
func TestCache_TemplateWorkspaceOwners (t * testing.T ) {
51
51
t .Parallel ()
52
- var ()
53
52
54
53
var (
55
- log = testutil .Logger (t )
56
- clock = quartz .NewReal ()
57
- cache , db = newMetricsCache (t , log , clock , metricscache.Intervals {
58
- TemplateBuildTimes : testutil .IntervalFast ,
59
- }, false )
54
+ ctx = testutil .Context (t , testutil .WaitShort )
55
+ log = testutil .Logger (t )
56
+ clock = quartz .NewMock (t )
60
57
)
61
58
59
+ trapTickerFunc := clock .Trap ().TickerFunc ("metricscache" )
60
+ defer trapTickerFunc .Close ()
61
+
62
+ cache , db := newMetricsCache (t , log , clock , metricscache.Intervals {
63
+ TemplateBuildTimes : time .Minute ,
64
+ DeploymentStats : time .Minute ,
65
+ }, false )
66
+
62
67
org := dbgen .Organization (t , db , database.Organization {})
63
68
user1 := dbgen .User (t , db , database.User {})
64
69
user2 := dbgen .User (t , db , database.User {})
@@ -67,38 +72,47 @@ func TestCache_TemplateWorkspaceOwners(t *testing.T) {
67
72
Provisioner : database .ProvisionerTypeEcho ,
68
73
CreatedBy : user1 .ID ,
69
74
})
70
- require .Eventuallyf (t , func () bool {
71
- count , ok := cache .TemplateWorkspaceOwners (template .ID )
72
- return ok && count == 0
73
- }, testutil .WaitShort , testutil .IntervalMedium ,
74
- "TemplateWorkspaceOwners never populated 0 owners" ,
75
- )
75
+
76
+ // Wait for both ticker functions to be created (template build times and deployment stats)
77
+ trapTickerFunc .MustWait (ctx ).MustRelease (ctx )
78
+ trapTickerFunc .MustWait (ctx ).MustRelease (ctx )
79
+
80
+ _ , wait := clock .AdvanceNext ()
81
+ wait .MustWait (ctx )
82
+ _ , wait = clock .AdvanceNext ()
83
+ wait .MustWait (ctx )
84
+
85
+ count , ok := cache .TemplateWorkspaceOwners (template .ID )
86
+ require .True (t , ok , "TemplateWorkspaceOwners should be populated" )
87
+ require .Equal (t , 0 , count , "should have 0 owners initially" )
76
88
77
89
dbgen .Workspace (t , db , database.WorkspaceTable {
78
90
OrganizationID : org .ID ,
79
91
TemplateID : template .ID ,
80
92
OwnerID : user1 .ID ,
81
93
})
82
94
83
- require .Eventuallyf (t , func () bool {
84
- count , _ := cache .TemplateWorkspaceOwners (template .ID )
85
- return count == 1
86
- }, testutil .WaitShort , testutil .IntervalMedium ,
87
- "TemplateWorkspaceOwners never populated 1 owner" ,
88
- )
95
+ _ , wait = clock .AdvanceNext ()
96
+ wait .MustWait (ctx )
97
+ _ , wait = clock .AdvanceNext ()
98
+ wait .MustWait (ctx )
99
+
100
+ count , _ = cache .TemplateWorkspaceOwners (template .ID )
101
+ require .Equal (t , 1 , count , "should have 1 owner after adding workspace" )
89
102
90
103
workspace2 := dbgen .Workspace (t , db , database.WorkspaceTable {
91
104
OrganizationID : org .ID ,
92
105
TemplateID : template .ID ,
93
106
OwnerID : user2 .ID ,
94
107
})
95
108
96
- require .Eventuallyf (t , func () bool {
97
- count , _ := cache .TemplateWorkspaceOwners (template .ID )
98
- return count == 2
99
- }, testutil .WaitShort , testutil .IntervalMedium ,
100
- "TemplateWorkspaceOwners never populated 2 owners" ,
101
- )
109
+ _ , wait = clock .AdvanceNext ()
110
+ wait .MustWait (ctx )
111
+ _ , wait = clock .AdvanceNext ()
112
+ wait .MustWait (ctx )
113
+
114
+ count , _ = cache .TemplateWorkspaceOwners (template .ID )
115
+ require .Equal (t , 2 , count , "should have 2 owners after adding second workspace" )
102
116
103
117
// 3rd workspace should not be counted since we have the same owner as workspace2.
104
118
dbgen .Workspace (t , db , database.WorkspaceTable {
@@ -112,12 +126,13 @@ func TestCache_TemplateWorkspaceOwners(t *testing.T) {
112
126
Deleted : true ,
113
127
})
114
128
115
- require .Eventuallyf (t , func () bool {
116
- count , _ := cache .TemplateWorkspaceOwners (template .ID )
117
- return count == 1
118
- }, testutil .WaitShort , testutil .IntervalMedium ,
119
- "TemplateWorkspaceOwners never populated 1 owner after delete" ,
120
- )
129
+ _ , wait = clock .AdvanceNext ()
130
+ wait .MustWait (ctx )
131
+ _ , wait = clock .AdvanceNext ()
132
+ wait .MustWait (ctx )
133
+
134
+ count , _ = cache .TemplateWorkspaceOwners (template .ID )
135
+ require .Equal (t , 1 , count , "should have 1 owner after deleting workspace" )
121
136
}
122
137
123
138
func clockTime (t time.Time , hour , minute , sec int ) time.Time {
@@ -205,22 +220,22 @@ func TestCache_BuildTime(t *testing.T) {
205
220
t .Run (tt .name , func (t * testing.T ) {
206
221
t .Parallel ()
207
222
208
- ctx := testutil .Context (t , testutil .WaitShort )
209
-
210
223
var (
224
+ ctx = testutil .Context (t , testutil .WaitShort )
211
225
log = testutil .Logger (t )
212
226
clock = quartz .NewMock (t )
213
227
)
214
228
229
+ clock .Set (someDay )
230
+
215
231
trapTickerFunc := clock .Trap ().TickerFunc ("metricscache" )
216
232
217
233
defer trapTickerFunc .Close ()
218
234
cache , db := newMetricsCache (t , log , clock , metricscache.Intervals {
219
- TemplateBuildTimes : testutil .IntervalFast ,
235
+ TemplateBuildTimes : time .Minute ,
236
+ DeploymentStats : time .Minute ,
220
237
}, false )
221
238
222
- clock .Set (someDay )
223
-
224
239
org := dbgen .Organization (t , db , database.Organization {})
225
240
user := dbgen .User (t , db , database.User {})
226
241
@@ -269,6 +284,8 @@ func TestCache_BuildTime(t *testing.T) {
269
284
270
285
_ , wait := clock .AdvanceNext ()
271
286
wait .MustWait (ctx )
287
+ _ , wait = clock .AdvanceNext ()
288
+ wait .MustWait (ctx )
272
289
273
290
if tt .want .loads {
274
291
wantTransition := codersdk .WorkspaceTransition (tt .args .transition )
@@ -295,9 +312,8 @@ func TestCache_BuildTime(t *testing.T) {
295
312
func TestCache_DeploymentStats (t * testing.T ) {
296
313
t .Parallel ()
297
314
298
- ctx := testutil .Context (t , testutil .WaitShort )
299
-
300
315
var (
316
+ ctx = testutil .Context (t , testutil .WaitShort )
301
317
log = testutil .Logger (t )
302
318
clock = quartz .NewMock (t )
303
319
)
@@ -306,8 +322,8 @@ func TestCache_DeploymentStats(t *testing.T) {
306
322
defer tickerTrap .Close ()
307
323
308
324
cache , db := newMetricsCache (t , log , clock , metricscache.Intervals {
309
- TemplateBuildTimes : testutil . IntervalFast ,
310
- DeploymentStats : testutil . IntervalFast ,
325
+ TemplateBuildTimes : time . Minute ,
326
+ DeploymentStats : time . Minute ,
311
327
}, false )
312
328
313
329
err := db .InsertWorkspaceAgentStats (context .Background (), database.InsertWorkspaceAgentStatsParams {
@@ -339,6 +355,8 @@ func TestCache_DeploymentStats(t *testing.T) {
339
355
340
356
_ , wait := clock .AdvanceNext ()
341
357
wait .MustWait (ctx )
358
+ _ , wait = clock .AdvanceNext ()
359
+ wait .MustWait (ctx )
342
360
343
361
stat , ok := cache .DeploymentStats ()
344
362
require .True (t , ok , "cache should be populated after refresh" )
0 commit comments