use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class ContinuousJobTest method shouldTerminateOnStop.
@Test
public void shouldTerminateOnStop() throws Exception {
// given: this task is gonna take >20 ms total
Semaphore semaphore = new Semaphore(-20);
Runnable task = () -> {
// 1 ms
LockSupport.parkNanos(1_000_000);
semaphore.release();
};
Neo4jJobScheduler scheduler = new Neo4jJobScheduler();
ContinuousJob continuousJob = new ContinuousJob(scheduler, jobGroup, task, NullLogProvider.getInstance());
// when
long startTime = System.currentTimeMillis();
try (Lifespan ignored = new Lifespan(scheduler, continuousJob)) {
semaphore.acquireUninterruptibly();
}
long runningTime = System.currentTimeMillis() - startTime;
// then
assertThat(runningTime, lessThan(DEFAULT_TIMEOUT_MS));
//noinspection StatementWithEmptyBody
while (semaphore.tryAcquire()) {
// consume all outstanding permits
}
// no more permits should be granted
semaphore.tryAcquire(10, MILLISECONDS);
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class ContinuousJobTest method shouldRunJobContinuously.
@Test
public void shouldRunJobContinuously() throws Throwable {
// given
CountDownLatch latch = new CountDownLatch(10);
Runnable task = latch::countDown;
Neo4jJobScheduler scheduler = new Neo4jJobScheduler();
ContinuousJob continuousJob = new ContinuousJob(scheduler, jobGroup, task, NullLogProvider.getInstance());
// when
try (Lifespan ignored = new Lifespan(scheduler, continuousJob)) {
//then
assertTrue(latch.await(DEFAULT_TIMEOUT_MS, MILLISECONDS));
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class TransactionLogCatchUpWriterTest method simulateStoreCopy.
private org.neo4j.kernel.impl.store.StoreId simulateStoreCopy() throws IOException {
// create an empty store
org.neo4j.kernel.impl.store.StoreId storeId;
NeoStoreDataSource ds = dsRule.getDataSource(storeDir, fs, pageCache, emptyMap());
try (Lifespan ignored = new Lifespan(ds)) {
storeId = ds.getStoreId();
}
// we don't have log files after a store copy
PhysicalLogFiles logFiles = new PhysicalLogFiles(storeDir, fsRule.get());
logFiles.accept((file, version) -> file.delete());
return storeId;
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class SegmentedRaftLogRotationTest method shouldBeAbleToRecoverToLatestStateAfterRotation.
@Test
public void shouldBeAbleToRecoverToLatestStateAfterRotation() throws Throwable {
// Given
int term = 0;
long indexToRestoreTo;
try (Lifespan lifespan = new Lifespan()) {
SegmentedRaftLog log = lifespan.add(createRaftLog(ROTATE_AT_SIZE_IN_BYTES));
log.append(new RaftLogEntry(term, replicatedStringOfBytes(ROTATE_AT_SIZE_IN_BYTES - 40)));
indexToRestoreTo = log.append(new RaftLogEntry(term, ReplicatedInteger.valueOf(1)));
}
// When
SegmentedRaftLog log = life.add(createRaftLog(ROTATE_AT_SIZE_IN_BYTES));
// Then
assertEquals(indexToRestoreTo, log.appendIndex());
assertEquals(term, log.readEntryTerm(indexToRestoreTo));
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class ReplicatedLockTokenStateMachineTest method shouldPersistAndRecoverState.
@Test
public void shouldPersistAndRecoverState() throws Exception {
// given
EphemeralFileSystemAbstraction fsa = fileSystemRule.get();
fsa.mkdir(testDir.directory());
StateMarshal<ReplicatedLockTokenState> marshal = new ReplicatedLockTokenState.Marshal(new MemberId.Marshal());
MemberId memberA = member(0);
MemberId memberB = member(1);
int candidateId;
DurableStateStorage<ReplicatedLockTokenState> storage = new DurableStateStorage<>(fsa, testDir.directory(), "state", marshal, 100, NullLogProvider.getInstance());
try (Lifespan lifespan = new Lifespan(storage)) {
ReplicatedLockTokenStateMachine stateMachine = new ReplicatedLockTokenStateMachine(storage);
// when
candidateId = 0;
stateMachine.applyCommand(new ReplicatedLockTokenRequest(memberA, candidateId), 0, r -> {
});
candidateId = 1;
stateMachine.applyCommand(new ReplicatedLockTokenRequest(memberB, candidateId), 1, r -> {
});
stateMachine.flush();
fsa.crash();
}
// then
DurableStateStorage<ReplicatedLockTokenState> storage2 = new DurableStateStorage<>(fsa, testDir.directory(), "state", marshal, 100, NullLogProvider.getInstance());
try (Lifespan lifespan = new Lifespan(storage2)) {
ReplicatedLockTokenState initialState = storage2.getInitialState();
assertEquals(memberB, initialState.get().owner());
assertEquals(candidateId, initialState.get().id());
}
}
Aggregations