use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableReduceTest method shouldReduceTo10EventsFlowable.
/**
* Make sure an asynchronous reduce with flatMap works.
* Original Reactor-Core test case: https://gist.github.com/jurna/353a2bd8ff83f0b24f0b5bc772077d61
*/
@Test
public void shouldReduceTo10EventsFlowable() {
final AtomicInteger count = new AtomicInteger();
Flowable.range(0, 10).flatMap(new Function<Integer, Publisher<String>>() {
@Override
public Publisher<String> apply(final Integer x) throws Exception {
return Flowable.range(0, 2).map(new Function<Integer, String>() {
@Override
public String apply(Integer y) throws Exception {
return blockingOp(x, y);
}
}).subscribeOn(Schedulers.io()).reduce(new BiFunction<String, String, String>() {
@Override
public String apply(String l, String r) throws Exception {
return l + "_" + r;
}
}).toFlowable().doOnNext(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
count.incrementAndGet();
System.out.println("Completed with " + s);
}
});
}
}).blockingLast();
assertEquals(10, count.get());
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class FlowableReduceTest method shouldReduceTo10Events.
/**
* Make sure an asynchronous reduce with flatMap works.
* Original Reactor-Core test case: https://gist.github.com/jurna/353a2bd8ff83f0b24f0b5bc772077d61
*/
@Test
public void shouldReduceTo10Events() {
final AtomicInteger count = new AtomicInteger();
Flowable.range(0, 10).flatMap(new Function<Integer, Publisher<String>>() {
@Override
public Publisher<String> apply(final Integer x) throws Exception {
return Flowable.range(0, 2).map(new Function<Integer, String>() {
@Override
public String apply(Integer y) throws Exception {
return blockingOp(x, y);
}
}).subscribeOn(Schedulers.io()).reduce(new BiFunction<String, String, String>() {
@Override
public String apply(String l, String r) throws Exception {
return l + "_" + r;
}
}).doOnSuccess(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
count.incrementAndGet();
System.out.println("Completed with " + s);
}
}).toFlowable();
}
}).blockingLast();
assertEquals(10, count.get());
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class ObservablePublishTest method altConnectCrash.
@Test
public void altConnectCrash() {
try {
new ObservablePublish<>(Observable.<Integer>empty()).connect(new Consumer<Disposable>() {
@Override
public void accept(Disposable t) throws Exception {
throw new TestException();
}
});
fail("Should have thrown");
} catch (TestException expected) {
// expected
}
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class ObservableGroupByTest method existingGroupValueSelectorFails.
@Test
public void existingGroupValueSelectorFails() {
TestObserver<Object> to1 = new TestObserver<>();
final TestObserver<Object> to2 = new TestObserver<>();
Observable.just(1, 2).groupBy(Functions.justFunction(1), new Function<Integer, Object>() {
@Override
public Object apply(Integer v) throws Throwable {
if (v == 2) {
throw new TestException();
}
return v;
}
}).doOnNext(new Consumer<GroupedObservable<Integer, Object>>() {
@Override
public void accept(GroupedObservable<Integer, Object> g) throws Throwable {
g.subscribe(to2);
}
}).subscribe(to1);
to1.assertValueCount(1).assertError(TestException.class).assertNotComplete();
to2.assertFailure(TestException.class, 1);
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class MaybeTest method using.
@Test
public void using() {
final AtomicInteger disposeCount = new AtomicInteger();
Maybe.using(Functions.justSupplier(1), new Function<Integer, MaybeSource<Integer>>() {
@Override
public MaybeSource<Integer> apply(Integer v) throws Exception {
return Maybe.just(v);
}
}, new Consumer<Integer>() {
@Override
public void accept(Integer d) throws Exception {
disposeCount.set(d);
}
}).map(new Function<Integer, Object>() {
@Override
public String apply(Integer v) throws Exception {
return "" + disposeCount.get() + v * 10;
}
}).test().assertResult("110");
}
Aggregations