Search in sources :

Example 31 with TestScheduler

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

Example 32 with TestScheduler

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

Example 33 with TestScheduler

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

Example 34 with TestScheduler

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();
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) 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 35 with TestScheduler

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());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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