-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix schedule(after:interval:leeway:)
disposal
#584
Changes from 1 commit
421f86c
4c47d4e
07570af
bed149e
74667b2
2fb93cf
4a4bebc
8e3299b
90f69c6
7911a73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,8 +193,14 @@ public final class QueueScheduler: DateScheduler { | |
|
||
public let queue: DispatchQueue | ||
|
||
private let lock: Lock | ||
|
||
private var timers: [DispatchSourceTimer] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
internal init(internalQueue: DispatchQueue) { | ||
queue = internalQueue | ||
lock = Lock.make() | ||
timers = [] | ||
} | ||
|
||
/// Initializes a scheduler that will target the given queue with its | ||
|
@@ -340,8 +346,16 @@ public final class QueueScheduler: DateScheduler { | |
timer.setEventHandler(handler: action) | ||
timer.resume() | ||
|
||
lock.lock() | ||
timers.append(timer) | ||
lock.unlock() | ||
|
||
return AnyDisposable { | ||
timer.cancel() | ||
|
||
self.lock.lock() | ||
self.timers = self.timers.filter { !$0.isEqual(timer) } | ||
self.lock.unlock() | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,6 +218,34 @@ class SchedulerSpec: QuickSpec { | |
scheduler.queue.resume() | ||
expect{count}.toEventually(equal(timesToRun)) | ||
} | ||
|
||
it("should repeatedly run actions after a given date when the disposable is not retained") { | ||
let disposable = SerialDisposable() | ||
|
||
var count = 0 | ||
let timesToIncrement = 3 | ||
|
||
// Start two repeating timers, dispose the first, and ensure only the second runs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd probably be better off as two separate test cases. One for cancellation, while another for not retaining the disposable. |
||
|
||
disposable.inner = scheduler.schedule(after: Date(), interval: .milliseconds(10), leeway: .seconds(0)) { | ||
expect(false).to(equal(true), description: "timer not cancelled on disposal") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may replace this with |
||
} | ||
|
||
scheduler.schedule(after: Date(), interval: .milliseconds(10), leeway: .seconds(0)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might scope the scheduling call in a function, so that the disposable is guaranteed to be deinitialised. |
||
expect(Thread.isMainThread) == false | ||
|
||
if count <= timesToIncrement { | ||
count += 1 | ||
} | ||
} | ||
|
||
disposable.dispose() | ||
|
||
expect(count) == 0 | ||
|
||
scheduler.queue.resume() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the cancellation test, I'd wait for 1.5-2x the interval after resuming the queue so that the test case could actually fail. |
||
expect{count}.toEventually(equal(timesToIncrement)) | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the PR no & your handle.