Skip to content

Commit 8244e14

Browse files
Make advance by TimeInterval in TestScheduler possible
The `currentDate` in `TestScheduler could already be advanced by `DispatchTimeInterval`. In unit tests the developer might want to reuse an existing constant, which often is a `Double`/`TimeInterval`. This commits adds a second method with exactly the same signature as the existing one; only the type of the argument is different.
1 parent 92a56d0 commit 8244e14

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Sources/Scheduler.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,17 @@ public final class TestScheduler: DateScheduler {
563563
lock.unlock()
564564
}
565565

566+
/// Advances the virtualized clock by the given interval, dequeuing and
567+
/// executing any actions along the way.
568+
///
569+
/// - parameters:
570+
/// - interval: Interval by which the current date will be advanced.
571+
public func advance(by interval: TimeInterval) {
572+
lock.lock()
573+
advance(to: currentDate.addingTimeInterval(interval))
574+
lock.unlock()
575+
}
576+
566577
/// Advances the virtualized clock to the given future date, dequeuing and
567578
/// executing any actions up until that point.
568579
///

Tests/ReactiveSwiftTests/SchedulerSpec.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ class SchedulerSpec: QuickSpec {
355355
expect(scheduler.currentDate) == Date.distantFuture
356356
expect(string) == "fuzzbuzzfoobar"
357357
}
358+
359+
it("should advance by DispatchTimeInterval same as by TimeInterval") {
360+
let schedulerB = TestScheduler(startDate: startDate)
361+
362+
scheduler.advance(by: .milliseconds(300))
363+
schedulerB.advance(by: 0.3)
364+
365+
expect(scheduler.currentDate).to(equal(schedulerB.currentDate))
366+
}
358367
}
359368
}
360369
}

0 commit comments

Comments
 (0)