Search in sources :

Example 11 with TestScheduler

use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.

the class TestSchedulerTest method withOnScheduleHookInitialTime.

@Test
public void withOnScheduleHookInitialTime() {
    AtomicInteger run = new AtomicInteger();
    AtomicInteger counter = new AtomicInteger();
    RxJavaPlugins.setScheduleHandler(r -> {
        counter.getAndIncrement();
        return r;
    });
    try {
        Runnable r = () -> run.getAndIncrement();
        TestScheduler ts = new TestScheduler(1, TimeUnit.HOURS, true);
        ts.createWorker().schedule(r);
        ts.createWorker().schedule(r, 1, TimeUnit.SECONDS);
        ts.advanceTimeBy(1, TimeUnit.SECONDS);
        assertEquals(2, run.get());
        assertEquals(2, counter.get());
        ts = new TestScheduler(1, TimeUnit.HOURS);
        ts.createWorker().schedule(r);
        ts.createWorker().schedule(r, 1, TimeUnit.SECONDS);
        ts.advanceTimeBy(1, TimeUnit.SECONDS);
        assertEquals(4, run.get());
        assertEquals(2, counter.get());
    } finally {
        RxJavaPlugins.setScheduleHandler(null);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 12 with TestScheduler

use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.

the class SingleTimerTest method timer.

@Test
public void timer() {
    final TestScheduler testScheduler = new TestScheduler();
    final AtomicLong atomicLong = new AtomicLong();
    Single.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Consumer<Long>() {

        @Override
        public void accept(final Long value) throws Exception {
            atomicLong.incrementAndGet();
        }
    });
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(1, atomicLong.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Example 13 with TestScheduler

use of io.reactivex.rxjava3.schedulers.TestScheduler 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 14 with TestScheduler

use of io.reactivex.rxjava3.schedulers.TestScheduler 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 15 with TestScheduler

use of io.reactivex.rxjava3.schedulers.TestScheduler in project RxJava by ReactiveX.

the class TestSchedulerTest method timedRunnableToString.

@Test
public void timedRunnableToString() {
    TimedRunnable r = new TimedRunnable((TestWorker) new TestScheduler().createWorker(), 5, new Runnable() {

        @Override
        public void run() {
        // deliberately no-op
        }

        @Override
        public String toString() {
            return "Runnable";
        }
    }, 1);
    assertEquals("TimedRunnable(time = 5, run = Runnable)", r.toString());
}
Also used : TestScheduler(io.reactivex.rxjava3.schedulers.TestScheduler) Test(org.junit.Test)

Aggregations

TestScheduler (io.reactivex.rxjava3.schedulers.TestScheduler)92 Test (org.junit.Test)84 TestException (io.reactivex.rxjava3.exceptions.TestException)38 InOrder (org.mockito.InOrder)30 Disposable (io.reactivex.rxjava3.disposables.Disposable)10 BooleanSubscription (io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 RxJavaTest (io.reactivex.rxjava3.core.RxJavaTest)7 Worker (io.reactivex.rxjava3.core.Scheduler.Worker)7 TestObserver (io.reactivex.rxjava3.observers.TestObserver)6 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)6 Action (io.reactivex.rxjava3.functions.Action)5 SchedulerWhen (io.reactivex.rxjava3.internal.schedulers.SchedulerWhen)4 Observable (io.reactivex.rxjava3.core.Observable)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)2 BaseObserveOnSubscriber (io.reactivex.rxjava3.internal.operators.flowable.FlowableObserveOn.BaseObserveOnSubscriber)1 CompletableSubject (io.reactivex.rxjava3.subjects.CompletableSubject)1 IOException (java.io.IOException)1