use of io.reactivex.Observable in project RxJava by ReactiveX.
the class FlowableElementAtTest method badSourceObservable.
@Test
public void badSourceObservable() {
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.onNext(2);
observer.onError(new TestException());
observer.onComplete();
}
}.elementAt(0).toFlowable().test().assertResult(1);
TestHelper.assertUndeliverable(errors, 0, TestException.class);
} finally {
RxJavaPlugins.reset();
}
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableConcatMapEagerTest method many.
@SuppressWarnings("unchecked")
@Test
@Ignore("Currently there are no 2-9 argument variants, use concatArrayEager()")
public void many() throws Exception {
for (int i = 2; i < 10; i++) {
Class<?>[] clazz = new Class[i];
Arrays.fill(clazz, Observable.class);
Observable<Integer>[] obs = new Observable[i];
Arrays.fill(obs, Observable.just(1));
Integer[] expected = new Integer[i];
Arrays.fill(expected, 1);
Method m = Observable.class.getMethod("concatEager", clazz);
TestObserver<Integer> ts = TestObserver.create();
((Observable<Integer>) m.invoke(null, (Object[]) obs)).subscribe(ts);
ts.assertValues(expected);
ts.assertNoErrors();
ts.assertComplete();
}
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableConcatTest method testNestedAsyncConcat.
/**
* Test an async Observable that emits more async Observables.
* @throws InterruptedException if the test is interrupted
*/
@Test
public void testNestedAsyncConcat() throws InterruptedException {
Observer<String> observer = TestHelper.mockObserver();
final TestObservable<String> o1 = new TestObservable<String>("one", "two", "three");
final TestObservable<String> o2 = new TestObservable<String>("four", "five", "six");
final TestObservable<String> o3 = new TestObservable<String>("seven", "eight", "nine");
final CountDownLatch allowThird = new CountDownLatch(1);
final AtomicReference<Thread> parent = new AtomicReference<Thread>();
final CountDownLatch parentHasStarted = new CountDownLatch(1);
final CountDownLatch parentHasFinished = new CountDownLatch(1);
Observable<Observable<String>> observableOfObservables = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {
@Override
public void subscribe(final Observer<? super Observable<String>> observer) {
final Disposable d = Disposables.empty();
observer.onSubscribe(d);
parent.set(new Thread(new Runnable() {
@Override
public void run() {
try {
// emit first
if (!d.isDisposed()) {
System.out.println("Emit o1");
observer.onNext(Observable.unsafeCreate(o1));
}
// emit second
if (!d.isDisposed()) {
System.out.println("Emit o2");
observer.onNext(Observable.unsafeCreate(o2));
}
// wait until sometime later and emit third
try {
allowThird.await();
} catch (InterruptedException e) {
observer.onError(e);
}
if (!d.isDisposed()) {
System.out.println("Emit o3");
observer.onNext(Observable.unsafeCreate(o3));
}
} catch (Throwable e) {
observer.onError(e);
} finally {
System.out.println("Done parent Observable");
observer.onComplete();
parentHasFinished.countDown();
}
}
}));
parent.get().start();
parentHasStarted.countDown();
}
});
Observable.concat(observableOfObservables).subscribe(observer);
// wait for parent to start
parentHasStarted.await();
try {
// wait for first 2 async observables to complete
System.out.println("Thread1 is starting ... waiting for it to complete ...");
o1.waitForThreadDone();
System.out.println("Thread2 is starting ... waiting for it to complete ...");
o2.waitForThreadDone();
} catch (Throwable e) {
throw new RuntimeException("failed waiting on threads", e);
}
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, times(1)).onNext("five");
inOrder.verify(observer, times(1)).onNext("six");
// we shouldn't have the following 3 yet
inOrder.verify(observer, never()).onNext("seven");
inOrder.verify(observer, never()).onNext("eight");
inOrder.verify(observer, never()).onNext("nine");
// we should not be completed yet
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
// now allow the third
allowThird.countDown();
try {
// wait for 3rd to complete
o3.waitForThreadDone();
} catch (Throwable e) {
throw new RuntimeException("failed waiting on threads", e);
}
try {
// wait for the parent to complete
if (!parentHasFinished.await(5, TimeUnit.SECONDS)) {
fail("Parent didn't finish within the time limit");
}
} catch (Throwable e) {
throw new RuntimeException("failed waiting on threads", e);
}
inOrder.verify(observer, times(1)).onNext("seven");
inOrder.verify(observer, times(1)).onNext("eight");
inOrder.verify(observer, times(1)).onNext("nine");
verify(observer, never()).onError(any(Throwable.class));
inOrder.verify(observer, times(1)).onComplete();
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableConcatTest method testConcatConcurrentWithInfinity.
@Test
public void testConcatConcurrentWithInfinity() {
final TestObservable<String> w1 = new TestObservable<String>("one", "two", "three");
//This Observable will send "hello" MAX_VALUE time.
final TestObservable<String> w2 = new TestObservable<String>("hello", Integer.MAX_VALUE);
Observer<String> observer = TestHelper.mockObserver();
@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.take(50).subscribe(observer);
//Wait for the thread to start up.
try {
w1.waitForThreadDone();
w2.waitForThreadDone();
} catch (InterruptedException e) {
e.printStackTrace();
}
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(47)).onNext("hello");
verify(observer, times(1)).onComplete();
verify(observer, never()).onError(any(Throwable.class));
}
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));
}
Aggregations