Search in sources :

Example 46 with Flowable

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

the class FlowableReplayTest method unsafeChildOnErrorThrowsSizeBound.

@Test
public void unsafeChildOnErrorThrowsSizeBound() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        Flowable<Integer> source = Flowable.<Integer>error(new IOException()).replay(1000).autoConnect();
        TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {

            @Override
            public void onError(Throwable t) {
                super.onError(t);
                throw new TestException();
            }
        };
        source.subscribe(ts);
        ts.assertFailure(IOException.class);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    });
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) IOException(java.io.IOException)

Example 47 with Flowable

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

the class FlowableReplayTest 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 48 with Flowable

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

the class FlowableReplayTest method unsafeChildOnErrorThrows.

@Test
public void unsafeChildOnErrorThrows() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        Flowable<Integer> source = Flowable.<Integer>error(new IOException()).replay().autoConnect();
        TestSubscriber<Integer> ts = new TestSubscriber<Integer>() {

            @Override
            public void onError(Throwable t) {
                super.onError(t);
                throw new TestException();
            }
        };
        source.subscribe(ts);
        ts.assertFailure(IOException.class);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    });
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) IOException(java.io.IOException)

Example 49 with Flowable

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

the class FlowableReplayTest method delayedUpstreamSubscription.

@Test
public void delayedUpstreamSubscription() {
    AtomicReference<Subscriber<? super Integer>> ref = new AtomicReference<>();
    Flowable<Integer> f = Flowable.<Integer>unsafeCreate(ref::set);
    TestSubscriber<Integer> ts = f.replay().autoConnect().test();
    AtomicLong requested = new AtomicLong();
    ref.get().onSubscribe(new Subscription() {

        @Override
        public void request(long n) {
            BackpressureHelper.add(requested, n);
        }

        @Override
        public void cancel() {
        }
    });
    assertEquals(Long.MAX_VALUE, requested.get());
    ref.get().onComplete();
    ts.assertResult();
}
Also used : TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Example 50 with Flowable

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

the class FlowableReplayTest method issue2191_SchedulerUnsubscribe.

/**
 * Specifically test interaction with a Scheduler with subscribeOn.
 *
 * @throws Throwable functional interfaces declare throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void issue2191_SchedulerUnsubscribe() throws Throwable {
    // setup mocks
    Consumer<Integer> sourceNext = mock(Consumer.class);
    Action sourceCompleted = mock(Action.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
    ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).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(sourceNext, times(1)).accept(1);
    verify(sourceNext, times(1)).accept(2);
    verify(sourceNext, times(1)).accept(3);
    verify(sourceCompleted, times(1)).run();
    verify(mockScheduler, times(1)).createWorker();
    verify(spiedWorker, times(1)).schedule((Runnable) notNull());
    verifyObserverMock(mockObserverBeforeConnect, 2, 6);
    verifyObserverMock(mockObserverAfterConnect, 2, 6);
    // FIXME publish calls cancel too
    verify(spiedWorker, times(1)).dispose();
    verify(sourceUnsubscribed, never()).run();
    verifyNoMoreInteractions(sourceNext);
    verifyNoMoreInteractions(sourceCompleted);
    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)

Aggregations

Test (org.junit.Test)159 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)121 TestException (io.reactivex.rxjava3.exceptions.TestException)95 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)49 InOrder (org.mockito.InOrder)41 IOException (java.io.IOException)27 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 Disposable (io.reactivex.rxjava3.disposables.Disposable)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 io.reactivex.rxjava3.core (io.reactivex.rxjava3.core)11 io.reactivex.rxjava3.testsupport (io.reactivex.rxjava3.testsupport)9 java.util (java.util)9 Assert (org.junit.Assert)9 Subscriber (org.reactivestreams.Subscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 java.util.concurrent.atomic (java.util.concurrent.atomic)8 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 io.reactivex.rxjava3.functions (io.reactivex.rxjava3.functions)7 Functions (io.reactivex.rxjava3.internal.functions.Functions)7 RxJavaPlugins (io.reactivex.rxjava3.plugins.RxJavaPlugins)7