Search in sources :

Example 1 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class CompletableTimerTest method timer.

@Test
public void timer() {
    final TestScheduler testScheduler = new TestScheduler();
    final AtomicLong atomicLong = new AtomicLong();
    Completable.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Action() {

        @Override
        public void run() 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) Action(io.reactivex.functions.Action) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 2 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class FlowableThrottleWithTimeoutTests method testThrottle.

@Test
public void testThrottle() {
    Subscriber<Integer> observer = TestHelper.mockSubscriber();
    TestScheduler s = new TestScheduler();
    PublishProcessor<Integer> o = PublishProcessor.create();
    o.throttleWithTimeout(500, TimeUnit.MILLISECONDS, s).subscribe(observer);
    // send events with simulated time increments
    s.advanceTimeTo(0, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(1);
    // deliver
    o.onNext(2);
    s.advanceTimeTo(501, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(3);
    s.advanceTimeTo(600, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(4);
    s.advanceTimeTo(700, TimeUnit.MILLISECONDS);
    // skip
    o.onNext(5);
    // deliver at 1300 after 500ms has passed since onNext(5)
    o.onNext(6);
    s.advanceTimeTo(1300, TimeUnit.MILLISECONDS);
    // deliver
    o.onNext(7);
    s.advanceTimeTo(1800, TimeUnit.MILLISECONDS);
    o.onComplete();
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer).onNext(2);
    inOrder.verify(observer).onNext(6);
    inOrder.verify(observer).onNext(7);
    inOrder.verify(observer).onComplete();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 3 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class FlowableDebounceTest method before.

@Before
public void before() {
    scheduler = new TestScheduler();
    observer = TestHelper.mockSubscriber();
    innerScheduler = scheduler.createWorker();
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler)

Example 4 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class CompletableDisposeOnTest method error.

@Test
public void error() {
    TestScheduler scheduler = new TestScheduler();
    final int[] call = { 0 };
    Completable.error(new TestException()).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            call[0]++;
        }
    }).unsubscribeOn(scheduler).test().assertFailure(TestException.class);
    scheduler.triggerActions();
    assertEquals(0, call[0]);
}
Also used : Action(io.reactivex.functions.Action) TestException(io.reactivex.exceptions.TestException) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 5 with TestScheduler

use of io.reactivex.schedulers.TestScheduler in project RxJava by ReactiveX.

the class CompletableDisposeOnTest method cancelDelayed.

@Test
public void cancelDelayed() {
    TestScheduler scheduler = new TestScheduler();
    PublishSubject<Integer> ps = PublishSubject.create();
    ps.ignoreElements().unsubscribeOn(scheduler).test().cancel();
    assertTrue(ps.hasObservers());
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertFalse(ps.hasObservers());
}
Also used : TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

TestScheduler (io.reactivex.schedulers.TestScheduler)110 Test (org.junit.Test)66 InOrder (org.mockito.InOrder)26 TestObserver (io.reactivex.observers.TestObserver)16 TestSubscriber (io.reactivex.subscribers.TestSubscriber)14 Disposable (io.reactivex.disposables.Disposable)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 TestException (io.reactivex.exceptions.TestException)11 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Schedulers (io.reactivex.schedulers.Schedulers)9 TimeUnit (java.util.concurrent.TimeUnit)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 FlowableSingleDeferUntilRequest (org.davidmoten.rx.internal.FlowableSingleDeferUntilRequest)9 Flowable (io.reactivex.Flowable)8 Scheduler (io.reactivex.Scheduler)8 SingleObserver (io.reactivex.SingleObserver)8 UndeliverableException (io.reactivex.exceptions.UndeliverableException)8 Consumer (io.reactivex.functions.Consumer)8 RxJavaPlugins (io.reactivex.plugins.RxJavaPlugins)8