use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class ElectionTest method candidateShouldWinElectionAndBecomeLeader.
@Test
public void candidateShouldWinElectionAndBecomeLeader() throws Exception {
// given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
raft.startTimers();
timeouts.invokeTimeout(RaftMachine.Timeouts.ELECTION);
// when
raft.handle(voteResponse().from(member1).term(1).grant().build());
raft.handle(voteResponse().from(member2).term(1).grant().build());
// then
assertEquals(1, raft.term());
assertEquals(LEADER, raft.currentRole());
verify(outbound).send(eq(member1), isA(RaftMessages.AppendEntries.Request.class));
verify(outbound).send(eq(member2), isA(RaftMessages.AppendEntries.Request.class));
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class ElectionTest method candidateShouldVoteForTheSameCandidateInTheSameTerm.
@Test
public void candidateShouldVoteForTheSameCandidateInTheSameTerm() throws Exception {
// given
FakeClock fakeClock = Clocks.fakeClock();
ControlledRenewableTimeoutService timeouts = new ControlledRenewableTimeoutService(fakeClock);
RaftMachine raft = new RaftMachineBuilder(myself, 3, RaftTestMemberSetBuilder.INSTANCE).outbound(outbound).timeoutService(timeouts).clock(fakeClock).build();
raft.installCoreState(new RaftCoreState(new MembershipEntry(0, asSet(myself, member1, member2))));
// when
raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
raft.handle(voteRequest().from(member1).candidate(member1).term(1).build());
// then
verify(outbound, times(2)).send(member1, voteResponse().term(1).grant().build());
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class ResourcePoolTest method shouldBuildUpGracefullyWhilePassingMinPoolSizeBeforeTimerRings.
@Test
public void shouldBuildUpGracefullyWhilePassingMinPoolSizeBeforeTimerRings() throws InterruptedException {
// GIVEN
StatefulMonitor stateMonitor = new StatefulMonitor();
FakeClock clock = getFakeClocks();
final ResourcePool<Something> pool = getResourcePool(stateMonitor, clock, 5);
// WHEN
acquireFromPool(pool, 15);
// THEN
assertEquals(-1, stateMonitor.currentPeakSize.get());
assertEquals(15, stateMonitor.created.get());
assertEquals(-1, stateMonitor.targetSize.get());
assertEquals(0, stateMonitor.disposed.get());
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class ResourcePoolTest method shouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreReleased.
@Test
public void shouldSlowlyReduceTheNumberOfResourcesInThePoolWhenResourcesAreReleased() throws Exception {
// given
final int poolMinSize = 50;
final int poolMaxSize = 200;
StatefulMonitor stateMonitor = new StatefulMonitor();
FakeClock clock = getFakeClocks();
final SomethingResourcePool pool = getResourcePool(stateMonitor, clock, poolMinSize);
acquireResourcesAndExceedTimeout(pool, clock, poolMaxSize);
// when
// After the peak, stay below MIN_SIZE concurrent usage, using up all already present resources.
exceedTimeout(clock);
for (int i = 0; i < poolMaxSize; i++) {
acquireFromPool(pool, 1).get(0).release();
}
// then
// currentPeakSize must have reset from the latest check to minimum size.
// because of timeout
assertEquals(1, stateMonitor.currentPeakSize.get());
// targetSize must be set to MIN_SIZE since currentPeakSize was that 2 checks ago and didn't increase
assertEquals(poolMinSize, stateMonitor.targetSize.get());
// Only pooled resources must be used, disposing what is in excess
// +1 that was used to trigger exceed timeout check
assertEquals(poolMinSize, pool.unusedSize());
assertEquals(poolMaxSize - poolMinSize + 1, stateMonitor.disposed.get());
}
use of org.neo4j.time.FakeClock in project neo4j by neo4j.
the class ResourcePoolTest method shouldTimeoutGracefully.
@Test
public void shouldTimeoutGracefully() throws InterruptedException {
FakeClock clock = getFakeClocks();
ResourcePool.CheckStrategy timeStrategy = new ResourcePool.CheckStrategy.TimeoutCheckStrategy(TIMEOUT_MILLIS, clock);
while (clock.millis() <= TIMEOUT_MILLIS) {
assertFalse(timeStrategy.shouldCheck());
clock.forward(10, TimeUnit.MILLISECONDS);
}
assertTrue(timeStrategy.shouldCheck());
clock.forward(1, TimeUnit.MILLISECONDS);
assertFalse(timeStrategy.shouldCheck());
}
Aggregations