Search in sources :

Example 11 with JobHandle

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();
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 12 with JobHandle

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);
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 13 with JobHandle

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);
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 14 with JobHandle

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);
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 15 with JobHandle

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));
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) CountDownLatch(java.util.concurrent.CountDownLatch) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Aggregations

JobHandle (org.neo4j.scheduler.JobHandle)20 Test (org.junit.jupiter.api.Test)14 RepeatedTest (org.junit.jupiter.api.RepeatedTest)9 TimeUnit (java.util.concurrent.TimeUnit)3 JobMonitoringParams (org.neo4j.scheduler.JobMonitoringParams)3 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 IndexPopulator (org.neo4j.kernel.api.index.IndexPopulator)2 CancellationException (java.util.concurrent.CancellationException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Dependencies (org.neo4j.collection.Dependencies)1 NULL_CONTEXT (org.neo4j.internal.kernel.api.QueryContext.NULL_CONTEXT)1 IndexProviderDescriptor (org.neo4j.internal.schema.IndexProviderDescriptor)1 IndexDirectoryStructure (org.neo4j.kernel.api.index.IndexDirectoryStructure)1 PopulationWorkScheduler (org.neo4j.kernel.api.index.IndexPopulator.PopulationWorkScheduler)1 TransactionMonitorScheduler (org.neo4j.kernel.impl.api.transaction.monitor.TransactionMonitorScheduler)1 CentralJobScheduler (org.neo4j.kernel.impl.scheduler.CentralJobScheduler)1