Search in sources :

Example 6 with Worker

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

the class HandlerSchedulerTest method workerScheduleOnceWithDelayPostsWithDelay.

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

Example 7 with Worker

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

Example 8 with Worker

use of io.reactivex.rxjava3.core.Scheduler.Worker 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.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)

Example 9 with Worker

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

the class HandlerSchedulerTest method workerScheduleOnceInputValidation.

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

Example 10 with Worker

use of io.reactivex.rxjava3.core.Scheduler.Worker 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.rxjava3.android.testutil.CountingRunnable) Worker(io.reactivex.rxjava3.core.Scheduler.Worker) Test(org.junit.Test)

Aggregations

Worker (io.reactivex.rxjava3.core.Scheduler.Worker)91 Test (org.junit.Test)87 Disposable (io.reactivex.rxjava3.disposables.Disposable)28 CountingRunnable (io.reactivex.rxjava3.android.testutil.CountingRunnable)22 Scheduler (io.reactivex.rxjava3.core.Scheduler)18 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)9 Ignore (org.junit.Ignore)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)4 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)4 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)4 IoScheduler (io.reactivex.rxjava3.internal.schedulers.IoScheduler)3 WorkerCallback (io.reactivex.rxjava3.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ThrowingRunnable (org.junit.function.ThrowingRunnable)3 Message (android.os.Message)2 CompositeDisposable (io.reactivex.rxjava3.disposables.CompositeDisposable)2 TestException (io.reactivex.rxjava3.exceptions.TestException)2 NewThreadWorker (io.reactivex.rxjava3.internal.schedulers.NewThreadWorker)2