@Test(expected = NullPointerException.class) public void fromActionNull() { Completable.fromAction(null); }
@Test(expected = NullPointerException.class) public void fromActionNull() { Completable.fromAction(null); }
/** * Returns a Completable instance that reacts to the termination of the given Future in a blocking fashion. * <p> * Note that cancellation from any of the subscribers to this Completable will cancel the future. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code fromFuture} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * @param future the future to react to * @return the new Completable instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Completable fromFuture(final Future<?> future) { ObjectHelper.requireNonNull(future, "future is null"); return fromAction(Functions.futureAction(future)); }
@Test(timeout = 5000) public void retryPredicate5Times() { final AtomicInteger calls = new AtomicInteger(5); Completable c = Completable.fromAction(new Action() { @Override public void run() { if (calls.decrementAndGet() != 0) { throw new TestException(); } } }).retry(new Predicate<Throwable>() { @Override public boolean test(Throwable e) { return true; } }); c.blockingAwait(); }
/** * Returns a Completable instance that reacts to the termination of the given Future in a blocking fashion. * <p> * Note that cancellation from any of the subscribers to this Completable will cancel the future. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code fromFuture} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * @param future the future to react to * @return the new Completable instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public static Completable fromFuture(final Future<?> future) { ObjectHelper.requireNonNull(future, "future is null"); return fromAction(Functions.futureAction(future)); }
@Test(timeout = 5000, expected = TestException.class) public void fromActionThrows() { Completable c = Completable.fromAction(new Action() { @Override public void run() { throw new TestException(); } }); c.blockingAwait(); }
@Test(timeout = 5000) public void fromActionNormal() { final AtomicInteger calls = new AtomicInteger(); Completable c = Completable.fromAction(new Action() { @Override public void run() { calls.getAndIncrement(); } }); c.blockingAwait(); Assert.assertEquals(1, calls.get()); }
@Test(timeout = 5000) public void retryWhen5Times() { final AtomicInteger calls = new AtomicInteger(5); Completable c = Completable.fromAction(new Action() { @Override public void run() { if (calls.decrementAndGet() != 0) { throw new TestException(); } } }).retryWhen(new Function<Observable<? extends Throwable>, Observable<Object>>() { @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Observable<Object> apply(Observable<? extends Throwable> o) { return (Observable)o; } }); c.blockingAwait(); }
@Test(timeout = 5000) public void retry5Times() { final AtomicInteger calls = new AtomicInteger(5); Completable c = Completable.fromAction(new Action() { @Override public void run() { if (calls.decrementAndGet() != 0) { throw new TestException(); } } }).retry(); c.blockingAwait(); }
@Test(timeout = 5000) public void retryTimes5Normal() { final AtomicInteger calls = new AtomicInteger(5); Completable c = Completable.fromAction(new Action() { @Override public void run() { if (calls.decrementAndGet() != 0) { throw new TestException(); } } }).retry(5); c.blockingAwait(); }
@Test public void fromAction() { final AtomicInteger atomicInteger = new AtomicInteger(); Completable.fromAction(new Action() { @Override public void run() throws Exception { atomicInteger.incrementAndGet(); } }) .test() .assertResult(); assertEquals(1, atomicInteger.get()); }
@Test public void fromActionThrows() { Completable.fromAction(new Action() { @Override public void run() throws Exception { throw new UnsupportedOperationException(); } }) .test() .assertFailure(UnsupportedOperationException.class); } }
@Test public void fromActionInvokesLazy() { final AtomicInteger atomicInteger = new AtomicInteger(); Completable completable = Completable.fromAction(new Action() { @Override public void run() throws Exception { atomicInteger.incrementAndGet(); } }); assertEquals(0, atomicInteger.get()); completable .test() .assertResult(); assertEquals(1, atomicInteger.get()); }
@Test public void fromActionTwice() { final AtomicInteger atomicInteger = new AtomicInteger(); Action run = new Action() { @Override public void run() throws Exception { atomicInteger.incrementAndGet(); } }; Completable.fromAction(run) .test() .assertResult(); assertEquals(1, atomicInteger.get()); Completable.fromAction(run) .test() .assertResult(); assertEquals(2, atomicInteger.get()); }
@Test public void whenCounted() { final int[] counter = { 0 }; Completable.fromAction(new Action() { @Override public void run() throws Exception { counter[0]++; } }) .repeatWhen(new Function<Observable<Object>, Observable<Object>>() { @Override public Observable<Object> apply(Observable<Object> f) throws Exception { final int[] j = { 3 }; return f.takeWhile(new Predicate<Object>() { @Override public boolean test(Object v) throws Exception { return j[0]-- != 0; } }); } }) .subscribe(); assertEquals(4, counter[0]); } }
@Test public void timeoutContinueOther() throws Exception { final int[] call = { 0 }; Completable other = Completable.fromAction(new Action() { @Override public void run() throws Exception { call[0]++; } }); Completable.never() .timeout(100, TimeUnit.MILLISECONDS, Schedulers.io(), other) .test() .awaitDone(5, TimeUnit.SECONDS) .assertResult(); assertEquals(1, call[0]); }