Search in sources :

Example 46 with Subscriber

use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.

the class TrackerSubscriberWhiteboxVerificationTckTest method createSubscriber.

@Override
public Subscriber<Tracker> createSubscriber(WhiteboxSubscriberProbe<Tracker> probe) {
    int actualPort = server.actualPort();
    ProtonClient client = ProtonClient.create(vertx);
    AtomicReference<Subscriber<Tracker>> ref = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    client.connect("localhost", actualPort, result -> {
        if (result.succeeded()) {
            ProtonConnection conn = result.result();
            conn.open();
            // Modified stream impl, overriding methods to add test probe instrumentation
            ProtonSubscriber<Tracker> stream = new ProtonSubscriberImpl("myAddress", (ProtonConnectionImpl) conn) {

                @Override
                public void onSubscribe(final Subscription s) {
                    super.onSubscribe(s);
                    probe.registerOnSubscribe(new SubscriberPuppet() {

                        @Override
                        public void triggerRequest(long n) {
                            s.request(n);
                        }

                        @Override
                        public void signalCancel() {
                            s.cancel();
                        }
                    });
                }

                @Override
                public void onNext(Tracker value) {
                    probe.registerOnNext(value);
                    super.onNext(value);
                }

                @Override
                public void onError(Throwable t) {
                    probe.registerOnError(t);
                    super.onError(t);
                }

                @Override
                public void onComplete() {
                    probe.registerOnComplete();
                    super.onComplete();
                }
            };
            ref.set(stream);
        } else {
            LOG.error("Connection failed");
        }
        latch.countDown();
    });
    try {
        LOG.trace("Awaiting connection");
        boolean res = latch.await(2, TimeUnit.SECONDS);
        LOG.trace("Client connected: " + res);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while creating subscriber", e);
    }
    return ref.get();
}
Also used : Tracker(io.vertx.proton.streams.Tracker) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClient(io.vertx.proton.ProtonClient) CountDownLatch(java.util.concurrent.CountDownLatch) ProtonConnection(io.vertx.proton.ProtonConnection) Subscriber(org.reactivestreams.Subscriber) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) ProtonSubscriberImpl(io.vertx.proton.streams.impl.ProtonSubscriberImpl) Subscription(org.reactivestreams.Subscription)

Example 47 with Subscriber

use of org.reactivestreams.Subscriber in project ratpack by ratpack.

the class ByteBufComposingPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super CompositeByteBuf> subscriber) {
    subscriber.onSubscribe(new ManagedSubscription<CompositeByteBuf>(subscriber, ByteBuf::release) {

        private Subscription subscription;

        private CompositeByteBuf composite;

        private volatile State state;

        @Override
        protected void onRequest(long n) {
            if (subscription == null) {
                upstream.subscribe(new Subscriber<ByteBuf>() {

                    @Override
                    public void onSubscribe(Subscription s) {
                        subscription = s;
                        state = State.Fetching;
                        s.request(1);
                    }

                    @Override
                    public void onNext(ByteBuf t) {
                        if (state == State.Closed) {
                            t.release();
                            return;
                        }
                        if (composite == null) {
                            composite = alloc.compositeBuffer(maxNum);
                        }
                        composite.addComponent(true, t);
                        if (composite.numComponents() == maxNum || composite.readableBytes() >= watermark) {
                            state = State.Writing;
                            emitNext(composite);
                            composite = null;
                            maybeFetch();
                        } else {
                            subscription.request(1);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        state = State.Closed;
                        ReferenceCountUtil.release(composite);
                        emitError(t);
                    }

                    @Override
                    public void onComplete() {
                        state = State.Closed;
                        if (composite != null) {
                            emitNext(composite);
                        }
                        emitComplete();
                    }
                });
            } else {
                maybeFetch();
            }
        }

        private void maybeFetch() {
            if (getDemand() > 0 && state != State.Fetching) {
                state = State.Fetching;
                subscription.request(1);
            }
        }

        @Override
        protected void onCancel() {
            state = State.Closed;
            ReferenceCountUtil.release(composite);
            if (subscription != null) {
                subscription.cancel();
            }
        }
    });
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 48 with Subscriber

use of org.reactivestreams.Subscriber in project ratpack by ratpack.

the class ConcatPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super T> s) {
    s.onSubscribe(new ManagedSubscription<T>(s, disposer) {

        Iterator<? extends Publisher<? extends T>> iterator = publishers.iterator();

        Subscription current;

        @Override
        protected void onRequest(long n) {
            if (current == null) {
                if (iterator.hasNext()) {
                    Publisher<? extends T> publisher = iterator.next();
                    publisher.subscribe(new Subscriber<T>() {

                        @Override
                        public void onSubscribe(Subscription s) {
                            current = s;
                            s.request(n);
                        }

                        @Override
                        public void onNext(T t) {
                            emitNext(t);
                        }

                        @Override
                        public void onError(Throwable t) {
                            emitError(t);
                        }

                        @Override
                        public void onComplete() {
                            current = null;
                            long demand = getDemand();
                            if (demand > 0) {
                                onRequest(demand);
                            }
                        }
                    });
                } else {
                    emitComplete();
                }
            } else {
                current.request(n);
            }
        }

        @Override
        protected void onCancel() {
            if (current != null) {
                current.cancel();
            }
        }
    });
}
Also used : Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) TransformablePublisher(ratpack.stream.TransformablePublisher) Subscription(org.reactivestreams.Subscription)

