Skip to content

Commit 6b187df

Browse files
committed
Fix deadline is only check when the job run
1 parent 5d65389 commit 6b187df

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Sources/SwiftQueue/Constrains+Deadline.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ internal class DeadlineConstraint: JobConstraint {
1717
}
1818

1919
func run(operation: SwiftQueueJob) -> Bool {
20+
if let delay = operation.deadline {
21+
runInBackgroundAfter(delay.timeIntervalSince(Date()), callback: { [weak operation = operation] in
22+
operation?.run()
23+
})
24+
}
2025
return true
2126
}
2227

Tests/SwiftQueueTests/ConstraintTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ class ConstraintTests: XCTestCase {
6363
XCTAssertEqual(job2.onCancelCalled, 1)
6464
}
6565

66+
func testDeadlineAfterSchedule() {
67+
let job1 = TestJob()
68+
let type1 = UUID().uuidString
69+
70+
let creator = TestCreator([type1: job1])
71+
72+
let manager = SwiftQueueManager(creators: [creator])
73+
JobBuilder(type: type1)
74+
.delay(inSecond: 60)
75+
.deadline(date: Date(timeIntervalSinceNow: TimeInterval(2)))
76+
.retry(max: 5)
77+
.schedule(manager: manager)
78+
79+
manager.waitUntilAllOperationsAreFinished()
80+
job1.await()
81+
82+
XCTAssertEqual(job1.onRunJobCalled, 0)
83+
XCTAssertEqual(job1.onCompleteCalled, 0)
84+
XCTAssertEqual(job1.onRetryCalled, 0)
85+
XCTAssertEqual(job1.onCancelCalled, 1)
86+
}
87+
6688
func testDeadlineWhenDeserialize() {
6789
let group = UUID().uuidString
6890

0 commit comments

Comments
 (0)