use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class FlowableDoOnUnsubscribeTest method doOnUnSubscribeWorksWithRefCount.
@Test
public void doOnUnSubscribeWorksWithRefCount() throws Exception {
int subCount = 3;
final CountDownLatch upperLatch = new CountDownLatch(1);
final CountDownLatch lowerLatch = new CountDownLatch(1);
final CountDownLatch onNextLatch = new CountDownLatch(subCount);
final AtomicInteger upperCount = new AtomicInteger();
final AtomicInteger lowerCount = new AtomicInteger();
Flowable<Long> longs = Flowable.interval(50, TimeUnit.MILLISECONDS).doOnCancel(new Action() {
@Override
public void run() {
// Test that upper stream will be notified for un-subscription
upperLatch.countDown();
upperCount.incrementAndGet();
}
}).doOnNext(new Consumer<Long>() {
@Override
public void accept(Long aLong) {
// Ensure there is at least some onNext events before un-subscription happens
onNextLatch.countDown();
}
}).doOnCancel(new Action() {
@Override
public void run() {
// Test that lower stream will be notified for un-subscription
lowerLatch.countDown();
lowerCount.incrementAndGet();
}
}).publish().refCount();
List<Disposable> subscriptions = new ArrayList<>();
List<TestSubscriber<Long>> subscribers = new ArrayList<>();
for (int i = 0; i < subCount; ++i) {
TestSubscriber<Long> subscriber = new TestSubscriber<>();
longs.subscribe(subscriber);
subscriptions.add(Disposable.fromSubscription(subscriber));
subscribers.add(subscriber);
}
onNextLatch.await();
for (int i = 0; i < subCount; ++i) {
subscriptions.get(i).dispose();
// Test that unsubscribe() method is not affected in any way
}
upperLatch.await();
lowerLatch.await();
assertEquals("There should exactly 1 un-subscription events for upper stream", 1, upperCount.get());
assertEquals("There should exactly 1 un-subscription events for lower stream", 1, lowerCount.get());
}
use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class FlowableDoOnUnsubscribeTest method doOnUnsubscribe.
@Test
public void doOnUnsubscribe() throws Exception {
int subCount = 3;
final CountDownLatch upperLatch = new CountDownLatch(subCount);
final CountDownLatch lowerLatch = new CountDownLatch(subCount);
final CountDownLatch onNextLatch = new CountDownLatch(subCount);
final AtomicInteger upperCount = new AtomicInteger();
final AtomicInteger lowerCount = new AtomicInteger();
Flowable<Long> longs = Flowable.interval(50, TimeUnit.MILLISECONDS).doOnCancel(new Action() {
@Override
public void run() {
// Test that upper stream will be notified for un-subscription
// from a child subscriber
upperLatch.countDown();
upperCount.incrementAndGet();
}
}).doOnNext(new Consumer<Long>() {
@Override
public void accept(Long aLong) {
// Ensure there is at least some onNext events before un-subscription happens
onNextLatch.countDown();
}
}).doOnCancel(new Action() {
@Override
public void run() {
// Test that lower stream will be notified for a direct un-subscription
lowerLatch.countDown();
lowerCount.incrementAndGet();
}
});
List<Disposable> subscriptions = new ArrayList<>();
List<TestSubscriber<Long>> subscribers = new ArrayList<>();
for (int i = 0; i < subCount; ++i) {
TestSubscriber<Long> subscriber = new TestSubscriber<>();
subscriptions.add(Disposable.fromSubscription(subscriber));
longs.subscribe(subscriber);
subscribers.add(subscriber);
}
onNextLatch.await();
for (int i = 0; i < subCount; ++i) {
subscriptions.get(i).dispose();
// Test that unsubscribe() method is not affected in any way
}
upperLatch.await();
lowerLatch.await();
assertEquals(String.format("There should exactly %d un-subscription events for upper stream", subCount), subCount, upperCount.get());
assertEquals(String.format("There should exactly %d un-subscription events for lower stream", subCount), subCount, lowerCount.get());
}
use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class ObservableRetryTest method noCancelPreviousRetryUntil.
@Test
public void noCancelPreviousRetryUntil() {
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger times = new AtomicInteger();
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> get() throws Exception {
if (times.getAndIncrement() < 4) {
return Observable.error(new TestException());
}
return Observable.just(1);
}
}).doOnDispose(new Action() {
@Override
public void run() throws Exception {
counter.getAndIncrement();
}
});
source.retryUntil(new BooleanSupplier() {
@Override
public boolean getAsBoolean() throws Exception {
return false;
}
}).test().assertResult(1);
assertEquals(0, counter.get());
}
use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class ObservableUnsubscribeOnTest method error.
@Test
public void error() {
final int[] calls = { 0 };
Observable.error(new TestException()).doOnDispose(new Action() {
@Override
public void run() throws Exception {
calls[0]++;
}
}).unsubscribeOn(Schedulers.single()).test().assertFailure(TestException.class);
assertEquals(0, calls[0]);
}
use of io.reactivex.rxjava3.functions.Action in project RxJava by ReactiveX.
the class ObservableRetryTest method noCancelPreviousRepeatWhen.
@Test
public void noCancelPreviousRepeatWhen() {
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger times = new AtomicInteger();
Observable<Integer> source = Observable.defer(new Supplier<ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> get() throws Exception {
if (times.get() < 4) {
return Observable.error(new TestException());
}
return Observable.just(1);
}
}).doOnDispose(new Action() {
@Override
public void run() throws Exception {
counter.getAndIncrement();
}
});
source.retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(Observable<Throwable> e) throws Exception {
return e.takeWhile(new Predicate<Object>() {
@Override
public boolean test(Object v) throws Exception {
return times.getAndIncrement() < 4;
}
});
}
}).test().assertResult(1);
assertEquals(0, counter.get());
}
Aggregations