@@ -51,7 +51,7 @@ final class JobsTests: XCTestCase {
51
51
52
52
func createJobQueue(
53
53
numWorkers: Int ,
54
- configuration: PostgresJobQueue . Configuration ,
54
+ configuration: PostgresJobQueue . Configuration = . init ( ) ,
55
55
function: String = #function
56
56
) async throws -> JobQueue < PostgresJobQueue > {
57
57
let logger = {
@@ -87,6 +87,9 @@ final class JobsTests: XCTestCase {
87
87
/// shutdown correctly
88
88
@discardableResult public func testJobQueue< T> (
89
89
jobQueue: JobQueue < PostgresJobQueue > ,
90
+ failedJobsInitialization: PostgresJobQueue . JobInitialization = . remove,
91
+ processingJobsInitialization: PostgresJobQueue . JobInitialization = . remove,
92
+ pendingJobsInitialization: PostgresJobQueue . JobInitialization = . doNothing,
90
93
revertMigrations: Bool = false ,
91
94
test: ( JobQueue < PostgresJobQueue > ) async throws -> T
92
95
) async throws -> T {
@@ -110,6 +113,7 @@ final class JobsTests: XCTestCase {
110
113
try await migrations. revert ( client: client, groups: [ . jobQueue] , logger: logger, dryRun: false )
111
114
}
112
115
try await migrations. apply ( client: client, groups: [ . jobQueue] , logger: logger, dryRun: false )
116
+ try await jobQueue. queue. cleanup ( failedJobs: failedJobsInitialization, processingJobs: processingJobsInitialization)
113
117
let value = try await test ( jobQueue)
114
118
await serviceGroup. triggerGracefulShutdown ( )
115
119
return value
@@ -134,13 +138,20 @@ final class JobsTests: XCTestCase {
134
138
/// shutdown correctly
135
139
@discardableResult public func testJobQueue< T> (
136
140
numWorkers: Int ,
137
- configuration: PostgresJobQueue . Configuration = . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
141
+ failedJobsInitialization: PostgresJobQueue . JobInitialization = . remove,
142
+ processingJobsInitialization: PostgresJobQueue . JobInitialization = . remove,
138
143
revertMigrations: Bool = true ,
139
144
function: String = #function,
140
145
test: ( JobQueue < PostgresJobQueue > ) async throws -> T
141
146
) async throws -> T {
142
- let jobQueue = try await self . createJobQueue ( numWorkers: numWorkers, configuration: configuration, function: function)
143
- return try await self . testJobQueue ( jobQueue: jobQueue, revertMigrations: revertMigrations, test: test)
147
+ let jobQueue = try await self . createJobQueue ( numWorkers: numWorkers, configuration: . init( ) , function: function)
148
+ return try await self . testJobQueue (
149
+ jobQueue: jobQueue,
150
+ failedJobsInitialization: failedJobsInitialization,
151
+ processingJobsInitialization: processingJobsInitialization,
152
+ revertMigrations: revertMigrations,
153
+ test: test
154
+ )
144
155
}
145
156
146
157
func testBasic( ) async throws {
@@ -368,14 +379,13 @@ final class JobsTests: XCTestCase {
368
379
finished. store ( true , ordering: . relaxed)
369
380
}
370
381
let jobQueue = try await createJobQueue (
371
- numWorkers: 1 ,
372
- configuration: . init( pendingJobsInitialization: . remove, failedJobsInitialization: . rerun)
382
+ numWorkers: 1
373
383
)
374
384
jobQueue. registerJob ( job)
375
- try await self . testJobQueue ( jobQueue : jobQueue , revertMigrations : true ) { jobQueue in
376
- // stall to give onInit a chance to run, so it can remove any pendng jobs
377
- try await Task . sleep ( for : . milliseconds ( 100 ) )
378
-
385
+ try await self . testJobQueue (
386
+ jobQueue : jobQueue ,
387
+ revertMigrations : true
388
+ ) { jobQueue in
379
389
try await jobQueue. push ( id: jobIdentifer, parameters: 0 )
380
390
381
391
await self . wait ( for: [ failedExpectation] , timeout: 10 )
@@ -384,9 +394,9 @@ final class JobsTests: XCTestCase {
384
394
XCTAssertFalse ( finished. load ( ordering: . relaxed) )
385
395
}
386
396
387
- let jobQueue2 = try await createJobQueue ( numWorkers: 1 , configuration : . init ( failedJobsInitialization : . rerun ) )
397
+ let jobQueue2 = try await createJobQueue ( numWorkers: 1 )
388
398
jobQueue2. registerJob ( job)
389
- try await self . testJobQueue ( jobQueue: jobQueue2) { _ in
399
+ try await self . testJobQueue ( jobQueue: jobQueue2, failedJobsInitialization : . rerun ) { _ in
390
400
await self . wait ( for: [ succeededExpectation] , timeout: 10 )
391
401
XCTAssertTrue ( finished. load ( ordering: . relaxed) )
392
402
}
@@ -414,7 +424,6 @@ final class JobsTests: XCTestCase {
414
424
. postgres(
415
425
client: postgresClient,
416
426
migrations: postgresMigrations,
417
- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
418
427
logger: logger
419
428
) ,
420
429
numWorkers: 2 ,
@@ -425,7 +434,6 @@ final class JobsTests: XCTestCase {
425
434
. postgres(
426
435
client: postgresClient,
427
436
migrations: postgresMigrations2,
428
- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
429
437
logger: logger
430
438
) ,
431
439
numWorkers: 2 ,
@@ -447,6 +455,8 @@ final class JobsTests: XCTestCase {
447
455
}
448
456
try await postgresMigrations. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
449
457
try await postgresMigrations2. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
458
+ try await jobQueue. queue. cleanup ( failedJobs: . remove, processingJobs: . remove)
459
+ try await jobQueue2. queue. cleanup ( failedJobs: . remove, processingJobs: . remove)
450
460
do {
451
461
for i in 0 ..< 200 {
452
462
try await jobQueue. push ( id: jobIdentifer, parameters: i)
@@ -475,7 +485,6 @@ final class JobsTests: XCTestCase {
475
485
let jobQueue = await PostgresJobQueue (
476
486
client: postgresClient,
477
487
migrations: postgresMigrations,
478
- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
479
488
logger: logger
480
489
)
481
490
try await postgresMigrations. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
0 commit comments