@Override public SingleSource<Integer> apply(final String s) throws NumberFormatException { //return Single.just(Integer.valueOf(s)); //This works return Single.fromCallable(new Callable<Integer>() { @Override public Integer call() throws NumberFormatException { return Integer.valueOf(s); } }); } })
@Override public SingleSource<Integer> apply(final String s) throws NumberFormatException { //return Single.just(Integer.valueOf(s)); //This works return Single.fromCallable(new Callable<Integer>() { @Override public Integer call() throws NumberFormatException { return Integer.valueOf(s); } }); } })
@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(); }
@Test(expected = NullPointerException.class) public void fromCallableNull() { Single.fromCallable(null); }
@Test(expected = NullPointerException.class) public void fromCallableReturnsNull() { Single.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return null; } }).blockingGet(); }
@Test public void shouldAllowToThrowCheckedException() { final Exception checkedException = new Exception("test exception"); Single<Object> fromCallableObservable = Single.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw checkedException; } }); SingleObserver<Object> observer = TestHelper.mockSingleObserver(); fromCallableObservable.subscribe(observer); verify(observer).onSubscribe(any(Disposable.class)); verify(observer).onError(checkedException); verifyNoMoreInteractions(observer); }
@Test public void disposedOnCall() { final TestObserver<Integer> to = new TestObserver<Integer>(); Single.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { to.cancel(); return 1; } }) .subscribe(to); to.assertEmpty(); }
@Test public void fromCallableTwice() { final AtomicInteger atomicInteger = new AtomicInteger(); Callable<Integer> callable = new Callable<Integer>() { @Override public Integer call() throws Exception { return atomicInteger.incrementAndGet(); } }; Single.fromCallable(callable) .test() .assertResult(1); assertEquals(1, atomicInteger.get()); Single.fromCallable(callable) .test() .assertResult(2); assertEquals(2, atomicInteger.get()); }
@Test public void fromCallableValue() { Single.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { return 5; } }) .test() .assertResult(5); }
@Test public void fromCallableError() { Single.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { throw new UnsupportedOperationException(); } }) .test() .assertFailure(UnsupportedOperationException.class); }
@Test public void fromCallableNull() { Single.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { return null; } }) .test() .assertFailureAndMessage(NullPointerException.class, "The callable returned a null value"); }
@Test public void retryTimesPredicateWithMatchingPredicate() { final AtomicInteger atomicInteger = new AtomicInteger(3); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Single.fromCallable(new Callable<Boolean>() { @Override public Boolean call() throws Exception { numberOfSubscribeCalls.incrementAndGet(); if (atomicInteger.decrementAndGet() != 0) { throw new RuntimeException(); } throw new IllegalArgumentException(); } }) .retry(Integer.MAX_VALUE, new Predicate<Throwable>() { @Override public boolean test(final Throwable throwable) throws Exception { return !(throwable instanceof IllegalArgumentException); } }) .test() .assertFailure(IllegalArgumentException.class); assertEquals(3, numberOfSubscribeCalls.get()); }
@Test public void retryTimesPredicateWithMatchingRetryAmount() { final AtomicInteger atomicInteger = new AtomicInteger(3); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Single.fromCallable(new Callable<Boolean>() { @Override public Boolean call() throws Exception { numberOfSubscribeCalls.incrementAndGet(); if (atomicInteger.decrementAndGet() != 0) { throw new RuntimeException(); } return true; } }) .retry(2, Functions.alwaysTrue()) .test() .assertResult(true); assertEquals(3, numberOfSubscribeCalls.get()); }
@Test public void retryTimesPredicateWithNotMatchingRetryAmount() { final AtomicInteger atomicInteger = new AtomicInteger(3); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Single.fromCallable(new Callable<Boolean>() { @Override public Boolean call() throws Exception { numberOfSubscribeCalls.incrementAndGet(); if (atomicInteger.decrementAndGet() != 0) { throw new RuntimeException(); } return true; } }) .retry(1, Functions.alwaysTrue()) .test() .assertFailure(RuntimeException.class); assertEquals(2, numberOfSubscribeCalls.get()); }
@Test public void retryTimesPredicateWithZeroRetries() { final AtomicInteger atomicInteger = new AtomicInteger(2); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Single.fromCallable(new Callable<Boolean>() { @Override public Boolean call() throws Exception { numberOfSubscribeCalls.incrementAndGet(); if (atomicInteger.decrementAndGet() != 0) { throw new RuntimeException(); } return true; } }) .retry(0, Functions.alwaysTrue()) .test() .assertFailure(RuntimeException.class); assertEquals(1, numberOfSubscribeCalls.get()); } }
@Test public void disposedOnArrival() { final int[] count = { 0 }; Single.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { count[0]++; return 1; } }) .test(true) .assertEmpty(); assertEquals(0, count[0]); }
@Test public void retry() { Single.fromCallable(new Callable<Object>() { int c; @Override public Object call() throws Exception { if (++c != 5) { throw new TestException(); } return 1; } }) .retry() .test() .assertResult(1); }
@Test public void retryTimes() { Single.fromCallable(new Callable<Object>() { int c; @Override public Object call() throws Exception { if (++c != 5) { throw new TestException(); } return 1; } }) .retry(5) .test() .assertResult(1); }
@Test public void toObservableTake() { Single.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return 1; } }) .toObservable() .take(1) .test() .assertResult(1); }
@Test public void toObservableAndBack() { Single.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { return 1; } }) .toObservable() .singleOrError() .test() .assertResult(1); } }