Search in sources :

Example 1 with Schedulers

use of io.reactivex.rxjava3.schedulers.Schedulers in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method directSchedulePeriodicallyUsesHookOnce.

@Test
@Ignore("Implementation delegated to default RxJava implementation")
public void directSchedulePeriodicallyUsesHookOnce() {
    final CountingRunnable newCounter = new CountingRunnable();
    final AtomicReference<Runnable> runnableRef = new AtomicReference<>();
    RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {

        @Override
        public Runnable apply(Runnable runnable) {
            runnableRef.set(runnable);
            return newCounter;
        }
    });
    CountingRunnable counter = new CountingRunnable();
    scheduler.schedulePeriodicallyDirect(counter, 1, 1, MINUTES);
    // Verify our action was passed to the schedulers hook.
    assertSame(counter, runnableRef.get());
    runnableRef.set(null);
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    // Verify the scheduled action was the one returned from the hook.
    assertEquals(1, newCounter.get());
    assertEquals(0, counter.get());
    // Ensure the hook was not called again when the runnable re-scheduled itself.
    assertNull(runnableRef.get());
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with Schedulers

use of io.reactivex.rxjava3.schedulers.Schedulers in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method directScheduleOnceUsesHook.

@Test
public void directScheduleOnceUsesHook() {
    final CountingRunnable newCounter = new CountingRunnable();
    final AtomicReference<Runnable> runnableRef = new AtomicReference<>();
    RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {

        @Override
        public Runnable apply(Runnable runnable) {
            runnableRef.set(runnable);
            return newCounter;
        }
    });
    CountingRunnable counter = new CountingRunnable();
    scheduler.scheduleDirect(counter);
    // Verify our runnable was passed to the schedulers hook.
    assertSame(counter, runnableRef.get());
    runUiThreadTasks();
    // Verify the scheduled runnable was the one returned from the hook.
    assertEquals(1, newCounter.get());
    assertEquals(0, counter.get());
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 3 with Schedulers

use of io.reactivex.rxjava3.schedulers.Schedulers in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method workerScheduleOnceWithDelayUsesHook.

@Test
public void workerScheduleOnceWithDelayUsesHook() {
    final CountingRunnable newCounter = new CountingRunnable();
    final AtomicReference<Runnable> runnableRef = new AtomicReference<>();
    RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {

        @Override
        public Runnable apply(Runnable runnable) {
            runnableRef.set(runnable);
            return newCounter;
        }
    });
    Worker worker = scheduler.createWorker();
    CountingRunnable counter = new CountingRunnable();
    worker.schedule(counter, 1, MINUTES);
    // Verify our runnable was passed to the schedulers hook.
    assertSame(counter, runnableRef.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    // Verify the scheduled runnable was the one returned from the hook.
    assertEquals(1, newCounter.get());
    assertEquals(0, counter.get());
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 4 with Schedulers

use of io.reactivex.rxjava3.schedulers.Schedulers in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method workerScheduleOnceUsesHook.

@Test
public void workerScheduleOnceUsesHook() {
    final CountingRunnable newCounter = new CountingRunnable();
    final AtomicReference<Runnable> runnableRef = new AtomicReference<>();
    RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {

        @Override
        public Runnable apply(Runnable runnable) {
            runnableRef.set(runnable);
            return newCounter;
        }
    });
    Worker worker = scheduler.createWorker();
    CountingRunnable counter = new CountingRunnable();
    worker.schedule(counter);
    // Verify our runnable was passed to the schedulers hook.
    assertSame(counter, runnableRef.get());
    runUiThreadTasks();
    // Verify the scheduled runnable was the one returned from the hook.
    assertEquals(1, newCounter.get());
    assertEquals(0, counter.get());
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 5 with Schedulers

use of io.reactivex.rxjava3.schedulers.Schedulers in project RxJava by ReactiveX.

the class RxJavaPluginsTest method overrideInitIoScheduler.

@Test
public void overrideInitIoScheduler() {
    // make sure the Schedulers is initialized;
    final Scheduler s = Schedulers.io();
    Supplier<Scheduler> c = new Supplier<Scheduler>() {

        @Override
        public Scheduler get() throws Exception {
            return s;
        }
    };
    try {
        RxJavaPlugins.setInitIoSchedulerHandler(initReplaceWithImmediate);
        assertSame(ImmediateThinScheduler.INSTANCE, RxJavaPlugins.initIoScheduler(c));
    } finally {
        RxJavaPlugins.reset();
    }
    // make sure the reset worked
    assertSame(s, RxJavaPlugins.initIoScheduler(c));
}
Also used : ImmediateThinScheduler(io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)4 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)3 Ignore (org.junit.Ignore)2