Search in sources :

Example 91 with Consumer

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

the class MaybeTimerTest method timer.

@Test
public void timer() {
    final TestScheduler testScheduler = new TestScheduler();
    final AtomicLong atomicLong = new AtomicLong();
    Maybe.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 92 with Consumer

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

the class FlowableDelayTest method onErrorCalledOnScheduler.

@Test
public void onErrorCalledOnScheduler() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Thread> thread = new AtomicReference<>();
    Flowable.<String>error(new Exception()).delay(0, TimeUnit.MILLISECONDS, Schedulers.newThread()).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            thread.set(Thread.currentThread());
            latch.countDown();
        }
    }).onErrorResumeWith(Flowable.<String>empty()).subscribe();
    latch.await();
    assertNotEquals(Thread.currentThread(), thread.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

Example 93 with Consumer

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

the class FlowableDoOnEachTest method ignoreCancel.

@Test
public void ignoreCancel() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.fromPublisher(new Publisher<Object>() {

            @Override
            public void subscribe(Subscriber<? super Object> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onNext(1);
                s.onNext(2);
                s.onError(new IOException());
                s.onComplete();
            }
        }).doOnNext(new Consumer<Object>() {

            @Override
            public void accept(Object e) throws Exception {
                throw new TestException();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) IOException(java.io.IOException) IOException(java.io.IOException)

Example 94 with Consumer

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

the class FlowableDelaySubscriptionOtherTest method noSubscriptionIfOtherErrors.

@Test
public void noSubscriptionIfOtherErrors() {
    PublishProcessor<Object> other = PublishProcessor.create();
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    final AtomicInteger subscribed = new AtomicInteger();
    Flowable.<Integer>error(new TestException()).doOnSubscribe(new Consumer<Subscription>() {

        @Override
        public void accept(Subscription s) {
            subscribed.getAndIncrement();
        }
    }).delaySubscription(other).subscribe(ts);
    ts.assertNotComplete();
    ts.assertNoErrors();
    ts.assertNoValues();
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    other.onError(new TestException());
    Assert.assertEquals("Premature subscription", 0, subscribed.get());
    ts.assertNoValues();
    ts.assertNotComplete();
    ts.assertError(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Subscription(org.reactivestreams.Subscription)

Example 95 with Consumer

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

the class FlowableGenerateTest method disposerThrows.

@Test
public void disposerThrows() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.generate(new Supplier<Object>() {

            @Override
            public Object get() throws Exception {
                return 1;
            }
        }, new BiConsumer<Object, Emitter<Object>>() {

            @Override
            public void accept(Object s, Emitter<Object> e) throws Exception {
                e.onComplete();
            }
        }, new Consumer<Object>() {

            @Override
            public void accept(Object d) throws Exception {
                throw new TestException();
            }
        }).test().assertResult();
        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)

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