Search in sources :

Example 6 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)

Example 7 with JobHandle

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

the class Neo4jJobSchedulerTest method shouldNotifyCancelListeners.

@Test
public void shouldNotifyCancelListeners() throws Exception {
    // GIVEN
    Neo4jJobScheduler neo4jJobScheduler = new Neo4jJobScheduler();
    neo4jJobScheduler.init();
    // WHEN
    AtomicBoolean halted = new AtomicBoolean();
    Runnable job = () -> {
        while (!halted.get()) {
            LockSupport.parkNanos(MILLISECONDS.toNanos(10));
        }
    };
    JobHandle handle = neo4jJobScheduler.schedule(indexPopulation, job);
    handle.registerCancelListener(mayBeInterrupted -> halted.set(true));
    handle.cancel(false);
    // THEN
    assertTrue(halted.get());
    neo4jJobScheduler.shutdown();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JobHandle(org.neo4j.kernel.impl.util.JobScheduler.JobHandle) 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