From bab6ab9d9c5e3da2194dda27333bdf5ef0fb6381 Mon Sep 17 00:00:00 2001 From: Lucas Nelaupe <7482647+lucas34@users.noreply.github.com> Date: Thu, 14 Dec 2017 21:24:11 +0800 Subject: [PATCH] Change signature of onRemove to forward job result (#32) --- Sources/SwiftQueue/Job.swift | 2 +- Sources/SwiftQueue/SwiftQueueJob.swift | 5 +++-- Tests/SwiftQueueTests/TestUtils.swift | 11 ++++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Sources/SwiftQueue/Job.swift b/Sources/SwiftQueue/Job.swift index a4c4a02a..70a6e853 100644 --- a/Sources/SwiftQueue/Job.swift +++ b/Sources/SwiftQueue/Job.swift @@ -125,6 +125,6 @@ public protocol Job { func onRetry(error: Swift.Error) -> RetryConstraint /// Job is removed from the queue and will never run again - func onRemove(error: Swift.Error?) + func onRemove(result: JobCompletion) } diff --git a/Sources/SwiftQueue/SwiftQueueJob.swift b/Sources/SwiftQueue/SwiftQueueJob.swift index 9b6f6d9d..9d742054 100644 --- a/Sources/SwiftQueue/SwiftQueueJob.swift +++ b/Sources/SwiftQueue/SwiftQueueJob.swift @@ -76,7 +76,7 @@ internal final class SwiftQueueJob: Operation, JobResult { internal func abort(error: Swift.Error) { lastError = error // Need to be called manually since the task is actually not in the queue. So cannot call cancel() - handler.onRemove(error: error) + handler.onRemove(result: .fail(error)) } internal func run() { @@ -105,7 +105,8 @@ internal final class SwiftQueueJob: Operation, JobResult { } internal func remove() { - handler.onRemove(error: lastError) + let result = lastError.map(JobCompletion.fail) ?? JobCompletion.success + handler.onRemove(result: result) } func done(_ result: JobCompletion) { diff --git a/Tests/SwiftQueueTests/TestUtils.swift b/Tests/SwiftQueueTests/TestUtils.swift index 3a3ff041..41c42804 100644 --- a/Tests/SwiftQueueTests/TestUtils.swift +++ b/Tests/SwiftQueueTests/TestUtils.swift @@ -43,13 +43,18 @@ class TestJob: Job { return retryConstraint } - func onRemove(error: Error?) { - if error == nil { + func onRemove(result: JobCompletion) { + switch result { + case .success: onCompleteCalled += 1 semaphore.signal() - } else { + break + + case .fail: onCancelCalled += 1 semaphore.signal() + break + } }