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
startDate
The start date of the scheduler.
-
Enqueues an action on the scheduler.
Note
The work is executed on
currentDate
as it is understood by the scheduler.Declaration
Swift
@discardableResult public func schedule(_ action: @escaping () -> Void) -> Disposable?
Parameters
action
An action that will be performed on scheduler’s
currentDate
.Return Value
Optional
Disposable
that 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
delay
A delay for execution.
action
A 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
date
A starting date.
action
A 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
delay
A delay for action’s dispatch.
interval
A repetition interval.
leeway
Some delta for repetition interval.
action
A closure of the action to repeat.
Return Value
Optional
Disposable
that 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
date
A date to schedule the first action for.
interval
A repetition interval.
leeway
Some delta for repetition interval.
action
A closure of the action to repeat.
Return Value
Optional
Disposable
that 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
interval
Interval 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
newDate
Future 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
interval
An interval by which the current date will be retreated.