Search in sources :

Example 31 with Subscriber

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

the class ParallelJoinTest method overflowSlowpathDelayError.

@Test
public void overflowSlowpathDelayError() {
    @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);
            if (t == 1) {
                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;
        }
    }.sequentialDelayError(1).subscribe(ts);
    ts.request(1);
    ts.assertFailure(MissingBackpressureException.class, 1, 2);
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Subscriber(org.reactivestreams.Subscriber) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 32 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.rxjava3.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Subscriber(org.reactivestreams.Subscriber) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 33 with Subscriber

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

the class ParallelJoinTest method onNextMissingBackpressureDelayErrorRace.

@Test
public void onNextMissingBackpressureDelayErrorRace() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
            AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>();
            AtomicReference<Subscriber<? super Integer>> ref2 = new AtomicReference<>();
            Flowable<Integer> f1 = new Flowable<Integer>() {

                @Override
                public void subscribeActual(Subscriber<? super Integer> s) {
                    s.onSubscribe(new BooleanSubscription());
                    ref1.set(s);
                }
            };
            Flowable<Integer> f2 = new Flowable<Integer>() {

                @Override
                public void subscribeActual(Subscriber<? super Integer> s) {
                    s.onSubscribe(new BooleanSubscription());
                    ref2.set(s);
                }
            };
            ParallelFlowable.fromArray(f1, f2).sequentialDelayError(1).test(0);
            TestHelper.race(() -> {
                ref1.get().onNext(1);
                ref1.get().onNext(2);
            }, () -> {
                ref2.get().onNext(3);
                ref2.get().onNext(4);
            });
            errors.clear();
        }
    });
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Subscriber(org.reactivestreams.Subscriber) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 34 with Subscriber

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

the class ParallelMapTest method conditionalCancelIgnored.

@Test
public void conditionalCancelIgnored() {
    Flowable<Integer> f = new Flowable<Integer>() {

        @Override
        protected void subscribeActual(@NonNull Subscriber<@NonNull ? super @NonNull Integer> s) {
            @SuppressWarnings("unchecked") ConditionalSubscriber<Integer> subscriber = (ConditionalSubscriber<Integer>) s;
            subscriber.onSubscribe(new BooleanSubscription());
            subscriber.tryOnNext(1);
            subscriber.tryOnNext(2);
        }
    };
    ParallelFlowable.fromArray(f).map(v -> {
        throw new TestException();
    }).filter(v -> true).sequential().test().assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) io.reactivex.rxjava3.functions(io.reactivex.rxjava3.functions) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) Test(org.junit.Test) NonNull(io.reactivex.rxjava3.annotations.NonNull) TimeUnit(java.util.concurrent.TimeUnit) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) List(java.util.List) TestHelper(io.reactivex.rxjava3.testsupport.TestHelper) Functions(io.reactivex.rxjava3.internal.functions.Functions) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) Subscriber(org.reactivestreams.Subscriber) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) Subscriber(org.reactivestreams.Subscriber) NonNull(io.reactivex.rxjava3.annotations.NonNull) ConditionalSubscriber(io.reactivex.rxjava3.operators.ConditionalSubscriber) Test(org.junit.Test)

Example 35 with Subscriber

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

the class Receiver method main.

public static void main(String[] args) throws Exception {
    try {
        Vertx vertx = Vertx.vertx();
        ProtonClient client = ProtonClient.create(vertx);
        // Connect, then use the event loop thread to process the connection
        client.connect("localhost", 5672, res -> {
            Context ctx = Vertx.currentContext();
            if (res.succeeded()) {
                System.out.println("We're connected");
                ProtonConnection connection = res.result();
                connection.open();
                Publisher<Message> consumerPublisher = ProtonStreams.createConsumer(connection, "queue");
                Subscriber<Message> subscriber = new Subscriber<Message>() {

                    AtomicInteger receievedCount = new AtomicInteger();

                    Subscription subscription;

                    @Override
                    public void onSubscribe(Subscription s) {
                        subscription = s;
                        // Initial request for some messages
                        subscription.request(REQ_WINDOW);
                    }

                    @Override
                    public void onNext(Message env) {
                        int msgNum = receievedCount.incrementAndGet();
                        String content = (String) ((AmqpValue) env.getBody()).getValue();
                        System.out.println("Received message: " + content);
                        if (msgNum % REQ_WINDOW == 0 && msgNum < COUNT) {
                            // Request more messages
                            subscription.request(REQ_WINDOW);
                        } else if (msgNum == COUNT) {
                            // Done, clean up.
                            subscription.cancel();
                            closeConnection(vertx, connection);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        System.out.println("onError(): " + t);
                        closeConnection(vertx, connection);
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("onComplete()");
                        closeConnection(vertx, connection);
                    }

                    private void closeConnection(Vertx vertx, ProtonConnection connection) {
                        ctx.runOnContext(x -> {
                            System.out.println("Subscriber finished, closing connection.");
                            connection.closeHandler(y -> {
                                System.out.println("Connection closed.");
                                connection.disconnect();
                                vertx.close();
                            }).close();
                        });
                    }
                };
                consumerPublisher.subscribe(subscriber);
            } else {
                System.out.println("Failed to connect, exiting: " + res.cause());
                System.exit(1);
            }
        });
    } catch (Exception exp) {
        System.out.println("Caught exception, exiting.");
        exp.printStackTrace(System.out);
        System.exit(1);
    }
}
Also used : Context(io.vertx.core.Context) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonStreams(io.vertx.proton.streams.ProtonStreams) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) Vertx(io.vertx.core.Vertx) Message(org.apache.qpid.proton.message.Message) ProtonClient(io.vertx.proton.ProtonClient) Subscriber(org.reactivestreams.Subscriber) Context(io.vertx.core.Context) Message(org.apache.qpid.proton.message.Message) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription)

Aggregations

Subscriber (org.reactivestreams.Subscriber)114 Subscription (org.reactivestreams.Subscription)75 Test (org.junit.Test)55 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)38 List (java.util.List)34 Arrays (java.util.Arrays)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 Publisher (org.reactivestreams.Publisher)26 ArrayList (java.util.ArrayList)24 ProtonClient (io.vertx.proton.ProtonClient)22 ProtonConnection (io.vertx.proton.ProtonConnection)22 Message (org.apache.qpid.proton.message.Message)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 Collections (java.util.Collections)18 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)18 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