@@ -2180,6 +2180,11 @@ func TestGetProvisionerJobsByIDsWithQueuePosition(t *testing.T) {
2180
2180
daemonTags []database.StringMap
2181
2181
queueSizes []int64
2182
2182
queuePositions []int64
2183
+ // GetProvisionerJobsByIDsWithQueuePosition takes jobIDs as a parameter.
2184
+ // If skipJobIDs is empty, all jobs are passed to the function; otherwise, the specified jobs are skipped.
2185
+ // NOTE: Skipping job IDs means they will be excluded from the result,
2186
+ // but this should not affect the queue position or queue size of other jobs.
2187
+ skipJobIDs map [int ]struct {}
2183
2188
}{
2184
2189
{
2185
2190
name :"test-case-1" ,
@@ -2195,6 +2200,7 @@ func TestGetProvisionerJobsByIDsWithQueuePosition(t *testing.T) {
2195
2200
queueSizes : []int64 {2 ,2 ,0 },
2196
2201
queuePositions : []int64 {1 ,1 ,0 },
2197
2202
},
2203
+ // Similar to the previous case, but includes an additional provisioner.
2198
2204
{
2199
2205
name :"test-case-2" ,
2200
2206
jobTags : []database.StringMap {
@@ -2210,6 +2216,83 @@ func TestGetProvisionerJobsByIDsWithQueuePosition(t *testing.T) {
2210
2216
queueSizes : []int64 {3 ,3 ,3 },
2211
2217
queuePositions : []int64 {1 ,1 ,3 },
2212
2218
},
2219
+ // Similar to the previous case, but skips job at index 0
2220
+ {
2221
+ name :"test-case-3" ,
2222
+ jobTags : []database.StringMap {
2223
+ {"a" :"1" ,"b" :"2" },
2224
+ {"a" :"1" },
2225
+ {"a" :"1" ,"c" :"3" },
2226
+ },
2227
+ daemonTags : []database.StringMap {
2228
+ {"a" :"1" ,"b" :"2" },
2229
+ {"a" :"1" },
2230
+ {"a" :"1" ,"b" :"2" ,"c" :"3" },
2231
+ },
2232
+ queueSizes : []int64 {3 ,3 },
2233
+ queuePositions : []int64 {1 ,3 },
2234
+ skipJobIDs :map [int ]struct {}{
2235
+ 0 : {},
2236
+ },
2237
+ },
2238
+ // Skips job at index 1
2239
+ {
2240
+ name :"test-case-4" ,
2241
+ jobTags : []database.StringMap {
2242
+ {"a" :"1" ,"b" :"2" },
2243
+ {"a" :"1" },
2244
+ {"a" :"1" ,"c" :"3" },
2245
+ },
2246
+ daemonTags : []database.StringMap {
2247
+ {"a" :"1" ,"b" :"2" },
2248
+ {"a" :"1" },
2249
+ {"a" :"1" ,"b" :"2" ,"c" :"3" },
2250
+ },
2251
+ queueSizes : []int64 {3 ,3 },
2252
+ queuePositions : []int64 {1 ,3 },
2253
+ skipJobIDs :map [int ]struct {}{
2254
+ 1 : {},
2255
+ },
2256
+ },
2257
+ // Skips job at index 2
2258
+ {
2259
+ name :"test-case-5" ,
2260
+ jobTags : []database.StringMap {
2261
+ {"a" :"1" ,"b" :"2" },
2262
+ {"a" :"1" },
2263
+ {"a" :"1" ,"c" :"3" },
2264
+ },
2265
+ daemonTags : []database.StringMap {
2266
+ {"a" :"1" ,"b" :"2" },
2267
+ {"a" :"1" },
2268
+ {"a" :"1" ,"b" :"2" ,"c" :"3" },
2269
+ },
2270
+ queueSizes : []int64 {3 ,3 },
2271
+ queuePositions : []int64 {1 ,1 },
2272
+ skipJobIDs :map [int ]struct {}{
2273
+ 2 : {},
2274
+ },
2275
+ },
2276
+ // Skips jobs at indexes 0 and 2
2277
+ {
2278
+ name :"test-case-6" ,
2279
+ jobTags : []database.StringMap {
2280
+ {"a" :"1" ,"b" :"2" },
2281
+ {"a" :"1" },
2282
+ {"a" :"1" ,"c" :"3" },
2283
+ },
2284
+ daemonTags : []database.StringMap {
2285
+ {"a" :"1" ,"b" :"2" },
2286
+ {"a" :"1" },
2287
+ {"a" :"1" ,"b" :"2" ,"c" :"3" },
2288
+ },
2289
+ queueSizes : []int64 {3 },
2290
+ queuePositions : []int64 {1 },
2291
+ skipJobIDs :map [int ]struct {}{
2292
+ 0 : {},
2293
+ 2 : {},
2294
+ },
2295
+ },
2213
2296
}
2214
2297
2215
2298
for _ ,tc := range testCases {
@@ -2247,19 +2330,28 @@ func TestGetProvisionerJobsByIDsWithQueuePosition(t *testing.T) {
2247
2330
require .Equal (t ,database .ProvisionerJobStatusPending ,job .JobStatus ,"expected job %d to have status %s" ,idx ,database .ProvisionerJobStatusPending )
2248
2331
}
2249
2332
2250
- var jobIDs []uuid.UUID
2251
- for _ ,job := range allJobs {
2252
- jobIDs = append (jobIDs ,job .ID )
2333
+ filteredJobs := make ([]database.ProvisionerJob ,0 )
2334
+ filteredJobIDs := make ([]uuid.UUID ,0 )
2335
+ for idx ,job := range allJobs {
2336
+ if _ ,skip := tc .skipJobIDs [idx ];skip {
2337
+ continue
2338
+ }
2339
+
2340
+ filteredJobs = append (filteredJobs ,job )
2341
+ filteredJobIDs = append (filteredJobIDs ,job .ID )
2253
2342
}
2254
2343
2255
2344
// When: we fetch the jobs by their IDs
2256
- actualJobs ,err := db .GetProvisionerJobsByIDsWithQueuePosition (ctx ,jobIDs )
2345
+ actualJobs ,err := db .GetProvisionerJobsByIDsWithQueuePosition (ctx ,filteredJobIDs )
2257
2346
require .NoError (t ,err )
2258
- require .Len (t ,actualJobs ,len (allJobs ),"should return all jobs" )
2347
+ require .Len (t ,actualJobs ,len (filteredJobs ),"should return all unskipped jobs" )
2259
2348
2260
- // Then: the jobs should be returned in the correct order (by IDs in the input slice)
2349
+ // Then: the jobs should be returned in the correct order (sorted by createdAt)
2350
+ sort .Slice (filteredJobs ,func (i ,j int )bool {
2351
+ return filteredJobs [i ].CreatedAt .Before (filteredJobs [j ].CreatedAt )
2352
+ })
2261
2353
for idx ,job := range actualJobs {
2262
- assert .EqualValues (t ,allJobs [idx ],job .ProvisionerJob )
2354
+ assert .EqualValues (t ,filteredJobs [idx ],job .ProvisionerJob )
2263
2355
}
2264
2356
2265
2357
// Then: the queue size should be set correctly
@@ -2395,7 +2487,10 @@ func TestGetProvisionerJobsByIDsWithQueuePosition_MixedStatuses(t *testing.T) {
2395
2487
require .NoError (t ,err )
2396
2488
require .Len (t ,actualJobs ,len (allJobs ),"should return all jobs" )
2397
2489
2398
- // Then: the jobs should be returned in the correct order (by IDs in the input slice)
2490
+ // Then: the jobs should be returned in the correct order (sorted by createdAt)
2491
+ sort .Slice (allJobs ,func (i ,j int )bool {
2492
+ return allJobs [i ].CreatedAt .Before (allJobs [j ].CreatedAt )
2493
+ })
2399
2494
for idx ,job := range actualJobs {
2400
2495
assert .EqualValues (t ,allJobs [idx ],job .ProvisionerJob )
2401
2496
}
@@ -2405,14 +2500,14 @@ func TestGetProvisionerJobsByIDsWithQueuePosition_MixedStatuses(t *testing.T) {
2405
2500
for _ ,job := range actualJobs {
2406
2501
queueSizes = append (queueSizes ,job .QueueSize )
2407
2502
}
2408
- assert .EqualValues (t , []int64 {2 , 2 ,0 ,0 ,0 ,0 , 0 },queueSizes ,"expected queue positions to be set correctly" )
2503
+ assert .EqualValues (t , []int64 {0 , 0 ,0 ,0 ,0 ,2 , 2 },queueSizes ,"expected queue positions to be set correctly" )
2409
2504
2410
2505
// Then: the queue position should be set correctly:
2411
2506
var queuePositions []int64
2412
2507
for _ ,job := range actualJobs {
2413
2508
queuePositions = append (queuePositions ,job .QueuePosition )
2414
2509
}
2415
- assert .EqualValues (t , []int64 {2 , 1 ,0 ,0 ,0 ,0 , 0 },queuePositions ,"expected queue positions to be set correctly" )
2510
+ assert .EqualValues (t , []int64 {0 , 0 ,0 ,0 ,0 ,1 , 2 },queuePositions ,"expected queue positions to be set correctly" )
2416
2511
}
2417
2512
2418
2513
func TestGetProvisionerJobsByIDsWithQueuePosition_OrderValidation (t * testing.T ) {
@@ -2489,7 +2584,10 @@ func TestGetProvisionerJobsByIDsWithQueuePosition_OrderValidation(t *testing.T)
2489
2584
require .NoError (t ,err )
2490
2585
require .Len (t ,actualJobs ,len (allJobs ),"should return all jobs" )
2491
2586
2492
- // Then: the jobs should be returned in the correct order (by IDs in the input slice)
2587
+ // Then: the jobs should be returned in the correct order (sorted by createdAt)
2588
+ sort .Slice (allJobs ,func (i ,j int )bool {
2589
+ return allJobs [i ].CreatedAt .Before (allJobs [j ].CreatedAt )
2590
+ })
2493
2591
for idx ,job := range actualJobs {
2494
2592
assert .EqualValues (t ,allJobs [idx ],job .ProvisionerJob )
2495
2593
assert .EqualValues (t ,allJobs [idx ].CreatedAt ,job .ProvisionerJob .CreatedAt )
@@ -2507,7 +2605,7 @@ func TestGetProvisionerJobsByIDsWithQueuePosition_OrderValidation(t *testing.T)
2507
2605
for _ ,job := range actualJobs {
2508
2606
queuePositions = append (queuePositions ,job .QueuePosition )
2509
2607
}
2510
- assert .EqualValues (t , []int64 {3 ,2 ,1 ,4 ,5 ,6 },queuePositions ,"expected queue positions to be set correctly" )
2608
+ assert .EqualValues (t , []int64 {1 ,2 ,3 ,4 ,5 ,6 },queuePositions ,"expected queue positions to be set correctly" )
2511
2609
}
2512
2610
2513
2611
func TestGroupRemovalTrigger (t * testing.T ) {