use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class ObservableDelayTest method delayWithObservableSubscriptionFunctionThrows.
@Test
public void delayWithObservableSubscriptionFunctionThrows() {
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> delay = PublishSubject.create();
Supplier<Observable<Integer>> subFunc = new Supplier<Observable<Integer>>() {
@Override
public Observable<Integer> get() {
throw new TestException();
}
};
Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return delay;
}
};
Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
source.delay(Observable.defer(subFunc), delayFunc).subscribe(o);
source.onNext(1);
delay.onNext(1);
source.onNext(2);
inOrder.verify(o).onError(any(TestException.class));
inOrder.verifyNoMoreInteractions();
verify(o, never()).onNext(any());
verify(o, never()).onComplete();
}
use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class ObservableDelayTest method delayWithObservableSubscriptionRunCompletion.
@Test
public void delayWithObservableSubscriptionRunCompletion() {
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> sdelay = PublishSubject.create();
final PublishSubject<Integer> delay = PublishSubject.create();
Supplier<Observable<Integer>> subFunc = new Supplier<Observable<Integer>>() {
@Override
public Observable<Integer> get() {
return sdelay;
}
};
Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return delay;
}
};
Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
source.delay(Observable.defer(subFunc), delayFunc).subscribe(o);
source.onNext(1);
sdelay.onComplete();
source.onNext(2);
delay.onNext(2);
inOrder.verify(o).onNext(2);
inOrder.verifyNoMoreInteractions();
verify(o, never()).onError(any(Throwable.class));
verify(o, never()).onComplete();
}
use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class ObservableFromSupplierTest 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";
}
});
Observable<String> fromSupplierObservable = Observable.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);
}
use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class FlowableRetryTest method noCancelPreviousRetryWhile.
@Test
public void noCancelPreviousRetryWhile() {
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, Functions.alwaysTrue()).test().assertResult(1);
assertEquals(0, counter.get());
}
use of io.reactivex.rxjava3.functions.Supplier in project RxJava by ReactiveX.
the class FlowableFromSupplierTest method shouldAllowToThrowCheckedException.
@Test
public void shouldAllowToThrowCheckedException() {
final Exception checkedException = new Exception("test exception");
Flowable<Object> fromSupplierFlowable = Flowable.fromSupplier(new Supplier<Object>() {
@Override
public Object get() throws Exception {
throw checkedException;
}
});
Subscriber<Object> subscriber = TestHelper.mockSubscriber();
fromSupplierFlowable.subscribe(subscriber);
verify(subscriber).onSubscribe(any(Subscription.class));
verify(subscriber).onError(checkedException);
verifyNoMoreInteractions(subscriber);
}
Aggregations