Search in sources :

Example 1 with CountingRunnable

use of io.reactivex.rxjava3.android.testutil.CountingRunnable in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method disposedWorkerReturnsDisposedDisposables.

@Test
public void disposedWorkerReturnsDisposedDisposables() {
    Worker worker = scheduler.createWorker();
    worker.dispose();
    Disposable disposable = worker.schedule(new CountingRunnable());
    assertTrue(disposable.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Example 2 with CountingRunnable

use of io.reactivex.rxjava3.android.testutil.CountingRunnable in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method directSchedulePeriodicallyDisposedDuringRunDoesNotReschedule.

@Test
@Ignore("Implementation delegated to default RxJava implementation")
public void directSchedulePeriodicallyDisposedDuringRunDoesNotReschedule() {
    final AtomicReference<Disposable> disposableRef = new AtomicReference<>();
    CountingRunnable counter = new CountingRunnable() {

        @Override
        public void run() {
            super.run();
            if (get() == 2) {
                disposableRef.get().dispose();
            }
        }
    };
    Disposable disposable = scheduler.schedulePeriodicallyDirect(counter, 1, 1, MINUTES);
    disposableRef.set(disposable);
    runUiThreadTasks();
    assertEquals(0, counter.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(1, counter.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(2, counter.get());
    // Dispose will have happened here during the last run() execution.
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(2, counter.get());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with CountingRunnable

use of io.reactivex.rxjava3.android.testutil.CountingRunnable in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method directScheduleOnceWithDelayDisposedDoesNotRun.

@Test
public void directScheduleOnceWithDelayDisposedDoesNotRun() {
    CountingRunnable counter = new CountingRunnable();
    Disposable disposable = scheduler.scheduleDirect(counter, 1, MINUTES);
    idleMainLooper(30, SECONDS);
    disposable.dispose();
    idleMainLooper(30, SECONDS);
    runUiThreadTasks();
    assertEquals(0, counter.get());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Test(org.junit.Test)

Example 4 with CountingRunnable

use of io.reactivex.rxjava3.android.testutil.CountingRunnable 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 5 with CountingRunnable

use of io.reactivex.rxjava3.android.testutil.CountingRunnable in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method directScheduleOnceInputValidation.

@Test
public void directScheduleOnceInputValidation() {
    try {
        scheduler.scheduleDirect(null);
        fail();
    } catch (NullPointerException e) {
        assertEquals("run == null", e.getMessage());
    }
    try {
        scheduler.scheduleDirect(null, 1, MINUTES);
        fail();
    } catch (NullPointerException e) {
        assertEquals("run == null", e.getMessage());
    }
    try {
        scheduler.scheduleDirect(new CountingRunnable(), 1, null);
        fail();
    } catch (NullPointerException e) {
        assertEquals("unit == null", e.getMessage());
    }
}
Also used : CountingRunnable(io.reactivex.rxjava3.android.testutil.CountingRunnable) Test(org.junit.Test)

Aggregations

CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)33 Test (org.junit.Test)33 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)19 Ignore (org.junit.Ignore)12 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9