use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class ObservableRetryTest method noCancelPreviousRepeatWhen.
@Test
public void noCancelPreviousRepeatWhen() {
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger times = new AtomicInteger();
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> get() throws Exception {
if (times.get() < 4) {
return Observable.error(new TestException());
}
return Observable.just(1);
}
}).doOnDispose(new Action() {
@Override
public void run() throws Exception {
counter.getAndIncrement();
}
});
source.retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(Observable<Throwable> e) throws Exception {
return e.takeWhile(new Predicate<Object>() {
@Override
public boolean test(Object v) throws Exception {
return times.getAndIncrement() < 4;
}
});
}
}).test().assertResult(1);
assertEquals(0, counter.get());
}
use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class ObservableRetryTest method noCancelPreviousRetry.
@Test
public void noCancelPreviousRetry() {
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger times = new AtomicInteger();
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> get() throws Exception {
if (times.getAndIncrement() < 4) {
return Observable.error(new TestException());
}
return Observable.just(1);
}
}).doOnDispose(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 CompletableFromSupplierTest method fromSupplier.
@Test
public void fromSupplier() {
final AtomicInteger atomicInteger = new AtomicInteger();
Completable.fromSupplier(new Supplier<Object>() {
@Override
public Object get() throws Exception {
atomicInteger.incrementAndGet();
return null;
}
}).test().assertResult();
assertEquals(1, atomicInteger.get());
}
use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class CompletableFromSupplierTest method fromActionErrorsDisposed.
@Test
@SuppressUndeliverable
public void fromActionErrorsDisposed() {
final AtomicInteger calls = new AtomicInteger();
Completable.fromSupplier(new Supplier<Object>() {
@Override
public Object get() throws Exception {
calls.incrementAndGet();
throw new TestException();
}
}).test(true).assertEmpty();
assertEquals(1, calls.get());
}
use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class CompletableFromSupplierTest 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";
}
});
Completable fromSupplierObservable = Completable.fromSupplier(func);
Observer<Object> observer = TestHelper.mockObserver();
TestObserver<String> outer = new TestObserver<>(observer);
fromSupplierObservable.subscribeOn(Schedulers.computation()).subscribe(outer);
// Wait until func will be invoked
observerLatch.await();
// Unsubscribing before emission
outer.dispose();
// Emitting result
funcLatch.countDown();
// func must be invoked
verify(func).get();
// Observer must not be notified at all
verify(observer).onSubscribe(any(Disposable.class));
verifyNoMoreInteractions(observer);
}
Aggregations