ClusterElderManager { public ElderState getElderState(boolean waitToBecomeElder) throws InterruptedException { if (waitToBecomeElder) { waitForElder(clusterDistributionManager.getId()); } if (!isElder() && !waitToBecomeElder) { return null; } if (this.elderStateInitialized) { return this.elderState; } else { return initializeElderState(); } } 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 getElderStateAsElder() throws InterruptedException { Supplier<ElderState> elderStateSupplier = mock(Supplier.class); ElderState elderState = mock(ElderState.class); when(elderStateSupplier.get()).thenReturn(elderState); ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager, elderStateSupplier); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member0, member1)); assertThat(clusterElderManager.getElderState(false)).isEqualTo(elderState); verify(elderStateSupplier, times(1)).get(); }
@Test public void getElderStateGetsBuiltOnceAsElder() throws InterruptedException { Supplier<ElderState> elderStateSupplier = mock(Supplier.class); ElderState elderState = mock(ElderState.class); when(elderStateSupplier.get()).thenReturn(elderState); ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager, elderStateSupplier); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member0, member1)); assertThat(clusterElderManager.getElderState(false)).isEqualTo(elderState); assertThat(clusterElderManager.getElderState(false)).isEqualTo(elderState); verify(elderStateSupplier, times(1)).get(); }
@Test public void getElderStateFromMultipleThreadsAsElder() { Supplier<ElderState> elderStateSupplier = mock(Supplier.class); ElderState elderState = mock(ElderState.class); when(elderStateSupplier.get()).thenReturn(elderState); ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager, elderStateSupplier); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member0, member1)); Callable<ElderState> callable = () -> clusterElderManager.getElderState(false); concurrencyRule.add(callable).expectValue(elderState); concurrencyRule.add(callable).expectValue(elderState); concurrencyRule.executeInParallel(); verify(elderStateSupplier, times(1)).get(); }
@Test public void getElderStateNotAsElder() throws InterruptedException { Supplier<ElderState> elderStateSupplier = mock(Supplier.class); ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager, elderStateSupplier); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member1, member0)); assertThat(clusterElderManager.getElderState(false)).isEqualTo(null); verify(elderStateSupplier, times(0)).get(); }
@Test public void getElderStateWaitsToBecomeElder() { Supplier<ElderState> elderStateSupplier = mock(Supplier.class); ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager, elderStateSupplier); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member1, member0)); when(clusterDistributionManager.isCurrentMember(eq(member0))).thenReturn(true); assertThatInterruptableRunnableWaits(() -> { try { clusterElderManager.getElderState(true); } catch (InterruptedException e) { } }); verify(elderStateSupplier, times(0)).get(); }
@Test public void getElderStateReturnsElderStateIfWaitsToBecomeElder() throws Exception { Supplier<ElderState> elderStateSupplier = mock(Supplier.class); ElderState elderState = mock(ElderState.class); when(elderStateSupplier.get()).thenReturn(elderState); ClusterElderManager clusterElderManager = new ClusterElderManager(clusterDistributionManager, elderStateSupplier); when(clusterDistributionManager.getId()).thenReturn(member0); when(clusterDistributionManager.isCloseInProgress()).thenReturn(true); when(clusterDistributionManager.getViewMembers()).thenReturn(Arrays.asList(member1)); assertThat(clusterElderManager.getElderState(true)).isEqualTo(elderState); }