Search in sources :

Example 91 with TestScheduler

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

the class FlowableTimeoutTests method shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout.

@Test
public void shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout() throws InterruptedException {
    // From https://github.com/ReactiveX/RxJava/pull/951
    final Subscription s = mock(Subscription.class);
    Flowable<String> never = Flowable.unsafeCreate(new Publisher<String>() {

        @Override
        public void subscribe(Subscriber<? super String> subscriber) {
            subscriber.onSubscribe(s);
        }
    });
    TestScheduler testScheduler = new TestScheduler();
    Flowable<String> observableWithTimeout = never.timeout(1000, TimeUnit.MILLISECONDS, testScheduler);
    TestSubscriberEx<String> subscriber = new TestSubscriberEx<>();
    observableWithTimeout.subscribe(subscriber);
    testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS);
    subscriber.assertFailureAndMessage(TimeoutException.class, timeoutMessage(1000, TimeUnit.MILLISECONDS));
    verify(s, times(1)).cancel();
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Example 92 with TestScheduler

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

the class FlowableSkipTimedTest method skipTimedErrorAfterTime.

@Test
public void skipTimedErrorAfterTime() {
    TestScheduler scheduler = new TestScheduler();
    PublishProcessor<Integer> source = PublishProcessor.create();
    Flowable<Integer> result = source.skip(1, TimeUnit.SECONDS, scheduler);
    Subscriber<Object> subscriber = TestHelper.mockSubscriber();
    result.subscribe(subscriber);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    source.onNext(4);
    source.onNext(5);
    source.onNext(6);
    source.onError(new TestException());
    InOrder inOrder = inOrder(subscriber);
    inOrder.verify(subscriber, never()).onNext(1);
    inOrder.verify(subscriber, never()).onNext(2);
    inOrder.verify(subscriber, never()).onNext(3);
    inOrder.verify(subscriber).onNext(4);
    inOrder.verify(subscriber).onNext(5);
    inOrder.verify(subscriber).onNext(6);
    inOrder.verify(subscriber).onError(any(TestException.class));
    inOrder.verifyNoMoreInteractions();
    verify(subscriber, never()).onComplete();
}
Also used : InOrder(org.mockito.InOrder) TestException(io.reactivex.rxjava3.exceptions.TestException) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 93 with TestScheduler

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

the class SingleTimeoutTest method errorTimeoutRace.

@Test
public void errorTimeoutRace() {
    final TestException ex = new TestException();
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        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.onError(ex);
                }
            };
            Runnable r2 = new Runnable() {

                @Override
                public void run() {
                    sch.advanceTimeBy(1, TimeUnit.MILLISECONDS);
                }
            };
            TestHelper.race(r1, r2);
            if (!fallback.hasObservers()) {
                to.assertFailure(TestException.class);
            } else {
                to.assertEmpty();
            }
            if (!errors.isEmpty()) {
                TestHelper.assertUndeliverable(errors, 0, TestException.class);
            }
        }
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 94 with TestScheduler

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

the class SingleTimeoutTest method fallbackDispose.

@Test
public void fallbackDispose() {
    TestScheduler sch = new TestScheduler();
    SingleSubject<Integer> subj = SingleSubject.create();
    SingleSubject<Integer> fallback = SingleSubject.create();
    TestObserver<Integer> to = subj.timeout(1, TimeUnit.SECONDS, sch, fallback).test();
    assertFalse(fallback.hasObservers());
    sch.advanceTimeBy(1, TimeUnit.SECONDS);
    assertFalse(subj.hasObservers());
    assertTrue(fallback.hasObservers());
    to.dispose();
    assertFalse(fallback.hasObservers());
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 95 with TestScheduler

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

the class SingleTimeoutTest method disposeWhenFallback.

@Test
public void disposeWhenFallback() {
    TestScheduler sch = new TestScheduler();
    SingleSubject<Integer> subj = SingleSubject.create();
    subj.timeout(1, TimeUnit.SECONDS, sch, Single.just(1)).test(true).assertEmpty();
    assertFalse(subj.hasObservers());
}
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