Search in sources :

Example 11 with CountingRunnable

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

the class HandlerSchedulerTest method workerUnsubscriptionDoesNotAffectOtherWorkers.

@Test
public void workerUnsubscriptionDoesNotAffectOtherWorkers() {
    Worker workerA = scheduler.createWorker();
    CountingRunnable counterA = new CountingRunnable();
    workerA.schedule(counterA, 1, MINUTES);
    Worker workerB = scheduler.createWorker();
    CountingRunnable counterB = new CountingRunnable();
    workerB.schedule(counterB, 1, MINUTES);
    workerA.dispose();
    runUiThreadTasksIncludingDelayedTasks();
    assertEquals(0, counterA.get());
    assertEquals(1, counterB.get());
}
Also used : CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Worker(io.reactivex.Scheduler.Worker) Test(org.junit.Test)

Example 12 with CountingRunnable

use of io.reactivex.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.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.android.testutil.CountingRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with CountingRunnable

use of io.reactivex.android.testutil.CountingRunnable 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.android.testutil.CountingRunnable) CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Worker(io.reactivex.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 14 with CountingRunnable

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

the class HandlerSchedulerTest method workerDisposeCancelsScheduled.

@Test
public void workerDisposeCancelsScheduled() {
    Worker worker = scheduler.createWorker();
    CountingRunnable counter = new CountingRunnable();
    worker.schedule(counter, 1, MINUTES);
    worker.dispose();
    runUiThreadTasks();
    assertEquals(0, counter.get());
}
Also used : CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Worker(io.reactivex.Scheduler.Worker) Test(org.junit.Test)

Example 15 with CountingRunnable

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

the class HandlerSchedulerTest method directScheduleOnceWithNegativeDelayPostsImmediately.

@Test
public void directScheduleOnceWithNegativeDelayPostsImmediately() {
    CountingRunnable counter = new CountingRunnable();
    scheduler.scheduleDirect(counter, -1, TimeUnit.MINUTES);
    runUiThreadTasks();
    assertEquals(1, counter.get());
}
Also used : CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Test(org.junit.Test)

Aggregations

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