@Override public MaybeSource<Integer> apply(final String s) throws NumberFormatException { //return Single.just(Integer.valueOf(s)); //This works return Maybe.fromCallable(new Callable<Integer>() { @Override public Integer call() throws NumberFormatException { return Integer.valueOf(s); } }); } })
@Override public MaybeSource<Integer> apply(final String s) throws NumberFormatException { //return Maybe.just(Integer.valueOf(s)); //This works return Maybe.fromCallable(new Callable<Integer>() { @Override public Integer call() throws NumberFormatException { return Integer.valueOf(s); } }); } })
@Test(expected = NullPointerException.class) public void fromCallableNull() { Maybe.fromCallable(null); }
@SuppressWarnings("unchecked") @Test public void callable() throws Exception { final int[] counter = { 0 }; Maybe<Integer> m = Maybe.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { counter[0]++; return 0; } }); assertTrue(m.getClass().toString(), m instanceof Callable); assertEquals(0, ((Callable<Void>)m).call()); assertEquals(1, counter[0]); }
@Test public void fromCallable() { final AtomicInteger atomicInteger = new AtomicInteger(); Maybe.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { atomicInteger.incrementAndGet(); return null; } }) .test() .assertResult(); assertEquals(1, atomicInteger.get()); }
public Maybe<Long> saveStreamState(@NonNull final StreamInfo info, final long progressTime) { return Maybe.fromCallable(() -> database.runInTransaction(() -> { final long streamId = streamTable.upsert(new StreamEntity(info)); return streamStateTable.upsert(new StreamStateEntity(streamId, progressTime)); })).subscribeOn(Schedulers.io()); }
@Test public void fromCallableThrows() { Maybe.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new TestException(); } }) .test() .assertFailure(TestException.class); }
@Test public void fromCallableThrows() { Maybe.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new UnsupportedOperationException(); } }) .test() .assertFailure(UnsupportedOperationException.class); }
@Test public void retryTimesPredicateWithMatchingPredicate() { final AtomicInteger atomicInteger = new AtomicInteger(3); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Maybe.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 fromCallableTwice() { final AtomicInteger atomicInteger = new AtomicInteger(); Callable<Object> callable = new Callable<Object>() { @Override public Object call() throws Exception { atomicInteger.incrementAndGet(); return null; } }; Maybe.fromCallable(callable) .test() .assertResult(); assertEquals(1, atomicInteger.get()); Maybe.fromCallable(callable) .test() .assertResult(); assertEquals(2, atomicInteger.get()); }
@Test public void fromCallableInvokesLazy() { final AtomicInteger atomicInteger = new AtomicInteger(); Maybe<Object> completable = Maybe.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { atomicInteger.incrementAndGet(); return null; } }); assertEquals(0, atomicInteger.get()); completable .test() .assertResult(); assertEquals(1, atomicInteger.get()); }
@SuppressWarnings("unused") public Maybe<StreamStateEntity> loadStreamState(final StreamInfo info) { return Maybe.fromCallable(() -> streamTable.upsert(new StreamEntity(info))) .flatMap(streamId -> streamStateTable.getState(streamId).firstElement()) .flatMap(states -> states.isEmpty() ? Maybe.empty() : Maybe.just(states.get(0))) .subscribeOn(Schedulers.io()); }
Maybe<String> fromCallableObservable = Maybe.fromCallable(func);
@Test public void retryTimesPredicateWithMatchingRetryAmount() { final AtomicInteger atomicInteger = new AtomicInteger(3); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Maybe.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 retryTimesPredicateWithZeroRetries() { final AtomicInteger atomicInteger = new AtomicInteger(2); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Maybe.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 retryTimesPredicateWithNotMatchingRetryAmount() { final AtomicInteger atomicInteger = new AtomicInteger(3); final AtomicInteger numberOfSubscribeCalls = new AtomicInteger(0); Maybe.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()); }
for (int i = 0; i < 3; i++) { final int j = i + 1; sources[i] = Maybe.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { .subscribeOn(Schedulers.io()); sources[1] = Maybe.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception {
@Test public void noErrorLoss() throws Exception { List<Throwable> errors = TestHelper.trackPluginErrors(); try { final CountDownLatch cdl1 = new CountDownLatch(1); final CountDownLatch cdl2 = new CountDownLatch(1); TestObserver<Integer> to = Maybe.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { cdl1.countDown(); cdl2.await(5, TimeUnit.SECONDS); return 1; } }).subscribeOn(Schedulers.single()).test(); assertTrue(cdl1.await(5, TimeUnit.SECONDS)); to.cancel(); int timeout = 10; while (timeout-- > 0 && errors.isEmpty()) { Thread.sleep(100); } TestHelper.assertUndeliverable(errors, 0, InterruptedException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void subscribeOnSuccess() { String main = Thread.currentThread().getName(); Maybe.fromCallable(new Callable<String>() { @Override public String call() throws Exception { return Thread.currentThread().getName(); } }) .subscribeOn(Schedulers.single()) .test() .awaitDone(5, TimeUnit.SECONDS) .assertOf(TestHelper.observerSingleNot(main)) ; }
@Test public void delayErrorWithMaxConcurrencyAsync() { final AtomicInteger count = new AtomicInteger(); @SuppressWarnings("unchecked") Maybe<Integer>[] sources = new Maybe[3]; for (int i = 0; i < 3; i++) { final int j = i + 1; sources[i] = Maybe.fromCallable(new Callable<Integer>() { @Override public Integer call() throws Exception { return count.incrementAndGet() - j; } }) .subscribeOn(Schedulers.io()); } for (int i = 0; i < 1000; i++) { count.set(0); Maybe.mergeDelayError( Flowable.fromArray(sources), 1) .test() .awaitDone(5, TimeUnit.SECONDS) .assertResult(0, 0, 0); } }