use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableTests method publishLast.
@Test
public void publishLast() throws InterruptedException {
final AtomicInteger count = new AtomicInteger();
ConnectableFlowable<String> connectable = Flowable.<String>unsafeCreate(new Publisher<String>() {
@Override
public void subscribe(final Subscriber<? super String> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
count.incrementAndGet();
new Thread(new Runnable() {
@Override
public void run() {
subscriber.onNext("first");
subscriber.onNext("last");
subscriber.onComplete();
}
}).start();
}
}).takeLast(1).publish();
// subscribe once
final CountDownLatch latch = new CountDownLatch(1);
connectable.subscribe(new Consumer<String>() {
@Override
public void accept(String value) {
assertEquals("last", value);
latch.countDown();
}
});
// subscribe twice
connectable.subscribe();
Disposable subscription = connectable.connect();
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
assertEquals(1, count.get());
subscription.dispose();
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableTests method replay.
@Test
public void replay() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
ConnectableFlowable<String> f = Flowable.<String>unsafeCreate(new Publisher<String>() {
@Override
public void subscribe(final Subscriber<? super String> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
new Thread(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
subscriber.onNext("one");
subscriber.onComplete();
}
}).start();
}
}).replay();
// we connect immediately and it will emit the value
Disposable connection = f.connect();
try {
// we then expect the following 2 subscriptions to get that same value
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
f.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
// subscribe again
f.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
} finally {
connection.dispose();
}
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableTests method cache.
@Test
public void cache() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Flowable<String> f = Flowable.<String>unsafeCreate(new Publisher<String>() {
@Override
public void subscribe(final Subscriber<? super String> subscriber) {
subscriber.onSubscribe(new BooleanSubscription());
new Thread(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
subscriber.onNext("one");
subscriber.onComplete();
}
}).start();
}
}).cache();
// we then expect the following 2 subscriptions to get that same value
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
f.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
// subscribe again
f.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableRepeatTest method repeatTakeWithSubscribeOn.
@Test
public void repeatTakeWithSubscribeOn() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Flowable<Integer> oi = Flowable.unsafeCreate(new Publisher<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer> sub) {
sub.onSubscribe(new BooleanSubscription());
counter.incrementAndGet();
sub.onNext(1);
sub.onNext(2);
sub.onComplete();
}
}).subscribeOn(Schedulers.newThread());
Object[] ys = oi.repeat().subscribeOn(Schedulers.newThread()).map(new Function<Integer, Integer>() {
@Override
public Integer apply(Integer t1) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
return t1;
}
}).take(4).toList().blockingGet().toArray();
assertEquals(2, counter.get());
assertArrayEquals(new Object[] { 1, 2, 1, 2 }, ys);
}
use of io.reactivex.rxjava3.core.Flowable in project RxJava by ReactiveX.
the class FlowableRepeatTest method noCancelPreviousRepeat.
@Test
public void noCancelPreviousRepeat() {
final AtomicInteger counter = new AtomicInteger();
Flowable<Integer> source = Flowable.just(1).doOnCancel(new Action() {
@Override
public void run() throws Exception {
counter.getAndIncrement();
}
});
source.repeat(5).test().assertResult(1, 1, 1, 1, 1);
assertEquals(0, counter.get());
}
Aggregations