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());
}
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());
}
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);
}
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));
}
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));
}
Aggregations