use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableSubscriberTest method onCompleteThrows.
@Test
public void onCompleteThrows() {
ForEachWhileSubscriber<Integer> s = new ForEachWhileSubscriber<>(new Predicate<Integer>() {
@Override
public boolean test(Integer v) throws Exception {
return true;
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable e) throws Exception {
}
}, new Action() {
@Override
public void run() throws Exception {
throw new TestException("Inner");
}
});
List<Throwable> list = TestHelper.trackPluginErrors();
try {
s.onSubscribe(new BooleanSubscription());
s.onComplete();
TestHelper.assertUndeliverable(list, 0, TestException.class, "Inner");
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableReplayEagerTruncateTest method issue2191_SchedulerUnsubscribe.
/**
* Specifically test interaction with a Scheduler with subscribeOn.
*
* @throws Throwable functional interfaces declare throws Exception
*/
@SuppressWarnings("unchecked")
@Test
public void issue2191_SchedulerUnsubscribe() throws Throwable {
// setup mocks
Consumer<Integer> sourceNext = mock(Consumer.class);
Action sourceCompleted = mock(Action.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
ConnectableFlowable<Integer> replay = Flowable.just(1, 2, 3).doOnNext(sourceNext).doOnCancel(sourceUnsubscribed).doOnComplete(sourceCompleted).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(sourceNext, times(1)).accept(1);
verify(sourceNext, times(1)).accept(2);
verify(sourceNext, times(1)).accept(3);
verify(sourceCompleted, times(1)).run();
verify(mockScheduler, times(1)).createWorker();
verify(spiedWorker, times(1)).schedule((Runnable) notNull());
verifyObserverMock(mockObserverBeforeConnect, 2, 6);
verifyObserverMock(mockObserverAfterConnect, 2, 6);
// FIXME publish calls cancel too
verify(spiedWorker, times(1)).dispose();
verify(sourceUnsubscribed, never()).run();
verifyNoMoreInteractions(sourceNext);
verifyNoMoreInteractions(sourceCompleted);
verifyNoMoreInteractions(sourceUnsubscribed);
verifyNoMoreInteractions(spiedWorker);
verifyNoMoreInteractions(mockSubscription);
verifyNoMoreInteractions(mockScheduler);
verifyNoMoreInteractions(mockObserverBeforeConnect);
verifyNoMoreInteractions(mockObserverAfterConnect);
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableReplayEagerTruncateTest method connectConsumerThrows.
@Test
public void connectConsumerThrows() {
ConnectableFlowable<Integer> cf = Flowable.range(1, 2).replay();
try {
cf.connect(new Consumer<Disposable>() {
@Override
public void accept(Disposable t) throws Exception {
throw new TestException();
}
});
fail("Should have thrown");
} catch (TestException ex) {
// expected
}
cf.test().assertEmpty().cancel();
cf.connect();
cf.test().assertResult(1, 2);
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableGroupByTest method groupsWithNestedObserveOn.
@Test
public void groupsWithNestedObserveOn() throws InterruptedException {
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);
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) {
return group.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).map(new Function<Integer, String>() {
@Override
public String apply(Integer t1) {
return "first groups: " + t1;
}
});
}
}).blockingForEach(new Consumer<String>() {
@Override
public void accept(String s) {
results.add(s);
}
});
System.out.println("Results: " + results);
assertEquals(4, results.size());
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableGroupByTest method firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenObservesOnAndDelaysAndThenCompletes.
@Test
public void firstGroupsCompleteAndParentSlowToThenEmitFinalGroupsWhichThenObservesOnAndDelaysAndThenCompletes() 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.observeOn(Schedulers.newThread()).delay(400, TimeUnit.MILLISECONDS).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());
}
Aggregations