Search in sources :

Example 71 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class FlowableObserveOnTest method badSource.

@Test
public void badSource() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        TestScheduler scheduler = new TestScheduler();
        TestSubscriber<Integer> ts = new Flowable<Integer>() {

            @Override
            protected void subscribeActual(Subscriber<? super Integer> subscriber) {
                subscriber.onSubscribe(new BooleanSubscription());
                subscriber.onComplete();
                subscriber.onNext(1);
                subscriber.onError(new TestException());
                subscriber.onComplete();
            }
        }.observeOn(scheduler).test();
        scheduler.triggerActions();
        ts.assertResult();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) BaseObserveOnSubscriber(io.reactivex.rxjava3.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber) Test(org.junit.Test)

Example 72 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class FlowableObserveOnTest method observeOnTheSameSchedulerTwice.

@Test
public void observeOnTheSameSchedulerTwice() {
    Scheduler scheduler = ImmediateThinScheduler.INSTANCE;
    Flowable<Integer> f = Flowable.just(1, 2, 3);
    Flowable<Integer> f2 = f.observeOn(scheduler);
    Subscriber<Object> subscriber1 = TestHelper.mockSubscriber();
    Subscriber<Object> subscriber2 = TestHelper.mockSubscriber();
    InOrder inOrder1 = inOrder(subscriber1);
    InOrder inOrder2 = inOrder(subscriber2);
    f2.subscribe(subscriber1);
    f2.subscribe(subscriber2);
    inOrder1.verify(subscriber1, times(1)).onNext(1);
    inOrder1.verify(subscriber1, times(1)).onNext(2);
    inOrder1.verify(subscriber1, times(1)).onNext(3);
    inOrder1.verify(subscriber1, times(1)).onComplete();
    verify(subscriber1, never()).onError(any(Throwable.class));
    inOrder1.verifyNoMoreInteractions();
    inOrder2.verify(subscriber2, times(1)).onNext(1);
    inOrder2.verify(subscriber2, times(1)).onNext(2);
    inOrder2.verify(subscriber2, times(1)).onNext(3);
    inOrder2.verify(subscriber2, times(1)).onComplete();
    verify(subscriber2, never()).onError(any(Throwable.class));
    inOrder2.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Test(org.junit.Test)

Example 73 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class FlowableReplayEagerTruncateTest method issue2191_SchedulerUnsubscribeOnError.

/**
 * Specifically test interaction with a Scheduler with subscribeOn.
 *
 * @throws Throwable functional interfaces declare throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void issue2191_SchedulerUnsubscribeOnError() throws Throwable {
    // setup mocks
    Consumer<Integer> sourceNext = mock(Consumer.class);
    Action sourceCompleted = mock(Action.class);
    Consumer<Throwable> sourceError = mock(Consumer.class);
    Action sourceUnsubscribed = mock(Action.class);
    final Scheduler mockScheduler = mock(Scheduler.class);
    final Disposable mockSubscription = mock(Disposable.class);
    Worker spiedWorker = workerSpy(mockSubscription);
    Subscriber<Integer> mockObserverBeforeConnect = TestHelper.mockSubscriber();
    Subscriber<Integer> mockObserverAfterConnect = TestHelper.mockSubscriber();
    when(mockScheduler.createWorker()).thenReturn(spiedWorker);
    // Flowable under test
    Function<Integer, Integer> mockFunc = mock(Function.class);
    IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
    when(mockFunc.apply(1)).thenReturn(1);
    when(mockFunc.apply(2)).thenThrow(illegalArgumentException);
    ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).map(mockFunc).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).doOnError(sourceError).subscribeOn(mockScheduler).replay();
    replay.subscribe(mockObserverBeforeConnect);
    replay.subscribe(mockObserverBeforeConnect);
    replay.connect();
    replay.subscribe(mockObserverAfterConnect);
    replay.subscribe(mockObserverAfterConnect);
    verify(mockObserverBeforeConnect, times(2)).onSubscribe((Subscription) any());
    verify(mockObserverAfterConnect, times(2)).onSubscribe((Subscription) any());
    // verify interactions
    verify(mockScheduler, times(1)).createWorker();
    verify(spiedWorker, times(1)).schedule((Runnable) notNull());
    verify(sourceNext, times(1)).accept(1);
    verify(sourceError, times(1)).accept(illegalArgumentException);
    verifyObserver(mockObserverBeforeConnect, 2, 2, illegalArgumentException);
    verifyObserver(mockObserverAfterConnect, 2, 2, illegalArgumentException);
    // FIXME publish also calls cancel
    verify(spiedWorker, times(1)).dispose();
    verify(sourceUnsubscribed, never()).run();
    verifyNoMoreInteractions(sourceNext);
    verifyNoMoreInteractions(sourceCompleted);
    verifyNoMoreInteractions(sourceError);
    verifyNoMoreInteractions(sourceUnsubscribed);
    verifyNoMoreInteractions(spiedWorker);
    verifyNoMoreInteractions(mockSubscription);
    verifyNoMoreInteractions(mockScheduler);
    verifyNoMoreInteractions(mockObserverBeforeConnect);
    verifyNoMoreInteractions(mockObserverAfterConnect);
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker)

Example 74 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class ObservableObserveOnTest method observeOnTheSameSchedulerTwice.

@Test
public void observeOnTheSameSchedulerTwice() {
    Scheduler scheduler = ImmediateThinScheduler.INSTANCE;
    Observable<Integer> o = Observable.just(1, 2, 3);
    Observable<Integer> o2 = o.observeOn(scheduler);
    Observer<Object> observer1 = TestHelper.mockObserver();
    Observer<Object> observer2 = TestHelper.mockObserver();
    InOrder inOrder1 = inOrder(observer1);
    InOrder inOrder2 = inOrder(observer2);
    o2.subscribe(observer1);
    o2.subscribe(observer2);
    inOrder1.verify(observer1, times(1)).onNext(1);
    inOrder1.verify(observer1, times(1)).onNext(2);
    inOrder1.verify(observer1, times(1)).onNext(3);
    inOrder1.verify(observer1, times(1)).onComplete();
    verify(observer1, never()).onError(any(Throwable.class));
    inOrder1.verifyNoMoreInteractions();
    inOrder2.verify(observer2, times(1)).onNext(1);
    inOrder2.verify(observer2, times(1)).onNext(2);
    inOrder2.verify(observer2, times(1)).onNext(3);
    inOrder2.verify(observer2, times(1)).onComplete();
    verify(observer2, never()).onError(any(Throwable.class));
    inOrder2.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) DisposeTrackingScheduler(io.reactivex.rxjava3.internal.operators.flowable.FlowableObserveOnTest.DisposeTrackingScheduler) Test(org.junit.Test)

Example 75 with Scheduler

use of io.reactivex.rxjava3.core.Scheduler in project RxJava by ReactiveX.

the class ObservableSkipLastTimedTest method skipLastTimedErrorBeforeTime.

@Test
public void skipLastTimedErrorBeforeTime() {
    TestScheduler scheduler = new TestScheduler();
    PublishSubject<Integer> source = PublishSubject.create();
    Observable<Integer> result = source.skipLast(1, TimeUnit.SECONDS, scheduler);
    Observer<Object> o = TestHelper.mockObserver();
    result.subscribe(o);
    source.onNext(1);
    source.onNext(2);
    source.onNext(3);
    source.onError(new TestException());
    scheduler.advanceTimeBy(1050, TimeUnit.MILLISECONDS);
    verify(o).onError(any(TestException.class));
    verify(o, never()).onComplete();
    verify(o, never()).onNext(any());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)169 Disposable (io.reactivex.rxjava3.disposables.Disposable)69 Scheduler (io.reactivex.rxjava3.core.Scheduler)61 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)54 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)50 TestException (io.reactivex.rxjava3.exceptions.TestException)34 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)32 InOrder (org.mockito.InOrder)32 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)20 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)8 Observable (io.reactivex.rxjava3.core.Observable)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 Action (io.reactivex.rxjava3.functions.Action)5 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5