TestScheduler
public final class TestScheduler : DateScheduler
A scheduler that implements virtualized time, for use in testing.
-
The virtual date that the scheduler is currently at.
Declaration
Swift
public var currentDate: Date { get } -
Initializes a TestScheduler with the given start date.
Declaration
Swift
public init(startDate: Date = Date(timeIntervalSinceReferenceDate: 0))Parameters
startDateThe start date of the scheduler.
-
Enqueues an action on the scheduler.
Note
The work is executed on
currentDateas it is understood by the scheduler.Declaration
Swift
@discardableResult public func schedule(_ action: @escaping () -> Void) -> Disposable?Parameters
actionAn action that will be performed on scheduler’s
currentDate.Return Value
Optional
Disposablethat can be used to cancel the work before it begins. -
Schedules an action for execution after some delay.
Declaration
Swift
@discardableResult public func schedule(after delay: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable?Parameters
delayA delay for execution.
actionA closure of the action to perform.
Return Value
Optional disposable that can be used to cancel the work before it begins.
-
Schedules an action for execution at or after the given date.
Declaration
Swift
@discardableResult public func schedule(after date: Date, action: @escaping () -> Void) -> Disposable?Parameters
dateA starting date.
actionA closure of the action to perform.
Return Value
Optional disposable that can be used to cancel the work before it begins.
-
Schedules a recurring action after given delay repeated at the given, interval, beginning at the given interval counted from
currentDate.Declaration
Swift
@discardableResult public func schedule(after delay: DispatchTimeInterval, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .seconds(0), action: @escaping () -> Void) -> Disposable?Parameters
delayA delay for action’s dispatch.
intervalA repetition interval.
leewaySome delta for repetition interval.
actionA closure of the action to repeat.
Return Value
Optional
Disposablethat can be used to cancel the work before it begins. -
Schedules a recurring action at the given interval with provided leeway, beginning at the given start date.
Declaration
Swift
public func schedule(after date: Date, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .seconds(0), action: @escaping () -> Void) -> Disposable?Parameters
dateA date to schedule the first action for.
intervalA repetition interval.
leewaySome delta for repetition interval.
actionA closure of the action to repeat.
Return Value
Optional
Disposablethat can be used to cancel the work before it begins. -
Advances the virtualized clock by an extremely tiny interval, dequeuing and executing any actions along the way.
This is intended to be used as a way to execute actions that have been scheduled to run as soon as possible.
Declaration
Swift
public func advance() -
Advances the virtualized clock by the given interval, dequeuing and executing any actions along the way.
Declaration
Swift
public func advance(by interval: DispatchTimeInterval)Parameters
intervalInterval by which the current date will be advanced.
-
Advances the virtualized clock to the given future date, dequeuing and executing any actions up until that point.
Declaration
Swift
public func advance(to newDate: Date)Parameters
newDateFuture date to which the virtual clock will be advanced.
-
Dequeues and executes all scheduled actions, leaving the scheduler’s date at
Date.distantFuture().Declaration
Swift
public func run() -
Rewinds the virtualized clock by the given interval. This simulates that user changes device date.
Declaration
Swift
public func rewind(by interval: DispatchTimeInterval)Parameters
intervalAn interval by which the current date will be retreated.
View on GitHub
Install in Dash
TestScheduler Class Reference