Search in sources :

Example 21 with Action

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

the class ObservableRepeatTest method noCancelPreviousRepeat.

@Test
public void noCancelPreviousRepeat() {
    final AtomicInteger counter = new AtomicInteger();
    Observable<Integer> source = Observable.just(1).doOnDispose(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 22 with Action

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

the class FlowableSubscriberTest method onNextCrashes.

@Test
public void onNextCrashes() {
    final TestSubscriber<Integer> ts = new TestSubscriber<>();
    ts.onSubscribe(new BooleanSubscription());
    ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

        @Override
        public boolean test(Integer v) throws Exception {
            throw new TestException();
        }
    }, new Consumer<Throwable>() {

        @Override
        public void accept(Throwable e) throws Exception {
            ts.onError(e);
        }
    }, new Action() {

        @Override
        public void run() throws Exception {
            ts.onComplete();
        }
    });
    BooleanSubscription b = new BooleanSubscription();
    s.onSubscribe(b);
    s.onNext(1);
    assertTrue(b.isCancelled());
    ts.assertFailure(TestException.class);
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) Test(org.junit.Test)

Example 23 with Action

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

the class FlowableSubscriberTest method suppressAfterCompleteEvents.

@Test
public void suppressAfterCompleteEvents() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        final TestSubscriber<Integer> ts = new TestSubscriber<>();
        ts.onSubscribe(new BooleanSubscription());
        ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {

            @Override
            public boolean test(Integer v) throws Exception {
                ts.onNext(v);
                return true;
            }
        }, new Consumer<Throwable>() {

            @Override
            public void accept(Throwable e) throws Exception {
                ts.onError(e);
            }
        }, new Action() {

            @Override
            public void run() throws Exception {
                ts.onComplete();
            }
        });
        s.onComplete();
        s.onNext(1);
        s.onError(new TestException());
        s.onComplete();
        ts.assertResult();
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) ForEachWhileSubscriber(io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber) Test(org.junit.Test)

Example 24 with Action

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

the class DisposableTest method fromAction.

@Test
public void fromAction() throws Throwable {
    Action action = mock(Action.class);
    Disposable d = Disposable.fromAction(action);
    assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=false, "));
    d.dispose();
    assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=true, "));
    d.dispose();
    assertTrue(d.toString(), d.toString().contains("ActionDisposable(disposed=true, "));
    verify(action, times(1)).run();
}
Also used : Action(io.reactivex.rxjava3.functions.Action) RxJavaTest(io.reactivex.rxjava3.core.RxJavaTest) Test(org.junit.Test)

Example 25 with Action

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

the class CompletableRetryTest method retryTimesPredicateWithMatchingPredicate.

@Test
public void retryTimesPredicateWithMatchingPredicate() {
    final AtomicInteger atomicInteger = new AtomicInteger(3);
    final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0);
    Completable.fromAction(new Action() {

        @Override
        public void run() throws Exception {
            numberOfSubscribeCalls.incrementAndGet();
            if (atomicInteger.decrementAndGet() != 0) {
                throw new RuntimeException();
            }
            throw new IllegalArgumentException();
        }
    }).retry(Integer.MAX_VALUE, new Predicate<Throwable>() {

        @Override
        public boolean test(final Throwable throwable) throws Exception {
            return !(throwable instanceof IllegalArgumentException);
        }
    }).test().assertFailure(IllegalArgumentException.class);
    assertEquals(3, numberOfSubscribeCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestException(io.reactivex.rxjava3.exceptions.TestException) 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