Search in sources :

Example 41 with Scheduler

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

the class SchedulerTest method disposePeriodicDirect.

@Test
public void disposePeriodicDirect() {
    final int[] count = { 0 };
    TestScheduler scheduler = new TestScheduler();
    Disposable d = scheduler.schedulePeriodicallyDirect(new Runnable() {

        @Override
        public void run() {
            count[0]++;
        }
    }, 100, 100, TimeUnit.MILLISECONDS);
    d.dispose();
    assertEquals(0, count[0]);
    assertTrue(d.isDisposed());
    scheduler.advanceTimeBy(200, TimeUnit.MILLISECONDS);
    assertEquals(0, count[0]);
    assertTrue(d.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 42 with Scheduler

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

the class SchedulerTest method disposeSelfPeriodicDirect.

@Test
public void disposeSelfPeriodicDirect() {
    final int[] count = { 0 };
    TestScheduler scheduler = new TestScheduler();
    final SequentialDisposable sd = new SequentialDisposable();
    Disposable d = scheduler.schedulePeriodicallyDirect(new Runnable() {

        @Override
        public void run() {
            count[0]++;
            sd.dispose();
        }
    }, 100, 100, TimeUnit.MILLISECONDS);
    sd.set(d);
    assertEquals(0, count[0]);
    assertFalse(d.isDisposed());
    scheduler.advanceTimeBy(400, TimeUnit.MILLISECONDS);
    assertEquals(1, count[0]);
    assertTrue(d.isDisposed());
}
Also used : Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 43 with Scheduler

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

the class ExecutorSchedulerFairTest method plainExecutor.

@Test
public void plainExecutor() throws Exception {
    Scheduler s = Schedulers.from(new Executor() {

        @Override
        public void execute(Runnable r) {
            r.run();
        }
    }, false, true);
    final CountDownLatch cdl = new CountDownLatch(5);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            cdl.countDown();
        }
    };
    s.scheduleDirect(r);
    s.scheduleDirect(r, 50, TimeUnit.MILLISECONDS);
    Disposable d = s.schedulePeriodicallyDirect(r, 10, 10, TimeUnit.MILLISECONDS);
    try {
        assertTrue(cdl.await(5, TimeUnit.SECONDS));
    } finally {
        d.dispose();
    }
    assertTrue(d.isDisposed());
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 44 with Scheduler

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

the class ExecutorSchedulerFairTest method runnableDisposed.

@Test
public void runnableDisposed() {
    final Scheduler s = Schedulers.from(new Executor() {

        @Override
        public void execute(Runnable r) {
            r.run();
        }
    }, false, true);
    Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE);
    assertTrue(d.isDisposed());
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) Test(org.junit.Test)

Example 45 with Scheduler

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

the class ExecutorSchedulerFairTest method runnableDisposedAsyncTimed2.

@Test
public void runnableDisposedAsyncTimed2() throws Exception {
    ExecutorService executorScheduler = Executors.newScheduledThreadPool(1, new RxThreadFactory("TestCustomPoolTimed"));
    try {
        final Scheduler s = Schedulers.from(executorScheduler, false, true);
        Disposable d = s.scheduleDirect(Functions.EMPTY_RUNNABLE, 1, TimeUnit.MILLISECONDS);
        while (!d.isDisposed()) {
            Thread.sleep(1);
        }
    } finally {
        executorScheduler.shutdownNow();
    }
}
Also used : EmptyDisposable(io.reactivex.rxjava3.internal.disposables.EmptyDisposable) Disposable(io.reactivex.rxjava3.disposables.Disposable) RxThreadFactory(io.reactivex.rxjava3.internal.schedulers.RxThreadFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)169 Disposable (io.reactivex.rxjava3.disposables.Disposable)69 Scheduler (io.reactivex.rxjava3.core.Scheduler)61 TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)54 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)50 TestException (io.reactivex.rxjava3.exceptions.TestException)34 EmptyDisposable (io.reactivex.rxjava3.internal.disposables.EmptyDisposable)32 InOrder (org.mockito.InOrder)32 TrampolineScheduler (io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler)20 ImmediateThinScheduler (io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 EmptyScheduler (io.reactivex.rxjava3.android.testutil.EmptyScheduler)8 Observable (io.reactivex.rxjava3.core.Observable)7 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)6 SequentialDisposable (io.reactivex.rxjava3.internal.disposables.SequentialDisposable)6 Action (io.reactivex.rxjava3.functions.Action)5 SuppressUndeliverable (io.reactivex.rxjava3.testsupport.SuppressUndeliverable)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5