use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableConcatTest method testConcatUnsubscribeConcurrent.
/**
* All observables will be running in different threads so subscribe() is unblocked. CountDownLatch is only used in order to call unsubscribe() in a predictable manner.
*/
@Test
public void testConcatUnsubscribeConcurrent() {
final CountDownLatch callOnce = new CountDownLatch(1);
final CountDownLatch okToContinue = new CountDownLatch(1);
final TestObservable<String> w1 = new TestObservable<String>("one", "two", "three");
final TestObservable<String> w2 = new TestObservable<String>(callOnce, okToContinue, "four", "five", "six");
Observer<String> observer = TestHelper.mockObserver();
TestObserver<String> ts = new TestObserver<String>(observer);
@SuppressWarnings("unchecked") TestObservable<Observable<String>> observableOfObservables = new TestObservable<Observable<String>>(Observable.unsafeCreate(w1), Observable.unsafeCreate(w2));
Observable<String> concatF = Observable.concat(Observable.unsafeCreate(observableOfObservables));
concatF.subscribe(ts);
try {
//Block main thread to allow Observable "w1" to complete and Observable "w2" to call onNext exactly once.
callOnce.await();
//"four" from w2 has been processed by onNext()
ts.dispose();
//"five" and "six" will NOT be processed by onNext()
//Unblock the Observable to continue.
okToContinue.countDown();
w1.t.join();
w2.t.join();
} catch (Throwable e) {
e.printStackTrace();
fail(e.getMessage());
}
InOrder inOrder = inOrder(observer);
inOrder.verify(observer, times(1)).onNext("one");
inOrder.verify(observer, times(1)).onNext("two");
inOrder.verify(observer, times(1)).onNext("three");
inOrder.verify(observer, times(1)).onNext("four");
inOrder.verify(observer, never()).onNext("five");
inOrder.verify(observer, never()).onNext("six");
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableConcatTest method concatVeryLongObservableOfObservables.
@Test
public void concatVeryLongObservableOfObservables() {
final int n = 10000;
Observable<Observable<Integer>> source = Observable.range(0, n).map(new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer v) {
return Observable.just(v);
}
});
Single<List<Integer>> result = Observable.concat(source).toList();
SingleObserver<List<Integer>> o = TestHelper.mockSingleObserver();
InOrder inOrder = inOrder(o);
result.subscribe(o);
List<Integer> list = new ArrayList<Integer>(n);
for (int i = 0; i < n; i++) {
list.add(i);
}
inOrder.verify(o).onSuccess(list);
verify(o, never()).onError(any(Throwable.class));
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableBufferTest method bufferWithStartEndStartThrows.
@Test
public void bufferWithStartEndStartThrows() {
PublishSubject<Integer> start = PublishSubject.create();
Function<Integer, Observable<Integer>> end = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return Observable.never();
}
};
PublishSubject<Integer> source = PublishSubject.create();
Observable<List<Integer>> result = source.buffer(start, end);
Observer<Object> o = TestHelper.mockObserver();
result.subscribe(o);
start.onNext(1);
source.onNext(1);
source.onNext(2);
start.onError(new TestException());
verify(o, never()).onNext(any());
verify(o, never()).onComplete();
verify(o).onError(any(TestException.class));
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservablePublishTest method badSource.
@Test
public void badSource() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
new Observable<Integer>() {
@Override
protected void subscribeActual(Observer<? super Integer> observer) {
observer.onSubscribe(Disposables.empty());
observer.onNext(1);
observer.onComplete();
observer.onNext(2);
observer.onError(new TestException());
observer.onComplete();
}
}.publish().autoConnect().test().assertResult(1);
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableDelayTest method testDelayWithObservableSubscriptionFunctionThrows.
@Test
public void testDelayWithObservableSubscriptionFunctionThrows() {
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> delay = PublishSubject.create();
Callable<Observable<Integer>> subFunc = new Callable<Observable<Integer>>() {
@Override
public Observable<Integer> call() {
throw new TestException();
}
};
Function<Integer, Observable<Integer>> delayFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return delay;
}
};
Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
source.delay(Observable.defer(subFunc), delayFunc).subscribe(o);
source.onNext(1);
delay.onNext(1);
source.onNext(2);
inOrder.verify(o).onError(any(TestException.class));
inOrder.verifyNoMoreInteractions();
verify(o, never()).onNext(any());
verify(o, never()).onComplete();
}
Aggregations