Example #1
0
  public void testAppendOnNonEmptyLog() throws Throwable {
    runOnServer(
        () -> {
          serverContext.setTerm(1);
          append(1, 1);

          AppendRequest request =
              AppendRequest.builder()
                  .withTerm(1)
                  .withLeader(
                      serverContext
                          .getClusterState()
                          .getActiveMemberStates()
                          .iterator()
                          .next()
                          .getMember()
                          .id())
                  .withLogIndex(0)
                  .withLogTerm(0)
                  .withCommitIndex(2)
                  .withGlobalIndex(0)
                  .withEntries(new TestEntry().setIndex(2).setTerm(1))
                  .build();

          AppendResponse response = state.append(request).get();

          assertEquals(response.status(), Status.OK);
          assertTrue(response.succeeded());
          assertEquals(response.term(), 1L);
          assertEquals(response.logIndex(), 2L);

          assertEquals(serverContext.getLog().length(), 2L);
          assertNotNull(get(2));
        });
  }
Example #2
0
  public void testCandidateRejectsVoteAndTransitionsOnTerm() throws Throwable {
    runOnServer(
        () -> {
          int candidate =
              serverState.getCluster().getActiveMembers().iterator().next().getAddress().hashCode();
          serverState.setTerm(1);

          append(2, 1);

          state.startElection();

          assertEquals(serverState.getTerm(), 2L);

          VoteRequest request =
              VoteRequest.builder()
                  .withTerm(3)
                  .withCandidate(candidate)
                  .withLogTerm(0)
                  .withLogIndex(0)
                  .build();

          VoteResponse response = state.vote(request).get();

          assertEquals(response.status(), Response.Status.OK);
          assertFalse(response.voted());
          assertEquals(serverState.getTerm(), 3L);
          assertEquals(serverState.getLastVotedFor(), 0);
          assertEquals(response.term(), 3L);
          assertEquals(serverState.getState(), RaftServer.State.FOLLOWER);
        });
  }
Example #3
0
  @SuppressWarnings("unchecked")
  public void testRejectAppendOnTerm() throws Throwable {
    runOnServer(
        () -> {
          serverContext.setTerm(2);
          append(2, 2);

          AppendRequest request =
              AppendRequest.builder()
                  .withTerm(1)
                  .withLeader(
                      serverContext
                          .getClusterState()
                          .getActiveMemberStates()
                          .iterator()
                          .next()
                          .getMember()
                          .id())
                  .withEntries(Collections.EMPTY_LIST)
                  .withLogIndex(2)
                  .withLogTerm(2)
                  .withCommitIndex(0)
                  .withGlobalIndex(0)
                  .build();

          AppendResponse response = state.append(request).get();

          assertEquals(response.status(), Status.OK);
          assertFalse(response.succeeded());
          assertEquals(response.term(), 2L);
          assertEquals(response.logIndex(), 2L);
        });
  }