Search in sources :

Example 26 with Action

use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.

the class FlowableRepeatTest method noCancelPreviousRepeat.

@Test
public void noCancelPreviousRepeat() {
    final AtomicInteger counter = new AtomicInteger();
    Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.repeat(5).test().assertResult(1, 1, 1, 1, 1);
    assertEquals(0, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 27 with Action

use of io.reactivex.rxjava3.functions.Action 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 28 with Action

use of io.reactivex.rxjava3.functions.Action 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)

Example 29 with Action

use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.

the class FlowableThrottleLatestTest method missingBackpressureExceptionFirst.

@Test
public void missingBackpressureExceptionFirst() throws Throwable {
    TestScheduler sch = new TestScheduler();
    Action onCancel = mock(Action.class);
    Flowable.just(1, 2).doOnCancel(onCancel).throttleLatest(1, TimeUnit.MINUTES, sch).test(0).assertFailure(MissingBackpressureException.class);
    verify(onCancel).run();
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 30 with Action

use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.

the class FlowableThrottleLatestTest method missingBackpressureExceptionLatestComplete.

@Test
public void missingBackpressureExceptionLatestComplete() throws Throwable {
    TestScheduler sch = new TestScheduler();
    Action onCancel = mock(Action.class);
    PublishProcessor<Integer> pp = PublishProcessor.create();
    TestSubscriber<Integer> ts = pp.doOnCancel(onCancel).throttleLatest(1, TimeUnit.SECONDS, sch, true).test(1);
    pp.onNext(1);
    pp.onNext(2);
    ts.assertValuesOnly(1);
    pp.onComplete();
    ts.assertFailure(MissingBackpressureException.class, 1);
    verify(onCancel, never()).run();
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)109 TestException (io.reactivex.rxjava3.exceptions.TestException)64 Action (io.reactivex.rxjava3.functions.Action)49 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)13 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 TestObserver (io.reactivex.rxjava3.observers.TestObserver)10 Disposable (io.reactivex.rxjava3.disposables.Disposable)9 IOException (java.io.IOException)9 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 Observable (io.reactivex.rxjava3.core.Observable)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)3 Flowable (io.reactivex.rxjava3.core.Flowable)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ConditionalSubscriber (io.reactivex.rxjava3.operators.ConditionalSubscriber)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3