Search in sources :

Example 11 with ReplayProcessor

use of io.reactivex.rxjava3.processors.ReplayProcessor in project redisson by redisson.

the class PublisherAdder method addAll.

public Single<Boolean> addAll(Publisher<? extends V> c) {
    final Flowable<? extends V> cc = Flowable.fromPublisher(c);
    final ReplayProcessor<Boolean> p = ReplayProcessor.create();
    return p.doOnRequest(new LongConsumer() {

        @Override
        public void accept(long t) throws Exception {
            final AtomicBoolean completed = new AtomicBoolean();
            final AtomicLong values = new AtomicLong();
            final AtomicBoolean lastSize = new AtomicBoolean();
            cc.subscribe(new Consumer<V>() {

                @Override
                public void accept(V t) throws Exception {
                    values.getAndIncrement();
                    add(t).whenComplete((res, e) -> {
                        if (e != null) {
                            p.onError(e);
                            return;
                        }
                        if (res) {
                            lastSize.set(true);
                        }
                        if (values.decrementAndGet() == 0 && completed.get()) {
                            p.onNext(lastSize.get());
                            p.onComplete();
                        }
                    });
                }
            }, new Consumer<Throwable>() {

                @Override
                public void accept(Throwable t) throws Exception {
                    p.onError(t);
                }
            }, new Action() {

                @Override
                public void run() throws Exception {
                    completed.set(true);
                    if (values.get() == 0) {
                        p.onNext(lastSize.get());
                        p.onComplete();
                    }
                }
            });
        }
    }).singleOrError();
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Single(io.reactivex.rxjava3.core.Single) Flowable(io.reactivex.rxjava3.core.Flowable) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.reactivex.rxjava3.functions.Consumer) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) Action(io.reactivex.rxjava3.functions.Action) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) Action(io.reactivex.rxjava3.functions.Action) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 12 with ReplayProcessor

use of io.reactivex.rxjava3.processors.ReplayProcessor in project redisson by redisson.

the class RedissonKeysRx method createKeysIterator.

private Publisher<String> createKeysIterator(MasterSlaveEntry entry, String pattern, int count) {
    ReplayProcessor<String> p = ReplayProcessor.create();
    return p.doOnRequest(new LongConsumer() {

        private RedisClient client;

        private List<String> firstValues;

        private long nextIterPos;

        private long currentIndex;

        @Override
        public void accept(long value) {
            currentIndex = value;
            nextValues();
        }

        protected void nextValues() {
            instance.scanIteratorAsync(client, entry, nextIterPos, pattern, count).whenComplete((res, e) -> {
                if (e != null) {
                    p.onError(e);
                    return;
                }
                client = res.getRedisClient();
                long prevIterPos = nextIterPos;
                if (nextIterPos == 0 && firstValues == null) {
                    firstValues = (List<String>) (Object) res.getValues();
                } else if (res.getValues().equals(firstValues)) {
                    p.onComplete();
                    currentIndex = 0;
                    return;
                }
                nextIterPos = res.getPos();
                if (prevIterPos == nextIterPos) {
                    nextIterPos = -1;
                }
                for (Object val : res.getValues()) {
                    p.onNext((String) val);
                    currentIndex--;
                    if (currentIndex == 0) {
                        p.onComplete();
                        return;
                    }
                }
                if (nextIterPos == -1) {
                    p.onComplete();
                    currentIndex = 0;
                }
                if (currentIndex == 0) {
                    return;
                }
                nextValues();
            });
        }
    });
}
Also used : RedisClient(org.redisson.client.RedisClient) List(java.util.List) Flowable(io.reactivex.rxjava3.core.Flowable) RedissonKeys(org.redisson.RedissonKeys) Publisher(org.reactivestreams.Publisher) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisClient(org.redisson.client.RedisClient) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) ArrayList(java.util.ArrayList) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with ReplayProcessor

use of io.reactivex.rxjava3.processors.ReplayProcessor in project RxJava by ReactiveX.

the class SerializedProcessorTest method replaySubjectError.

@Test
public void replaySubjectError() {
    ReplayProcessor<Integer> async = ReplayProcessor.create();
    TestException te = new TestException();
    async.onError(te);
    FlowableProcessor<Integer> serial = async.toSerialized();
    assertFalse(serial.hasSubscribers());
    assertFalse(serial.hasComplete());
    assertTrue(serial.hasThrowable());
    assertSame(te, serial.getThrowable());
    assertNull(async.getValue());
    assertFalse(async.hasValue());
    assertArrayEquals(new Object[] {}, async.getValues());
    assertArrayEquals(new Integer[] {}, async.getValues(new Integer[0]));
    assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 }));
    assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 }));
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 14 with ReplayProcessor

use of io.reactivex.rxjava3.processors.ReplayProcessor in project RxJava by ReactiveX.

the class SerializedProcessorTest method replaySubjectBoundedError.

@Test
public void replaySubjectBoundedError() {
    ReplayProcessor<Integer> async = ReplayProcessor.createWithSize(1);
    TestException te = new TestException();
    async.onError(te);
    FlowableProcessor<Integer> serial = async.toSerialized();
    assertFalse(serial.hasSubscribers());
    assertFalse(serial.hasComplete());
    assertTrue(serial.hasThrowable());
    assertSame(te, serial.getThrowable());
    assertNull(async.getValue());
    assertFalse(async.hasValue());
    assertArrayEquals(new Object[] {}, async.getValues());
    assertArrayEquals(new Integer[] {}, async.getValues(new Integer[0]));
    assertArrayEquals(new Integer[] { null }, async.getValues(new Integer[] { 0 }));
    assertArrayEquals(new Integer[] { null, 0 }, async.getValues(new Integer[] { 0, 0 }));
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 15 with ReplayProcessor

use of io.reactivex.rxjava3.processors.ReplayProcessor in project RxJava by ReactiveX.

the class ReplayProcessorTest method sizeBoundZeroRequestError.

@Test
public void sizeBoundZeroRequestError() {
    final ReplayProcessor<Integer> source = ReplayProcessor.createWithSize(16);
    source.onError(new TestException());
    source.test(0).assertFailure(TestException.class);
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException)

Aggregations

TestException (io.reactivex.rxjava3.exceptions.TestException)16 Flowable (io.reactivex.rxjava3.core.Flowable)3 LongConsumer (io.reactivex.rxjava3.functions.LongConsumer)3 ReplayProcessor (io.reactivex.rxjava3.processors.ReplayProcessor)3 Action (io.reactivex.rxjava3.functions.Action)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Test (org.junit.Test)2 Publisher (org.reactivestreams.Publisher)2 RFuture (org.redisson.api.RFuture)2 Single (io.reactivex.rxjava3.core.Single)1 Disposable (io.reactivex.rxjava3.disposables.Disposable)1 Consumer (io.reactivex.rxjava3.functions.Consumer)1 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 RedissonKeys (org.redisson.RedissonKeys)1 RTopic (org.redisson.api.RTopic)1 MessageListener (org.redisson.api.listener.MessageListener)1 RedisClient (org.redisson.client.RedisClient)1