@Test(expected = NullPointerException.class) public void delaySchedulerNull() { just1.delay(1, TimeUnit.SECONDS, null); }
@Test(expected = NullPointerException.class) public void delayUnitNull() { just1.delay(1, null); }
/** * Delays the emission of the success signal from the current Single by the specified amount. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code delay} operates by default on the {@code computation} {@link Scheduler}.</dd> * </dl> * * @param time the time amount to delay the emission of the success signal * @param unit the time unit * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single<T> delay(long time, TimeUnit unit) { return delay(time, unit, Schedulers.computation()); }
/** * Delays the emission of the success or error signal from the current Single by * the specified amount. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code delay} operates by default on the {@code computation} {@link Scheduler}.</dd> * </dl> * * @param time the time amount to delay the signals * @param unit the time unit * @return the new Single instance * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Single<T> delay(long time, TimeUnit unit) { return delay(time, unit, Schedulers.computation()); }
@Override public Integer apply(Integer v) throws Exception { Single.just(1).delay(10, TimeUnit.SECONDS).blockingGet(); return v; } })
@Test public void getAwait() throws Exception { Future<Integer> f = Single.just(1).delay(100, TimeUnit.MILLISECONDS).toFuture(); assertEquals(1, f.get(5, TimeUnit.SECONDS).intValue()); }
@Test public void delay() throws Exception { final AtomicInteger value = new AtomicInteger(); Single.just(1).delay(200, TimeUnit.MILLISECONDS) .subscribe(new BiConsumer<Integer, Throwable>() { @Override public void accept(Integer v, Throwable e) throws Exception { value.set(v); } }); Thread.sleep(100); assertEquals(0, value.get()); Thread.sleep(200); assertEquals(1, value.get()); }
@Test public void testOnErrorCalledOnScheduler() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Thread> thread = new AtomicReference<Thread>(); Single.<String>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(); } }) .onErrorResumeNext(Single.just("")) .subscribe(); latch.await(); assertNotEquals(Thread.currentThread(), thread.get()); }
@Test public void delayError() { Single.error(new TestException()).delay(5, TimeUnit.SECONDS) .test() .awaitDone(1, TimeUnit.SECONDS) .assertFailure(TestException.class); }
@Test public void andThenSingleSubscribeOn() { TestObserver<String> ts = new TestObserver<String>(); TestScheduler scheduler = new TestScheduler(); Completable.complete().andThen(Single.just("foo").delay(1, TimeUnit.SECONDS, scheduler)) .subscribe(ts); ts.assertNoValues(); ts.assertNotTerminated(); scheduler.advanceTimeBy(1, TimeUnit.SECONDS); ts.assertValue("foo"); ts.assertComplete(); ts.assertNoErrors(); }