Search in sources :

Example 1 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project redisson by redisson.

the class RedissonTopicRx method getMessages.

public <M> Flowable<M> getMessages(Class<M> type) {
    ReplayProcessor<M> p = ReplayProcessor.create();
    return p.doOnRequest(new LongConsumer() {

        @Override
        public void accept(long n) throws Exception {
            AtomicLong counter = new AtomicLong(n);
            RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() {

                @Override
                public void onMessage(CharSequence channel, M msg) {
                    p.onNext(msg);
                    if (counter.decrementAndGet() == 0) {
                        topic.removeListenerAsync(this);
                        p.onComplete();
                    }
                }
            });
            t.whenComplete((id, e) -> {
                if (e != null) {
                    p.onError(e);
                    return;
                }
                p.doOnCancel(new Action() {

                    @Override
                    public void run() throws Exception {
                        topic.removeListenerAsync(id);
                    }
                });
            });
        }
    });
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flowable(io.reactivex.rxjava3.core.Flowable) RTopic(org.redisson.api.RTopic) MessageListener(org.redisson.api.listener.MessageListener) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) Action(io.reactivex.rxjava3.functions.Action) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(io.reactivex.rxjava3.functions.Action) MessageListener(org.redisson.api.listener.MessageListener) RFuture(org.redisson.api.RFuture)

Example 2 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableFromStreamTest method requestRaceBase.

void requestRaceBase(boolean conditional) throws Exception {
    ExecutorService exec = Executors.newCachedThreadPool();
    try {
        for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
            AtomicInteger counter = new AtomicInteger();
            int max = 100;
            Flowable<Integer> source = Flowable.fromStream(IntStream.rangeClosed(1, max).boxed());
            if (conditional) {
                source = source.filter(v -> true);
            }
            CountDownLatch cdl = new CountDownLatch(1);
            source.subscribe(new FlowableSubscriber<Integer>() {

                @NonNull
                Subscription upstream;

                @Override
                public void onSubscribe(@NonNull Subscription s) {
                    this.upstream = s;
                    s.request(1);
                }

                @Override
                public void onNext(Integer t) {
                    counter.getAndIncrement();
                    AtomicInteger sync = new AtomicInteger(2);
                    exec.submit(() -> {
                        if (sync.decrementAndGet() != 0) {
                            while (sync.get() != 0) {
                            }
                        }
                        upstream.request(1);
                    });
                    if (sync.decrementAndGet() != 0) {
                        while (sync.get() != 0) {
                        }
                    }
                }

                @Override
                public void onError(Throwable t) {
                    t.printStackTrace();
                    cdl.countDown();
                }

                @Override
                public void onComplete() {
                    counter.getAndIncrement();
                    cdl.countDown();
                }
            });
            assertTrue(cdl.await(60, TimeUnit.SECONDS));
            assertEquals(max + 1, counter.get());
        }
    } finally {
        exec.shutdown();
    }
}
Also used : SimpleQueue(io.reactivex.rxjava3.operators.SimpleQueue) java.util(java.util) TestException(io.reactivex.rxjava3.exceptions.TestException) java.util.concurrent(java.util.concurrent) java.util.stream(java.util.stream) java.util.concurrent.atomic(java.util.concurrent.atomic) Test(org.junit.Test) NonNull(io.reactivex.rxjava3.annotations.NonNull) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) QueueFuseable(io.reactivex.rxjava3.operators.QueueFuseable) Subscription(org.reactivestreams.Subscription) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) QueueSubscription(io.reactivex.rxjava3.operators.QueueSubscription) io.reactivex.rxjava3.testsupport(io.reactivex.rxjava3.testsupport) NonNull(io.reactivex.rxjava3.annotations.NonNull) Subscription(org.reactivestreams.Subscription) QueueSubscription(io.reactivex.rxjava3.operators.QueueSubscription)

Example 3 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableFromStreamTest method requestOneByOneBase.

void requestOneByOneBase(boolean conditional) {
    List<Object> list = new ArrayList<>();
    Flowable<Integer> source = Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed());
    if (conditional) {
        source = source.filter(v -> true);
    }
    source.subscribe(new FlowableSubscriber<Integer>() {

        @NonNull
        Subscription upstream;

        @Override
        public void onSubscribe(@NonNull Subscription s) {
            this.upstream = s;
            s.request(1);
        }

        @Override
        public void onNext(Integer t) {
            list.add(t);
            upstream.request(1);
        }

        @Override
        public void onError(Throwable t) {
            list.add(t);
        }

        @Override
        public void onComplete() {
            list.add(100);
        }
    });
    assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100), list);
}
Also used : SimpleQueue(io.reactivex.rxjava3.operators.SimpleQueue) java.util(java.util) TestException(io.reactivex.rxjava3.exceptions.TestException) java.util.concurrent(java.util.concurrent) java.util.stream(java.util.stream) java.util.concurrent.atomic(java.util.concurrent.atomic) Test(org.junit.Test) NonNull(io.reactivex.rxjava3.annotations.NonNull) Schedulers(io.reactivex.rxjava3.schedulers.Schedulers) QueueFuseable(io.reactivex.rxjava3.operators.QueueFuseable) Subscription(org.reactivestreams.Subscription) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) QueueSubscription(io.reactivex.rxjava3.operators.QueueSubscription) io.reactivex.rxjava3.testsupport(io.reactivex.rxjava3.testsupport) NonNull(io.reactivex.rxjava3.annotations.NonNull) Subscription(org.reactivestreams.Subscription) QueueSubscription(io.reactivex.rxjava3.operators.QueueSubscription)

