use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method testPeriodicSchedulingUnsubscription.
@SuppressWarnings("unchecked")
// mocking is unchecked, unfortunately
@Test
public final void testPeriodicSchedulingUnsubscription() throws Exception {
final Function<Long, Void> calledOp = mock(Function.class);
final TestScheduler scheduler = new TestScheduler();
final Scheduler.Worker inner = scheduler.createWorker();
try {
final Disposable subscription = 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);
subscription.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 testNestedSchedule.
@Test
public final void testNestedSchedule() {
final TestScheduler scheduler = new TestScheduler();
final Scheduler.Worker inner = scheduler.createWorker();
try {
final Runnable calledOp = mock(Runnable.class);
Flowable<Object> poller;
poller = Flowable.unsafeCreate(new Publisher<Object>() {
@Override
public void subscribe(final Subscriber<? super Object> aSubscriber) {
final BooleanSubscription bs = new BooleanSubscription();
aSubscriber.onSubscribe(bs);
inner.schedule(new Runnable() {
@Override
public void run() {
if (!bs.isCancelled()) {
calledOp.run();
inner.schedule(this, 5, TimeUnit.SECONDS);
}
}
});
}
});
InOrder inOrder = Mockito.inOrder(calledOp);
Disposable sub;
sub = poller.subscribe();
scheduler.advanceTimeTo(6, TimeUnit.SECONDS);
inOrder.verify(calledOp, times(2)).run();
sub.dispose();
scheduler.advanceTimeTo(11, TimeUnit.SECONDS);
inOrder.verify(calledOp, never()).run();
sub = poller.subscribe();
scheduler.advanceTimeTo(12, TimeUnit.SECONDS);
inOrder.verify(calledOp, times(1)).run();
} finally {
inner.dispose();
}
}
use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.
the class TestSchedulerTest method testImmediateUnsubscribes.
@Test
public final void testImmediateUnsubscribes() {
TestScheduler s = new TestScheduler();
final Scheduler.Worker inner = s.createWorker();
final AtomicInteger counter = new AtomicInteger(0);
try {
inner.schedule(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
System.out.println("counter: " + counter.get());
inner.schedule(this);
}
});
inner.dispose();
assertEquals(0, counter.get());
} finally {
inner.dispose();
}
}
use of io.reactivex.schedulers.TestScheduler in project RxRelay by JakeWharton.
the class ReplayRelayTest method takeSizeAndTime.
@Test
public void takeSizeAndTime() {
TestScheduler scheduler = new TestScheduler();
ReplayRelay<Integer> rp = ReplayRelay.createWithTimeAndSize(1, TimeUnit.SECONDS, scheduler, 2);
rp.accept(1);
rp.accept(2);
rp.accept(3);
rp.take(1).test().assertResult(2);
}
Aggregations