use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableUsingTest method performTestUsing.
private void performTestUsing(boolean disposeEagerly) {
final Resource resource = mock(Resource.class);
when(resource.getTextFromWeb()).thenReturn("Hello world!");
Callable<Resource> resourceFactory = new Callable<Resource>() {
@Override
public Resource call() {
return resource;
}
};
Function<Resource, Observable<String>> observableFactory = new Function<Resource, Observable<String>>() {
@Override
public Observable<String> apply(Resource res) {
return Observable.fromArray(res.getTextFromWeb().split(" "));
}
};
Observer<String> observer = TestHelper.mockObserver();
Observable<String> o = Observable.using(resourceFactory, observableFactory, new DisposeAction(), disposeEagerly);
o.subscribe(observer);
InOrder inOrder = inOrder(observer);
inOrder.verify(observer, times(1)).onNext("Hello");
inOrder.verify(observer, times(1)).onNext("world!");
inOrder.verify(observer, times(1)).onComplete();
inOrder.verifyNoMoreInteractions();
// The resouce should be closed
verify(resource, times(1)).dispose();
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableWindowWithObservableTest method newBoundaryCalledAfterWindowClosed.
@Test
public void newBoundaryCalledAfterWindowClosed() {
final AtomicInteger calls = new AtomicInteger();
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> boundary = PublishSubject.create();
Callable<Observable<Integer>> boundaryFunc = new Callable<Observable<Integer>>() {
@Override
public Observable<Integer> call() {
calls.getAndIncrement();
return boundary;
}
};
TestObserver<Observable<Integer>> ts = new TestObserver<Observable<Integer>>();
source.window(boundaryFunc).subscribe(ts);
source.onNext(1);
boundary.onNext(1);
assertTrue(boundary.hasObservers());
source.onNext(2);
boundary.onNext(2);
assertTrue(boundary.hasObservers());
source.onNext(3);
boundary.onNext(3);
assertTrue(boundary.hasObservers());
source.onNext(4);
source.onComplete();
ts.assertNoErrors();
ts.assertValueCount(4);
ts.assertComplete();
assertFalse(source.hasObservers());
assertFalse(boundary.hasObservers());
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorSubsequentObservableThrows.
@Test
public void testTimeoutSelectorSubsequentObservableThrows() {
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> timeout = PublishSubject.create();
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return Observable.<Integer>error(new TestException());
}
};
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
source.timeout(timeout, timeoutFunc, other).subscribe(o);
source.onNext(1);
inOrder.verify(o).onNext(1);
inOrder.verify(o).onError(any(TestException.class));
verify(o, never()).onComplete();
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorWithTimeoutFirstAndNoOtherObservable.
@Test
public void testTimeoutSelectorWithTimeoutFirstAndNoOtherObservable() {
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> timeout = PublishSubject.create();
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
return timeout;
}
};
Observer<Object> o = TestHelper.mockObserver();
source.timeout(PublishSubject.create(), timeoutFunc).subscribe(o);
source.onNext(1);
timeout.onNext(1);
InOrder inOrder = inOrder(o);
inOrder.verify(o).onNext(1);
inOrder.verify(o).onError(isA(TimeoutException.class));
inOrder.verifyNoMoreInteractions();
}
use of io.reactivex.Observable in project RxJava by ReactiveX.
the class ObservableTimeoutWithSelectorTest method testTimeoutSelectorSubsequentThrows.
@Test
public void testTimeoutSelectorSubsequentThrows() {
PublishSubject<Integer> source = PublishSubject.create();
final PublishSubject<Integer> timeout = PublishSubject.create();
Function<Integer, Observable<Integer>> timeoutFunc = new Function<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> apply(Integer t1) {
throw new TestException();
}
};
Observable<Integer> other = Observable.fromIterable(Arrays.asList(100));
Observer<Object> o = TestHelper.mockObserver();
InOrder inOrder = inOrder(o);
source.timeout(timeout, timeoutFunc, other).subscribe(o);
source.onNext(1);
inOrder.verify(o).onNext(1);
inOrder.verify(o).onError(any(TestException.class));
verify(o, never()).onComplete();
}
Aggregations