use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.
the class ReplayProcessorTest method testReplayTimestampedDirectly.
@Test
public void testReplayTimestampedDirectly() {
TestScheduler scheduler = new TestScheduler();
ReplayProcessor<Integer> source = ReplayProcessor.createWithTime(1, TimeUnit.SECONDS, scheduler);
source.onNext(1);
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
Subscriber<Integer> o = TestHelper.mockSubscriber();
source.subscribe(o);
source.onNext(2);
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
source.onNext(3);
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
source.onComplete();
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
verify(o, never()).onError(any(Throwable.class));
verify(o, never()).onNext(1);
verify(o).onNext(2);
verify(o).onNext(3);
verify(o).onComplete();
}
use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method workerDisposed.
@Test
public void workerDisposed() {
TestScheduler scheduler = new TestScheduler();
Worker w = scheduler.createWorker();
w.dispose();
assertTrue(w.isDisposed());
}
use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method testPeriodicScheduling.
@SuppressWarnings("unchecked")
// mocking is unchecked, unfortunately
@Test
public final void testPeriodicScheduling() throws Exception {
final Function<Long, Void> calledOp = mock(Function.class);
final TestScheduler scheduler = new TestScheduler();
final Scheduler.Worker inner = scheduler.createWorker();
try {
inner.schedulePeriodically(new Runnable() {
@Override
public void run() {
System.out.println(scheduler.now(TimeUnit.MILLISECONDS));
try {
calledOp.apply(scheduler.now(TimeUnit.MILLISECONDS));
} catch (Throwable ex) {
ExceptionHelper.wrapOrThrow(ex);
}
}
}, 1, 2, TimeUnit.SECONDS);
verify(calledOp, never()).apply(anyLong());
InOrder inOrder = Mockito.inOrder(calledOp);
scheduler.advanceTimeBy(999L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, never()).apply(anyLong());
scheduler.advanceTimeBy(1L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, times(1)).apply(1000L);
scheduler.advanceTimeBy(1999L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, never()).apply(3000L);
scheduler.advanceTimeBy(1L, TimeUnit.MILLISECONDS);
inOrder.verify(calledOp, times(1)).apply(3000L);
scheduler.advanceTimeBy(5L, TimeUnit.SECONDS);
inOrder.verify(calledOp, times(1)).apply(5000L);
inOrder.verify(calledOp, times(1)).apply(7000L);
inner.dispose();
scheduler.advanceTimeBy(11L, TimeUnit.SECONDS);
inOrder.verify(calledOp, never()).apply(anyLong());
} finally {
inner.dispose();
}
}
use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method testImmediateUnsubscribes2.
@Test
public final void testImmediateUnsubscribes2() {
TestScheduler s = new TestScheduler();
final Scheduler.Worker inner = s.createWorker();
try {
final AtomicInteger counter = new AtomicInteger(0);
final Disposable subscription = inner.schedule(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
System.out.println("counter: " + counter.get());
inner.schedule(this);
}
});
subscription.dispose();
assertEquals(0, counter.get());
} finally {
inner.dispose();
}
}
use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.
the class SingleTimerTest method timer.
@Test
public void timer() {
final TestScheduler testScheduler = new TestScheduler();
final AtomicLong atomicLong = new AtomicLong();
Single.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Consumer<Long>() {
@Override
public void accept(final Long value) throws Exception {
atomicLong.incrementAndGet();
}
});
assertEquals(0, atomicLong.get());
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
assertEquals(0, atomicLong.get());
testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
assertEquals(1, atomicLong.get());
}
Aggregations