Search in sources :

Example 36 with Supplier

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

the class FlowableRetryTest method noCancelPreviousRetry.

@Test
public void noCancelPreviousRetry() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {

        @Override
        public Flowable<Integer> get() throws Exception {
            if (times.getAndIncrement() < 4) {
                return Flowable.error(new TestException());
            }
            return Flowable.just(1);
        }
    }).doOnCancel(new Action() {

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

Example 37 with Supplier

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

the class FlowableRetryTest method noCancelPreviousRetryUntil.

@Test
public void noCancelPreviousRetryUntil() {
    final AtomicInteger counter = new AtomicInteger();
    final AtomicInteger times = new AtomicInteger();
    Flowable<Integer> source = Flowable.defer(new Supplier<Flowable<Integer>>() {

        @Override
        public Flowable<Integer> get() throws Exception {
            if (times.getAndIncrement() < 4) {
                return Flowable.error(new TestException());
            }
            return Flowable.just(1);
        }
    }).doOnCancel(new Action() {

        @Override
        public void run() throws Exception {
            counter.getAndIncrement();
        }
    });
    source.retryUntil(new BooleanSupplier() {

        @Override
        public boolean getAsBoolean() throws Exception {
            return false;
        }
    }).test().assertResult(1);
    assertEquals(0, counter.get());
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 38 with Supplier

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

the class FlowableFromSupplierTest method shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission.

@SuppressWarnings("unchecked")
@Test
public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Throwable {
    Supplier<String> func = mock(Supplier.class);
    final CountDownLatch funcLatch = new CountDownLatch(1);
    final CountDownLatch observerLatch = new CountDownLatch(1);
    when(func.get()).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            observerLatch.countDown();
            try {
                funcLatch.await();
            } catch (InterruptedException e) {
                // It's okay, unsubscription causes Thread interruption
                // Restoring interruption status of the Thread
                Thread.currentThread().interrupt();
            }
            return "should_not_be_delivered";
        }
    });
    Flowable<String> fromSupplierFlowable = Flowable.fromSupplier(func);
    Subscriber<String> subscriber = TestHelper.mockSubscriber();
    TestSubscriber<String> outer = new TestSubscriber<>(subscriber);
    fromSupplierFlowable.subscribeOn(Schedulers.computation()).subscribe(outer);
    // Wait until func will be invoked
    observerLatch.await();
    // Unsubscribing before emission
    outer.cancel();
    // Emitting result
    funcLatch.countDown();
    // func must be invoked
    verify(func).get();
    // Observer must not be notified at all
    verify(subscriber).onSubscribe(any(Subscription.class));
    verifyNoMoreInteractions(subscriber);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 39 with Supplier

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

the class RxJavaPluginsTest method overrideInitSingleScheduler.

@Test
public void overrideInitSingleScheduler() {
    // make sure the Schedulers is initialized
    final Scheduler s = Schedulers.single();
    Supplier<Scheduler> c = new Supplier<Scheduler>() {

        @Override
        public Scheduler get() throws Exception {
            return s;
        }
    };
    try {
        RxJavaPlugins.setInitSingleSchedulerHandler(initReplaceWithImmediate);
        assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initSingleScheduler(c));
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    assertSame(s, RxJavaPlugins.initSingleScheduler(c));
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Test(org.junit.Test)

Example 40 with Supplier

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

the class RxJavaPluginsTest method overrideInitComputationScheduler.

@Test
public void overrideInitComputationScheduler() {
    // make sure the Schedulers is initialized
    final Scheduler s = Schedulers.computation();
    Supplier<Scheduler> c = new Supplier<Scheduler>() {

        @Override
        public Scheduler get() throws Exception {
            return s;
        }
    };
    try {
        RxJavaPlugins.setInitComputationSchedulerHandler(initReplaceWithImmediate);
        assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initComputationScheduler(c));
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    assertSame(s, RxJavaPlugins.initComputationScheduler(c));
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)37 TestException (io.reactivex.rxjava3.exceptions.TestException)33 Observable (io.reactivex.rxjava3.core.Observable)13 IOException (java.io.IOException)9 InOrder (org.mockito.InOrder)8 java.util (java.util)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Disposable (io.reactivex.rxjava3.disposables.Disposable)5 Supplier (io.reactivex.rxjava3.functions.Supplier)5 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)5 TestObserver (io.reactivex.rxjava3.observers.TestObserver)5 Observer (io.reactivex.rxjava3.core.Observer)4 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)4 ImmutableList (com.google.common.collect.ImmutableList)3 io.reactivex.rxjava3.processors (io.reactivex.rxjava3.processors)3 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)3 java.util.function (java.util.function)3 java.util.stream (java.util.stream)3 Assert.assertFalse (org.junit.Assert.assertFalse)3 SQLUtils (com.alibaba.druid.sql.SQLUtils)2