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);
}
}
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());
}
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());
}
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());
}
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());
}
Aggregations