@@ -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 . JobCleanup = . remove,
91
+ processingJobsInitialization: PostgresJobQueue . JobCleanup = . remove,
92
+ pendingJobsInitialization: PostgresJobQueue . JobCleanup = . remove,
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,22 @@ 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 . JobCleanup = . remove,
142
+ processingJobsInitialization: PostgresJobQueue . JobCleanup = . remove,
143
+ pendingJobsInitialization: PostgresJobQueue . JobCleanup = . remove,
138
144
revertMigrations: Bool = true ,
139
145
function: String = #function,
140
146
test: ( JobQueue < PostgresJobQueue > ) async throws -> T
141
147
) 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)
148
+ let jobQueue = try await self . createJobQueue ( numWorkers: numWorkers, configuration: . init( ) , function: function)
149
+ return try await self . testJobQueue (
150
+ jobQueue: jobQueue,
151
+ failedJobsInitialization: failedJobsInitialization,
152
+ processingJobsInitialization: processingJobsInitialization,
153
+ pendingJobsInitialization: pendingJobsInitialization,
154
+ revertMigrations: revertMigrations,
155
+ test: test
156
+ )
144
157
}
145
158
146
159
func testBasic( ) async throws {
@@ -368,14 +381,13 @@ final class JobsTests: XCTestCase {
368
381
finished. store ( true , ordering: . relaxed)
369
382
}
370
383
let jobQueue = try await createJobQueue (
371
- numWorkers: 1 ,
372
- configuration: . init( pendingJobsInitialization: . remove, failedJobsInitialization: . rerun)
384
+ numWorkers: 1
373
385
)
374
386
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
-
387
+ try await self . testJobQueue (
388
+ jobQueue : jobQueue ,
389
+ revertMigrations : true
390
+ ) { jobQueue in
379
391
try await jobQueue. push ( id: jobIdentifer, parameters: 0 )
380
392
381
393
await self . wait ( for: [ failedExpectation] , timeout: 10 )
@@ -384,9 +396,9 @@ final class JobsTests: XCTestCase {
384
396
XCTAssertFalse ( finished. load ( ordering: . relaxed) )
385
397
}
386
398
387
- let jobQueue2 = try await createJobQueue ( numWorkers: 1 , configuration : . init ( failedJobsInitialization : . rerun ) )
399
+ let jobQueue2 = try await createJobQueue ( numWorkers: 1 )
388
400
jobQueue2. registerJob ( job)
389
- try await self . testJobQueue ( jobQueue: jobQueue2) { _ in
401
+ try await self . testJobQueue ( jobQueue: jobQueue2, failedJobsInitialization : . rerun ) { _ in
390
402
await self . wait ( for: [ succeededExpectation] , timeout: 10 )
391
403
XCTAssertTrue ( finished. load ( ordering: . relaxed) )
392
404
}
@@ -414,7 +426,6 @@ final class JobsTests: XCTestCase {
414
426
. postgres(
415
427
client: postgresClient,
416
428
migrations: postgresMigrations,
417
- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
418
429
logger: logger
419
430
) ,
420
431
numWorkers: 2 ,
@@ -425,7 +436,6 @@ final class JobsTests: XCTestCase {
425
436
. postgres(
426
437
client: postgresClient,
427
438
migrations: postgresMigrations2,
428
- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
429
439
logger: logger
430
440
) ,
431
441
numWorkers: 2 ,
@@ -447,6 +457,8 @@ final class JobsTests: XCTestCase {
447
457
}
448
458
try await postgresMigrations. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
449
459
try await postgresMigrations2. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
460
+ try await jobQueue. queue. cleanup ( failedJobs: . remove, processingJobs: . remove)
461
+ try await jobQueue2. queue. cleanup ( failedJobs: . remove, processingJobs: . remove)
450
462
do {
451
463
for i in 0 ..< 200 {
452
464
try await jobQueue. push ( id: jobIdentifer, parameters: i)
@@ -475,7 +487,6 @@ final class JobsTests: XCTestCase {
475
487
let jobQueue = await PostgresJobQueue (
476
488
client: postgresClient,
477
489
migrations: postgresMigrations,
478
- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
479
490
logger: logger
480
491
)
481
492
try await postgresMigrations. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
0 commit comments