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