use of io.reactivex.rxjava3.functions.Consumer 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.Consumer 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.Consumer in project RxJava by ReactiveX.
the class MaybeSafeSubscribeTest method normalEmpty.
@Test
public void normalEmpty() throws Throwable {
TestHelper.withErrorTracking(errors -> {
@SuppressWarnings("unchecked") MaybeObserver<Integer> consumer = mock(MaybeObserver.class);
Maybe.<Integer>empty().safeSubscribe(consumer);
InOrder order = inOrder(consumer);
order.verify(consumer).onSubscribe(any(Disposable.class));
order.verify(consumer).onComplete();
order.verifyNoMoreInteractions();
});
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class MaybeSafeSubscribeTest method onCompleteCrash.
@Test
public void onCompleteCrash() throws Throwable {
TestHelper.withErrorTracking(errors -> {
@SuppressWarnings("unchecked") MaybeObserver<Integer> consumer = mock(MaybeObserver.class);
doThrow(new TestException()).when(consumer).onComplete();
new Maybe<Integer>() {
@Override
protected void subscribeActual(@NonNull MaybeObserver<? super Integer> observer) {
observer.onSubscribe(Disposable.empty());
// none of the following should arrive at the consumer
observer.onComplete();
}
}.safeSubscribe(consumer);
InOrder order = inOrder(consumer);
order.verify(consumer).onSubscribe(any(Disposable.class));
order.verify(consumer).onComplete();
order.verifyNoMoreInteractions();
TestHelper.assertUndeliverable(errors, 0, TestException.class);
});
}
use of io.reactivex.rxjava3.functions.Consumer in project RxJava by ReactiveX.
the class ObservableWindowWithStartEndObservableTest method windowCloseIngoresCancel.
@Test
public void windowCloseIngoresCancel() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
BehaviorSubject.createDefault(1).window(BehaviorSubject.createDefault(1), new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer f) throws Exception {
return new Observable<Integer>() {
@Override
protected void subscribeActual(Observer<? super Integer> observer) {
observer.onSubscribe(Disposable.empty());
observer.onNext(1);
observer.onNext(2);
observer.onError(new TestException());
}
};
}
}).doOnNext(new Consumer<Observable<Integer>>() {
@Override
public void accept(Observable<Integer> w) throws Throwable {
// avoid abandonment
w.subscribe(Functions.emptyConsumer(), Functions.emptyConsumer());
}
}).test().assertValueCount(1).assertNoErrors().assertNotComplete();
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
Aggregations