Example 49 with Subscriber

use of org.reactivestreams.Subscriber in project ratpack by ratpack.

the class FlatMapPublisher method subscribe.

@Override
public void subscribe(final Subscriber<? super O> outSubscriber) {
    input.subscribe(new Subscriber<I>() {

        private Subscription subscription;

        private final AtomicBoolean done = new AtomicBoolean();

        @Override
        public void onSubscribe(Subscription subscription) {
            this.subscription = new Subscription() {

                @Override
                public void request(long n) {
                    subscription.request(n);
                }

                @Override
                public void cancel() {
                    subscription.cancel();
                }
            };
            outSubscriber.onSubscribe(this.subscription);
        }

        @Override
        public void onNext(I in) {
            if (done.get()) {
                return;
            }
            Promise<? extends O> out;
            try {
                out = function.apply(in);
            } catch (Throwable throwable) {
                subscription.cancel();
                innerOnError(throwable);
                return;
            }
            out.onError(e -> {
                subscription.cancel();
                innerOnError(e);
            }).then(v -> {
                if (!done.get()) {
                    outSubscriber.onNext(v);
                }
            });
        }

        @Override
        public void onError(Throwable t) {
            Promise.value(t).then(this::innerOnError);
        }

        public void innerOnError(Throwable t) {
            if (done.compareAndSet(false, true)) {
                outSubscriber.onError(t);
            }
        }

        @Override
        public void onComplete() {
            Operation.noop().then(() -> {
                if (done.compareAndSet(false, true)) {
                    outSubscriber.onComplete();
                }
            });
        }
    });
}
Also used : Function(ratpack.func.Function) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) TransformablePublisher(ratpack.stream.TransformablePublisher) Subscriber(org.reactivestreams.Subscriber) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Promise(ratpack.exec.Promise) Subscription(org.reactivestreams.Subscription)

Example 50 with Subscriber

use of org.reactivestreams.Subscriber in project RxJava by ReactiveX.

the class FlowableSequenceEqualTest method doubleErrorFlowable.

@Test
public void doubleErrorFlowable() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.sequenceEqual(Flowable.never(), new Flowable<Object>() {

            @Override
            protected void subscribeActual(Subscriber<? super Object> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onError(new TestException("First"));
                s.onError(new TestException("Second"));
            }
        }, 8).toFlowable().test().assertFailureAndMessage(TestException.class, "First");
        TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Subscriber(org.reactivestreams.Subscriber)

Aggregations

Subscriber (org.reactivestreams.Subscriber)92 Subscription (org.reactivestreams.Subscription)56 Test (org.junit.Test)49 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 ProtonClient (io.vertx.proton.ProtonClient)22 ProtonConnection (io.vertx.proton.ProtonConnection)22 Message (org.apache.qpid.proton.message.Message)20 List (java.util.List)18 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 Handler (io.vertx.core.Handler)16 Logger (io.vertx.core.impl.logging.Logger)16 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)16 ProtonStreams (io.vertx.proton.streams.ProtonStreams)16 ExecutionException (java.util.concurrent.ExecutionException)16 Symbol (org.apache.qpid.proton.amqp.Symbol)16 Section (org.apache.qpid.proton.amqp.messaging.Section)16 ArrayList (java.util.ArrayList)14 Collections (java.util.Collections)13