@Test(expected = NullPointerException.class) public void timerUnitNull() { Single.timer(1, null); }
@Test(expected = NullPointerException.class) public void timerSchedulerNull() { Single.timer(1, TimeUnit.SECONDS, null); }
/** * Signals success with 0L value after the given delay for each SingleObserver. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code timer} operates by default on the {@code computation} {@link Scheduler}.</dd> * </dl> * @param delay the delay amount * @param unit the time unit of the delay * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Single<Long> timer(long delay, TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); }
/** * Signals success with 0L value after the given delay for each SingleObserver. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code timer} operates by default on the {@code computation} {@link Scheduler}.</dd> * </dl> * @param delay the delay amount * @param unit the time unit of the delay * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public static Single<Long> timer(long delay, TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); }
@Test public void disposed() { TestHelper.checkDisposed(Single.timer(1, TimeUnit.SECONDS, new TestScheduler())); }
@Test public void timer() throws Exception { Single.timer(100, TimeUnit.MILLISECONDS) .test() .awaitDone(5, TimeUnit.SECONDS) .assertResult(0L); }
@Test public void timer() { final TestScheduler testScheduler = new TestScheduler(); final AtomicLong atomicLong = new AtomicLong(); Single.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Consumer<Long>() { @Override public void accept(final Long value) 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<Long> ts = Single.timer(1, TimeUnit.MILLISECONDS, s) .map(new Function<Long, Long>() { @Override public Long apply(Long v) throws Exception { try { Thread.sleep(3000); } catch (InterruptedException ex) { interrupted.set(true); } return v; } }) .test(); Thread.sleep(500); ts.cancel(); Thread.sleep(500); assertTrue(s.getClass().getSimpleName(), interrupted.get()); } } finally { exec.shutdown(); } }
@Test public void delaySubscriptionSingle() throws Exception { Single.just(1).delaySubscription(Single.timer(100, TimeUnit.MILLISECONDS)) .test() .awaitDone(5, TimeUnit.SECONDS) .assertResult(1); }