Search in sources :

Example 6 with Subscriber

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);
        }
    }));
}
Also used : Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription)

Example 7 with Subscriber

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!");
}
Also used : SubscriberPuppet(org.reactivestreams.tck.SubscriberWhiteboxVerification.SubscriberPuppet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WhiteboxSubscriberProbe(org.reactivestreams.tck.SubscriberWhiteboxVerification.WhiteboxSubscriberProbe) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) Test(org.testng.annotations.Test)

Example 8 with Subscriber

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();
    }
}
Also used : TestException(io.reactivex.exceptions.TestException) Subscriber(org.reactivestreams.Subscriber)

Example 9 with Subscriber

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();
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.exceptions.TestException) Subscriber(org.reactivestreams.Subscriber) Test(org.junit.Test)

Example 10 with Subscriber

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);
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Subscriber(org.reactivestreams.Subscriber) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Aggregations

Subscriber (org.reactivestreams.Subscriber)26 Subscription (org.reactivestreams.Subscription)16 Publisher (org.reactivestreams.Publisher)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Test (org.junit.Test)5 ByteBuf (io.netty.buffer.ByteBuf)4 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)4 TestSubscriber (io.reactivex.subscribers.TestSubscriber)3 InetSocketAddress (java.net.InetSocketAddress)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Exchange (org.apache.camel.Exchange)3 Test (org.testng.annotations.Test)3 TransformablePublisher (ratpack.stream.TransformablePublisher)3 ReactiveSubscription (reactor.rx.subscription.ReactiveSubscription)3 Flowable (io.reactivex.Flowable)2 TestException (io.reactivex.exceptions.TestException)2 ExecutorService (java.util.concurrent.ExecutorService)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2