Search in sources :

Example 91 with TestScheduler

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();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) Worker(io.reactivex.Scheduler.Worker) TestScheduler(io.reactivex.schedulers.TestScheduler) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 92 with TestScheduler

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();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) Worker(io.reactivex.Scheduler.Worker) TestScheduler(io.reactivex.schedulers.TestScheduler) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 93 with TestScheduler

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();
    }
}
Also used : Worker(io.reactivex.Scheduler.Worker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestScheduler(io.reactivex.schedulers.TestScheduler) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 94 with TestScheduler

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);
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

TestScheduler (io.reactivex.schedulers.TestScheduler)94 Test (org.junit.Test)53 InOrder (org.mockito.InOrder)26 TestException (io.reactivex.exceptions.TestException)11 Worker (io.reactivex.Scheduler.Worker)6 TestSubscriber (io.reactivex.subscribers.TestSubscriber)6 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)5 TestObserver (io.reactivex.observers.TestObserver)5 Disposable (io.reactivex.disposables.Disposable)3 Action (io.reactivex.functions.Action)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NonBlockingConnectionPool (org.davidmoten.rx.jdbc.pool.NonBlockingConnectionPool)1 Before (org.junit.Before)1