ClusterElderManager { public boolean waitForElder(final InternalDistributedMember desiredElder) throws InterruptedException { MembershipChangeListener changeListener = new MembershipChangeListener(); clusterDistributionManager.addMembershipListener(changeListener); InternalDistributedMember currentElder; try { while (true) { if (clusterDistributionManager.isCloseInProgress()) { return false; } currentElder = getElderCandidate(); if (desiredElder.equals(currentElder)) { return true; } if (logger.isDebugEnabled()) { logger.debug("Expecting Elder to be {} but it is {}.", desiredElder, currentElder); } if (!clusterDistributionManager.isCurrentMember(desiredElder)) { return false; } if (!clusterDistributionManager.getId().equals(desiredElder) && clusterDistributionManager.getId().equals(currentElder)) { return false; } if (logger.isDebugEnabled()) { logger.debug("Waiting for membership to change"); } changeListener.waitForMembershipChange(); } } finally { clusterDistributionManager.removeMembershipListener(changeListener); } } ClusterElderManager(ClusterDistributionManager clusterDistributionManager);  ClusterElderManager(ClusterDistributionManager clusterDistributionManager,
      Supplier<ElderState> elderStateSupplier); InternalDistributedMember getElderId(); boolean isElder(); ElderState getElderState(boolean waitToBecomeElder); boolean waitForElder(final InternalDistributedMember desiredElder);  }
@Test public void waitForElderReturnsTrueIfAnotherMemberIsElder() throws InterruptedException { ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member1, member0)); assertThat(clusterElderManager.waitForElder(member1)).isTrue(); }
@Test public void waitForElderReturnsFalseIfWeAreElder() throws InterruptedException { ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.isCurrentMember(eq(member1))).thenReturn(true); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member0, member1)); assertThat(clusterElderManager.waitForElder(member1)).isFalse(); }
@Test public void waitForElderReturnsFalseIfDesiredElderIsNotACurrentMember() throws InterruptedException { ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()) .thenReturn(Arrays.asList(member2, member0, member1)); assertThat(clusterElderManager.waitForElder(member1)).isFalse(); }
@Test public void waitForElderWaits() { ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member1, member0)); when(clusterDistributionManager.isCurrentMember(eq(member0))).thenReturn(true); when(clusterDistributionManager.isCloseInProgress()).thenReturn(false); assertThatInterruptableRunnableWaits(() -> { try { clusterElderManager.waitForElder(member0); } catch (InterruptedException e) { } }); }
@Test public void waitForElderDoesNotWaitIfShuttingDown() throws InterruptedException { ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member1, member0)); when(clusterDistributionManager.isCurrentMember(eq(member0))).thenReturn(true); when(clusterDistributionManager.isCloseInProgress()).thenReturn(true); assertThat(clusterElderManager.waitForElder(member0)).isFalse(); }