Example #1
0
  @SuppressWarnings("unchecked")
  public void testAppendUpdatesLeaderAndTerm() throws Throwable {
    runOnServer(
        () -> {
          serverContext.setTerm(1);
          AppendRequest request =
              AppendRequest.builder()
                  .withTerm(2)
                  .withLeader(members.get(1).hashCode())
                  .withEntries(Collections.EMPTY_LIST)
                  .withLogIndex(0)
                  .withLogTerm(0)
                  .withCommitIndex(0)
                  .withGlobalIndex(0)
                  .build();

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

          threadAssertEquals(serverContext.getTerm(), 2L);
          threadAssertEquals(
              serverContext.getLeader().serverAddress(), members.get(1).serverAddress());
          threadAssertEquals(serverContext.getLastVotedFor(), 0);
          threadAssertEquals(response.term(), 2L);
          threadAssertTrue(response.succeeded());
        });
  }
  /** Tests a membership group. */
  private void testMembershipGroup(
      Atomix client1, Atomix client2, Function<Atomix, DistributedMembershipGroup> factory)
      throws Throwable {
    DistributedMembershipGroup group1 = factory.apply(client1);
    DistributedMembershipGroup group2 = factory.apply(client2);

    LocalGroupMember localMember = group2.join().get();
    assertEquals(group2.members().size(), 1);

    group1
        .join()
        .thenRun(
            () -> {
              threadAssertEquals(group1.members().size(), 2);
              threadAssertEquals(group2.members().size(), 2);
              resume();
            });

    await(5000);

    group1.onLeave(member -> resume());
    localMember.leave().thenRun(this::resume);

    await(5000, 2);
  }