use of org.reactivestreams.Subscriber in project ratpack by ratpack.
the class Streams method reduce.
/**
* Reduces the stream to a single value, by applying the given function successively.
*
* @param publisher the publisher to reduce
* @param seed the initial value
* @param reducer the reducing function
* @param <R> the type of result
* @return a promise for the reduced value
* @since 1.4
*/
public static <T, R> Promise<R> reduce(Publisher<T> publisher, R seed, BiFunction<? super R, ? super T, ? extends R> reducer) {
return Promise.async(d -> publisher.subscribe(new Subscriber<T>() {
private Subscription subscription;
private volatile R value = seed;
private AtomicInteger count = new AtomicInteger();
@Override
public void onSubscribe(Subscription s) {
subscription = s;
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(T t) {
count.incrementAndGet();
try {
value = reducer.apply(value, t);
} catch (Throwable e) {
subscription.cancel();
d.error(e);
}
}
@Override
public void onError(Throwable t) {
d.error(t);
}
@Override
public void onComplete() {
d.success(value);
}
}));
}
use of org.reactivestreams.Subscriber in project reactive-streams-jvm by reactive-streams.
the class SubscriberWhiteboxVerificationTest method required_spec208_mustBePreparedToReceiveOnNextSignalsAfterHavingCalledSubscriptionCancel_shouldFail.
@Test
public void required_spec208_mustBePreparedToReceiveOnNextSignalsAfterHavingCalledSubscriptionCancel_shouldFail() throws Throwable {
requireTestFailure(new ThrowingRunnable() {
@Override
public void run() throws Throwable {
customSubscriberVerification(new Function<WhiteboxSubscriberProbe<Integer>, Subscriber<Integer>>() {
@Override
public Subscriber<Integer> apply(WhiteboxSubscriberProbe<Integer> probe) throws Throwable {
final AtomicBoolean subscriptionCancelled = new AtomicBoolean(false);
return new SimpleSubscriberWithProbe(probe) {
@Override
public void onSubscribe(final Subscription s) {
this.subscription = s;
probe.registerOnSubscribe(new SubscriberPuppet() {
@Override
public void triggerRequest(long elements) {
s.request(elements);
}
@Override
public void signalCancel() {
subscriptionCancelled.set(true);
s.cancel();
}
});
}
@Override
public void onNext(Integer element) {
if (subscriptionCancelled.get()) {
// but this test aims to simulate a Subscriber where someone got it's internals wrong and "blows up".
throw new RuntimeException("But I thought it's cancelled!");
} else {
probe.registerOnNext(element);
}
}
};
}
}).required_spec208_mustBePreparedToReceiveOnNextSignalsAfterHavingCalledSubscriptionCancel();
}
}, "But I thought it's cancelled!");
}
use of org.reactivestreams.Subscriber in project RxJava by ReactiveX.
the class BlockingFlowableLatestTest method onError.
@SuppressWarnings("unchecked")
@Test
public void onError() {
Iterator<Object> it = Flowable.never().blockingLatest().iterator();
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
((Subscriber<Object>) it).onError(new TestException());
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of org.reactivestreams.Subscriber in project RxJava by ReactiveX.
the class MaybeDelaySubscriptionTest method withPublisherCallAfterTerminalEvent.
@Test
public void withPublisherCallAfterTerminalEvent() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
Flowable<Integer> f = new Flowable<Integer>() {
@Override
protected void subscribeActual(Subscriber<? super Integer> observer) {
observer.onSubscribe(new BooleanSubscription());
observer.onNext(1);
observer.onError(new TestException());
observer.onComplete();
observer.onNext(2);
}
};
Maybe.just(1).delaySubscription(f).test().assertResult(1);
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of org.reactivestreams.Subscriber in project RxJava by ReactiveX.
the class ParallelJoinTest method overflowSlowpath.
@Test
public void overflowSlowpath() {
@SuppressWarnings("unchecked") final Subscriber<? super Integer>[] subs = new Subscriber[1];
TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) {
@Override
public void onNext(Integer t) {
super.onNext(t);
subs[0].onNext(2);
subs[0].onNext(3);
}
};
new ParallelFlowable<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer>[] subscribers) {
subs[0] = subscribers[0];
subscribers[0].onSubscribe(new BooleanSubscription());
subscribers[0].onNext(1);
}
@Override
public int parallelism() {
return 1;
}
}.sequential(1).subscribe(ts);
ts.assertFailure(MissingBackpressureException.class, 1);
}
Aggregations