use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableConcatTest method testConcatNonBlockingObservables.
@Test
public void testConcatNonBlockingObservables() {
final CountDownLatch okToContinueW1 = new CountDownLatch(1);
final CountDownLatch okToContinueW2 = new CountDownLatch(1);
final TestObservable<String> w1 = new TestObservable<String>(null, okToContinueW1, "one", "two", "three");
final TestObservable<String> w2 = new TestObservable<String>(null, okToContinueW2, "four", "five", "six");
Observer<String> observer = TestHelper.mockObserver();
Observable<Observable<String>> observableOfObservables = Observable.unsafeCreate(new ObservableSource<Observable<String>>() {
@Override
public void subscribe(Observer<? super Observable<String>> observer) {
observer.onSubscribe(Disposables.empty());
// simulate what would happen in an Observable
observer.onNext(Observable.unsafeCreate(w1));
observer.onNext(Observable.unsafeCreate(w2));
observer.onComplete();
}
});
Observable<String> concat = Observable.concat(observableOfObservables);
concat.subscribe(observer);
verify(observer, times(0)).onComplete();
try {
// release both threads
okToContinueW1.countDown();
okToContinueW2.countDown();
// wait for both to finish
w1.t.join();
w2.t.join();
} 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(1)).onNext("four");
inOrder.verify(observer, times(1)).onNext("five");
inOrder.verify(observer, times(1)).onNext("six");
verify(observer, times(1)).onComplete();
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableBufferTest method bufferWithStartEndEndThrows.
@Test
public void bufferWithStartEndEndThrows() {
PublishSubject<Integer> start = PublishSubject.create();
Function<Integer, Observable<Integer>> end = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return Observable.error(new TestException());
}
};
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);
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 ObservableFlatMapTest method testFlatMapSelectorMaxConcurrent.
@Test
public void testFlatMapSelectorMaxConcurrent() {
final int m = 4;
final AtomicInteger subscriptionCount = new AtomicInteger();
Observable<Integer> source = Observable.range(1, 10).flatMap(new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return composer(Observable.range(t1 * 10, 2), subscriptionCount, m).subscribeOn(Schedulers.computation());
}
}, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 * 1000 + t2;
}
}, m);
TestObserver<Integer> ts = new TestObserver<Integer>();
source.subscribe(ts);
ts.awaitTerminalEvent();
ts.assertNoErrors();
Set<Integer> expected = new HashSet<Integer>(Arrays.asList(1010, 1011, 2020, 2021, 3030, 3031, 4040, 4041, 5050, 5051, 6060, 6061, 7070, 7071, 8080, 8081, 9090, 9091, 10100, 10101));
Assert.assertEquals(expected.size(), ts.valueCount());
System.out.println("--> testFlatMapSelectorMaxConcurrent: " + ts.values());
Assert.assertTrue(expected.containsAll(ts.values()));
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableFlatMapTest method testFlatMapTransformsMaxConcurrentNormal.
@Test
public void testFlatMapTransformsMaxConcurrentNormal() {
final int m = 2;
final AtomicInteger subscriptionCount = new AtomicInteger();
Observable<Integer> onNext = composer(Observable.fromIterable(Arrays.asList(1, 2, 3)).observeOn(Schedulers.computation()), subscriptionCount, m).subscribeOn(Schedulers.computation());
Observable<Integer> onComplete = composer(Observable.fromIterable(Arrays.asList(4)), subscriptionCount, m).subscribeOn(Schedulers.computation());
Observable<Integer> onError = Observable.fromIterable(Arrays.asList(5));
Observable<Integer> source = Observable.fromIterable(Arrays.asList(10, 20, 30));
Observer<Object> o = TestHelper.mockObserver();
TestObserver<Object> ts = new TestObserver<Object>(o);
Function<Throwable, Observable<Integer>> just = just(onError);
source.flatMap(just(onNext), just, just0(onComplete), m).subscribe(ts);
ts.awaitTerminalEvent(1, TimeUnit.SECONDS);
ts.assertNoErrors();
ts.assertTerminated();
verify(o, times(3)).onNext(1);
verify(o, times(3)).onNext(2);
verify(o, times(3)).onNext(3);
verify(o).onNext(4);
verify(o).onComplete();
verify(o, never()).onNext(5);
verify(o, never()).onError(any(Throwable.class));
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableDelayTest method testDelayWithObservableAsTimed.
@Test
public void testDelayWithObservableAsTimed() {
Observable<Long> source = Observable.interval(1L, TimeUnit.SECONDS, scheduler).take(3);
final Observable<Long> delayer = Observable.timer(500L, TimeUnit.MILLISECONDS, scheduler);
Function<Long, Observable<Long>> delayFunc = new Function<Long, Observable<Long>>() {
@Override
public Observable<Long> apply(Long t1) {
return delayer;
}
};
Observable<Long> delayed = source.delay(delayFunc);
delayed.subscribe(observer);
InOrder inOrder = inOrder(observer);
scheduler.advanceTimeTo(1499L, TimeUnit.MILLISECONDS);
verify(observer, never()).onNext(anyLong());
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(1500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(0L);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(2400L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(2500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(1L);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(3400L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, never()).onNext(anyLong());
verify(observer, never()).onComplete();
verify(observer, never()).onError(any(Throwable.class));
scheduler.advanceTimeTo(3500L, TimeUnit.MILLISECONDS);
inOrder.verify(observer, times(1)).onNext(2L);
verify(observer, times(1)).onComplete();
verify(observer, never()).onError(any(Throwable.class));
}
Aggregations