Skip to content

Commit 5f4daa9

Browse files
vanniktechakarnokd
authored andcommitted
2.x: Add public constructor for TestScheduler that takes the time. (#5906)
* 2.x: Add public constructor for TestScheduler that takes the time. * Wording.
1 parent 7646371 commit 5f4daa9

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/main/java/io/reactivex/schedulers/TestScheduler.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ public final class TestScheduler extends Scheduler {
3535
// Storing time in nanoseconds internally.
3636
volatile long time;
3737

38+
/**
39+
* Creates a new TestScheduler with initial virtual time of zero.
40+
*/
41+
public TestScheduler() {
42+
// No-op.
43+
}
44+
45+
/**
46+
* Creates a new TestScheduler with the specified initial virtual time.
47+
*
48+
* @param delayTime
49+
* the point in time to move the Scheduler's clock to
50+
* @param unit
51+
* the units of time that {@code delayTime} is expressed in
52+
*/
53+
public TestScheduler(long delayTime, TimeUnit unit) {
54+
time = unit.toNanos(delayTime);
55+
}
56+
3857
static final class TimedRunnable implements Comparable<TimedRunnable> {
3958

4059
final long time;

src/test/java/io/reactivex/schedulers/TestSchedulerTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,5 +253,10 @@ public void workerDisposed() {
253253
assertTrue(w.isDisposed());
254254
}
255255

256-
256+
@Test
257+
public void constructorTimeSetsTime() {
258+
TestScheduler ts = new TestScheduler(5, TimeUnit.SECONDS);
259+
assertEquals(5, ts.now(TimeUnit.SECONDS));
260+
assertEquals(5000, ts.now(TimeUnit.MILLISECONDS));
261+
}
257262
}

0 commit comments

Comments
 (0)