Example 4 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableCollectWithCollectorTest method collectorAccumulatorDropSignals.

@Test
public void collectorAccumulatorDropSignals() throws Throwable {
    TestHelper.withErrorTracking(errors -> {
        Flowable<Integer> source = new Flowable<Integer>() {

            @Override
            protected void subscribeActual(Subscriber<? super Integer> s) {
                s.onSubscribe(new BooleanSubscription());
                s.onNext(1);
                s.onNext(2);
                s.onError(new IOException());
                s.onComplete();
            }
        };
        source.collect(new Collector<Integer, Integer, Integer>() {

            @Override
            public Supplier<Integer> supplier() {
                return () -> 1;
            }

            @Override
            public BiConsumer<Integer, Integer> accumulator() {
                return (a, b) -> {
                    throw new TestException();
                };
            }

            @Override
            public BinaryOperator<Integer> combiner() {
                return (a, b) -> a + b;
            }

            @Override
            public Function<Integer, Integer> finisher() {
                return a -> a;
            }

            @Override
            public Set<Characteristics> characteristics() {
                return Collections.emptySet();
            }
        }).test().assertFailure(TestException.class);
        TestHelper.assertUndeliverable(errors, 0, IOException.class);
    });
}
Also used : BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) TestException(io.reactivex.rxjava3.exceptions.TestException) IOException(java.io.IOException) Subscriber(org.reactivestreams.Subscriber) Test(org.junit.Test)

Example 5 with Flowable

use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.

the class FlowableWindowWithStartEndFlowableTest method windowOpenMainCompletes.

@Test
public void windowOpenMainCompletes() {
    AtomicReference<Subscriber<? super Integer>> ref1 = new AtomicReference<>();
    PublishProcessor<Object> pp = PublishProcessor.create();
    Flowable<Integer> f1 = Flowable.<Integer>unsafeCreate(ref1::set);
    AtomicInteger counter = new AtomicInteger();
    TestSubscriber<Flowable<Object>> ts = pp.window(f1, v -> Flowable.never()).doOnNext(w -> {
        if (counter.getAndIncrement() == 0) {
            ref1.get().onNext(2);
            pp.onNext(1);
            pp.onComplete();
        }
        w.test();
    }).test();
    ref1.get().onSubscribe(new BooleanSubscription());
    ref1.get().onNext(1);
    ts.assertComplete();
}
Also used : java.util(java.util) io.reactivex.rxjava3.functions(io.reactivex.rxjava3.functions) java.util.concurrent.atomic(java.util.concurrent.atomic) io.reactivex.rxjava3.subscribers(io.reactivex.rxjava3.subscribers) IOException(java.io.IOException) io.reactivex.rxjava3.exceptions(io.reactivex.rxjava3.exceptions) io.reactivex.rxjava3.processors(io.reactivex.rxjava3.processors) TimeUnit(java.util.concurrent.TimeUnit) Functions(io.reactivex.rxjava3.internal.functions.Functions) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) org.reactivestreams(org.reactivestreams) org.junit(org.junit) io.reactivex.rxjava3.core(io.reactivex.rxjava3.core) Assert(org.junit.Assert) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription) io.reactivex.rxjava3.testsupport(io.reactivex.rxjava3.testsupport) RxJavaPlugins(io.reactivex.rxjava3.plugins.RxJavaPlugins) BooleanSubscription(io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)

Aggregations

Test (org.junit.Test)177 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)121 TestException (io.reactivex.rxjava3.exceptions.TestException)95 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)67 InOrder (org.mockito.InOrder)41 IOException (java.io.IOException)27 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)22 Disposable (io.reactivex.rxjava3.disposables.Disposable)21 Flowable (io.reactivex.rxjava3.core.Flowable)19 TimeUnit (java.util.concurrent.TimeUnit)19 FlowableRxInvokerProvider (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvokerProvider)18 FlowableRxInvoker (org.apache.cxf.jaxrs.rx3.client.FlowableRxInvoker)17 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)15 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)13 ClientBuilder (javax.ws.rs.client.ClientBuilder)13 MediaType (javax.ws.rs.core.MediaType)13 AbstractResourceInfo (org.apache.cxf.jaxrs.model.AbstractResourceInfo)13 AbstractBusClientServerTestBase (org.apache.cxf.testutil.common.AbstractBusClientServerTestBase)13 Assert.assertTrue (org.junit.Assert.assertTrue)13 BeforeClass (org.junit.BeforeClass)13