Skip to content

Commit

Permalink
2.x: Add public constructor for TestScheduler that takes the time. (#…
Browse files Browse the repository at this point in the history
…5906)

* 2.x: Add public constructor for TestScheduler that takes the time.

* Wording.
  • Loading branch information
vanniktech authored and akarnokd committed Mar 9, 2018
1 parent 7646371 commit 5f4daa9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/main/java/io/reactivex/schedulers/TestScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ public final class TestScheduler extends Scheduler {
// Storing time in nanoseconds internally.
volatile long time;

/**
* Creates a new TestScheduler with initial virtual time of zero.
*/
public TestScheduler() {
// No-op.
}

/**
* Creates a new TestScheduler with the specified initial virtual time.
*
* @param delayTime
* the point in time to move the Scheduler's clock to
* @param unit
* the units of time that {@code delayTime} is expressed in
*/
public TestScheduler(long delayTime, TimeUnit unit) {
time = unit.toNanos(delayTime);
}

static final class TimedRunnable implements Comparable<TimedRunnable> {

final long time;
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/io/reactivex/schedulers/TestSchedulerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,10 @@ public void workerDisposed() {
assertTrue(w.isDisposed());
}


@Test
public void constructorTimeSetsTime() {
TestScheduler ts = new TestScheduler(5, TimeUnit.SECONDS);
assertEquals(5, ts.now(TimeUnit.SECONDS));
assertEquals(5000, ts.now(TimeUnit.MILLISECONDS));
}
}

0 comments on commit 5f4daa9

Please sign in to comment.