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);
}
});
});
}
});
}
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();
}
}
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);
}
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);
});
}
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();
}
Aggregations