use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.
the class ObservableDelaySubscriptionOtherTest method testNoPrematureSubscription.
@Test
public void testNoPrematureSubscription() {
PublishSubject<Object> other = PublishSubject.create();
TestObserver<Integer> ts = new TestObserver<Integer>();
final AtomicInteger subscribed = new AtomicInteger();
Observable.just(1).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable d) {
subscribed.getAndIncrement();
}
}).delaySubscription(other).subscribe(ts);
ts.assertNotComplete();
ts.assertNoErrors();
ts.assertNoValues();
Assert.assertEquals("Premature subscription", 0, subscribed.get());
other.onNext(1);
Assert.assertEquals("No subscription", 1, subscribed.get());
ts.assertValue(1);
ts.assertNoErrors();
ts.assertComplete();
}
use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.
the class ObservableOnErrorResumeNextViaObservableTest method testMapResumeAsyncNext.
@Test
public void testMapResumeAsyncNext() {
Disposable sr = mock(Disposable.class);
// Trigger multiple failures
Observable<String> w = Observable.just("one", "fail", "two", "three", "fail");
// Resume Observable is async
TestObservable f = new TestObservable(sr, "twoResume", "threeResume");
Observable<String> resume = Observable.unsafeCreate(f);
// Introduce map function that fails intermittently (Map does not prevent this when the Observer is a
// rx.operator incl onErrorResumeNextViaObservable)
w = w.map(new Function<String, String>() {
@Override
public String apply(String s) {
if ("fail".equals(s)) {
throw new RuntimeException("Forced Failure");
}
System.out.println("BadMapper:" + s);
return s;
}
});
Observable<String> observable = w.onErrorResumeNext(resume);
Observer<String> observer = TestHelper.mockObserver();
observable.subscribe(observer);
try {
f.t.join();
} catch (InterruptedException e) {
fail(e.getMessage());
}
verify(observer, Mockito.never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
verify(observer, times(1)).onNext("one");
verify(observer, Mockito.never()).onNext("two");
verify(observer, Mockito.never()).onNext("three");
verify(observer, times(1)).onNext("twoResume");
verify(observer, times(1)).onNext("threeResume");
}
use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.
the class ObservableDelaySubscriptionOtherTest method testNoMultipleSubscriptions.
@Test
public void testNoMultipleSubscriptions() {
PublishSubject<Object> other = PublishSubject.create();
TestObserver<Integer> ts = new TestObserver<Integer>();
final AtomicInteger subscribed = new AtomicInteger();
Observable.just(1).doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable d) {
subscribed.getAndIncrement();
}
}).delaySubscription(other).subscribe(ts);
ts.assertNotComplete();
ts.assertNoErrors();
ts.assertNoValues();
Assert.assertEquals("Premature subscription", 0, subscribed.get());
other.onNext(1);
other.onNext(2);
Assert.assertEquals("No subscription", 1, subscribed.get());
ts.assertValue(1);
ts.assertNoErrors();
ts.assertComplete();
}
use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.
the class ObservableDoOnUnsubscribeTest method testDoOnUnSubscribeWorksWithRefCount.
@Test
public void testDoOnUnSubscribeWorksWithRefCount() 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<Disposable>();
List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
for (int i = 0; i < subCount; ++i) {
TestObserver<Long> observer = new TestObserver<Long>();
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());
}
use of io.reactivex.disposables.Disposable in project RxJava by ReactiveX.
the class ObservableDoOnUnsubscribeTest method testDoOnUnsubscribe.
@Test
public void testDoOnUnsubscribe() 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<Disposable>();
List<TestObserver<Long>> subscribers = new ArrayList<TestObserver<Long>>();
for (int i = 0; i < subCount; ++i) {
TestObserver<Long> observer = new TestObserver<Long>();
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());
}
Aggregations