Search in sources :

Example 1 with Worker

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

Example 2 with Worker

use of io.reactivex.Scheduler.Worker in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method workerSchedulePeriodicallyDisposedDoesNotRun.

@Test
@Ignore("Implementation delegated to default RxJava implementation")
public void workerSchedulePeriodicallyDisposedDoesNotRun() {
    Worker worker = scheduler.createWorker();
    CountingRunnable counter = new CountingRunnable();
    Disposable disposable = worker.schedulePeriodically(counter, 1, 1, MINUTES);
    runUiThreadTasks();
    assertEquals(0, counter.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(1, counter.get());
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(2, counter.get());
    disposable.dispose();
    idleMainLooper(1, MINUTES);
    runUiThreadTasks();
    assertEquals(2, counter.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Worker(io.reactivex.Scheduler.Worker) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with Worker

use of io.reactivex.Scheduler.Worker in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method workerUnsubscriptionDuringSchedulingCancelsScheduledAction.

@Test
public void workerUnsubscriptionDuringSchedulingCancelsScheduledAction() {
    final AtomicReference<Worker> workerRef = new AtomicReference<>();
    RxJavaPlugins.setScheduleHandler(new Function<Runnable, Runnable>() {

        @Override
        public Runnable apply(Runnable runnable) {
            // Purposefully unsubscribe in an asinine point after the normal unsubscribed check.
            workerRef.get().dispose();
            return runnable;
        }
    });
    Worker worker = scheduler.createWorker();
    workerRef.set(worker);
    CountingRunnable counter = new CountingRunnable();
    worker.schedule(counter);
    runUiThreadTasks();
    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 4 with Worker

use of io.reactivex.Scheduler.Worker in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method workerSchedulePeriodicallyInputValidation.

@Test
@Ignore("Implementation delegated to default RxJava implementation")
public void workerSchedulePeriodicallyInputValidation() {
    Worker worker = scheduler.createWorker();
    try {
        worker.schedulePeriodically(null, 1, 1, MINUTES);
        fail();
    } catch (NullPointerException e) {
        assertEquals("run == null", e.getMessage());
    }
    try {
        worker.schedulePeriodically(new CountingRunnable(), 1, -1, MINUTES);
        fail();
    } catch (IllegalArgumentException e) {
        assertEquals("period < 0: -1", e.getMessage());
    }
    try {
        worker.schedulePeriodically(new CountingRunnable(), 1, 1, null);
        fail();
    } catch (NullPointerException e) {
        assertEquals("unit == null", e.getMessage());
    }
}
Also used : CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Worker(io.reactivex.Scheduler.Worker) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Worker

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

Aggregations

Worker (io.reactivex.Scheduler.Worker)61 Test (org.junit.Test)40 CountingRunnable (io.reactivex.android.testutil.CountingRunnable)20 Disposable (io.reactivex.disposables.Disposable)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Ignore (org.junit.Ignore)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)3 EmptyDisposable (io.reactivex.internal.disposables.EmptyDisposable)2 Scheduler (io.reactivex.Scheduler)1 ConditionalSubscriber (io.reactivex.internal.fuseable.ConditionalSubscriber)1 SubscribeOnSubscriber (io.reactivex.internal.operators.flowable.FlowableSubscribeOn.SubscribeOnSubscriber)1 SpscArrayQueue (io.reactivex.internal.queue.SpscArrayQueue)1 ComputationScheduler (io.reactivex.internal.schedulers.ComputationScheduler)1 IoScheduler (io.reactivex.internal.schedulers.IoScheduler)1 NewThreadWorker (io.reactivex.internal.schedulers.NewThreadWorker)1 ScheduledWorker (io.reactivex.internal.schedulers.SingleScheduler.ScheduledWorker)1 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)1 TestScheduler (io.reactivex.schedulers.TestScheduler)1 TestSubscriber (io.reactivex.subscribers.TestSubscriber)1