use of io.reactivex.rxjava3.subscribers in project RxJava by ReactiveX.
the class ObservableDoOnUnsubscribeTest 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();
Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(new Action() {
@Override
public void run() {
// Test that upper stream will be notified for un-subscription
// from a child Observer
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();
}
}).doOnDispose(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<TestObserver<Long>> subscribers = new ArrayList<>();
for (int i = 0; i < subCount; ++i) {
TestObserver<Long> observer = new TestObserver<>();
subscriptions.add(observer);
longs.subscribe(observer);
subscribers.add(observer);
}
onNextLatch.await();
for (int i = 0; i < subCount; ++i) {
subscriptions.get(i).dispose();
// Test that unsubscribe() method is not affected in any way
// FIXME no longer valid
// subscribers.get(i).assertUnsubscribed();
}
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.subscribers in project RxJava by ReactiveX.
the class ObservableDoOnUnsubscribeTest 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();
Observable<Long> longs = Observable.interval(50, TimeUnit.MILLISECONDS).doOnDispose(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();
}
}).doOnDispose(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<TestObserver<Long>> subscribers = new ArrayList<>();
for (int i = 0; i < subCount; ++i) {
TestObserver<Long> observer = new TestObserver<>();
longs.subscribe(observer);
subscriptions.add(observer);
subscribers.add(observer);
}
onNextLatch.await();
for (int i = 0; i < subCount; ++i) {
subscriptions.get(i).dispose();
// Test that unsubscribe() method is not affected in any way
// FIXME no longer valid
// subscribers.get(i).assertUnsubscribed();
}
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());
}
Aggregations