Search in sources :

Example 6 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class TimeBasedTaskSchedulerTest method mustDelayExecution.

@Test
void mustDelayExecution() throws Exception {
    JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, counter::incrementAndGet, 100, 0);
    scheduler.tick();
    assertThat(counter.get()).isEqualTo(0);
    clock.forward(99, TimeUnit.NANOSECONDS);
    scheduler.tick();
    assertThat(counter.get()).isEqualTo(0);
    clock.forward(1, TimeUnit.NANOSECONDS);
    scheduler.tick();
    handle.waitTermination();
    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 7 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class TimeBasedTaskSchedulerTest method recurringTasksMustStopWhenCancelled.

@Test
void recurringTasksMustStopWhenCancelled() throws InterruptedException {
    MonitoredCancelListener cancelListener = new MonitoredCancelListener();
    Runnable recurring = () -> {
        counter.incrementAndGet();
        semaphore.release();
    };
    JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, recurring, 100, 100);
    handle.registerCancelListener(cancelListener);
    clock.forward(100, TimeUnit.NANOSECONDS);
    scheduler.tick();
    assertSemaphoreAcquire();
    clock.forward(100, TimeUnit.NANOSECONDS);
    scheduler.tick();
    assertSemaphoreAcquire();
    handle.cancel();
    clock.forward(100, TimeUnit.NANOSECONDS);
    scheduler.tick();
    clock.forward(100, TimeUnit.NANOSECONDS);
    scheduler.tick();
    pools.getThreadPool(Group.STORAGE_MAINTENANCE).shutDown();
    assertThat(counter.get()).isEqualTo(2);
    assertTrue(cancelListener.isCanceled());
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 8 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class IndexSamplingControllerTest method shouldNotWaitForAsyncIndexSamplesIfConfigured.

@Test
void shouldNotWaitForAsyncIndexSamplesIfConfigured() {
    final IndexSamplingController controller = newSamplingController(always(true), logProvider, Config.defaults(GraphDatabaseInternalSettings.async_recover_index_samples_wait, false));
    when(indexProxy.getState()).thenReturn(ONLINE);
    when(jobFactory.create(indexId, indexProxy)).thenReturn(job);
    final JobHandle jobHandle = mock(JobHandle.class);
    when(tracker.scheduleSamplingJob(any(IndexSamplingJob.class))).thenReturn(jobHandle);
    controller.recoverIndexSamples();
    verify(tracker).scheduleSamplingJob(job);
    verifyNoMoreInteractions(jobHandle);
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) Test(org.junit.jupiter.api.Test)

Example 9 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class IndexSamplingControllerTest method waitForAsyncIndexSamples.

@Test
void waitForAsyncIndexSamples() throws ExecutionException, InterruptedException {
    final IndexSamplingController controller = newSamplingController(always(true), logProvider);
    when(indexProxy.getState()).thenReturn(ONLINE);
    when(jobFactory.create(indexId, indexProxy)).thenReturn(job);
    final JobHandle jobHandle = mock(JobHandle.class);
    when(tracker.scheduleSamplingJob(any(IndexSamplingJob.class))).thenReturn(jobHandle);
    controller.recoverIndexSamples();
    verify(tracker).scheduleSamplingJob(job);
    verify(jobHandle).waitTermination();
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) Test(org.junit.jupiter.api.Test)

Example 10 with JobHandle

use of org.neo4j.scheduler.JobHandle in project neo4j by neo4j.

the class TimeBasedTaskSchedulerTest method cleanupCanceledHandles.

@Test
void cleanupCanceledHandles() {
    Runnable recurring = () -> counter.incrementAndGet();
    JobHandle handle = scheduler.submit(Group.STORAGE_MAINTENANCE, recurring, 0, 100);
    // initial delay is 0 so this task will be scheduled right away
    scheduler.tick();
    // wait until the task has been run (and re-enqueued since it's recurring).
    while (scheduler.tasksLeft() == 0) {
        Thread.yield();
    }
    assertThat(counter.get()).isEqualTo(1);
    handle.cancel();
    // cancelling doesn't remove from queued tasks
    assertEquals(1, scheduler.tasksLeft());
    clock.forward(100, TimeUnit.NANOSECONDS);
    // enough time has passed that this task, if not cancelled, would have been queued again
    scheduler.tick();
    // tick will remove cancelled tasks
    assertEquals(0, scheduler.tasksLeft());
    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)

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