|
15 | 15 | */
|
16 | 16 | package rx;
|
17 | 17 |
|
| 18 | +import static org.mockito.Matchers.anyLong; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.never; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | + |
18 | 24 | import java.util.Date;
|
19 | 25 | import java.util.concurrent.TimeUnit;
|
20 | 26 | import java.util.concurrent.atomic.AtomicBoolean;
|
21 | 27 |
|
| 28 | +import org.junit.Test; |
| 29 | +import org.mockito.InOrder; |
| 30 | +import org.mockito.Mockito; |
| 31 | + |
| 32 | +import rx.concurrency.TestScheduler; |
22 | 33 | import rx.subscriptions.Subscriptions;
|
23 | 34 | import rx.util.functions.Action0;
|
24 | 35 | import rx.util.functions.Func0;
|
@@ -97,9 +108,9 @@ public <T> Subscription schedulePeriodically(T state, final Func2<Scheduler, T,
|
97 | 108 | @Override
|
98 | 109 | public Subscription call(Scheduler scheduler, T state0) {
|
99 | 110 | if (! complete.get()) {
|
100 |
| - long startedAt = System.nanoTime(); |
| 111 | + long startedAt = now(); |
101 | 112 | final Subscription sub1 = action.call(scheduler, state0);
|
102 |
| - long timeTakenByActionInNanos = System.nanoTime() - startedAt; |
| 113 | + long timeTakenByActionInNanos = TimeUnit.MILLISECONDS.toNanos(now() - startedAt); |
103 | 114 | final Subscription sub2 = schedule(state0, this, periodInNanos - timeTakenByActionInNanos, TimeUnit.NANOSECONDS);
|
104 | 115 | return Subscriptions.create(new Action0() {
|
105 | 116 | @Override
|
@@ -325,4 +336,39 @@ public long now() {
|
325 | 336 | return System.currentTimeMillis();
|
326 | 337 | }
|
327 | 338 |
|
| 339 | + public static class UnitTest { |
| 340 | + @SuppressWarnings("unchecked") // mocking is unchecked, unfortunately |
| 341 | + @Test |
| 342 | + public void testPeriodicScheduling() { |
| 343 | + final Func1<Long, Void> calledOp = mock(Func1.class); |
| 344 | + |
| 345 | + final TestScheduler scheduler = new TestScheduler(); |
| 346 | + scheduler.schedulePeriodically(new Action0() { |
| 347 | + @Override public void call() { |
| 348 | + System.out.println(scheduler.now()); |
| 349 | + calledOp.call(scheduler.now()); |
| 350 | + } |
| 351 | + }, 1, 2, TimeUnit.SECONDS); |
| 352 | + |
| 353 | + verify(calledOp, never()).call(anyLong()); |
| 354 | + |
| 355 | + InOrder inOrder = Mockito.inOrder(calledOp); |
| 356 | + |
| 357 | + scheduler.advanceTimeBy(999L, TimeUnit.MILLISECONDS); |
| 358 | + inOrder.verify(calledOp, never()).call(anyLong()); |
| 359 | + |
| 360 | + scheduler.advanceTimeBy(1L, TimeUnit.MILLISECONDS); |
| 361 | + inOrder.verify(calledOp, times(1)).call(1000L); |
| 362 | + |
| 363 | + scheduler.advanceTimeBy(1999L, TimeUnit.MILLISECONDS); |
| 364 | + inOrder.verify(calledOp, never()).call(3000L); |
| 365 | + |
| 366 | + scheduler.advanceTimeBy(1L, TimeUnit.MILLISECONDS); |
| 367 | + inOrder.verify(calledOp, times(1)).call(3000L); |
| 368 | + |
| 369 | + scheduler.advanceTimeBy(5L, TimeUnit.SECONDS); |
| 370 | + inOrder.verify(calledOp, times(1)).call(5000L); |
| 371 | + inOrder.verify(calledOp, times(1)).call(7000L); |
| 372 | + } |
| 373 | + } |
328 | 374 | }
|
0 commit comments