Пример #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));
        });
  }
Пример #2
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);
        });
  }