Search in sources :

Example 66 with TestScheduler

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

the class ObservableTimeoutTests method setUp.

@Before
public void setUp() {
    underlyingSubject = PublishSubject.create();
    testScheduler = new TestScheduler();
    withTimeout = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler);
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 67 with TestScheduler

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

the class ObservableTimeoutTests method onNextOnTimeoutRaceFallback.

@Test
public void onNextOnTimeoutRaceFallback() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestScheduler sch = new TestScheduler();
        final PublishSubject<Integer> ps = PublishSubject.create();
        TestObserverEx<Integer> to = ps.timeout(1, TimeUnit.SECONDS, sch, Observable.just(2)).to(TestHelper.<Integer>testConsumer());
        Runnable r1 = new Runnable() {

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

            @Override
            public void run() {
                sch.advanceTimeBy(1, TimeUnit.SECONDS);
            }
        };
        TestHelper.race(r1, r2);
        if (to.isTerminated()) {
            int c = to.values().size();
            if (c == 1) {
                int v = to.values().get(0);
                assertTrue("" + v, v == 1 || v == 2);
            } else {
                to.assertResult(1, 2);
            }
        } else {
            to.assertValuesOnly(1);
        }
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 68 with TestScheduler

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

the class ObservableTimeoutTests method shouldSwitchToOtherIfOnErrorNotWithinTimeout.

@Test
public void shouldSwitchToOtherIfOnErrorNotWithinTimeout() {
    Observable<String> other = Observable.just("a", "b", "c");
    Observable<String> source = underlyingSubject.timeout(TIMEOUT, TIME_UNIT, testScheduler, other);
    Observer<String> observer = TestHelper.mockObserver();
    TestObserver<String> to = new TestObserver<>(observer);
    source.subscribe(to);
    testScheduler.advanceTimeBy(2, TimeUnit.SECONDS);
    underlyingSubject.onNext("One");
    testScheduler.advanceTimeBy(4, TimeUnit.SECONDS);
    underlyingSubject.onError(new UnsupportedOperationException());
    InOrder inOrder = inOrder(observer);
    inOrder.verify(observer, times(1)).onNext("One");
    inOrder.verify(observer, times(1)).onNext("a");
    inOrder.verify(observer, times(1)).onNext("b");
    inOrder.verify(observer, times(1)).onNext("c");
    inOrder.verify(observer, times(1)).onComplete();
    inOrder.verifyNoMoreInteractions();
    to.dispose();
}
Also used : InOrder(org.mockito.InOrder) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 69 with TestScheduler

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

the class ObservableTimeoutTests method onNextOnTimeoutRace.

@Test
public void onNextOnTimeoutRace() {
    for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
        final TestScheduler sch = new TestScheduler();
        final PublishSubject<Integer> ps = PublishSubject.create();
        TestObserverEx<Integer> to = ps.timeout(1, TimeUnit.SECONDS, sch).to(TestHelper.<Integer>testConsumer());
        Runnable r1 = new Runnable() {

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

            @Override
            public void run() {
                sch.advanceTimeBy(1, TimeUnit.SECONDS);
            }
        };
        TestHelper.race(r1, r2);
        if (to.values().size() != 0) {
            if (to.errors().size() != 0) {
                to.assertFailure(TimeoutException.class, 1);
                to.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
            } else {
                to.assertValuesOnly(1);
            }
        } else {
            to.assertFailure(TimeoutException.class);
            to.assertErrorMessage(timeoutMessage(1, TimeUnit.SECONDS));
        }
    }
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler)

Example 70 with TestScheduler

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

the class ObservableThrottleLatestTest method normalEmitLast.

@Test
public void normalEmitLast() {
    TestScheduler sch = new TestScheduler();
    PublishSubject<Integer> ps = PublishSubject.create();
    TestObserver<Integer> to = ps.throttleLatest(1, TimeUnit.SECONDS, sch, true).test();
    ps.onNext(1);
    to.assertValuesOnly(1);
    ps.onNext(2);
    to.assertValuesOnly(1);
    ps.onNext(3);
    to.assertValuesOnly(1);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertValuesOnly(1, 3);
    ps.onNext(4);
    to.assertValuesOnly(1, 3);
    ps.onNext(5);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertValuesOnly(1, 3, 5);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertValuesOnly(1, 3, 5);
    ps.onNext(6);
    to.assertValuesOnly(1, 3, 5, 6);
    ps.onNext(7);
    ps.onComplete();
    to.assertResult(1, 3, 5, 6, 7);
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    to.assertResult(1, 3, 5, 6, 7);
}
Also used : 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