use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.
the class TimeBasedTaskSchedulerTest method overdueRecurringTasksMustStartAsSoonAsPossible.
@Test
void overdueRecurringTasksMustStartAsSoonAsPossible() {
Runnable recurring = () -> {
counter.incrementAndGet();
semaphore.acquireUninterruptibly();
};
JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, recurring, 100, 100);
clock.forward(100, TimeUnit.NANOSECONDS);
scheduler.tick();
while (counter.get() < 1) {
// Spin.
Thread.yield();
}
clock.forward(100, TimeUnit.NANOSECONDS);
scheduler.tick();
clock.forward(100, TimeUnit.NANOSECONDS);
semaphore.release();
scheduler.tick();
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
while (counter.get() < 2 && System.nanoTime() < deadline) {
scheduler.tick();
Thread.yield();
}
assertThat(counter.get()).isEqualTo(2);
semaphore.release(Integer.MAX_VALUE);
handle.cancel();
}
use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.
the class TimeBasedTaskSchedulerTest method mustNotRescheduleDelayedTasks.
@Test
void mustNotRescheduleDelayedTasks() throws Exception {
JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, counter::incrementAndGet, 100, 0);
clock.forward(100, TimeUnit.NANOSECONDS);
scheduler.tick();
handle.waitTermination();
assertThat(counter.get()).isEqualTo(1);
clock.forward(100, TimeUnit.NANOSECONDS);
scheduler.tick();
handle.waitTermination();
pools.getThreadPool(Group.STORAGE_MAINTENANCE).shutDown();
assertThat(counter.get()).isEqualTo(1);
}
use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.
the class TimeBasedTaskSchedulerTest method ensureRescheduledThrowingTasksAreRescheduledCorrectly.
@RepeatedTest(value = 100)
void ensureRescheduledThrowingTasksAreRescheduledCorrectly() throws InterruptedException {
// This is a added in a try to provoke an issue where it looks like tasks are scheduled more times than they should
AtomicInteger timesScheduled = new AtomicInteger(0);
Runnable runnable = () -> {
timesScheduled.incrementAndGet();
semaphore.release();
throw new RuntimeException("boom");
};
JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, runnable, 10, 10);
// should run at MOST 2 times
clock.forward(20, TimeUnit.NANOSECONDS);
scheduler.tick();
assertSemaphoreAcquire();
assertThat(timesScheduled.get()).isLessThanOrEqualTo(2);
}
use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.
the class TimeBasedTaskSchedulerTest method mustOnlyScheduleTasksThatAreDue.
@Test
void mustOnlyScheduleTasksThatAreDue() throws Exception {
JobHandle handle1 = scheduler.submit(Group.STORAGE_MAINTENANCE, () -> counter.addAndGet(10), 100, 0);
JobHandle handle2 = scheduler.submit(Group.STORAGE_MAINTENANCE, () -> counter.addAndGet(100), 200, 0);
scheduler.tick();
assertThat(counter.get()).isEqualTo(0);
clock.forward(199, TimeUnit.NANOSECONDS);
scheduler.tick();
handle1.waitTermination();
assertThat(counter.get()).isEqualTo(10);
clock.forward(1, TimeUnit.NANOSECONDS);
scheduler.tick();
handle2.waitTermination();
assertThat(counter.get()).isEqualTo(110);
}
use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.
the class TimeBasedTaskSchedulerTest method mustRescheduleRecurringTasksThatThrows.
@Test
void mustRescheduleRecurringTasksThatThrows() throws Exception {
var executionCountDown = new CountDownLatch(20);
Runnable runnable = () -> {
try {
semaphore.release();
throw new RuntimeException("boom");
} finally {
executionCountDown.countDown();
}
};
JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, runnable, 10, 10);
clock.forward(100, TimeUnit.NANOSECONDS);
scheduler.tick();
assertSemaphoreAcquire();
do {
clock.forward(100, TimeUnit.NANOSECONDS);
scheduler.tick();
} while (!executionCountDown.await(1, TimeUnit.MILLISECONDS));
}
Aggregations