Search in sources :

Example 1 with JobHandle

use of org.neo4j.kernel.impl.util.JobScheduler.JobHandle in project neo4j by neo4j.

the class Neo4jJobSchedulerTest method shouldCancelRecurringJob.

@Test
public void shouldCancelRecurringJob() throws Exception {
    // Given
    long period = 2;
    life.start();
    JobScheduler.JobHandle jobHandle = scheduler.scheduleRecurring(indexPopulation, countInvocationsJob, period, MILLISECONDS);
    awaitFirstInvocation();
    // When
    jobHandle.cancel(false);
    // Then
    int recorded = invocations.get();
    sleep(period * 100);
    assertThat(invocations.get(), equalTo(recorded));
}
Also used : JobHandle(org.neo4j.kernel.impl.util.JobScheduler.JobHandle) Test(org.junit.Test)

Example 2 with JobHandle

use of org.neo4j.kernel.impl.util.JobScheduler.JobHandle in project neo4j by neo4j.

the class RobustJobSchedulerWrapperTest method recurringJobWithErrorShouldStop.

@Test
public void recurringJobWithErrorShouldStop() throws Exception {
    // given
    RobustJobSchedulerWrapper robustWrapper = new RobustJobSchedulerWrapper(actualScheduler, log);
    AtomicInteger count = new AtomicInteger();
    Error e = new Error();
    // when
    JobHandle jobHandle = robustWrapper.scheduleRecurring("JobName", 1, () -> {
        count.incrementAndGet();
        throw e;
    });
    // when
    // should not keep increasing during this time
    Thread.sleep(50);
    // then
    assertEventually("run count", count::get, Matchers.equalTo(1), DEFAULT_TIMEOUT_MS, MILLISECONDS);
    robustWrapper.cancelAndWaitTermination(jobHandle);
    verify(log, timeout(DEFAULT_TIMEOUT_MS).times(1)).error("Uncaught error rethrown", e);
}
Also used : JobHandle(org.neo4j.kernel.impl.util.JobScheduler.JobHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 3 with JobHandle

use of org.neo4j.kernel.impl.util.JobScheduler.JobHandle in project neo4j by neo4j.

the class RobustJobSchedulerWrapperTest method recurringJobWithExceptionShouldKeepRunning.

@Test
public void recurringJobWithExceptionShouldKeepRunning() throws Exception {
    // given
    RobustJobSchedulerWrapper robustWrapper = new RobustJobSchedulerWrapper(actualScheduler, log);
    AtomicInteger count = new AtomicInteger();
    IllegalStateException e = new IllegalStateException();
    // when
    int nRuns = 100;
    JobHandle jobHandle = robustWrapper.scheduleRecurring("JobName", 1, () -> {
        if (count.get() < nRuns) {
            count.incrementAndGet();
            throw e;
        }
    });
    // then
    assertEventually("run count", count::get, Matchers.equalTo(nRuns), DEFAULT_TIMEOUT_MS, MILLISECONDS);
    robustWrapper.cancelAndWaitTermination(jobHandle);
    verify(log, timeout(DEFAULT_TIMEOUT_MS).times(nRuns)).warn("Uncaught exception", e);
}
Also used : JobHandle(org.neo4j.kernel.impl.util.JobScheduler.JobHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 4 with JobHandle

use of org.neo4j.kernel.impl.util.JobScheduler.JobHandle in project neo4j by neo4j.

the class RobustJobSchedulerWrapperTest method shouldBeAbleToCancelJob.

@Test
public void shouldBeAbleToCancelJob() throws Exception {
    // given
    RobustJobSchedulerWrapper robustWrapper = new RobustJobSchedulerWrapper(actualScheduler, log);
    // when
    AtomicInteger count = new AtomicInteger();
    JobHandle jobHandle = robustWrapper.scheduleRecurring("JobName", 1, count::incrementAndGet);
    assertEventually("run count", count::get, Matchers.greaterThanOrEqualTo(100), DEFAULT_TIMEOUT_MS, MILLISECONDS);
    // then
    robustWrapper.cancelAndWaitTermination(jobHandle);
    int finalCount = count.get();
    // when
    // should not keep increasing during this time
    Thread.sleep(50);
    // then
    assertEquals(finalCount, count.get());
}
Also used : JobHandle(org.neo4j.kernel.impl.util.JobScheduler.JobHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 5 with JobHandle

use of org.neo4j.kernel.impl.util.JobScheduler.JobHandle in project neo4j by neo4j.

the class RobustJobSchedulerWrapperTest method oneOffJobWithExceptionShouldLog.

@Test
public void oneOffJobWithExceptionShouldLog() throws Exception {
    // given
    Log log = mock(Log.class);
    RobustJobSchedulerWrapper robustWrapper = new RobustJobSchedulerWrapper(actualScheduler, log);
    AtomicInteger count = new AtomicInteger();
    IllegalStateException e = new IllegalStateException();
    // when
    JobHandle jobHandle = robustWrapper.schedule("JobName", 100, () -> {
        count.incrementAndGet();
        throw e;
    });
    // then
    assertEventually("run count", count::get, Matchers.equalTo(1), DEFAULT_TIMEOUT_MS, MILLISECONDS);
    jobHandle.waitTermination();
    verify(log, timeout(DEFAULT_TIMEOUT_MS).times(1)).warn("Uncaught exception", e);
}
Also used : JobHandle(org.neo4j.kernel.impl.util.JobScheduler.JobHandle) Log(org.neo4j.logging.Log) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Aggregations

JobHandle (org.neo4j.kernel.impl.util.JobScheduler.JobHandle)7 Test (org.junit.Test)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BinaryLatch (org.neo4j.concurrent.BinaryLatch)1 Log (org.neo4j.logging.Log)1