Search in sources :

Example 6 with TestScheduler

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

the class SingleTimeoutTest method successTimeoutRace.

@Test
public void successTimeoutRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final SingleSubject<Integer> subj = SingleSubject.create();
        SingleSubject<Integer> fallback = SingleSubject.create();
        final TestScheduler sch = new TestScheduler();
        TestObserver<Integer> to = subj.timeout(1, TimeUnit.MILLISECONDS, sch, fallback).test();
        Runnable r1 = new Runnable() {

            @Override
            public void run() {
                subj.onSuccess(1);
            }
        };
        Runnable r2 = new Runnable() {

            @Override
            public void run() {
                sch.advanceTimeBy(1, TimeUnit.MILLISECONDS);
            }
        };
        TestHelper.race(r1, r2);
        if (!fallback.hasObservers()) {
            to.assertResult(1);
        } else {
            to.assertEmpty();
        }
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 7 with TestScheduler

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

the class SingleTimeoutTest method shouldUnsubscribeFromUnderlyingSubscriptionOnDispose.

@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnDispose() {
    final PublishSubject<String> subject = PublishSubject.create();
    final TestScheduler scheduler = new TestScheduler();
    final TestObserver<String> observer = subject.single("").timeout(100, TimeUnit.MILLISECONDS, scheduler).test();
    assertTrue(subject.hasObservers());
    observer.dispose();
    assertFalse(subject.hasObservers());
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 8 with TestScheduler

use of io.reactivex.rxjava3.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.rxjava3.core.Scheduler.Worker) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 9 with TestScheduler

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

the class TestSchedulerTest method nestedSchedule.

@Test
public final void nestedSchedule() {
    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.rxjava3.disposables.Disposable) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 10 with TestScheduler

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

the class TestSchedulerTest method withOnScheduleHook.

@Test
public void withOnScheduleHook() {
    AtomicInteger run = new AtomicInteger();
    AtomicInteger counter = new AtomicInteger();
    RxJavaPlugins.setScheduleHandler(r -> {
        counter.getAndIncrement();
        return r;
    });
    try {
        Runnable r = () -> run.getAndIncrement();
        TestScheduler ts = new TestScheduler(true);
        ts.createWorker().schedule(r);
        ts.createWorker().schedule(r, 1, TimeUnit.SECONDS);
        ts.advanceTimeBy(1, TimeUnit.SECONDS);
        assertEquals(2, run.get());
        assertEquals(2, counter.get());
        ts = new TestScheduler();
        ts.createWorker().schedule(r);
        ts.createWorker().schedule(r, 1, TimeUnit.SECONDS);
        ts.advanceTimeBy(1, TimeUnit.SECONDS);
        assertEquals(4, run.get());
        assertEquals(2, counter.get());
    } finally {
        RxJavaPlugins.setScheduleHandler(null);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)92 Test (org.junit.Test)84 TestException (io.reactivex.rxjava3.exceptions.TestException)38 InOrder (org.mockito.InOrder)30 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)7 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 TestObserver (io.reactivex.rxjava3.observers.TestObserver)6 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)6 Action (io.reactivex.rxjava3.functions.Action)5 SchedulerWhen (io.reactivex.rxjava3.internal.schedulers.SchedulerWhen)4 Observable (io.reactivex.rxjava3.core.Observable)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 BaseObserveOnSubscriber (io.reactivex.rxjava3.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber)1 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)1 IOException (java.io.IOException)1