Search in sources :

Example 6 with Function

use of io.reactivex.rxjava3.functions.Function in project RxAndroid by ReactiveX.

the class RxAndroidPluginsTest method initMainThreadHandlerCalled.

@Test
public void initMainThreadHandlerCalled() {
    final AtomicReference<Callable<Scheduler>> schedulerRef = new AtomicReference<>();
    final Scheduler newScheduler = new EmptyScheduler();
    RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() {

        @Override
        public Scheduler apply(Callable<Scheduler> scheduler) {
            schedulerRef.set(scheduler);
            return newScheduler;
        }
    });
    Callable<Scheduler> scheduler = new Callable<Scheduler>() {

        @Override
        public Scheduler call() throws Exception {
            throw new AssertionError();
        }
    };
    Scheduler actual = RxAndroidPlugins.initMainThreadScheduler(scheduler);
    assertSame(newScheduler, actual);
    assertSame(scheduler, schedulerRef.get());
}
Also used : EmptyScheduler(io.reactivex.rxjava3.android.testutil.EmptyScheduler) Scheduler(io.reactivex.rxjava3.core.Scheduler) EmptyScheduler(io.reactivex.rxjava3.android.testutil.EmptyScheduler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 7 with Function

use of io.reactivex.rxjava3.functions.Function in project RxAndroid by ReactiveX.

the class RxAndroidPluginsTest method resetClearsInitMainThreadHandler.

@Test
public void resetClearsInitMainThreadHandler() throws Exception {
    RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() {

        @Override
        public Scheduler apply(Callable<Scheduler> scheduler) {
            throw new AssertionError();
        }
    });
    final Scheduler scheduler = new EmptyScheduler();
    Callable<Scheduler> schedulerCallable = new Callable<Scheduler>() {

        @Override
        public Scheduler call() throws Exception {
            return scheduler;
        }
    };
    RxAndroidPlugins.reset();
    Scheduler actual = RxAndroidPlugins.initMainThreadScheduler(schedulerCallable);
    assertSame(schedulerCallable.call(), actual);
}
Also used : EmptyScheduler(io.reactivex.rxjava3.android.testutil.EmptyScheduler) Scheduler(io.reactivex.rxjava3.core.Scheduler) EmptyScheduler(io.reactivex.rxjava3.android.testutil.EmptyScheduler) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Example 8 with Function

use of io.reactivex.rxjava3.functions.Function 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 9 with Function

use of io.reactivex.rxjava3.functions.Function 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 10 with Function

use of io.reactivex.rxjava3.functions.Function 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)

Aggregations

Test (org.junit.Test)97 TestException (io.reactivex.rxjava3.exceptions.TestException)78 Observable (io.reactivex.rxjava3.core.Observable)41 InOrder (org.mockito.InOrder)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)21 Function (io.reactivex.rxjava3.functions.Function)20 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 IOException (java.io.IOException)13 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)9 TestObserver (io.reactivex.rxjava3.observers.TestObserver)9 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)9 GroupedFlowable (io.reactivex.rxjava3.flowables.GroupedFlowable)8 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)7 Observer (io.reactivex.rxjava3.core.Observer)7 Function (org.apache.cassandra.cql3.functions.Function)7 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)6 TestHelper (io.reactivex.rxjava3.testsupport.TestHelper)6 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)5