@Test public void testOnErrorCalledOnScheduler() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Thread> thread = new AtomicReference<Thread>(); Completable.error(new Exception()) .delay(0, TimeUnit.MILLISECONDS, Schedulers.newThread()) .doOnError(new Consumer<Throwable>() { @Override public void accept(Throwable throwable) throws Exception { thread.set(Thread.currentThread()); latch.countDown(); } }) .onErrorComplete() .subscribe(); latch.await(); assertNotEquals(Thread.currentThread(), thread.get()); }
/** * Returns a Completable that repeatedly subscribes to this Completable until cancelled. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code repeat} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * @return the new Completable instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Completable repeat() { return fromObservable(toObservable().repeat()); }
@Override public void run() throws Exception { Completable.complete().delay(10, TimeUnit.SECONDS).blockingGet(); } })
@Override public void run() throws Exception { Completable.complete().delay(10, TimeUnit.SECONDS).blockingAwait(); } })
@Override public void run() { c.subscribe(ts1); } };
@Test(timeout = 5000) public void subscribeDispose() throws InterruptedException { final AtomicBoolean complete = new AtomicBoolean(); Completable c = normal.completable .delay(200, TimeUnit.MILLISECONDS) .doOnComplete(new Action() { @Override public void run() { complete.set(true); } }); Disposable d = c.subscribe(); Thread.sleep(100); d.dispose(); Thread.sleep(150); Assert.assertFalse("Completed", complete.get()); }
@Test public void arrayFirstCancels() { final TestObserver<Void> to = new TestObserver<Void>(); Completable.concatArray(new Completable() { @Override protected void subscribeActual(CompletableObserver s) { s.onSubscribe(Disposables.empty()); to.cancel(); s.onComplete(); } }, Completable.complete()) .subscribe(to); to.assertEmpty(); }
@Override public Observable<Object> apply(Completable c) { return c.toObservable(); } });
@Test(timeout = 5000) public void ambArrayOneFires() { CompletableSubject ps1 = CompletableSubject.create(); CompletableSubject ps2 = CompletableSubject.create(); Completable c = Completable.ambArray(ps1, ps2); final AtomicBoolean complete = new AtomicBoolean(); c.subscribe(new Action() { @Override public void run() { complete.set(true); } }); Assert.assertTrue("First subject no subscribers", ps1.hasObservers()); Assert.assertTrue("Second subject no subscribers", ps2.hasObservers()); ps1.onComplete(); Assert.assertFalse("First subject has subscribers", ps1.hasObservers()); Assert.assertFalse("Second subject has subscribers", ps2.hasObservers()); Assert.assertTrue("Not completed", complete.get()); }
@Test(expected = TestException.class) public void concatIterableIterableThrows() { Completable c = Completable.concat(new Iterable<Completable>() { @Override public Iterator<Completable> iterator() { throw new TestException(); } }); c.blockingAwait(); }
@Test(timeout = 5000, expected = TestException.class) public void concatSingleSourceThrows() { Completable c = Completable.concatArray(error.completable); c.blockingAwait(); }
@Test public void arrayCancelled() { Completable.concatArray(Completable.complete(), Completable.complete()) .test(true) .assertEmpty(); }
@Test public void completeAfterMain() { CompletableSubject pp = CompletableSubject.create(); TestObserver<Void> to = Completable.mergeArray(Completable.complete(), pp) .test(); pp.onComplete(); to.assertResult(); }
@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]); }
/** * Returns a Completable which delays the emission of the completion event by the given time while * running on the specified scheduler. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code delay} operates on the {@link Scheduler} you specify.</dd> * </dl> * @param delay the delay time * @param unit the delay unit * @param scheduler the scheduler to run the delayed completion on * @return the new Completable instance * @throws NullPointerException if unit or scheduler is null */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) public final Completable delay(long delay, TimeUnit unit, Scheduler scheduler) { return delay(delay, unit, scheduler, false); }
@Test(timeout = 5000, expected = TestException.class) public void fromObservableError() { Completable c = Completable.fromObservable(Observable.error(new Callable<Throwable>() { @Override public Throwable call() { return new TestException(); } })); c.blockingAwait(); }