@@ -51,7 +51,7 @@ final class JobsTests: XCTestCase {
5151
5252 func createJobQueue(
5353 numWorkers: Int ,
54- configuration: PostgresJobQueue . Configuration ,
54+ configuration: PostgresJobQueue . Configuration = . init ( ) ,
5555 function: String = #function
5656 ) async throws -> JobQueue < PostgresJobQueue > {
5757 let logger = {
@@ -87,6 +87,9 @@ final class JobsTests: XCTestCase {
8787 /// shutdown correctly
8888 @discardableResult public func testJobQueue< T> (
8989 jobQueue: JobQueue < PostgresJobQueue > ,
90+ failedJobsInitialization: PostgresJobQueue . JobCleanup = . remove,
91+ processingJobsInitialization: PostgresJobQueue . JobCleanup = . remove,
92+ pendingJobsInitialization: PostgresJobQueue . JobCleanup = . remove,
9093 revertMigrations: Bool = false ,
9194 test: ( JobQueue < PostgresJobQueue > ) async throws -> T
9295 ) async throws -> T {
@@ -110,6 +113,7 @@ final class JobsTests: XCTestCase {
110113 try await migrations. revert ( client: client, groups: [ . jobQueue] , logger: logger, dryRun: false )
111114 }
112115 try await migrations. apply ( client: client, groups: [ . jobQueue] , logger: logger, dryRun: false )
116+ try await jobQueue. queue. cleanup ( failedJobs: failedJobsInitialization, processingJobs: processingJobsInitialization)
113117 let value = try await test ( jobQueue)
114118 await serviceGroup. triggerGracefulShutdown ( )
115119 return value
@@ -134,13 +138,22 @@ final class JobsTests: XCTestCase {
134138 /// shutdown correctly
135139 @discardableResult public func testJobQueue< T> (
136140 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,
138144 revertMigrations: Bool = true ,
139145 function: String = #function,
140146 test: ( JobQueue < PostgresJobQueue > ) async throws -> T
141147 ) 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+ )
144157 }
145158
146159 func testBasic( ) async throws {
@@ -368,14 +381,13 @@ final class JobsTests: XCTestCase {
368381 finished. store ( true , ordering: . relaxed)
369382 }
370383 let jobQueue = try await createJobQueue (
371- numWorkers: 1 ,
372- configuration: . init( pendingJobsInitialization: . remove, failedJobsInitialization: . rerun)
384+ numWorkers: 1
373385 )
374386 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
379391 try await jobQueue. push ( id: jobIdentifer, parameters: 0 )
380392
381393 await self . wait ( for: [ failedExpectation] , timeout: 10 )
@@ -384,9 +396,9 @@ final class JobsTests: XCTestCase {
384396 XCTAssertFalse ( finished. load ( ordering: . relaxed) )
385397 }
386398
387- let jobQueue2 = try await createJobQueue ( numWorkers: 1 , configuration : . init ( failedJobsInitialization : . rerun ) )
399+ let jobQueue2 = try await createJobQueue ( numWorkers: 1 )
388400 jobQueue2. registerJob ( job)
389- try await self . testJobQueue ( jobQueue: jobQueue2) { _ in
401+ try await self . testJobQueue ( jobQueue: jobQueue2, failedJobsInitialization : . rerun ) { _ in
390402 await self . wait ( for: [ succeededExpectation] , timeout: 10 )
391403 XCTAssertTrue ( finished. load ( ordering: . relaxed) )
392404 }
@@ -414,7 +426,6 @@ final class JobsTests: XCTestCase {
414426 . postgres(
415427 client: postgresClient,
416428 migrations: postgresMigrations,
417- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
418429 logger: logger
419430 ) ,
420431 numWorkers: 2 ,
@@ -425,7 +436,6 @@ final class JobsTests: XCTestCase {
425436 . postgres(
426437 client: postgresClient,
427438 migrations: postgresMigrations2,
428- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
429439 logger: logger
430440 ) ,
431441 numWorkers: 2 ,
@@ -447,6 +457,8 @@ final class JobsTests: XCTestCase {
447457 }
448458 try await postgresMigrations. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
449459 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)
450462 do {
451463 for i in 0 ..< 200 {
452464 try await jobQueue. push ( id: jobIdentifer, parameters: i)
@@ -475,7 +487,6 @@ final class JobsTests: XCTestCase {
475487 let jobQueue = await PostgresJobQueue (
476488 client: postgresClient,
477489 migrations: postgresMigrations,
478- configuration: . init( failedJobsInitialization: . remove, processingJobsInitialization: . remove) ,
479490 logger: logger
480491 )
481492 try await postgresMigrations. apply ( client: postgresClient, groups: [ . jobQueue] , logger: logger, dryRun: false )
0 commit comments