use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class ObservableDoOnEachTest method onCompleteCrash.
@Test
public void onCompleteCrash() {
Observable.wrap(new ObservableSource<Object>() {
@Override
public void subscribe(Observer<? super Object> observer) {
observer.onSubscribe(Disposable.empty());
observer.onComplete();
}
}).doOnComplete(new Action() {
@Override
public void run() throws Exception {
throw new IOException();
}
}).test().assertFailure(IOException.class);
}
use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class FlowableReplayEagerTruncateTest method issue2191_SchedulerUnsubscribeOnError.
/**
* Specifically test interaction with a Scheduler with subscribeOn.
*
* @throws Throwable functional interfaces declare throws Exception
*/
@SuppressWarnings("unchecked")
@Test
public void issue2191_SchedulerUnsubscribeOnError() throws Throwable {
// setup mocks
Consumer<Integer> sourceNext = mock(Consumer.class);
Action sourceCompleted = mock(Action.class);
Consumer<Throwable> sourceError = mock(Consumer.class);
Action sourceUnsubscribed = mock(Action.class);
final Scheduler mockScheduler = mock(Scheduler.class);
final Disposable mockSubscription = mock(Disposable.class);
Worker spiedWorker = workerSpy(mockSubscription);
Subscriber<Integer> mockObserverBeforeConnect = TestHelper.mockSubscriber();
Subscriber<Integer> mockObserverAfterConnect = TestHelper.mockSubscriber();
when(mockScheduler.createWorker()).thenReturn(spiedWorker);
// Flowable under test
Function<Integer, Integer> mockFunc = mock(Function.class);
IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
when(mockFunc.apply(1)).thenReturn(1);
when(mockFunc.apply(2)).thenThrow(illegalArgumentException);
ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).map(mockFunc).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).doOnError(sourceError).subscribeOn(mockScheduler).replay();
replay.subscribe(mockObserverBeforeConnect);
replay.subscribe(mockObserverBeforeConnect);
replay.connect();
replay.subscribe(mockObserverAfterConnect);
replay.subscribe(mockObserverAfterConnect);
verify(mockObserverBeforeConnect, times(2)).onSubscribe((Subscription) any());
verify(mockObserverAfterConnect, times(2)).onSubscribe((Subscription) any());
// verify interactions
verify(mockScheduler, times(1)).createWorker();
verify(spiedWorker, times(1)).schedule((Runnable) notNull());
verify(sourceNext, times(1)).accept(1);
verify(sourceError, times(1)).accept(illegalArgumentException);
verifyObserver(mockObserverBeforeConnect, 2, 2, illegalArgumentException);
verifyObserver(mockObserverAfterConnect, 2, 2, illegalArgumentException);
// FIXME publish also calls cancel
verify(spiedWorker, times(1)).dispose();
verify(sourceUnsubscribed, never()).run();
verifyNoMoreInteractions(sourceNext);
verifyNoMoreInteractions(sourceCompleted);
verifyNoMoreInteractions(sourceError);
verifyNoMoreInteractions(sourceUnsubscribed);
verifyNoMoreInteractions(spiedWorker);
verifyNoMoreInteractions(mockSubscription);
verifyNoMoreInteractions(mockScheduler);
verifyNoMoreInteractions(mockObserverBeforeConnect);
verifyNoMoreInteractions(mockObserverAfterConnect);
}
use of io.reactivex.rxjava3.functions.Action 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.Action in project RxJava by ReactiveX.
the class FlowableGroupByTest method firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete.
@Test
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsAndThenComplete() throws InterruptedException {
// there are two groups to first complete
final CountDownLatch first = new CountDownLatch(2);
final ArrayList<String> results = new ArrayList<>();
Flowable.unsafeCreate(new Publisher<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer> sub) {
sub.onSubscribe(new BooleanSubscription());
sub.onNext(1);
sub.onNext(2);
sub.onNext(1);
sub.onNext(2);
try {
first.await();
} catch (InterruptedException e) {
sub.onError(e);
return;
}
sub.onNext(3);
sub.onNext(3);
sub.onComplete();
}
}).groupBy(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer t) {
return t;
}
}).flatMap(new Function<GroupedFlowable<Integer, Integer>, Flowable<String>>() {
@Override
public Flowable<String> apply(final GroupedFlowable<Integer, Integer> group) {
if (group.getKey() < 3) {
return group.map(new Function<Integer, String>() {
@Override
public String apply(Integer t1) {
return "first groups: " + t1;
}
}).take(2).doOnComplete(new Action() {
@Override
public void run() {
first.countDown();
}
});
} else {
return group.map(new Function<Integer, String>() {
@Override
public String apply(Integer t1) {
return "last group: " + t1;
}
});
}
}
}).blockingForEach(new Consumer<String>() {
@Override
public void accept(String s) {
results.add(s);
}
});
System.out.println("Results: " + results);
assertEquals(6, results.size());
}
use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class FlowableMergeWithCompletableTest method normalBackpressured.
@Test
public void normalBackpressured() {
final TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
Flowable.range(1, 5).mergeWith(Completable.fromAction(new Action() {
@Override
public void run() throws Exception {
ts.onNext(100);
}
})).subscribe(ts);
ts.assertValue(100).requestMore(2).assertValues(100, 1, 2).requestMore(2).assertValues(100, 1, 2, 3, 4).requestMore(1).assertResult(100, 1, 2, 3, 4, 5);
}
Aggregations