@Test(expected = NullPointerException.class) public void timerSchedulerNull() { Completable.timer(1, TimeUnit.SECONDS, null); }
@Test(expected = NullPointerException.class) public void timerUnitNull() { Completable.timer(1, null); }
/** * Returns a Completable instance that fires its onComplete event after the given delay elapsed. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code timer} does operate by default on the {@code computation} {@link Scheduler}.</dd> * </dl> * @param delay the delay time * @param unit the delay unit * @return the new Completable instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Completable timer(long delay, TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); }
/** * Returns a Completable instance that fires its onComplete event after the given delay elapsed. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code timer} does operate by default on the {@code computation} {@link Scheduler}.</dd> * </dl> * @param delay the delay time * @param unit the delay unit * @return the new Completable instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Completable timer(long delay, TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); }
@Test(timeout = 1500) public void timer() { Completable c = Completable.timer(500, TimeUnit.MILLISECONDS); c.blockingAwait(); }
@Test(timeout = 1500) public void timerNewThread() { Completable c = Completable.timer(500, TimeUnit.MILLISECONDS, Schedulers.newThread()); c.blockingAwait(); }
@Test public void dispose() { TestHelper.checkDisposed(Completable.timer(1, TimeUnit.SECONDS, new TestScheduler())); }
@Test(timeout = 2000) public void timerCancel() throws InterruptedException { Completable c = Completable.timer(250, TimeUnit.MILLISECONDS); final SequentialDisposable sd = new SequentialDisposable(); final AtomicInteger calls = new AtomicInteger(); c.subscribe(new CompletableObserver() { @Override public void onSubscribe(Disposable d) { sd.replace(d); } @Override public void onError(Throwable e) { calls.getAndIncrement(); } @Override public void onComplete() { calls.getAndIncrement(); } }); Thread.sleep(100); sd.dispose(); Thread.sleep(200); Assert.assertEquals(0, calls.get()); }
@Test public void timer() { final TestScheduler testScheduler = new TestScheduler(); final AtomicLong atomicLong = new AtomicLong(); Completable.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Action() { @Override public void run() throws Exception { atomicLong.incrementAndGet(); } }); assertEquals(0, atomicLong.get()); testScheduler.advanceTimeBy(1, TimeUnit.SECONDS); assertEquals(0, atomicLong.get()); testScheduler.advanceTimeBy(1, TimeUnit.SECONDS); assertEquals(1, atomicLong.get()); } }
@Test public void timerInterruptible() throws Exception { ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); try { for (Scheduler s : new Scheduler[] { Schedulers.single(), Schedulers.computation(), Schedulers.newThread(), Schedulers.io(), Schedulers.from(exec) }) { final AtomicBoolean interrupted = new AtomicBoolean(); TestObserver<Void> ts = Completable.timer(1, TimeUnit.MILLISECONDS, s) .doOnComplete(new Action() { @Override public void run() throws Exception { try { Thread.sleep(3000); } catch (InterruptedException ex) { interrupted.set(true); } } }) .test(); Thread.sleep(500); ts.cancel(); Thread.sleep(500); assertTrue(s.getClass().getSimpleName(), interrupted.get()); } } finally { exec.shutdown(); } }
@Test(timeout = 5000) public void timerTestScheduler() { TestScheduler scheduler = new TestScheduler(); Completable c = Completable.timer(250, TimeUnit.MILLISECONDS, scheduler); final AtomicInteger calls = new AtomicInteger(); c.subscribe(new CompletableObserver() { @Override public void onSubscribe(Disposable d) { } @Override public void onComplete() { calls.getAndIncrement(); } @Override public void onError(Throwable e) { RxJavaCommonPlugins.onError(e); } }); scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); Assert.assertEquals(0, calls.get()); scheduler.advanceTimeBy(200, TimeUnit.MILLISECONDS); Assert.assertEquals(1, calls.get()); }