@Override public Publisher<Long> createPublisher(final long elements) { return Flowable.fromCallable(new Callable<Long>() { @Override public Long call() throws Exception { return 1L; } } ) ; }
@Override public Publisher<? extends Object> apply(Integer v) throws Exception { return Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return ++calls[0]; } }); } })
@Override public Publisher<? extends Object> apply(Integer v) throws Exception { return Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return ++calls[0]; } }); } })
@Override public Publisher<? extends Object> apply(Integer v) throws Exception { return Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return null; } }); } })
@Override public Publisher<? extends Object> apply(Integer v) throws Exception { return Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return null; } }); } })
@Override public Flowable<Integer> apply(Integer v) throws Exception { return Flowable.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { throw new TestException(); } }); } })
@Test(expected = NullPointerException.class) public void fromCallableNull() { Flowable.fromCallable(null); }
@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(); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnError() throws Exception { Callable<Object> func = mock(Callable.class); Throwable throwable = new IllegalStateException("Test exception"); when(func.call()).thenThrow(throwable); Flowable<Object> fromCallableFlowable = Flowable.fromCallable(func); Subscriber<Object> subscriber = TestHelper.mockSubscriber(); fromCallableFlowable.subscribe(subscriber); verify(subscriber, never()).onNext(any()); verify(subscriber, never()).onComplete(); verify(subscriber).onError(throwable); }
@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)); }
@Test(expected = NullPointerException.class) public void fromCallableReturnsNull() { Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return null; } }).blockingLast(); }
@Test public void cancelledUpFrontConnectAnyway() { final AtomicInteger call = new AtomicInteger(); Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return call.incrementAndGet(); } }) .cache() .test(1L, true) .assertNoValues(); assertEquals(1, call.get()); }
@Test public void scalarXMap() { Flowable.fromCallable(Functions.justCallable(1)) .flatMap(Functions.justFunction(Flowable.fromCallable(Functions.justCallable(2)))) .test() .assertResult(2); }
@Test public void callableThrows() { Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new TestException(); } }) .flatMapIterable(Functions.justFunction(Arrays.asList(1, 2, 3))) .test() .assertFailure(TestException.class); }
@Test public void callable() { Maybe.concat(Flowable.fromCallable(new Callable<Maybe<Integer>>() { @Override public Maybe<Integer> call() throws Exception { return Maybe.just(1); } })) .test() .assertResult(1); } }
@Test public void callable() { Single.concat(Flowable.fromCallable(new Callable<Single<Integer>>() { @Override public Single<Integer> call() throws Exception { return Single.just(1); } })) .test() .assertResult(1); } }
@Test public void mergeScalarError() { Flowable.merge(Flowable.just(Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new TestException(); } })).hide()) .test() .assertFailure(TestException.class); }
@Test public void callableCrash() { Flowable.just(1).hide() .concatMap(Functions.justFunction(Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new TestException(); } }))) .test() .assertFailure(TestException.class); }
@Test public void scalarXMap() { Flowable.fromCallable(Functions.justCallable(1)) .switchMap(Functions.justFunction(Flowable.just(1))) .test() .assertResult(1); }
@Test public void callableCrashDelayError() { Flowable.just(1).hide() .concatMapDelayError(Functions.justFunction(Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new TestException(); } }))) .test() .assertFailure(TestException.class); }