Search in sources :

Example 6 with Schedulers

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

the class RxJavaPluginsTest method overrideInitSingleScheduler.

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

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

Example 7 with Schedulers

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

the class RxJavaPluginsTest method overrideInitComputationScheduler.

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

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

Example 8 with Schedulers

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

the class RxJavaPluginsTest method overrideInitNewThreadScheduler.

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

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

Example 9 with Schedulers

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

the class HandlerSchedulerTest method directScheduleOnceWithDelayUsesHook.

@Test
public void directScheduleOnceWithDelayUsesHook() {
    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, 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) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 10 with Schedulers

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

the class HandlerSchedulerTest method workerSchedulePeriodicallyUsesHookOnce.

@Test
@Ignore("Implementation delegated to default RxJava implementation")
public void workerSchedulePeriodicallyUsesHookOnce() {
    Worker worker = scheduler.createWorker();
    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();
    worker.schedulePeriodically(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) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignore(org.junit.Ignore) 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