@Override public Completable requestConnectionPriority(int connectionPriority, long delay, @NonNull TimeUnit timeUnit) { return Completable.timer(delay, timeUnit); }
@Test(expected = NullPointerException.class) public void timerUnitNull() { Completable.timer(1, null); }
@Test(expected = NullPointerException.class) public void timerSchedulerNull() { Completable.timer(1, TimeUnit.SECONDS, null); }
/** * Returns a Completable instance that fires its onComplete event after the given delay elapsed. * <p> * <img width="640" height="413" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.timer.png" alt=""> * <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()); }
private void doSomeWork() { Completable completable = Completable.timer(1000, TimeUnit.MILLISECONDS); completable .subscribeOn(Schedulers.io()) // Be notified on the main thread .observeOn(AndroidSchedulers.mainThread()) .subscribe(getCompletableObserver()); }
/** * 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(); }
/** * Returns a Completable that delays the subscription to the source CompletableSource by a given amount of time, * both waiting and subscribing on a given Scheduler. * <p> * <img width="640" height="420" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Completable.delaySubscription.ts.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>You specify which {@link Scheduler} this operator will use.</dd> * </dl> * * @param delay the time to delay the subscription * @param unit the time unit of {@code delay} * @param scheduler the Scheduler on which the waiting and subscription will happen * @return a Completable that delays the subscription to the source CompletableSource by a given * amount, waiting and subscribing on the given Scheduler * @since 2.2.3 - experimental * @see <a href="http://reactivex.io/documentation/operators/delay.html">ReactiveX operators documentation: Delay</a> */ @CheckReturnValue @Experimental @SchedulerSupport(SchedulerSupport.CUSTOM) public final Completable delaySubscription(long delay, TimeUnit unit, Scheduler scheduler) { return Completable.timer(delay, unit, scheduler).andThen(this); }
@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())); }
@Override protected void doSomething() { mRxOperatorsText.append("Completable\n"); Log.e(TAG, "Completable\n"); Completable.timer(1, TimeUnit.SECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new CompletableObserver() { @Override public void onSubscribe(@NonNull Disposable d) { mRxOperatorsText.append("onSubscribe : d :" + d.isDisposed() + "\n"); Log.e(TAG, "onSubscribe : d :" + d.isDisposed() + "\n"); } @Override public void onComplete() { mRxOperatorsText.append("onComplete\n"); Log.e(TAG, "onComplete\n"); } @Override public void onError(@NonNull Throwable e) { mRxOperatorsText.append("onError :" + e.getMessage() + "\n"); Log.e(TAG, "onError :" + e.getMessage() + "\n"); } }); } }
@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 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> to = 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); to.cancel(); Thread.sleep(500); assertTrue(s.getClass().getSimpleName(), interrupted.get()); } } finally { exec.shutdown(); } }
@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(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) { RxJavaPlugins.onError(e); } }); scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS); Assert.assertEquals(0, calls.get()); scheduler.advanceTimeBy(200, TimeUnit.MILLISECONDS); Assert.assertEquals(1, calls.get()); }
public Completable start () { return Completable.create(e -> { Completable timer = Completable.timer(200, TimeUnit.MILLISECONDS); delayStartDisposable = timer.subscribe(() -> { startRecording(); e.onComplete(); }); }); }
public void stop () { if(delayStartDisposable != null) { delayStartDisposable.dispose(); } else { if(AudioRecorder.shared().duration() < 1000) { // Wait and then stop the recording Completable.timer(200, TimeUnit.MILLISECONDS).subscribe(() -> AudioRecorder.shared().stopRecording()); } else { AudioRecorder.shared().stopRecording(); durationMillis = AudioRecorder.shared().stopRecording(); } } }
public static void doSome() { Completable completable = Completable.timer(1000, TimeUnit.MILLISECONDS); completable .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new CompletableObserver() { @Override public void onSubscribe(Disposable d) { Log.d(TAG, " onSubscribe : " + d.isDisposed()); } @Override public void onComplete() { Log.i(TAG, " onComplete"); } @Override public void onError(Throwable e) { Log.e(TAG, " onError : " + e.getMessage()); } }); }
private void testCompletable() { Completable.timer(3, TimeUnit.SECONDS) .subscribeOn(Schedulers.computation()) .observeOn(AndroidSchedulers.mainThread()) .compose(RxLifecycle.bind(this).disposeCompletableWhen(LifecycleEvent.DESTROY_VIEW)) .subscribe(new Action() { @Override public void run() throws Exception { toast("Completable -> onComplete()"); } }); }
@Test public void test3() { Completable.timer(1, TimeUnit.SECONDS) .doOnSubscribe(s -> System.out.println("subscribing")) .doOnComplete(() -> { throw new RuntimeException(); }) .retryWhen(errors -> { AtomicInteger counter = new AtomicInteger(); return errors .takeWhile(e -> counter.getAndIncrement() != 3) .flatMap(e -> { System.out.println("delay retry by " + counter.get() + " second(s)"); return Flowable.timer(counter.get(), TimeUnit.SECONDS); }); }) .blockingAwait(); } }