Skip to content

Commit a98f6b5

Browse files
committed
Add testCancelledJobRetention
1 parent b64f334 commit a98f6b5

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

Tests/JobsPostgresTests/JobsTests.swift

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -852,21 +852,15 @@ final class JobsTests: XCTestCase {
852852
XCTAssertEqual(didRunNoneCancelledJob.withLockedValue { $0 }, true)
853853
}
854854

855-
func testJobRetention() async throws {
855+
func testCompletedJobRetention() async throws {
856856
struct TestParameters: JobParameters {
857-
static let jobName = "testJobRetention"
857+
static let jobName = "testCompletedJobRetention"
858858
let value: Int
859859
}
860860
let expectation = XCTestExpectation(description: "TestJob.execute was called", expectedFulfillmentCount: 3)
861861
try await self.testJobQueue(
862862
numWorkers: 1,
863-
configuration: .init(
864-
retentionPolicy: .init(
865-
cancelled: .retain,
866-
completed: .retain,
867-
failed: .retain
868-
)
869-
)
863+
configuration: .init(retentionPolicy: .init(completed: .retain))
870864
) { jobQueue in
871865
jobQueue.registerJob(parameters: TestParameters.self) { parameters, context in
872866
context.logger.info("Parameters=\(parameters.value)")
@@ -887,26 +881,47 @@ final class JobsTests: XCTestCase {
887881
}
888882
}
889883

884+
func testCancelledJobRetention() async throws {
885+
let jobQueue = try await self.createJobQueue(
886+
numWorkers: 1,
887+
configuration: .init(retentionPolicy: .init(cancelled: .retain))
888+
)
889+
let jobName = JobName<Int>("testCancelledJobRetention")
890+
jobQueue.registerJob(name: jobName) { _, _ in }
891+
892+
try await withThrowingTaskGroup(of: Void.self) { group in
893+
group.addTask {
894+
// run postgres client
895+
await jobQueue.queue.client.run()
896+
}
897+
try await jobQueue.queue.migrations.apply(client: jobQueue.queue.client, logger: jobQueue.logger, dryRun: false)
898+
899+
let jobId = try await jobQueue.push(jobName, parameters: 1)
900+
let jobId2 = try await jobQueue.push(jobName, parameters: 2)
901+
902+
try await jobQueue.cancelJob(jobID: jobId)
903+
try await jobQueue.cancelJob(jobID: jobId2)
904+
905+
var cancelledJobs = try await jobQueue.queue.getJobs(withStatus: .cancelled)
906+
XCTAssertEqual(cancelledJobs.count, 2)
907+
try await jobQueue.queue.cleanup(cancelledJobs: .remove(maxAge: .seconds(0)))
908+
cancelledJobs = try await jobQueue.queue.getJobs(withStatus: .cancelled)
909+
XCTAssertEqual(cancelledJobs.count, 0)
910+
911+
group.cancelAll()
912+
}
913+
}
914+
890915
func testCleanupJob() async throws {
891916
try await self.testJobQueue(
892917
numWorkers: 1,
893-
configuration: .init(
894-
retentionPolicy: .init(
895-
cancelled: .retain,
896-
completed: .doNotRetain,
897-
failed: .retain
898-
)
899-
)
918+
configuration: .init(retentionPolicy: .init(failed: .retain))
900919
) { jobQueue in
901920
try await self.testJobQueue(
902921
numWorkers: 1,
903922
configuration: .init(
904923
queueName: "SecondQueue",
905-
retentionPolicy: .init(
906-
cancelled: .retain,
907-
completed: .doNotRetain,
908-
failed: .retain
909-
)
924+
retentionPolicy: .init(failed: .retain)
910925
)
911926
) { jobQueue2 in
912927
let (stream, cont) = AsyncStream.makeStream(of: Void.self)

0 commit comments

Comments
 (0)