@Override public Scheduler apply(Callable<Scheduler> schedulerCallable) throws Exception { return schedulerCallable.call(); } };
@Override public void subscribeActual(Subscriber<? super T> s) { S state; try { state = stateSupplier.call(); } catch (Throwable e) { Exceptions.throwIfFatal(e); EmptySubscription.error(e, s); return; } s.onSubscribe(new GeneratorSubscription<T, S>(s, generator, disposeState, state)); }
@SuppressWarnings("unchecked") @Test public void shouldNotInvokeFuncUntilSubscription() throws Exception { Callable<Object> func = mock(Callable.class); when(func.call()).thenReturn(new Object()); Observable<Object> fromCallableObservable = Observable.fromCallable(func); verifyZeroInteractions(func); fromCallableObservable.subscribe(); verify(func).call(); }
@Override protected void subscribeActual(SingleObserver<? super T> observer) { Throwable error; try { error = ObjectHelper.requireNonNull(errorSupplier.call(), "Callable returned null throwable. Null values are generally not allowed in 2.x operators and sources."); } catch (Throwable e) { Exceptions.throwIfFatal(e); error = e; } EmptyDisposable.error(error, observer); }
@SuppressWarnings("unchecked") @Test public void shouldNotInvokeFuncUntilSubscription() throws Exception { Callable<Object> func = mock(Callable.class); when(func.call()).thenReturn(new Object()); Single<Object> fromCallableSingle = Single.fromCallable(func); verifyZeroInteractions(func); fromCallableSingle.subscribe(); verify(func).call(); }
@Override public void subscribeActual(Subscriber<? super T> s) { Throwable error; try { error = ObjectHelper.requireNonNull(errorSupplier.call(), "Callable returned null throwable. Null values are generally not allowed in 2.x operators and sources."); } catch (Throwable t) { Exceptions.throwIfFatal(t); error = t; } EmptySubscription.error(error, s); } }
@SuppressWarnings("unchecked") @Test public void shouldNotInvokeFuncUntilSubscription() throws Exception { Callable<Object> func = mock(Callable.class); when(func.call()).thenReturn(new Object()); Flowable<Object> fromCallableFlowable = Flowable.fromCallable(func); verifyZeroInteractions(func); fromCallableFlowable.subscribe(); verify(func).call(); }
@Override protected void subscribeActual(Subscriber<? super U> s) { U u; try { u = ObjectHelper.requireNonNull(initialSupplier.call(), "The initial value supplied is null"); } catch (Throwable e) { EmptySubscription.error(e, s); return; } source.subscribe(new CollectSubscriber<T, U>(s, u, collector)); }
@Override protected void subscribeActual(CompletableObserver observer) { CompletableSource c; try { c = ObjectHelper.requireNonNull(completableSupplier.call(), "The completableSupplier returned a null CompletableSource"); } catch (Throwable e) { Exceptions.throwIfFatal(e); EmptyDisposable.error(e, observer); return; } c.subscribe(observer); }
@Override protected void subscribeActual(SingleObserver<? super U> observer) { U u; try { u = ObjectHelper.requireNonNull(initialSupplier.call(), "The initialSupplier returned a null value"); } catch (Throwable e) { EmptyDisposable.error(e, observer); return; } source.subscribe(new CollectSubscriber<T, U>(observer, u, collector)); }
@Override protected void subscribeActual(SingleObserver<? super T> observer) { SingleSource<? extends T> next; try { next = ObjectHelper.requireNonNull(singleSupplier.call(), "The singleSupplier returned a null SingleSource"); } catch (Throwable e) { Exceptions.throwIfFatal(e); EmptyDisposable.error(e, observer); return; } next.subscribe(observer); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnNextAndOnCompleted() throws Exception { Callable<String> func = mock(Callable.class); when(func.call()).thenReturn("test_value"); Observable<String> fromCallableObservable = Observable.fromCallable(func); Observer<Object> observer = TestHelper.mockObserver(); fromCallableObservable.subscribe(observer); verify(observer).onNext("test_value"); verify(observer).onComplete(); verify(observer, never()).onError(any(Throwable.class)); }
@Test public void testDeferFunctionThrows() throws Exception { Callable<Observable<String>> factory = mock(Callable.class); when(factory.call()).thenThrow(new TestException()); Observable<String> result = Observable.defer(factory); Observer<String> o = TestHelper.mockObserver(); result.subscribe(o); verify(o).onError(any(TestException.class)); verify(o, never()).onNext(any(String.class)); verify(o, never()).onComplete(); } }
@Override protected void subscribeActual(SingleObserver<? super U> observer) { U coll; try { coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources."); } catch (Throwable e) { Exceptions.throwIfFatal(e); EmptyDisposable.error(e, observer); return; } source.subscribe(new ToListSubscriber<T, U>(observer, coll)); }
@Override protected void subscribeActual(Subscriber<? super U> s) { U coll; try { coll = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources."); } catch (Throwable e) { Exceptions.throwIfFatal(e); EmptySubscription.error(e, s); return; } source.subscribe(new ToListSubscriber<T, U>(s, coll)); }
@Override public void subscribeActual(Observer<? super R> t) { R r; try { r = ObjectHelper.requireNonNull(seedSupplier.call(), "The seed supplied is null"); } catch (Throwable e) { Exceptions.throwIfFatal(e); EmptyDisposable.error(e, t); return; } source.subscribe(new ScanSeedObserver<T, R>(t, accumulator, r)); }
@Override protected void subscribeActual(Subscriber<? super T> subscriber) { Collection<? super K> collection; try { collection = ObjectHelper.requireNonNull(collectionSupplier.call(), "The collectionSupplier returned a null collection. Null values are generally not allowed in 2.x operators and sources."); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); EmptySubscription.error(ex, subscriber); return; } source.subscribe(new DistinctSubscriber<T, K>(subscriber, keySelector, collection)); }
@Override public void onComplete() { MaybeSource<? extends R> source; try { source = ObjectHelper.requireNonNull(onCompleteSupplier.call(), "The onCompleteSupplier returned a null MaybeSource"); } catch (Exception ex) { Exceptions.throwIfFatal(ex); downstream.onError(ex); return; } source.subscribe(new InnerObserver()); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnNextAndOnCompleted() throws Exception { Callable<String> func = mock(Callable.class); when(func.call()).thenReturn("test_value"); Flowable<String> fromCallableFlowable = Flowable.fromCallable(func); Subscriber<String> subscriber = TestHelper.mockSubscriber(); fromCallableFlowable.subscribe(subscriber); verify(subscriber).onNext("test_value"); verify(subscriber).onComplete(); verify(subscriber, never()).onError(any(Throwable.class)); }
@Override protected void subscribeActual(Subscriber<? super R> s) { R r; try { r = ObjectHelper.requireNonNull(seedSupplier.call(), "The seed supplied is null"); } catch (Throwable e) { Exceptions.throwIfFatal(e); EmptySubscription.error(e, s); return; } source.subscribe(new ScanSeedSubscriber<T, R>(s, accumulator, r, bufferSize())); }