Search in sources :

Example 11 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class NonFollowerVoteRequestTest method shouldRejectVoteRequestFromCurrentTerm.

@Test
public void shouldRejectVoteRequestFromCurrentTerm() throws Exception {
    RaftState state = newState();
    // when
    final long candidateTerm = state.term();
    Outcome outcome = role.handler.handle(voteRequest().from(member1).term(candidateTerm).lastLogIndex(0).lastLogTerm(-1).build(), state, log());
    // then
    assertFalse(((RaftMessages.Vote.Response) messageFor(outcome, member1)).voteGranted());
    assertEquals(role, outcome.getRole());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Test(org.junit.Test)

Example 12 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class CandidateTest method shouldUpdateTermOnReceivingVoteResponseWithLaterTerm.

@Test
public void shouldUpdateTermOnReceivingVoteResponseWithLaterTerm() throws Exception {
    // given
    RaftState state = newState();
    final long voterTerm = state.term() + 1;
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(voterTerm).from(member1).grant().build(), state, log());
    // then
    assertEquals(FOLLOWER, outcome.getRole());
    assertEquals(voterTerm, outcome.getTerm());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Test(org.junit.Test)

Example 13 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class CandidateTest method shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm.

@Test
public void shouldBeElectedLeaderOnReceivingGrantedVoteResponseWithCurrentTerm() throws Exception {
    // given
    RaftState state = RaftStateBuilder.raftState().term(1).myself(myself).votingMembers(member1, member2).replicationMembers(member1, member2).build();
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(state.term()).from(member1).grant().build(), state, log());
    // then
    assertEquals(LEADER, outcome.getRole());
    assertEquals(true, outcome.electionTimeoutRenewed());
    assertThat(outcome.getLogCommands(), hasItem(new AppendLogEntry(0, new RaftLogEntry(state.term(), new NewLeaderBarrier()))));
    assertThat(outcome.getOutgoingMessages(), hasItems(new RaftMessages.Directed(member1, new RaftMessages.Heartbeat(myself, state.term(), -1, -1)), new RaftMessages.Directed(member2, new RaftMessages.Heartbeat(myself, state.term(), -1, -1))));
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) AppendLogEntry(org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry) RaftMessages(org.neo4j.causalclustering.core.consensus.RaftMessages) NewLeaderBarrier(org.neo4j.causalclustering.core.consensus.NewLeaderBarrier) RaftLogEntry(org.neo4j.causalclustering.core.consensus.log.RaftLogEntry) Test(org.junit.Test)

Example 14 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class CandidateTest method shouldRejectVoteResponseWithOldTerm.

@Test
public void shouldRejectVoteResponseWithOldTerm() throws Exception {
    // given
    RaftState state = newState();
    final long voterTerm = state.term() - 1;
    // when
    Outcome outcome = CANDIDATE.handler.handle(voteResponse().term(voterTerm).from(member1).grant().build(), state, log());
    // then
    assertEquals(CANDIDATE, outcome.getRole());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Test(org.junit.Test)

Example 15 with Outcome

use of org.neo4j.causalclustering.core.consensus.outcome.Outcome in project neo4j by neo4j.

the class FollowerTest method shouldBecomeCandidateOnReceivingElectionTimeoutMessage.

@Test
public void shouldBecomeCandidateOnReceivingElectionTimeoutMessage() throws Exception {
    // given
    RaftState state = raftState().myself(myself).votingMembers(asSet(myself, member1, member2)).build();
    Follower follower = new Follower();
    // when
    Outcome outcome = follower.handle(new Election(myself), state, log());
    // then
    assertEquals(CANDIDATE, outcome.getRole());
}
Also used : Outcome(org.neo4j.causalclustering.core.consensus.outcome.Outcome) RaftState(org.neo4j.causalclustering.core.consensus.state.RaftState) Election(org.neo4j.causalclustering.core.consensus.RaftMessages.Timeout.Election) Test(org.junit.Test)

Aggregations

Outcome (org.neo4j.causalclustering.core.consensus.outcome.Outcome)58 Test (org.junit.Test)53 RaftState (org.neo4j.causalclustering.core.consensus.state.RaftState)42 RaftLogEntry (org.neo4j.causalclustering.core.consensus.log.RaftLogEntry)27 InMemoryRaftLog (org.neo4j.causalclustering.core.consensus.log.InMemoryRaftLog)24 ReadableRaftState (org.neo4j.causalclustering.core.consensus.state.ReadableRaftState)19 RaftMessages (org.neo4j.causalclustering.core.consensus.RaftMessages)15 AppendEntries (org.neo4j.causalclustering.core.consensus.RaftMessages.AppendEntries)11 RaftLog (org.neo4j.causalclustering.core.consensus.log.RaftLog)11 ReadableRaftLog (org.neo4j.causalclustering.core.consensus.log.ReadableRaftLog)9 FollowerState (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerState)6 FollowerStates (org.neo4j.causalclustering.core.consensus.roles.follower.FollowerStates)6 Log (org.neo4j.logging.Log)6 BatchAppendLogEntries (org.neo4j.causalclustering.core.consensus.outcome.BatchAppendLogEntries)5 ShipCommand (org.neo4j.causalclustering.core.consensus.outcome.ShipCommand)5 MemberId (org.neo4j.causalclustering.identity.MemberId)5 RaftTestGroup (org.neo4j.causalclustering.core.consensus.membership.RaftTestGroup)4 AppendLogEntry (org.neo4j.causalclustering.core.consensus.outcome.AppendLogEntry)4 ReplicatedString (org.neo4j.causalclustering.core.consensus.ReplicatedString)3 TruncateLogCommand (org.neo4j.causalclustering.core.consensus.outcome.TruncateLogCommand)3