Search in sources :

Example 16 with Consumer

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

the class SingleDoAfterSuccessTest method consumerThrows.

@Test
public void consumerThrows() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Single.just(1).doAfterSuccess(new Consumer<Integer>() {

            @Override
            public void accept(Integer e) throws Exception {
                throw new TestException();
            }
        }).test().assertResult(1);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 17 with Consumer

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

the class SingleTimerTest method timer.

@Test
public void timer() {
    final TestScheduler testScheduler = new TestScheduler();
    final AtomicLong atomicLong = new AtomicLong();
    Single.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Consumer<Long>() {

        @Override
        public void accept(final Long value) throws Exception {
            atomicLong.incrementAndGet();
        }
    });
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(1, atomicLong.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 18 with Consumer

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

the class ObservableDelaySubscriptionOtherTest method noPrematureSubscription.

@Test
public void noPrematureSubscription() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> to = new TestObserver<>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.just(1).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(to);
    to.assertNotComplete();
    to.assertNoErrors();
    to.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onNext(1);
    Assert.assertEquals("No subscription", 1, subscribed.get());
    to.assertValue(1);
    to.assertNoErrors();
    to.assertComplete();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.rxjava3.disposables.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 19 with Consumer

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

the class ObservableDelaySubscriptionOtherTest method noPrematureSubscriptionToError.

@Test
public void noPrematureSubscriptionToError() {
    PublishSubject<Object> other = PublishSubject.create();
    TestObserver<Integer> to = new TestObserver<>();
    final AtomicInteger subscribed = new AtomicInteger();
    Observable.<Integer>error(new TestException()).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(Disposable d) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(to);
    to.assertNotComplete();
    to.assertNoErrors();
    to.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onComplete();
    Assert.assertEquals("No subscription", 1, subscribed.get());
    to.assertNoValues();
    to.assertNotComplete();
    to.assertError(TestException.class);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Disposable(io.reactivex.rxjava3.disposables.Disposable) TestException(io.reactivex.rxjava3.exceptions.TestException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestObserver(io.reactivex.rxjava3.observers.TestObserver)

Example 20 with Consumer

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

the class ObservableFlatMapTest method failingFusedInnerCancelsSource.

@Test
public void failingFusedInnerCancelsSource() {
    final AtomicInteger counter = new AtomicInteger();
    Observable.range(1, 5).doOnNext(new Consumer<Integer>() {

        @Override
        public void accept(Integer v) throws Exception {
            counter.getAndIncrement();
        }
    }).flatMap(new Function<Integer, Observable<Integer>>() {

        @Override
        public Observable<Integer> apply(Integer v) throws Exception {
            return Observable.<Integer>fromIterable(new Iterable<Integer>() {

                @Override
                public Iterator<Integer> iterator() {
                    return new Iterator<Integer>() {

                        @Override
                        public boolean hasNext() {
                            return true;
                        }

                        @Override
                        public Integer next() {
                            throw new TestException();
                        }

                        @Override
                        public void remove() {
                            throw new UnsupportedOperationException();
                        }
                    };
                }
            });
        }
    }).test().assertFailure(TestException.class);
    assertEquals(1, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observable(io.reactivex.rxjava3.core.Observable) IOException(java.io.IOException)

Aggregations

Test (org.junit.Test)98 TestException (io.reactivex.rxjava3.exceptions.TestException)57 Disposable (io.reactivex.rxjava3.disposables.Disposable)39 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)22 IOException (java.io.IOException)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 InOrder (org.mockito.InOrder)17 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Observable (io.reactivex.rxjava3.core.Observable)5 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)5 Consumer (io.reactivex.rxjava3.functions.Consumer)5 CompositeException (io.reactivex.rxjava3.exceptions.CompositeException)4 ForEachWhileSubscriber (io.reactivex.rxjava3.internal.subscribers.ForEachWhileSubscriber)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Observer (io.reactivex.rxjava3.core.Observer)3 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)3 ArgsToString (io.reactivex.rxjava3.internal.operators.flowable.FlowableZipTest.ArgsToString)3